Class: Tmdb::Search

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

Instance Method Summary collapse

Constructor Details

#initialize(resource = nil) ⇒ Search

Returns a new instance of Search.



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

def initialize(resource = nil)
  @params = {}
  @resource = resource.nil? ? '/search/movie' : resource
  self
end

Instance Method Details

#fetchObject

Sends back main data



59
60
61
# File 'lib/themoviedb/search.rb', line 59

def fetch
  fetch_response['results']
end

#fetch_response(conditions = {}) ⇒ Object

Send back whole response



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/themoviedb/search.rb', line 64

def fetch_response(conditions = {})
  if conditions[:external_source]
    options = @params.merge(Api.config.merge({ external_source: conditions[:external_source] }))
  else
    options = @params.merge(Api.config)
  end
  response = Api.get(@resource, query: options)

  original_etag = response.headers.fetch('etag', '')
  etag = original_etag.delete('"')

  Api.set_response('code' => response.code, 'etag' => etag)
  response.to_hash
end

#filter(conditions) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/themoviedb/search.rb', line 46

def filter(conditions)
  if conditions
    conditions.each do |key, value|
      if respond_to?(key)
        send(key, value)
      else
        @params[key] = value
      end
    end
  end
end

#primary_release_year(year) ⇒ Object



19
20
21
22
# File 'lib/themoviedb/search.rb', line 19

def primary_release_year(year)
  @params[:primary_release_year] = year.to_s
  self
end

#query(query) ⇒ Object



9
10
11
12
# File 'lib/themoviedb/search.rb', line 9

def query(query)
  @params[:query] = query.to_s
  self
end

#resource(resource) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/themoviedb/search.rb', line 24

def resource(resource)
  @resource = case resource
              when 'movie'
                '/search/movie'
              when 'collection'
                '/search/collection'
              when 'tv'
                '/search/tv'
              when 'person'
                '/search/person'
              when 'list'
                '/search/list'
              when 'company'
                '/search/company'
              when 'keyword'
                '/search/keyword'
              when 'find'
                '/find'
  end
  self
end

#year(year) ⇒ Object



14
15
16
17
# File 'lib/themoviedb/search.rb', line 14

def year(year)
  @params[:year] = year.to_s
  self
end