Imdb api with Ruby - ruby

I am trying to use the imdb API. I tried to search for Fargo, but when I run it, all I get is a black screen:
require 'net/http'
uri = URI.parse("http://imdbapi.org/")
response = Net::HTTP.post_form(uri, {"q" => "Fargo"})
Can anyone tell me what is wrong or provide an example on how to retrieve the data from Fargo with a json in ruby from that api?

Simple way:
require 'json'
require 'open-uri'
json = JSON.parse(open("http://imdbapi.org?q=Fargo") { |x| x.read }).first
To get individual element:
json['title']
#=> Fargo

This simple code below show us how to get a basic json data from imdb api:
require "net/http"
require "uri"
uri = URI.parse("http://imdbapi.org/?title=Fargo&type=json")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts response.body
I use a verb GET in the REST way.

Related

TypeError problem: no implicit conversion in Sinatra + JSON.parse

I'm trying to set up a web hook, following this GitHub tutorial
require 'sinatra'
require 'json'
require 'net/http'
require 'pp'
set :port, 31415
# Descarga las diferencias hechas para un push
post '/' do
push = JSON.parse(request.body.read)
piezas = push["compare"].split("/")
api_url = "/repos/#{piezas[3]}/#{piezas[4]}/compare/#{piezas[6]}"
diff = Net::HTTP.get(URI("https://api.github.com#{api_url}"))
puts diff.class
pp(JSON.parse(diff))
end
diff.class prints:
String
And, as a matter of fact, the last sentence works correctly, printing via pp the structure. However, after printing, it yields the error
[2018-10-25 20:00:23] ERROR TypeError: no implicit conversion of Array into String
It's not referencing any line in the script, but would it be possible that the error would be in the first JSON.parse? Could it be that request.body.read would be an array?
Update I couldn't golf it down to any of the JSON.parse separately. Downloading the hook payload works OK, downloading the JSON from the GitHub API works without a glithc. Somehow it's using them together what does not work.
It's possible the library is treating the response like text. Try adding an Accept header. This worked for me:
request["Accept"] = "application/json"
example:
uri = URI.parse("https://api.github.com")
req = Net::HTTP::Get.new(URI("https://api.github.com/repos/JJ/microservices-broker/compare/d5d39c5db99d...bbbf695d1bf2"))
req["Accept"] = 'application/json'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(req)
json = JSON.parse(response.body)
json['url']
# or
json = JSON.parse(response.body, symbolize_names: true)
json[:url]
(EDIT:) Also, Using Net::HTTP is really painful. Please checkout these libraries:
https://github.com/lostisland/faraday
https://github.com/octokit/octokit.rb

