Class: FilmBuff

Inherits:
Object
  • Object
show all
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

Classes: NotFound, Title

Constant Summary collapse

VERSION =

Version number of the latest gem release of Film Buff

'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = 'en_US', ssl: true) ⇒ FilmBuff

Create a new FilmBuff instance

Parameters:

  • locale (String) (defaults to: 'en_US')

    The locale to search with. The FilmBuff instance will also return results in the language matching the given locale. Defaults to ‘en_US`

  • ssl (Boolean) (defaults to: true)

    Whether or not to use SSL when searching by IMDb ID (IMDb does not currently support SSL when searching by title). Defaults to ‘true`



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

#localeString

Returns The locale currently used by the IMDb instance.

Returns:

  • (String)

    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

Examples:

Basic usage

movie = imdb_instance.look_up_id('tt0032138')

Parameters:

  • imdb_id (String)

    The IMDb ID for the title to look up

Returns:

  • (Title)

    The FilmBuff::Title object containing information on the 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

Examples:

Basic usage

movie = imdb_instance.search_for_title('The Wizard of Oz')

Return only 2 results

movie = imdb_instance.search_for_title('The Wizard of Oz', limit: 2)

Only return results containing the exact title provided

movie = imdb_instance.search_for_title('The Wizard of Oz',
                                       types: %w(title_exact))

Parameters:

  • title (String)

    The title to search for

  • limit (Integer) (defaults to: nil)

    The maximum number of results to return

  • types (Array) (defaults to: %w(title_popular title_exact title_approx title_substring))

    The types of matches to search for. These types will be searched in the provided order. Can be ‘title_popular`, `title_exact`, `title_approx`, and `title_substring`

Returns:

  • (Array<Hash>)

    An array of hashes, each representing a search result



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