Class: Play::Tmdb::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/play-tmdb/base.rb

Constant Summary collapse

@@options =
Tmdb::Base.default_options
@@base_url =
"http://api.themoviedb.org/3/"

Class Method Summary collapse

Class Method Details

.api_call(method, params = {}) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
# File 'lib/play-tmdb/base.rb', line 66

def self.api_call(method, params = {})
  raise ArgumentError.new("api method is required") if method.empty?

  url = build_api_url(method, params)
  response = Play::Tmdb::Base.get_url(url)

  body = JSON(response.body)
  OpenStruct.new body
end

.base_urlObject



43
44
45
# File 'lib/play-tmdb/base.rb', line 43

def self.base_url
  @@base_url
end

.default_optionsObject



11
12
13
14
15
16
# File 'lib/play-tmdb/base.rb', line 11

def self.default_options
  {
      api_key: "",
      language: "en"
  }
end

.get_url(uri_str, limit = 10) ⇒ Object

Get a URL and return a response object, follow upto ‘limit’ re-directs on the way



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/play-tmdb/base.rb', line 48

def self.get_url(uri_str, limit = 10)
  return false if limit == 0
  begin
    response = Net::HTTP.get_response(URI.parse(uri_str))
  rescue SocketError, Errno::ENETDOWN
    response = Net::HTTPBadRequest.new('404', 404, "Not Found")
    return response
  end
  case response
    when Net::HTTPSuccess then
      response
    when Net::HTTPRedirection then
      get_url(response['location'], limit - 1)
    else
      Net::HTTPBadRequest.new('404', 404, "Not Found")
  end
end

.optionsObject



39
40
41
# File 'lib/play-tmdb/base.rb', line 39

def self.options
  @@options
end

.options=(options) ⇒ Object



35
36
37
# File 'lib/play-tmdb/base.rb', line 35

def self.options=(options)
  @@options=options
end