Class: Mccandlish::Client
- Inherits:
-
Object
- Object
- Mccandlish::Client
- Includes:
- HTTParty
- Defined in:
- lib/mccandlish/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#page(page) ⇒ Object
readonly
response has maximum of 10 items, page is for pagination of results.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#query(query) ⇒ Object
readonly
Returns the value of attribute query.
-
#query_filters ⇒ Object
readonly
Returns the value of attribute query_filters.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#sort(sort) ⇒ Object
readonly
newest or oldest.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#build_request_url(params) ⇒ Object
Builds a request URI to call the API server.
- #check_response(response) ⇒ Object
-
#date(date) ⇒ Object
YYYY-MM-DD format.
- #dates(begin_date, end_date) ⇒ Object
-
#day_of_week(day) ⇒ Object
Full day name: Monday, Tuesday, Wednesday, etc.
-
#desk(desk) ⇒ Object
Foreign, Sports, Culture, etc.
-
#doc_type(doc_type) ⇒ Object
article, blogpost.
-
#initialize(api_key = nil) ⇒ Client
constructor
A new instance of Client.
- #invoke(params = {}) ⇒ Object
- #location(location) ⇒ Object
- #prepare_params(params, api_key) ⇒ Object
-
#reset ⇒ Object
clears out params, query_filters.
- #year(year) ⇒ Object
Constructor Details
#initialize(api_key = nil) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 |
# File 'lib/mccandlish/client.rb', line 13 def initialize(api_key=nil) @api_key = api_key @params = {} @query_filters = [] @uri end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def api_key @api_key end |
#page(page) ⇒ Object (readonly)
response has maximum of 10 items, page is for pagination of results
75 76 77 |
# File 'lib/mccandlish/client.rb', line 75 def page @page end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def params @params end |
#query(query) ⇒ Object (readonly)
Returns the value of attribute query.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def query @query end |
#query_filters ⇒ Object (readonly)
Returns the value of attribute query_filters.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def query_filters @query_filters end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def result @result end |
#sort(sort) ⇒ Object (readonly)
newest or oldest
69 70 71 |
# File 'lib/mccandlish/client.rb', line 69 def sort @sort end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
11 12 13 |
# File 'lib/mccandlish/client.rb', line 11 def uri @uri end |
Instance Method Details
#build_request_url(params) ⇒ Object
Builds a request URI to call the API server
28 29 30 |
# 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) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# 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) ⇒ Object
YYYY-MM-DD format
92 93 94 95 |
# File 'lib/mccandlish/client.rb', line 92 def date(date) query_filters << "pub_date:#{date}" self end |
#dates(begin_date, end_date) ⇒ Object
79 80 81 82 83 |
# 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) ⇒ Object
Full day name: Monday, Tuesday, Wednesday, etc.
86 87 88 89 |
# File 'lib/mccandlish/client.rb', line 86 def day_of_week(day) query_filters << "day_of_week:#{day}" self end |
#desk(desk) ⇒ Object
Foreign, Sports, Culture, etc.
115 116 117 118 119 |
# File 'lib/mccandlish/client.rb', line 115 def desk(desk) # validate desk query_filters << "news_desk:#{desk}" self end |
#doc_type(doc_type) ⇒ Object
article, blogpost
109 110 111 112 |
# File 'lib/mccandlish/client.rb', line 109 def doc_type(doc_type) query_filters << "document_type:#{doc_type}" self end |
#invoke(params = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# 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) ⇒ Object
102 103 104 105 106 |
# 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) ⇒ Object
32 33 34 |
# 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 ⇒ Object
clears out params, query_filters
21 22 23 24 25 |
# File 'lib/mccandlish/client.rb', line 21 def reset @params = {} @query_filters = [] self end |
#year(year) ⇒ Object
97 98 99 100 |
# File 'lib/mccandlish/client.rb', line 97 def year(year) query_filters << "pub_year:#{year}" self end |