harrisj-nytimes-articles 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/nytimes_articles/article.rb +31 -3
- data/lib/nytimes_articles/base.rb +0 -9
- data/test/nytimes/articles/test_article.rb +75 -9
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -16,6 +16,8 @@ module Nytimes
|
|
16
16
|
|
17
17
|
ALL_FIELDS = TEXT_FIELDS + RAW_FIELDS + NUMERIC_FIELDS + BOOLEAN_FIELDS + MULTIMEDIA_FIELDS + Facet::ALL_FACETS + IMAGE_FIELDS
|
18
18
|
|
19
|
+
EARLIEST_BEGIN_DATE = '19810101'
|
20
|
+
|
19
21
|
attr_reader *ALL_FIELDS
|
20
22
|
|
21
23
|
# special additional objects
|
@@ -164,11 +166,13 @@ module Nytimes
|
|
164
166
|
# * <tt>:only_facets</tt> - takes a single value or array of facets to search. Facets can either be specified as array pairs (like <tt>[Facet::GEOGRAPHIC, 'CALIFORNIA']</tt>) or facets returned from a previous search can be passed directly. A single string can be passed as well if you have hand-crafted string.
|
165
167
|
# * <tt>:except_facets</tt> - similar to <tt>:only_facets</tt> but is used to specify a list of facets to exclude.
|
166
168
|
#
|
169
|
+
# == TIME SEARCHES
|
170
|
+
# * <tt>:begin_date</tt>, <tt>:end_date</tt> - the parameters are used to specify a start and end date for search results. BOTH of these must be provided or the API will return an error. Accepts either a Time/Date argument or a string of the format YYYYMMDD. For convenience the following alternative methods are provided
|
171
|
+
# * <tt>:before</tt> - an alternative to :end_date. Automatically adds a :before_date of sometime in 1980 if no :since argument is also provided.
|
172
|
+
# * <tt>:since</tt> - An alternative to :begin_date. Automatically adds an :end_date of Time.now if no :before argument is provided.
|
173
|
+
#
|
167
174
|
# == OTHER SEARCH FIELDS
|
168
175
|
# * <tt>:fee</tt> - if set to true, only returns articles that must be purchased. If false, returns only free articles. If not specified, returns all articles
|
169
|
-
# * <tt>:begin_date</tt>, <tt>:end_date</tt> - the parameters are used to specify a start and end date for search results. BOTH of these must be provided or the API will return an error. Accepts either a Time/Date argument or a string of the format YYYYMMDD. For convenience the following alternative methods are provided
|
170
|
-
# * <tt>:before</tt> - an alternative to :end_date. Automatically adds a :before_date of sometime in 1980 if no :since argument is also provided; to be implemented
|
171
|
-
# * <tt>:since</tt> - An alternative to :begin_date. Automatically adds an :end_date of Time.now if no :before argument is provided; to be implemented.
|
172
176
|
# * <tt>:has_thumbnail</tt> - returns only articles that have thumbnail images associated. Note that to see the thumbnails, you must specify either <tt>:thumbnail</tt> or <tt>:all</tt> in the <tt>:fields</tt> argument).
|
173
177
|
# * <tt>:has_multimedia</tt> - to be implemented
|
174
178
|
#
|
@@ -395,6 +399,30 @@ module Nytimes
|
|
395
399
|
if in_params[:end_date]
|
396
400
|
out_params['end_date'] = date_argument(:end_date, in_params[:end_date])
|
397
401
|
end
|
402
|
+
|
403
|
+
if in_params[:since]
|
404
|
+
if in_params[:begin_date]
|
405
|
+
raise ArgumentError, "You can't specify both :begin_date and :since as arguments"
|
406
|
+
end
|
407
|
+
|
408
|
+
out_params['begin_date'] = date_argument(:since, in_params[:since])
|
409
|
+
end
|
410
|
+
|
411
|
+
if in_params[:before]
|
412
|
+
if in_params[:end_date]
|
413
|
+
raise ArgumentError, "You can't specify both :end_date and :before as arguments"
|
414
|
+
end
|
415
|
+
|
416
|
+
out_params['end_date'] = date_argument(:before, in_params[:before])
|
417
|
+
end
|
418
|
+
|
419
|
+
if in_params[:before] && out_params['begin_date'].nil?
|
420
|
+
out_params['begin_date'] = EARLIEST_BEGIN_DATE
|
421
|
+
end
|
422
|
+
|
423
|
+
if in_params[:since] && out_params['end_date'].nil?
|
424
|
+
out_params['end_date'] = date_argument(:end_date, Time.now)
|
425
|
+
end
|
398
426
|
end
|
399
427
|
|
400
428
|
def self.add_offset_params(out_params, in_params)
|
@@ -11,15 +11,8 @@ module Nytimes
|
|
11
11
|
API_BASE = "/svc/search/#{API_VERSION}/#{API_NAME}"
|
12
12
|
|
13
13
|
@@api_key = nil
|
14
|
-
@@copyright = nil
|
15
14
|
@@debug = false
|
16
15
|
|
17
|
-
##
|
18
|
-
# 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
|
-
def self.copyright
|
20
|
-
@@copyright
|
21
|
-
end
|
22
|
-
|
23
16
|
##
|
24
17
|
# 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 http://developer.nytimes.com/
|
25
18
|
def self.api_key=(key)
|
@@ -82,8 +75,6 @@ module Nytimes
|
|
82
75
|
# FIXME
|
83
76
|
#end
|
84
77
|
|
85
|
-
@@copyright = parsed_reply['copyright']
|
86
|
-
|
87
78
|
parsed_reply
|
88
79
|
rescue OpenURI::HTTPError => e
|
89
80
|
# FIXME: Return message from body?
|
@@ -55,18 +55,84 @@ class TestNytimes::TestArticles::TestArticle < Test::Unit::TestCase
|
|
55
55
|
should "raise an ArgumentError if the end_date is NOT a string and does not respond_to strftime" do
|
56
56
|
assert_raise(ArgumentError) { Article.search :end_date => 23 }
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
context ":before" do
|
60
|
-
should "
|
61
|
-
|
62
|
-
|
60
|
+
should "send the :before value through as a end_date" do
|
61
|
+
t = Time.now
|
62
|
+
Article.expects(:invoke).with(has_entry('end_date', t.strftime("%Y%m%d")))
|
63
|
+
Article.search :before => t
|
64
|
+
end
|
65
|
+
|
66
|
+
should "not send through :before as an argument to the API" do
|
67
|
+
t = Time.now
|
68
|
+
Article.expects(:invoke).with(Not(has_key('before')))
|
69
|
+
Article.search :before => t
|
70
|
+
end
|
71
|
+
|
72
|
+
should "raise an ArgumentError if the before_date is NOT a string and does not respond_to strftime" do
|
73
|
+
assert_raise(ArgumentError) { Article.search :before => 23 }
|
74
|
+
end
|
75
|
+
|
76
|
+
should "add a begin_date in 1980 if no :since or :begin_date argument is provided" do
|
77
|
+
Article.expects(:invoke).with(has_entry('begin_date', Article::EARLIEST_BEGIN_DATE))
|
78
|
+
Article.search :before => Time.now
|
79
|
+
end
|
80
|
+
|
81
|
+
should "not automatically add a begin_date is there is a :since argument" do
|
82
|
+
since = Time.now - 12000
|
83
|
+
Article.expects(:invoke).with(has_entry('begin_date', since.strftime("%Y%m%d")))
|
84
|
+
Article.search :before => Time.now, :since => since
|
85
|
+
end
|
86
|
+
|
87
|
+
should "not automatically add a begin_date if there is a :begin_date argument already" do
|
88
|
+
since = Time.now - 12000
|
89
|
+
Article.expects(:invoke).with(has_entry('begin_date', since.strftime("%Y%m%d")))
|
90
|
+
Article.search :before => Time.now, :begin_date => since
|
91
|
+
end
|
92
|
+
|
93
|
+
should "raise an ArgumentError if there is also an :end_date argument" do
|
94
|
+
assert_raise(ArgumentError) { Article.search :before => Time.now, :end_date => Time.now }
|
95
|
+
end
|
63
96
|
end
|
64
|
-
|
97
|
+
|
65
98
|
context ":since" do
|
66
|
-
should "
|
67
|
-
|
68
|
-
|
69
|
-
|
99
|
+
should "send the :since value through as a begin_date" do
|
100
|
+
t = Time.now - 1200
|
101
|
+
Article.expects(:invoke).with(has_entry('begin_date', t.strftime("%Y%m%d")))
|
102
|
+
Article.search :since => t
|
103
|
+
end
|
104
|
+
|
105
|
+
should "not send through :since as an argument to the API" do
|
106
|
+
t = Time.now
|
107
|
+
Article.expects(:invoke).with(Not(has_key('since')))
|
108
|
+
Article.search :since => t
|
109
|
+
end
|
110
|
+
|
111
|
+
should "raise an ArgumentError if the before_date is NOT a string and does not respond_to strftime" do
|
112
|
+
assert_raise(ArgumentError) { Article.search :since => 23 }
|
113
|
+
end
|
114
|
+
|
115
|
+
should "add a end_date of today if no :before or :end_date argument is provided" do
|
116
|
+
Article.expects(:invoke).with(has_entry('end_date', Time.now.strftime("%Y%m%d")))
|
117
|
+
Article.search :since => Time.now
|
118
|
+
end
|
119
|
+
|
120
|
+
should "not automatically add a end_date is there is a :before argument" do
|
121
|
+
since = '19990101'
|
122
|
+
Article.expects(:invoke).with(has_entry('end_date', '20030101'))
|
123
|
+
Article.search :before => '20030101', :since => since
|
124
|
+
end
|
125
|
+
|
126
|
+
should "not automatically add a end_date if there is a :end_date argument already" do
|
127
|
+
since = '19990101'
|
128
|
+
Article.expects(:invoke).with(has_entry('end_date', '20030101'))
|
129
|
+
Article.search :end_date => '20030101', :since => since
|
130
|
+
end
|
131
|
+
|
132
|
+
should "raise an ArgumentError if there is also an :begin_date argument" do
|
133
|
+
assert_raise(ArgumentError) { Article.search :since => Time.now, :begin_date => Time.now }
|
134
|
+
end
|
135
|
+
end
|
70
136
|
end
|
71
137
|
|
72
138
|
context "facets" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: harrisj-nytimes-articles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Harris
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-23 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|