passing result of method as global value - ruby

Can someone explain what I am doing wrong here? I am attempting to avoid having to type and re-declare all the variables for an account inside the method.
#https://github.com/nukeproof/oanda_api
require 'oanda_api'
#require 'rsruby'
require 'indicators'
def createOrder(instrument="EUR_USD",type="market",units)
order = $client.$account($acct_id).order(instrument: "EUR_USD",type: "market",side: type,units: units).create
puts "time: "+order.time+"\nprice: "+order.price+"\nid: "+order.trade_opened.id
return(order.trade_opened.id)
end
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE #bad practice...anyone who copies this code dont do this!
$acct_id=123456
$acct_token="abcd"
$client = OandaAPI::Client::TokenClient.new(:practice, $acct_token)
$account = client.account($acct_id).get
createOrder(units:1000)
Error
C:\Users\king\Desktop\_REPOS\misc\stock_analysis\forex\oanda\ruby>ruby basicnukeproof.rb
basicnukeproof.rb:7: syntax error, unexpected tGVAR, expecting '('
order = $client.$account($acct_id).order(instrument: ...

This part of the code is invalid
$client.$account(...)
If you write $client., then the next thing that Ruby expects to see is the name of a method, and method names cannot begin with $ so $account is invalid. Try removing the dollar sign from $account there.

Related

Ruby how to use method parameter embedded in call to another method other than just puts

In Ruby I want to use an input parameter for REST verb (like 'Post') in a call to Net::HTTP new but I can't work out how to embed the parameter in the statement. I have the param called restVerb which prints fine in puts "Verb is #{restVerb}" but not in request = Net::HTTP::#{restVerb}.new(uri) - I get undefined method request' for Net::HTTP:Class (NoMethodError)` so it clearly doesn't recognise the parameter's value in the statement. I could use a case statement but wanted to make it more generic. What am I doing wrong?
I've tried the above syntax and a few others like request = Net::HTTP::restVerb.new(uri) or request = Net::HTTP::$restVerb.new(uri) I'm new to Ruby so be gentle with me please.
Use Object.const_get to convert a string to an actual constant:
klass = Object.const_get("Net::HTTP::#{restVerb}")
=> Net::HTTP::Post
klass.new(uri)
=> #<Net::HTTP::Post POST>

Ruby: unexpected ',', expecting keyword_end

Very new to Ruby, unable to see the titular syntax error in this bit of code:
#! /usr/bin/env ruby
require 'sensu-plugin/metric/cli'
class MetricAvailableUpdates < Sensu::Plugin::Metric::CLI::Graphite
option :scheme,
description: 'Metric naming scheme',
long: '--scheme SCHEME',
short: '-s SCHEME',
default: "#{Socket.gethostname}"
def run
# Get the metrics.
output = %x[/usr/lib/update-notifier/apt-check --human-readable]
output_lines = output.split(/(\n)/)
metrics = {}
updates_pattern = " packages can be updated."
updates = output_lines[0].tr(upgrades_pattern, "").to_i
metrics[:available_updates] = updates
security_updates_pattern = " updates are security updates."
security_updates = output_lines[2].tr(security_updates_pattern, "").to_i
metrics[:available_security_updates] = security_updates
# Print them in graphite format.
metrics.each do |k, v|
output [config[:scheme], k].join('.'), v
end
# Done
ok
end
end
I can add the code that precedes this if the syntax error is in fact before this section. Edit: added complete file contents per comment request
The complete error, in case that is useful:
./metrics-available-updates.rb:29: syntax error, unexpected ',', expecting keyword_end
output [config[:scheme], k].join('.'), v
If you play around a bit, you will notice that the syntax error goes away either when you comment out the offending line, or alternatively the line
output = %x[/usr/lib/update-notifier/apt-check --human-readable]
When Ruby parses a file, it needs to guess, whether a symbol denotes a method call, or a variable reference. In this case, output springs into existence as a variable, but further down, you write
output [config[:scheme], k].join('.'), v
which means it suddenly becomes a method call.
I admit that the Ruby lexer should give a more helpful error message....
Add the parentheses
...
metrics.each do |k, v|
output([config[:scheme], k].join('.'), v)
end
...

Can you pass a block of code that returns an error to a method?

I often find myself dealing with these kind of scenarios:
require 'nokogiri'
require "open-uri"
url = "https://www.random_website.com/contains_info_I_want_to_parse"
nokodoc = Nokogiri::HTML(open(url))
# Let's say one of the following line breaks the ruby script
# because the element I'm searching doesn't contain an attribute.
a = nokodoc.search('#element-1').attribute('href').text
b = nokodoc.search('#element-2').attribute('href').text.gsub("a", "A")
c = nokodoc.search('#element-3 h1').attribute('style').text.strip
What happens is that I'll be creating about 30 variables all searching for different elements in a page, and I'll be looping that code over multiple pages. However, a few of these pages may have an ever-so-slightly different layout and won't have one of those div. This will break my code (because you can't call .attribute or .gsub on nil for example). But I can never guess which line before-hand.
My go-to solution is usually surround each line with:
begin
line #n
rescue
puts "line #n caused an error"
end
I'd like to be able to do something like:
url = "https://www.random_website.com/contains_info_I_want_to_parse"
nokodoc = Nokogiri::HTML(open(url))
catch_error(a, nokodoc.search('#element-1').attribute('href').text)
catch_error(b, nokodoc.search('#element-2').attribute('href').text.gsub("a", "A"))
catch_error(c, nokodoc.search('#element-3 h1').attribute('style').text.strip)
def catch_error(variable_name, code)
begin
variable_name = code
rescue
puts "Code in #{variable_name} caused an error"
end
variable_name
end
I know that putting & before each new method works:
nokodoc.search('#element-1')&.attribute('href')&.text
But I want to be able to display the error with a 'puts' in my terminal to see when my code gives an error.
Is it possible?
You can't pass your code as a regular argument to a method because it'll be evaluated (and raise an exception) before it gets passed to your catch_error method. You could pass it as a block--something like
a = catch_error('element_1 href text') do
nokodoc.search('#element-1').attribute('href').text
end
def catch_error(error_description)
yield
rescue
puts "#{error_description} caused an error"
end
Note that you can't pass a to the method as variable_name: it hasn't been defined anywhere before calling that method, so you'll get an undefined local variable or method error. Even if you define a earlier, it won't work correctly. If your code works without raising an exception, the method will return the right value but the value won't get stored anywhere outside the method scope. If there is an exception, variable_name will have whatever value a had before the method (nil if you defined it without setting it), so your error message would output something like Code in caused an error. That's why I added an error_description parameter.
You could also try logging the message and backtrace if you didn't want to have to specify an error description every time.
a = catch_error(nokodoc) do |doc|
doc.search('#element-1').attribute('href').text
end
def catch_error(doc)
yield doc
rescue => ex
puts doc.title # Or something else that identifies the document
puts ex.message
puts ex.backtrace.join("\n")
end
I made one additional change here: passing the document in as a parameter so that rescue could easily log something that identifies the document, in case that's important.

How do I call a function in Ruby?

I'm trying to call but I keep getting an error. This is my code:
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
class AlchemyAPI
#Setup the endpoints
##ENDPOINTS = {}
##ENDPOINTS['taxonomy'] = {}
##ENDPOINTS['taxonomy']['url'] = '/url/URLGetRankedTaxonomy'
##ENDPOINTS['taxonomy']['text'] = '/text/TextGetRankedTaxonomy'
##ENDPOINTS['taxonomy']['html'] = '/html/HTMLGetRankedTaxonomy'
##BASE_URL = 'http://access.alchemyapi.com/calls'
def initialize()
begin
key = File.read('C:\Users\KVadher\Desktop\api_key.txt')
key.strip!
if key.empty?
#The key file should't be blank
puts 'The api_key.txt file appears to be blank, please copy/paste your API key in the file: api_key.txt'
puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html'
Process.exit(1)
end
if key.length != 40
#Keys should be exactly 40 characters long
puts 'It appears that the key in api_key.txt is invalid. Please make sure the file only includes the API key, and it is the correct one.'
Process.exit(1)
end
#apiKey = key
rescue => err
#The file doesn't exist, so show the message and create the file.
puts 'API Key not found! Please copy/paste your API key into the file: api_key.txt'
puts 'If you do not have an API Key from AlchemyAPI please register for one at: http://www.alchemyapi.com/api/register.html'
#create a blank file to hold the key
File.open("api_key.txt", "w") {}
Process.exit(1)
end
end
# Categorizes the text for a URL, text or HTML.
# For an overview, please refer to: http://www.alchemyapi.com/products/features/text-categorization/
# For the docs, please refer to: http://www.alchemyapi.com/api/taxonomy/
#
# INPUT:
# flavor -> which version of the call, i.e. url, text or html.
# data -> the data to analyze, either the the url, text or html code.
# options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
#
# Available Options:
# showSourceText -> 0: disabled (default), 1: enabled.
#
# OUTPUT:
# The response, already converted from JSON to a Ruby object.
#
def taxonomy(flavor, data, options = {})
unless ##ENDPOINTS['taxonomy'].key?(flavor)
return { 'status'=>'ERROR', 'statusInfo'=>'Taxonomy info for ' + flavor + ' not available' }
end
#Add the URL encoded data to the options and analyze
options[flavor] = data
return analyze(##ENDPOINTS['taxonomy'][flavor], options)
print
end
**taxonomy(text,"trees",1)**
end
In ** ** I have entered my call. Am I doing something incorrect. The error I receive is:
C:/Users/KVadher/Desktop/testrub:139:in `<class:AlchemyAPI>': undefined local variable or method `text' for AlchemyAPI:Class (NameError)
from C:/Users/KVadher/Desktop/testrub:6:in `<main>'
I feel as though I'm calling as normal and that there is something wrong with the api code itself? Although I may be wrong.
Yes, as jon snow says, the function (method) call must be outside of the class. The methods are defined along with the class.
Also, Options should be a Hash, not a number, as you call options[flavor] = data, which is going to cause you another problem.
I believe maybe you meant to put text in quotes, as that is one of your flavors.
Furthermore, because you declared a class, this is called an instance method, and you must make an instance of the class to use this:
my_instance = AlchemyAPI.new
my_taxonomy = my_instance.taxonomy("text", "trees")
That's enough to get it to work, it seems like you have a ways to go to get this all working though. Good luck!

Can not pass a DBI::DatabaseHandle object as an argument to a ruby gem method

I'm new to ruby so forgive me in advance if this is a silly question. I've googled for answers but nothing relevant comes up and it seems the answer should be obvious.
I'm attempting to pass a DBI::DatabaseHandle as function argument and I'm getting a wrong "number of arguments" error when I run the function. Here's my code...
require 'rubygems'
require 'dbi'
class CmsTest
def self.get_dbi_connection(hostname, user, password)
connection = DBI.connect("DBI:OCI8:" + hostname, user, password)
return connection
end
def self.query(connection, sql)
puts connection
puts sql
begin
request = connection.prepare("#{query}")
request.execute
fetched = []
request.fetch do |row|
fetched << row.to_h
end
request.finish
return fetched
rescue DBI::DatabaseError => e
log "An error occurred"
log "Error code: #{e.err}"
log "Error message: #{e.errstr}"
ensure
end
end
end
So my code that calls this looks like so...
require 'rubygems'
require 'cms_test'
connection = CmsTest.get_dbi_connection('foo', 'bar', 'fubar')
CmsTest.query(connection, "<some sql query>")
So the first argument is a DBI::DatabaseHandle object and the second is some sql query string. When I run that I get this...
`query': wrong number of arguments (0 for 2) (ArgumentError)
This even though the query signature contains two arguments and and I'm passing the method two arguments. The really weird thing for me is that if I put and exit statement anywhere in the method body after the puts it will show that the method did indeed receive 2 arguments...
#<DBI::DatabaseHandle:0x007fa2a316c9f0>
select licensor_id, licensor_name from cf_licensor
I can't make any sense of this. Please help.
You have a method named query:
def self.query(connection, sql)
and then inside query you try to call query:
request = connection.prepare("#{query}")
# -- method call ---------------^^^^^
You probably want to use sql there and there's no need for string interpolation:
request = connection.prepare(sql)

Resources