Skip to content

Instantly share code, notes, and snippets.

@nithinbekal
Created September 10, 2012 07:29
Show Gist options
  • Save nithinbekal/3689425 to your computer and use it in GitHub Desktop.
Save nithinbekal/3689425 to your computer and use it in GitHub Desktop.
IMDB API wrapper
require 'net/http'
require 'uri'
require 'json'
require 'ap'
module ImdbApi
BASE_URL = "http://www.deanclatworthy.com/imdb"
def self.search(title)
uri = URI.parse "#{BASE_URL}/?q=#{URI.escape title}"
response = Net::HTTP.get_response(uri).body
json = JSON.parse response
Movie.new(json)
end
class Movie
attr_accessor :title, :year, :genres, :rating, :duration
def initialize(json)
@title = json['title']
@year = json['year']
@genres = json['genres'].split(',')
@rating = json['rating']
@duration = json['runtime']
end
def summary
"#{@title} (#{@year})\n" + \
"Rating: #{@rating}\n" + \
"Genres: #{@genres.join(', ')}\n" + \
"Duration: #{@duration}"
end
end
end
# Usage example
movie = ImdbApi.search "Shawshank Redemption"
ap movie.summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment