Class: Nytimes::Articles::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nytimes_articles/base.rb

Direct Known Subclasses

Article, ResultSet

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
@@decode_html_entities =
true

Class Method Summary collapse

Class Method Details

.api_keyObject

Returns the current value of the API Key



35
36
37
# File 'lib/nytimes_articles/base.rb', line 35

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/



19
20
21
# File 'lib/nytimes_articles/base.rb', line 19

def self.api_key=(key)
	@@api_key = key
end

.boolean_field(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nytimes_articles/base.rb', line 62

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



41
42
43
44
45
# File 'lib/nytimes_articles/base.rb', line 41

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.to_s)}"}.join('&')
end

.date_field(value) ⇒ Object



57
58
59
60
# File 'lib/nytimes_articles/base.rb', line 57

def self.date_field(value)
	return nil unless value =~ /^\d{8}$/
	Date.strptime(value, "%Y%m%d")
end

.debug=(flag) ⇒ Object



23
24
25
# File 'lib/nytimes_articles/base.rb', line 23

def self.debug=(flag)
	@@debug = flag
end

.decode_html_entities=(flag) ⇒ Object

Set whether or not to decode HTML entities when returning text fields.



29
30
31
# File 'lib/nytimes_articles/base.rb', line 29

def self.decode_html_entities=(flag)
 @@decode_html_entities = flag
end

.integer_field(value) ⇒ Object



52
53
54
55
# File 'lib/nytimes_articles/base.rb', line 52

def self.integer_field(value)
	return nil if value.nil?
	value.to_i
end

.invoke(params = {}) ⇒ Object



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
116
117
118
119
120
121
# File 'lib/nytimes_articles/base.rb', line 79

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.message
		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



47
48
49
50
# File 'lib/nytimes_articles/base.rb', line 47

def self.text_field(value)
	return nil if value.nil?
	@@decode_html_entities ? HTMLEntities.new.decode(value) : value
end