Class: FilmBuff
- Inherits:
-
Object
- Object
- FilmBuff
- Defined in:
- lib/filmbuff.rb,
lib/filmbuff/title.rb,
lib/filmbuff/version.rb
Overview
Interacts with IMDb and is used to look up titles
Defined Under Namespace
Constant Summary collapse
- VERSION =
Version number of the latest gem release of Film Buff
'1.0.0'
Instance Attribute Summary collapse
-
#locale ⇒ String
The locale currently used by the IMDb instance.
Instance Method Summary collapse
-
#initialize(locale = 'en_US', ssl: true) ⇒ FilmBuff
constructor
Create a new FilmBuff instance.
-
#look_up_id(imdb_id) ⇒ Title
Looks up the title with the IMDb ID imdb_id and returns a FilmBuff::Title object with information on that title.
-
#search_for_title(title, limit: nil, types: %w(title_popular title_exact title_approx title_substring)) ⇒ Array<Hash>
Searches IMDb for the title provided and returns an array with results.
Constructor Details
#initialize(locale = 'en_US', ssl: true) ⇒ FilmBuff
Create a new FilmBuff instance
22 23 24 25 |
# File 'lib/filmbuff.rb', line 22 def initialize(locale = 'en_US', ssl: true) @locale = locale @protocol = ssl ? 'https' : 'http' end |
Instance Attribute Details
#locale ⇒ String
Returns The locale currently used by the IMDb instance.
11 12 13 |
# File 'lib/filmbuff.rb', line 11 def locale @locale end |
Instance Method Details
#look_up_id(imdb_id) ⇒ Title
Looks up the title with the IMDb ID imdb_id and returns a FilmBuff::Title object with information on that title
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/filmbuff.rb', line 51 def look_up_id(imdb_id) response = HTTP.get("#{@protocol}://app.imdb.com/title/maindetails", params: { tconst: imdb_id, locale: @locale }) if response.status != 200 fail NotFound else Title.new(response.parse['data']) end end |
#search_for_title(title, limit: nil, types: %w(title_popular title_exact title_approx title_substring)) ⇒ Array<Hash>
Searches IMDb for the title provided and returns an array with results
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/filmbuff.rb', line 84 def search_for_title(title, limit: nil, types: %w(title_popular title_exact title_approx title_substring)) response = JSON.parse(HTTP.get('http://www.imdb.com/xml/find', params: { q: title, json: '1' }).to_s) output = [] results = response.select { |type| types.include? type } results.each_key do |type| response[type].each do |hash| break unless output.size < limit if limit next unless hash['id'] && hash['title'] && hash['description'] output << build_hash(type, hash) end end fail NotFound if output.empty? output end |