Class: Tmdb::Genre

Inherits:
Object
  • Object
show all
Defined in:
lib/themoviedb/genre.rb

Constant Summary collapse

@@fields =
[
  :id,
  :page,
  :total_pages,
  :total_results,
  :results
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Genre

Returns a new instance of Genre.



3
4
5
6
7
# File 'lib/themoviedb/genre.rb', line 3

def initialize(attributes = {})
  attributes.each do |key, value|
    instance_variable_set("@#{key}", value) if respond_to?(key.to_sym)
  end
end

Class Method Details

.detail(id, conditions = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/themoviedb/genre.rb', line 35

def self.detail(id, conditions = {})
  url = "/genre/#{id}/movies"
  unless conditions.empty?
    url << '?'
    conditions.each_with_index do |(key, value), index|
      url << "#{key}=#{value}"
      url << '&' if index != conditions.length - 1
    end
  end
  search = Tmdb::Search.new(url)
  new(search.fetch_response)
end

.listObject



30
31
32
33
# File 'lib/themoviedb/genre.rb', line 30

def self.list
  search = Tmdb::Search.new('/genre/list')
  search.fetch_response
end

.search(query) ⇒ Object Also known as: find



22
23
24
# File 'lib/themoviedb/genre.rb', line 22

def self.search(query)
  detail(list['genres'].detect { |g| g['name'] == query }['id'])
end

Instance Method Details

#get_page(page_number, conditions = {}) ⇒ Object



52
53
54
55
56
57
# File 'lib/themoviedb/genre.rb', line 52

def get_page(page_number, conditions = {})
  if page_number != @page && page_number > 0 && page_number <= @total_pages
    conditions[:page] = page_number
    self.class.detail(id, conditions)
  end
end

#nameObject



48
49
50
# File 'lib/themoviedb/genre.rb', line 48

def name
  @name ||= self.class.list['genres'].detect { |g| g['id'] == @id }['name']
end