Sending file with POST to server in pure ruby (or library that doesn't need build tools)

Writing extensions for Sketchup I need to get around their usage of their own ruby (2.0.0) interpreter. Most importantly, I can't install gems that require build tools.
How can I send a file per POST request to my local server which does some calculations and answers with a JSON object?
I'm aware how I can use rest-client to send the file, but due the mentioned restrictions I can't use it (it required build tools). Is there another comparable way or library that can help me?
require 'uri'
url = 'http://foourl.com'
uri = URI.parse(url)
data = File.read('fil_path')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.body = data
request.content_type = 'audio/amr'
response = http.request(request)
(Taken from here)
Don't forget to configure your content type
You can use Ruby's native Net::HTTP library.
Examples:
require 'net/http'
require 'uri'
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
puts res.body
or
response = http.post('/cgi-bin/search.rb', 'query=foo')

Why do I get an "unexpected token" error when parsing JSON using Ruby's json gem?

The JSON object I'm parsing is at http://api.4chan.org/3/catalog.json
Here is my Ruby code:
['open-uri','nokogiri','json'].each{|g| require g}
json_test = File.open('json_test.JSON','r').read
board_cat_body = Nokogiri::HTML(open('http://api.4chan.org/3/catalog.json'))
puts JSON.parse(board_cat_body)
Result (it's very long so I took a part of it out):
C:/Ruby193/lib/ruby/1.9.1/json/common.rb:148:in `parse': 387: unexpected token at '{"no":248019,"sticky":1,"closed":1,"now":"12\/19....
However, if I copy and paste the contents of http://api.4chan.org/3/catalog.json into a local JSON file and parse from that local JSON file, there is no problem.
Does anyone know what I'm doing wrong?
Remove the Nokogiri call. JSON isn't HTML.
['open-uri','json'].each{|g| require g}
json = JSON.parse(open('http://api.4chan.org/3/catalog.json').read)
puts json.inspect
The document you get in board_cat_body is not a JSON doc, it's HTML, as you can see if you print it. So, I propose to download the document this way:
require 'net/http'
require 'json'
url = URI.parse('http://api.4chan.org/3/catalog.json')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
and parse it:
puts JSON.parse(res.body)

How do I use ruby get JSON back from Instagram API

I am doing my best to get JSON back from the instagram API. Here is the code I am trying in my rake task within rails.
require 'net/http'
url = "https://api.instagram.com/v1/tags/snow/media/recent?access_token=522219.f59def8.95be7b2656ec42c08bff8a159a43d06f"
resp = Net::HTTP.get_response(URI.parse(url))
puts resp.body
All I end up with in the terminal is "rake aborted!
end of file reached"
If you look at the instagram docs http://instagram.com/developer/endpoints/tags/ and you paste the following URL in your browser you will get JSON back so I'm sure I am doing something wrong.
https://api.instagram.com/v1/tags/snow/media/recent?access_token=522219.f59def8.95be7b2656ec42c08bff8a159a43d06f
It has to do with HTTPS url you need to modify your code to include SSL
require "net/https"
require "uri"
uri = URI.parse("https://api.instagram.com/v1/tags/snow/media/recent?access_token=522219.f59def8.95be7b2656ec42c08bff8a159a43d06f")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
puts response.body
alternatively you could use somthing like https://github.com/jnunemaker/httparty to consume 3rd party services
Looks like you'd need to configure net/http to use SSL because you're using https.
Alternative : use this with Rails, it'll parse the json on the fly too :
ActiveSupport::JSON.decode(open(URI.encode(url)))
Returns a hash to play with

Simplest way to do a XMLHttpRequest in Ruby?

I want to do a XMLHttpRequest POST in Ruby. I don't want to use a framework like Watir. Something like Mechanize or Scrubyt would be fine. How can I do this?
Mechanize:
require 'mechanize'
agent = Mechanize.new
agent.post 'http://www.example.com/', :foo => 'bar'
Example with 'net/http', (ruby 1.9.3):
You only have to put an additional header for the XMLHttpRequest to your POST-request (see below).
require 'net/http'
require 'uri' # convenient for using parts of an URI
uri = URI.parse('http://server.com/path/to/resource')
# create a Net::HTTP object (the client with details of the server):
http_client = Net::HTTP.new(uri.host, uri.port)
# create a POST-object for the request:
your_post = Net::HTTP::Post.new(uri.path)
# the content (body) of your post-request:
your_post.body = 'your content'
# the headers for your post-request (you have to analyze before,
# which headers are mandatory for your request); for example:
your_post['Content-Type'] = 'put here the content-type'
your_post['Content-Length'] = your_post.body.size.to_s
# ...
# for an XMLHttpRequest you need (for example?) such header:
your_post['X-Requested-With'] = 'XMLHttpRequest'
# send the request to the server:
response = http_client.request(your_post)
# the body of the response:
puts response.body
XMLHTTPRequest is a browser concept, but since you're asking about Ruby, I assume all you want to do is simulate such a request from a ruby script? To that end, there's a gem called HTTParty which is very easy to use.
Here's a simple example (assuming you have the gem - install it with gem install httparty):
require 'httparty'
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
puts response.body, response.code, response.message, response.headers.inspect

Resources