class Mccandlish::Client
Attributes
api_key[R]
page[R]
params[R]
query[R]
query_filters[R]
result[R]
sort[R]
uri[R]
Public Class Methods
new(api_key=nil)
click to toggle source
# File lib/mccandlish/client.rb, line 13 def initialize(api_key=nil) @api_key = api_key @params = {} @query_filters = [] @uri end
Public Instance Methods
build_request_url(params)
click to toggle source
Builds a request URI to call the API server
# File lib/mccandlish/client.rb, line 28 def build_request_url(params) "http://api.nytimes.com/svc/search/v2/articlesearch.json?"+params end
check_response(response)
click to toggle source
# File lib/mccandlish/client.rb, line 45 def check_response(response) # replace with actual error handling raise "Authentication Error" if response.code == 403 raise "Bad Request" if response.code == 400 raise "Server Error" if response.code == 500 raise "Timeout" if response.code == 504 if response.code == 200 Oj.load(response.body) else return nil end end
date(date)
click to toggle source
YYYY-MM-DD format
# File lib/mccandlish/client.rb, line 92 def date(date) query_filters << "pub_date:#{date}" self end
dates(begin_date, end_date)
click to toggle source
# File lib/mccandlish/client.rb, line 79 def dates(begin_date, end_date) params['begin_date'] = begin_date if begin_date params['end_date'] = end_date if end_date self end
day_of_week(day)
click to toggle source
Full day name: Monday, Tuesday, Wednesday, etc.
# File lib/mccandlish/client.rb, line 86 def day_of_week(day) query_filters << "day_of_week:#{day}" self end
desk(desk)
click to toggle source
Foreign, Sports, Culture, etc.
# File lib/mccandlish/client.rb, line 115 def desk(desk) # validate desk query_filters << "news_desk:#{desk}" self end
doc_type(doc_type)
click to toggle source
article, blogpost
# File lib/mccandlish/client.rb, line 109 def doc_type(doc_type) query_filters << "document_type:#{doc_type}" self end
invoke(params={})
click to toggle source
# File lib/mccandlish/client.rb, line 36 def invoke(params={}) raise "You must initialize the API key before you run any API queries" if self.api_key.nil? full_params = prepare_params(params, api_key=self.api_key) @uri = build_request_url(full_params) response = HTTParty.get(@uri) parsed_response = check_response(response) Result.create_from_parsed_response(parsed_response) end
location(location)
click to toggle source
# File lib/mccandlish/client.rb, line 102 def location(location) loc = CGI.escape(location) query_filters << "glocations:#{loc}" self end
prepare_params(params, api_key)
click to toggle source
# File lib/mccandlish/client.rb, line 32 def prepare_params(params, api_key) params.map {|k,v| k+'='+v.to_s}.join('&')+"&api-key=#{api_key}" end
reset()
click to toggle source
clears out params, query_filters
# File lib/mccandlish/client.rb, line 21 def reset @params = {} @query_filters = [] self end
year(year)
click to toggle source
# File lib/mccandlish/client.rb, line 97 def year(year) query_filters << "pub_year:#{year}" self end