undefined local variable or method `http' for main:Object Ruby - ruby

I have a header and url as specified below:
header = { 'User-Agent' => 'SubDB/1.0 (Subtitle-Downloader /1.0; https://github.com/gautamsawhney/Subtitle-Downloader)' }
url = "http://sandbox.thesubdb.com/?action=download&hash=" + hash + "&language=en"
I am try to send an HTTP get request using
send_request method in ruby. I am basically trying to access the SubDB API. I have written the following code to send a request :
res = http.send_request('GET', url, header)
puts res.body
I have also done this require net/http , but I get this error
undefined local variable or method `http' for main:Object (NameError)
Can someone tell me what's wrong and if can I use any other method for the same? The inclusion of the header is necessary.

In your example, You need to instantiate the Net::Http before using that
http = Net::HTTP.new "sandbox.thesubdb.com"
res = http.send_request("GET", "/?action=download....", nil, headers)

Related

Correct way to get response from Slack API

I want to move this code from the console:
curl --data "token=TOKEN_NO&email=USER#EXAMPLE.COM" https://slack.com/api/users.lookupByEmail
to the class method and get response from Slack (user details) exactly like from the console. I was trying to do something like this, but I don't see any results:
require 'net/http'
module Slack
class GetUserID
SLACK_API_ENDPOINT = 'https://slack.com/api/users.lookupByEmail'
def call
escaped_address = URI.decode_www_form_component(SLACK_API_ENDPOINT)
uri = URI.parse(escaped_address)
puts Net::HTTP.post(uri, params)
end
private
def params
{
token: 'TOKEN_NO',
email: 'USER#EXAMPLE.COM'
}
end
end
end
Right now I have an error:
send_request_with_body': undefined methodbytesize' for #Hash:0x00007f9d85093100> (NoMethodError)
Where am I wrong? Should I use HTTParty instead?
The second argument passed to Net::HTTP.post should be a string, read more about the method signature here.
You will need to convert the params hash into a query string format that looks like:
token=TOKEN_NO&email=USER#EXAMPLE.COM

Ruby/Sinatra - How can I call post in lambda class?

I'm make a little program in sinatra and I'm wanted to perfom some dynamic call of post, with diynamic uri, so I make a Connexion class like this:
class Connexion
def initialize(path)
#path = path
end
def sinatraPost
post "/#{#path}" do
# some code
end
end
end
But when I'm launch sinatraPost, I've got this error:
undefined method `post' for #<Connexion:0x000000026206b8> (NoMethodError)
How can I call the sinatra post method in my class ?
EDIT: Okay ! So, I change my strategy, I have this following code:
class Webhook < Sinatra::Base
get '/:name' do
# compare with names array
end
end
Webhook.run!
Thank's to everyone !
It looks like you're going about this the wrong way. If you want to set up your app to receive a POST request, you'll need routing logic in your controller. Sinatra controllers normally look like this:
require 'sinatra'
get '/route1' do
# do stuff
end
post '/route2' do
# do stuff
end
If you're using a modular app, you'll want to have your app inherit from Sinatra::Base. See the Sinatra docs for more.
Making a post request is different, and doesn't rely on Sinatra methods.
require 'net/http'
uri = URI("http://google.com")
headers = {}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, headers)
response = http.request(request)
Or something like that. Good luck!

valid_json? check in ruby is not working

I am trying to check whether the response is valid JSON. I am making HTTParty or Restclient request to some urls and checking whether the responses returned are valid JSON?
I referred the link here. This is not working.
My code:
require 'json'
def get_parsed_response(response)
if not response.is_a? String or not response.valid_json?
# code
end
end
Error:
/home/user/.rvm/gems/ruby-2.1.0/gems/httparty-0.13.1/lib/httparty/response.rb:66:in `method_missing': undefined method `valid_json?' for #<HTTParty::Response:0x00000002497918> (NoMethodError)
More specifically than in my comment, I suggest you use something like this:
value = nil
begin
value = JSON.parse(response)
# do whatever you do when not error
rescue JSON::ParserError, TypeError => e
puts "Not a string, or not a valid JSON"
# do whatever you do when error
end
You should be calling response.body.
response is an HTTParty::Response object. What you really want to be working with is the String object that represents the HTTP response body.

Ruby NoMethodError (undefined method `map') in Post Request

I'm trying to do a POST request from a ruby app and I'm getting the following error.
Here is the code:
def action_reply(token,action_id,reply_text)
require 'uri'
require 'net/http'
require 'net/https'
#reply = { 'ACTION_ID' => action_id, 'text' => reply_text }.to_json
#A token is required to do this post
#token_url = 'https://example.com/reply?oauth_token=' + token
uri = URI.parse(#token_url)
response = Net::HTTP.post_form(uri,#reply)
end
I'm getting an error in the last step that says:
NoMethodError (undefined method `map' for #<String:0x000000063798e8>)
Any idea why this is?
Thanks!
Because you're passing a string to a method expecting a hash: remove the to_json call.
Unrelated, but is it necessary to have those instance variables be instance variables?

Mechanize and typhoeus

I am using Typhoeus with Hydra in order to make parallel requests . my end goal is to parse the typhoeus response into mechanize object.
url = "http://example.com/"
hydra = Typhoeus::Hydra.new
agent = Mechanize.new
request = Typhoeus::Request.new(url, :method => :get, :proxy => "#{proxy_host}:#{proxy_port}")
request.on_complete do |response| #Typhoeus::response object
body = response.body
uri = request.parsed_uri
page = agent.parse(uri, response, body)
end
hydra.queue(request)
hydra.run
the agent.parse method is giving me error because it cannot parse the typhoeus response object
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/mechanize-2.5.1/lib/mechanize.rb:1165:in `parse': undefined method `[]' for #<Typhoeus::Response:0x00000012cd9da0> (NoMethodError)
Is there anyway i can convert Typhoeus response into Net::HTTPResponse object ?
Or is there any other way I can club Mechanize and Typhoeus together? So that, I can make parallel requests with typhoeus and scrape the data with Mechanize library.
I tried to create a Net::HTTPResponse(https://github.com/ruby/ruby/blob/trunk/lib/net/http/response.rb) from a Typhoeus::Response, but it didn't work out. Calling the initializer is easy, but setting the response body or headers not.
I looked into mechanize to see if it can be changed to use Typhoeus for making requests but I don't think thats possible right now. Net/http is really hard-wired into mechanize. I thought of a mechanize-typhoeus adapter, which would be nice.

Resources