Class: Tmdby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tmdby/client.rb

Constant Summary collapse

@@netloc =
"api.themoviedb.org/3"

Class Method Summary collapse

Class Method Details

.api_call(method_call, api_route, params = {}, post_params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tmdby/client.rb', line 17

def self.api_call(method_call, api_route, params = {}, post_params = {})
  if Tmdby::Setup.key
    uri = self.get_uri api_route, params
    # puts "[#{method_call}] #{uri}"

    case method_call
    when "get"
      response = Net::HTTP.get_response(uri)
    when "post"
      response = Net::HTTP.post_form(uri, post_params)
    when "delete"
      response = Net::HTTP.new(uri.host, uri.port).delete("#{uri.path}?#{uri.query}")
    else
      raise RuntimeError, "An error has occured : \"#{method_call}\" is unknown method_call"
    end

    if response.is_a? Net::HTTPSuccess
      Tmdby::Response.new(response, uri, method_call, post_params)
    else
      raise RuntimeError, "An error has occured : #{response.body}"
    end
  else
    raise RuntimeError, 'An error has occured : please specify TMDB API KEY'
  end
end

.get_uri(api_route, params = {}) ⇒ Object

Returns complete api route to call



10
11
12
13
14
# File 'lib/tmdby/client.rb', line 10

def self.get_uri(api_route, params = {})
  params["api_key"] = Tmdby::Setup.key

  URI("#{Tmdby::Setup.api_scheme}://#{@@netloc}/#{api_route}?#{URI.encode_www_form(params)}")
end