Class: Nytimes::Movies::Base
- Inherits:
-
Object
- Object
- Nytimes::Movies::Base
- Defined in:
- lib/nytimes/movies/base.rb
Constant Summary collapse
- API_SERVER =
'api.nytimes.com'
- API_VERSION =
'v2'
- API_NAME =
'movies'
- API_BASE =
"/svc/#{API_NAME}/#{API_VERSION}"
- @@api_key =
nil
- @@copyright =
nil
Class Method Summary collapse
- .api_key ⇒ Object
-
.api_key=(key) ⇒ Object
Set the API key used for operations.
-
.build_request_url(path, params) ⇒ Object
Builds a request URI to call the API server.
-
.copyright ⇒ Object
The copyright footer to be placed at the bottom of any data from the New York Times.
- .invoke(path, params = {}) ⇒ Object
Class Method Details
.api_key ⇒ Object
29 30 31 |
# File 'lib/nytimes/movies/base.rb', line 29 def api_key @@api_key end |
.api_key=(key) ⇒ Object
Set the API key used for operations. This needs to be called before any requests against the API. To obtain an API key, go to developer.nytimes.com/
25 26 27 |
# File 'lib/nytimes/movies/base.rb', line 25 def api_key=(key) @@api_key = key end |
.build_request_url(path, params) ⇒ Object
Builds a request URI to call the API server
35 36 37 38 39 |
# File 'lib/nytimes/movies/base.rb', line 35 def build_request_url(path, params) URI::HTTP.build :host => API_SERVER, :path => "#{API_BASE}/#{path}", :query => params.map {|k,v| "#{k}=#{v}"}.join('&') end |
.copyright ⇒ Object
The copyright footer to be placed at the bottom of any data from the New York Times. Note this is only set after an API call.
19 20 21 |
# File 'lib/nytimes/movies/base.rb', line 19 def copyright @@copyright end |
.invoke(path, params = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nytimes/movies/base.rb', line 41 def invoke(path, params={}) begin if @@api_key.nil? raise "You must initialize the API key before you run any API queries" end full_params = params.merge 'api-key' => @@api_key uri = build_request_url(path, full_params) # puts "Request [#{uri}]" reply = uri.read parsed_reply = JSON.parse reply if parsed_reply.nil? # FIXME raise "Empty reply returned from API" end #case parsed_reply['status'] # FIXME #end @@copyright = parsed_reply['copyright'] parsed_reply rescue OpenURI::HTTPError => e if e. =~ /^404/ return nil end raise "Error connecting to URL #{uri} #{e}" #rescue JSON::ParserError => e # raise RuntimeError, "Invalid JSON returned from CRNR:\n#{reply}" end end |