private method `open' called for URI:Module ERROR - ruby-on-rails

Working with a gem but its throwing an error on an older build
private method `open' called for URI:Module
Extracted source (around line #135):
133
134
135
136
137
138
url = construct_url(path)
#URI::open(url, read_timeout: 600).read
URI.open(url, read_timeout: 600).read
rescue OpenURI::HTTPError => e
if error = JSON.load(e.io.read)["error"]
puts "server returns error for url: #{url}"
I'm running Rails 4.2.6 on Ruby ruby 2.3.8p459
Im a LOT out of my depth here :(
this is the code that calls the error in the gem
I unpacked and rebuilt with the comment out above as can't find documentation bth URI::open( and URI.open throw same error, private method called.
require 'google_search_results'
require 'open-uri'
params = {
q: #q,
location: "United Kingdom",
hl: "en",
gl: "uk",
google_domain: "google.co.uk",
api_key: ENV["google_search_api_key"],
num: 20
}
search = GoogleSearch.new(params)
#hash_results = search.get_hash
I know its to do with the version of Rails / Ruby Im running but don't know where to look or terminology of question to ask.

You can use the method like that from ruby 2.4 onwards, but for 2.3 you should just use it as:
open(url, read_timeout: 600).read

Related

getting errors when scrapping data with kimurai ruby on rails

FATAL -- web_scrapper_spider: Spider: stopped: {:spider_name=>"web_scrapper_spider", :status=>:failed, :error=>"#<NameError: uninitialized constant URI::HTTP\n\n raise InvalidUrlError, "Requested url is invalid: #{url}" unless URI.parse(url).kind_of?(URI::HTTP)\n >", :environment=>"development", :start_time=>2022-11-17 19:03:32.418101772 +0530, :stop_time=>2022-11-17 19:03:32.420534612 +0530, :running_time=>"0s", :visits=>{:requests=>0, :responses=>0}, :items=>{:sent=>0, :processed=>0}, :events=>{:requests_errors=>{}, :drop_items_errors=>{}, :custom=>{}}}
~/.rvm/gems/ruby-3.1.2/gems/kimurai-1.4.0/lib/kimurai/base.rb:194:in `request_to': uninitialized constant URI::HTTP (NameError)
raise InvalidUrlError, "Requested url is invalid: #{url}" unless URI.parse(url).kind_of?(URI::HTTP)
^^^^^^
from ~/.rvm/gems/ruby-3.1.2/gems/kimurai-1.4.0/lib/kimurai/base.rb:128:in `block in crawl!'
require 'open-uri'
require 'nokogiri'
require 'kimurai'
class WebScrapper < Kimurai::Base
#name = "web_scrapper_spider"
#engine = :mechanize
#start_urls = ["https://metaruby.com/"]
#config = {
user_agent: "Chrome/68.0.3440.84"
}
def parse(response, url:, data: {})
blogs = []
response.xpath("//table[#class='topic-list']//tbody//tr").each do |tr|
scrapped_data = {
title: tr.at('td[1]//span').text,
category: tr.at('td[1]//div//span').text,
date: tr.at('td[3]').text.strip
}
blogs << scrapped_data
save_to "results.json", scrapped_data.as_json, format: :json
end
end
end
From the logs posted in the question, it is clear that you are using Ruby version 3.1.2.
The gem Kimura that you are using is not maintained since 2020 and hence it is not compatible with Ruby 3.x due to the separation of positional and keyword arguments from 3.0.
Refer to Kimura issue #66 for more details and updates on the reported issue.
For now, you can use the gem Kimura with earlier Ruby versions.
I tried your code with Ruby 2.x and found no issues with the code.

NoMethodError Ruby 1.9.3

I am using Ruby 1.9.3 and getting the following error on the following line while trying to use the SendGrid API.
ERROR [on this line below "mailer.mail(mail_defaults)"]:
NoMethodError (undefined method `to_h' for #<Hash:0x00000005da0958>):
CODE:
assuming some users
recipients = []
recipient = SendGrid::Recipient.new('jn#geo.com')
recipient.add_substitution('name', 'Billy Bob')
recipient.add_substitution('number', 1234)
recipient.add_substitution('time', '10:30pm')
recipients << recipient
# then initialize a template
template = SendGrid::Template.new('blahblah7bef2-d25b00')
# create the client
# see client above
# set some defaults
mail_defaults = {
:from => 'no-reply#geo.com',
:html => '<h1>I like email tests</h1>',
:text => 'I like email tests',
:subject =>'Test Email is great',
}
mailer = SendGrid::TemplateMailer.new(client, template, recipients)
# then mail the whole thing at once
mailer.mail(mail_defaults)
I thought it might be my mail_defaults array so I tried this (see below) and received the same error on the same line.
mail_defaults = {
from: 'no-reply#geo.com',
html: '<h1>I like email tests</h1>',
text: 'I like email tests',
subject: 'Test Email is great',
}
Do I have an error in my code or is their an error in SendGrids mailer.mail method?
This problem is with mail.mailer. This uses the to_h method internally, which was implemented in Ruby 2.1. Effectively, the sendgrid sdk requires ruby 2.1 now. I had the same issue with Ruby 1.9.3 and solved it by upgrading to 2.1.
This is what I used to upgrade to Ruby 2.1 on CentOS 6 (not my gist):
https://gist.github.com/mustafaturan/8290150
Hope this helps.

Parsing XML response with Rails 4 and Savon 2

I´m using Rails and Savon 2 to get data from a SOAP Webservice.
This is the code:
client = Savon.client(wsdl: "http://www.webservicex.net/periodictable.asmx?WSDL",
log_level: :debug,
pretty_print_xml: true)
message = {'ElementName' => 'Zinc'}
response = client.call(:get_element_symbol, message: message)
logger.debug "Body=" + response.body.to_s
symbol = response.to_hash[:get_element_symbol_response][:get_element_symbol_result][:NewDataDet][:Table][:Symbol]
The request is ok and I´m getting the data in the response:
Body={:get_element_symbol_response=>{:get_element_symbol_result=>"<NewDataSet>\n <Table>\n <Symbol>Zn</Symbol>\n </Table>\n</NewDataSet>", :#xmlns=>"http://www.webserviceX.NET"}}
But now, I don´t know how to parse this response correctly to get the "Symbol".
I´m getting this error:
`TypeError (no implicit conversion of Symbol into Integer):`
UPDATE:
If I do:
symbol = response.to_hash[:get_element_symbol_response][:get_element_symbol_result]
logger.debug "Symbol=" + symbol.inspect
I get this: Symbol="<NewDataSet>\n <Table>\n <Symbol>Zn</Symbol>\n </Table>\n</NewDataSet>"
I think the error is that I´m trying to get Symbol in hash mode, and it is not. But how can I get the symbol? I can´t believe I have to parse the string manually...
You can use nokogiri to parse XML text:
require 'nokogiri'
text = response.body[:get_element_symbol_response][:get_element_symbol_result]
Nokogiri::XML(text).css('Symbol').text # => Zn
# or
Nokogiri::XML(text).xpath('//NewDataSet/Table/Symbol').text # => Zn

Unknown Argument Error "-p" when deploying to heroku

We are deploying a rails app to Heroku. The app should be making a youtube api call, using the Trollop Gem as a command line parser. We keep getting this error back.
2014-07-30T23:17:57.526014+00:00 app[web.1]: Error: unknown argument '-p'.
2014-07-30T23:17:57.526020+00:00 app[web.1]: Try --help for help.
2014-07-30T23:17:57.526541+00:00 app[web.1]: Completed 500 Internal Server Error in 7466ms
This is what our Trollop code looks like.
def self.youtube_search(query)
youtube_service_api_name = "youtube"
youtube_api_version = "v3"
# opts = HTTParty.get("https://www.youtube.com/results?search_query=russia")
opts = Trollop::options do
opt :q, 'Search term', :source => String, :default => query
opt :maxResults, 'Max results', :source => :int, :default => 25
end
What's much stranger is that it was working an hour ago and now it's not. Does anyone have any ideas? This doesn't seem to be documented anywhere.
Trollop will fail when provided with undefined arguments. In your code the -q option is defined but -p is not.
Here are the relevant lines in the parse method:
trollop.rb
339 unless sym
340 next 0 if ignore_invalid_options
341 raise CommandlineError, "unknown argument '#{arg}'" unless sym
342 end
As for why it was working previously, perhaps you were passing -q then and inadvertently passing -p now?
yeah i got the same problem. unknown argument -p and the 500 internal error.
i decided to give up on Trollop and just hard code my own opts.
so i just initialized an opts hash and inserted key value pairs and no more problems.

ruby net_dav sample puts

I'm trying to put a file on a site with WEB_DAV. (a ruby gem)
When I follow the example, I get a nil exception
#### GEMS
require 'rubygems'
begin
gem "net_dav"
rescue LoadError
system("gem install net_dav")
Gem.clear_paths
end
require 'net/dav'
uri = URI('https://staging.web.mysite');
user = "dave"
pasw = "correcthorsebatterystaple"
dav = Net::DAV.new(uri, :curl => false)
dav.verify_server = false
dav.credentials(user, pasw)
cargo = ("testing.txt")
File.open(cargo, "rb") { |stream|
dav.put(urI.path +'/'+ cargo, stream, File.size(cargo))
}
when I run this I get
`digest_auth': can't convert nil into String (TypeError)
this relates to line 197 in my nav.rb file.
request_digest << ':' << params['nonce']
So what I'm wondering is what step did I not add?
Is there a reasonable example of the correct use of this gem? Something that does something that works would be sweet :)
SIDE QUESTION: Is this the correct gem to use to do web_DAV? It seems an old unmaintained gem, perhaps there's something used by more to accomplish the task?
Try referencing the hash with a symbol rather than a string, i.e.
request_digest << ':' << params[:nonce]
In a simple test
baz = "baz"
params = {:foo => "bar"}
baz << ':' << params['foo']
results in the same error as you're getting.

Resources