Class: Nytimes::Articles::Base
- Inherits:
-
Object
- Object
- Nytimes::Articles::Base
- Defined in:
- lib/nytimes_articles/base.rb
Constant Summary collapse
- API_SERVER =
'api.nytimes.com'- API_VERSION =
'v1'- API_NAME =
'article'- API_BASE =
"/svc/search/#{API_VERSION}/#{API_NAME}"- @@api_key =
nil- @@debug =
false
Class Method Summary collapse
-
.api_key ⇒ Object
Returns the current value of the API Key.
-
.api_key=(key) ⇒ Object
Set the API key used for operations.
- .boolean_field(value) ⇒ Object
-
.build_request_url(params) ⇒ Object
Builds a request URI to call the API server.
- .date_field(value) ⇒ Object
- .debug=(flag) ⇒ Object
- .integer_field(value) ⇒ Object
- .invoke(params = {}) ⇒ Object
- .text_field(value) ⇒ Object
Class Method Details
.api_key ⇒ Object
Returns the current value of the API Key
28 29 30 |
# File 'lib/nytimes_articles/base.rb', line 28 def self.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/
18 19 20 |
# File 'lib/nytimes_articles/base.rb', line 18 def self.api_key=(key) @@api_key = key end |
.boolean_field(value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/nytimes_articles/base.rb', line 56 def self.boolean_field(value) case value when nil false when TrueClass true when FalseClass false when 'Y' true when 'N' false else false end end |
.build_request_url(params) ⇒ Object
Builds a request URI to call the API server
34 35 36 37 38 |
# File 'lib/nytimes_articles/base.rb', line 34 def self.build_request_url(params) URI::HTTP.build :host => API_SERVER, :path => API_BASE, :query => params.map {|k,v| "#{URI.escape(k)}=#{URI.escape(v)}"}.join('&') end |
.date_field(value) ⇒ Object
51 52 53 54 |
# File 'lib/nytimes_articles/base.rb', line 51 def self.date_field(value) return nil unless value =~ /^\d{8}$/ Date.strptime(value, "%Y%m%d") end |
.debug=(flag) ⇒ Object
22 23 24 |
# File 'lib/nytimes_articles/base.rb', line 22 def self.debug=(flag) @@debug = flag end |
.integer_field(value) ⇒ Object
46 47 48 49 |
# File 'lib/nytimes_articles/base.rb', line 46 def self.integer_field(value) return nil if value.nil? value.to_i end |
.invoke(params = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/nytimes_articles/base.rb', line 73 def self.invoke(params={}) begin if @@api_key.nil? raise AuthenticationError, "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(full_params) puts "REQUEST: #{uri}" if @@debug reply = uri.read parsed_reply = JSON.parse reply if parsed_reply.nil? raise BadResponseError, "Empty reply returned from API" end #case parsed_reply['status'] # FIXME #end parsed_reply rescue OpenURI::HTTPError => e # FIXME: Return message from body? case e. when /^400/ raise BadRequestError when /^403/ raise AuthenticationError when /^404/ return nil when /^500/ raise ServerError else raise ConnectionError end raise "Error connecting to URL #{uri} #{e}" rescue JSON::ParserError => e raise BadResponseError, "Invalid JSON returned from API:\n#{reply}" end end |
.text_field(value) ⇒ Object
40 41 42 43 44 |
# File 'lib/nytimes_articles/base.rb', line 40 def self.text_field(value) return nil if value.nil? coder = HTMLEntities.new coder.decode(value) end |