harrisj-nytimes-articles 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +4 -0
- data/lib/nytimes_articles.rb +8 -0
- data/lib/nytimes_articles/article.rb +397 -0
- data/lib/nytimes_articles/base.rb +110 -0
- data/lib/nytimes_articles/exceptions.rb +24 -0
- data/lib/nytimes_articles/facet.rb +70 -0
- data/lib/nytimes_articles/result_set.rb +40 -0
- data/test/nytimes/articles/test_article.rb +527 -0
- data/test/nytimes/articles/test_base.rb +92 -0
- data/test/nytimes/articles/test_facet.rb +46 -0
- data/test/nytimes/articles/test_result_set.rb +62 -0
- data/test/test_helper.rb +31 -0
- metadata +78 -0
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
|
3
|
+
class TestNytimes::TestArticles::TestBase < Test::Unit::TestCase
|
4
|
+
include Nytimes::Articles
|
5
|
+
context "Base.build_request_url" do
|
6
|
+
should "call v1 of the API" do
|
7
|
+
assert_match %r{v1}, api_url_for
|
8
|
+
end
|
9
|
+
|
10
|
+
should "be of the form /svc/search/VERSION/article" do
|
11
|
+
assert_match %r{/svc/search/[^/]+/article}, api_url_for
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "Base.invoke" do
|
16
|
+
setup do
|
17
|
+
init_test_key
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when the API key has not been set" do
|
21
|
+
setup do
|
22
|
+
Base.api_key = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
should "raise an AuthenticationError" do
|
26
|
+
assert_raise(AuthenticationError) do
|
27
|
+
Base.invoke
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when the Articles API returns a 403 forbidden error (API key issue)" do
|
33
|
+
setup do
|
34
|
+
FakeWeb.register_uri(api_url_for, :status => ["403", "Forbidden"])
|
35
|
+
end
|
36
|
+
|
37
|
+
should "raise an AuthenticationError" do
|
38
|
+
assert_raise(AuthenticationError) do
|
39
|
+
Base.invoke
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when the Articles API returns a 400 bad request error" do
|
45
|
+
setup do
|
46
|
+
FakeWeb.register_uri(api_url_for, :status => ["400", "Bad Request"])
|
47
|
+
end
|
48
|
+
|
49
|
+
should "raise an BadRequestError" do
|
50
|
+
assert_raise(BadRequestError) do
|
51
|
+
Base.invoke
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "When calling the Articles API returns a 500 server error" do
|
57
|
+
setup do
|
58
|
+
FakeWeb.register_uri(api_url_for, :status => ["500", "Server Error"])
|
59
|
+
end
|
60
|
+
|
61
|
+
should "raise an ServerError" do
|
62
|
+
assert_raise(ServerError) do
|
63
|
+
Base.invoke
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when the Articles API returns any other HTTP error" do
|
69
|
+
setup do
|
70
|
+
FakeWeb.register_uri(api_url_for, :status => ["502", "Random Error"])
|
71
|
+
end
|
72
|
+
|
73
|
+
should "raise an ConnectionError" do
|
74
|
+
assert_raise(ConnectionError) do
|
75
|
+
Base.invoke
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "When the Articles API returns JSON that can't be parsed" do
|
81
|
+
setup do
|
82
|
+
FakeWeb.register_uri(api_url_for, :text => "e2131421212121221 => 3i109sdfvinp;2 112")
|
83
|
+
end
|
84
|
+
|
85
|
+
should "raise an BadResponseError" do
|
86
|
+
assert_raise(BadResponseError) do
|
87
|
+
Base.invoke
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
|
3
|
+
class TestNytimes::TestArticles::TestFacet < Test::Unit::TestCase
|
4
|
+
include Nytimes::Articles
|
5
|
+
|
6
|
+
context "read-only attributes" do
|
7
|
+
setup do
|
8
|
+
@facet = Facet.new("facet_type", "term", 103)
|
9
|
+
end
|
10
|
+
|
11
|
+
%w(term count facet_type).each do |field|
|
12
|
+
should "have a #{field} attribute" do
|
13
|
+
assert @facet.respond_to?(field)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "be read-only for the #{field} attribute" do
|
17
|
+
assert_raise(NoMethodError) do
|
18
|
+
@facet.send "#{field}=", "new value"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "Facet.init_from_api" do
|
25
|
+
setup do
|
26
|
+
@hash = {"date" => [{"count" => 36 , "term" => "19960630"} , {"count" => 36 , "term" => "19990523"}]}
|
27
|
+
@facets = Facet.init_from_api(@hash)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "return nil if reply from API has no facets section" do
|
31
|
+
assert_nil Facet.init_from_api(nil)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return a hash indexed by the facet name from the API" do
|
35
|
+
assert_kind_of(Hash, @facets)
|
36
|
+
assert_same_elements @hash.keys, @facets.keys
|
37
|
+
end
|
38
|
+
|
39
|
+
should "return an array of Facet objects as the value for a key" do
|
40
|
+
first_key = @facets.keys.first
|
41
|
+
assert_kind_of Array, @facets[first_key]
|
42
|
+
assert @facets[first_key].all? {|f| f.is_a? Facet }
|
43
|
+
assert @facets[first_key].all? {|f| f.facet_type == first_key }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
|
3
|
+
RESULT_SET_HASH = {"results"=>[{"lead_paragraph"=>"", "classifiers_facet"=>["Top/News/Business", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/News/Business/Markets"], "title"=>"Wall Street on Even Keel After Sell-Off", "nytd_title"=>"Wall Street on Even Keel After Sell-Off", "byline"=>"By JACK HEALY and MATTHEW SALTMARSH", "body"=>"A day after fleeing stocks in a fierce sell-off, investors on Wall Street stayed mostly at bay on Wednesday. Shares on Wall Street were hovering slightly higher at midday Wednesday as investors tried to get a better grip on Washington’s newly announced plans to stabilize the shaky banking system with up to $2.5 trillion in public and private", "material_type_facet"=>["News"], "url"=>"http://www.nytimes.com/2009/02/12/business/12markets.html", "publication_month"=>"02", "date"=>"20090212", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Business", "publication_day"=>"12", "des_facet"=>["STOCKS AND BONDS"], "day_of_week_facet"=>"Thursday"}, {"page_facet"=>"8", "lead_paragraph"=>"", "classifiers_facet"=>["Top/News/Business", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/News/Business/Markets"], "title"=>"Wall St. Treads Water as It Waits on Washington", "nytd_title"=>"Wall St. Treads Water as It Waits on Washington", "byline"=>"By JACK HEALY", "body"=>"Wall Street held its breath on Monday as it awaited details on a banking bailout from Washington. Investors had expected to start the week with an announcement from the Treasury Department outlining its latest plans to stabilize the financial system. But the Obama administration delayed releasing the details until at least Tuesday to keep the focus", "material_type_facet"=>["News"], "url"=>"http://www.nytimes.com/2009/02/10/business/10markets.html", "publication_month"=>"02", "date"=>"20090210", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Business", "publication_day"=>"10", "des_facet"=>["STOCKS AND BONDS"], "day_of_week_facet"=>"Tuesday"}, {"lead_paragraph"=>"", "geo_facet"=>["WALL STREET (NYC)"], "classifiers_facet"=>["Top/Opinion/Opinion/Letters", "Top/Opinion", "Top/Opinion/Opinion", "Top/Features/Travel/Guides/Destinations/North America", "Top/Features/Travel/Guides/Destinations/North America/United States/New York", "Top/Features/Travel/Guides/Destinations/North America/United States"], "title"=>"LETTER; Wall Street Blues", "nytd_title"=>"Wall Street Blues", "body"=>"To the Editor: Re “Wall St., a Financial Epithet, Stirs Outrage and Punch Lines” (front page, Feb. 3): I have some news for the Wall Street worker who ponders working for a nonprofit from 8 to 4 instead of on Wall Street, where long hours are the norm. I was a college administrator and English teacher for 15 years. Every night and all", "material_type_facet"=>["Letter"], "url"=>"http://www.nytimes.com/2009/02/09/opinion/lweb09econ.html", "publication_month"=>"02", "date"=>"20090209", "column_facet"=>"Letter", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Opinion", "publication_day"=>"09", "des_facet"=>["NONPROFIT ORGANIZATIONS", "EXECUTIVE COMPENSATION"], "day_of_week_facet"=>"Monday"}, {"page_facet"=>"29", "lead_paragraph"=>"", "geo_facet"=>["WALL STREET (NYC)"], "small_image_width"=>"75", "classifiers_facet"=>["Top/News/New York and Region", "Top/Classifieds/Job Market/Job Categories/Education", "Top/Features/Travel/Guides/Destinations/North America", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/Features/Travel/Guides/Destinations/North America/United States/New York", "Top/Features/Travel/Guides/Destinations/North America/United States", "Top/News/Education"], "title"=>"OUR TOWNS; As Pipeline to Wall Street Narrows, Princeton Students Adjust Sights", "nytd_title"=>"As Pipeline to Wall Street Narrows, Princeton Students Adjust Sights", "byline"=>"By PETER APPLEBOME", "body"=>"Princeton, N.J. There must be a screenplay in the fabulous Schoppe twins, Christine and Jennifer, Princeton University juniors from Houston. They had the same G.P.A. and SATs in high school, where they became Gold Award Girl Scouts , sort of the female version of Eagle Scouts. They live together and take all the same courses, wear identical", "material_type_facet"=>["News"], "url"=>"http://www.nytimes.com/2009/02/08/nyregion/08towns.html", "publication_month"=>"02", "small_image_height"=>"75", "date"=>"20090208", "column_facet"=>"Our Towns", "small_image"=>"Y", "publication_year"=>"2009", "source_facet"=>"The New York Times", "org_facet"=>["PRINCETON UNIVERSITY"], "desk_facet"=>"New York Region", "publication_day"=>"08", "small_image_url"=>"http://graphics8.nytimes.com/images/2009/02/08/nyregion/08towns.751.jpg", "des_facet"=>["EDUCATION AND SCHOOLS", "BANKS AND BANKING"], "day_of_week_facet"=>"Sunday"}, {"page_facet"=>"1", "lead_paragraph"=>"", "geo_facet"=>["WALL STREET (NYC)"], "classifiers_facet"=>["Top/News/Business", "Top/News/Business/Markets", "Top/Features/Travel/Guides/Destinations/North America", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/Features/Travel/Guides/Destinations/North America/United States/New York", "Top/Features/Travel/Guides/Destinations/North America/United States"], "title"=>"Wall St. Pay Is Cyclical. Guess Where We Are Now.", "nytd_title"=>"Wall St. Pay Is Cyclical. Guess Where We Are Now.", "byline"=>"By STEVE LOHR", "body"=>"To most people, a salary cap of $500,000 would be anything but punishment. But in Wall Street’s executive suites, it amounts to a humbling pay cut — and, just maybe, the beginning of a cultural shift. True, the rich always seem to find new ways to get richer. But in the sweep of history, high pay on Wall Street comes and goes through", "material_type_facet"=>["News"], "url"=>"http://www.nytimes.com/2009/02/05/business/05bonus.html", "publication_month"=>"02", "date"=>"20090205", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Business", "publication_day"=>"05", "des_facet"=>["BONUSES", "BANKS AND BANKING", "SUBPRIME MORTGAGE CRISIS", "EXECUTIVE COMPENSATION"], "day_of_week_facet"=>"Thursday"}, {"page_facet"=>"26", "lead_paragraph"=>"", "geo_facet"=>["WALL STREET (NYC)"], "small_image_width"=>"75", "classifiers_facet"=>["Top/Opinion/Opinion/Letters", "Top/Opinion", "Top/Opinion/Opinion", "Top/Features/Travel/Guides/Destinations/North America", "Top/Features/Travel/Guides/Destinations/North America/United States/New York", "Top/Features/Travel/Guides/Destinations/North America/United States"], "title"=>"LETTERS; Wall St. and the $500,000 Question", "nytd_title"=>"Wall St. and the $500,000 Question", "body"=>"To the Editor: Re “Amid Fury, U.S. Is Set to Curb Executives’ Pay After Bailouts” (front page, Feb. 4): It may be that the $500,000 cap is a bit extreme, but America should celebrate that there is at last some mechanism to rein in top corporate compensation. Public outrage and shareholder revolts have not managed to do so, since", "material_type_facet"=>["Letter"], "url"=>"http://www.nytimes.com/2009/02/05/opinion/l05econ.html", "publication_month"=>"02", "small_image_height"=>"75", "date"=>"20090205", "column_facet"=>"Letters", "small_image"=>"Y", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Opinion", "publication_day"=>"05", "small_image_url"=>"http://graphics8.nytimes.com/images/2009/02/05/opinion/05letter.75.gif", "des_facet"=>["EMERGENCY ECONOMIC STABILIZATION ACT (2008)", "EXECUTIVES AND MANAGEMENT", "WAGES AND SALARIES", "SUBPRIME MORTGAGE CRISIS"], "day_of_week_facet"=>"Thursday"}, {"page_facet"=>"1", "lead_paragraph"=>"", "geo_facet"=>["UNITED STATES"], "classifiers_facet"=>["Top/News/World/Countries and Territories/United States", "Top/News/Business/Personal Finance", "Top/News/Front Page", "Top/Features/Travel/Guides/Destinations/North America", "Top/News/Business", "Top/News/Business/Markets", "Top/Classifieds/Job Market/Job Categories/Executive", "Top/News", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/Features/Travel/Guides/Destinations/North America/United States"], "title"=>"Wall St., a Financial Epithet, Stirs Outrage and Punch Lines", "nytd_title"=>"Wall St., a Financial Epithet, Stirs Outrage", "byline"=>"By DAVID SEGAL", "body"=>"Monday was the last day of Iris Chau's 11-year career at JPMorgan Chase and she says there's a lot she'll miss about the job, including her colleagues, her paycheck and her role managing a technical support team. But one thing she won't miss about JPMorgan: telling people that she works there. ''For a long time, it was kind of glamorous and I had", "material_type_facet"=>["News"], "word_count"=>"1472", "url"=>"http://www.nytimes.com/2009/02/03/business/03bankers.html", "publication_month"=>"02", "abstract"=>"Financial industry has become national pariah, as country fumes over billions spent on bailouts and bonuses; many workers feel they are unfairly singled out for blame for crisis that was created by numerous players; there has been huge loss of status and cultural cachet as industry has become target of rage, raw material for talk-show tirades and comedians; fall in stature has taken toll on those industry veterans who are even willing to discuss it and, when they do, they attribute much of financial crisis to others; photos (M)", "small_image_height"=>"75", "date"=>"20090203", "fee"=>"N", "small_image"=>"Y", "publication_year"=>"2009", "source_facet"=>"The New York Times", "nytd_lead_paragraph"=>"As investment bankers adjust to a new role as targets of scorn, they aren\342\200\231t expecting pity, or a sympathetic ear.", "desk_facet"=>"Business/Financial Desk", "publication_day"=>"03", "small_image_url"=>"http://graphics8.nytimes.com/images/2009/02/02/business/03bankers_75.jpg", "nytd_des_facet"=>["Executives and Management", "Investment Banking", "Personal Finances", "Layoffs and Job Reductions", "High Net Worth Individuals", "Bonuses", "Executive Compensation", "Banks and Banking"], "des_facet"=>["EXECUTIVES AND MANAGEMENT", "INVESTMENT BANKING", "PERSONAL FINANCES", "BANKS AND BANKING", "LAYOFFS AND JOB REDUCTIONS", "LABOR", "BONUSES", "STOCKS AND BONDS", "BUDGETS AND BUDGETING", "EXECUTIVE COMPENSATION", "HIGH NET WORTH INDIVIDUALS", "FINANCES"], "day_of_week_facet"=>"Tuesday"}, {"page_facet"=>"20", "lead_paragraph"=>"", "classifiers_facet"=>["Top/Opinion/Opinion/Letters", "Top/Opinion", "Top/Opinion/Opinion"], "title"=>"LETTERS; Wall St. Bonuses: Enough Is Enough", "body"=>"To the Editor: Re ''Money for Nothing,'' by Dave Krasne (Op-Ed, Jan. 27): In the discussion of regulating executive compensation, the question has been asked, ''Without the most significant of financial rewards, how will we attract the very best to occupy these lofty positions?'' The implicit assumption of this statement is that society's very best", "material_type_facet"=>["Letter"], "word_count"=>"760", "url"=>"http://query.nytimes.com/gst/fullpage.html?res=9401E3D91431F931A35751C0A96F9C8B63", "publication_month"=>"02", "date"=>"20090202", "column_facet"=>"LETTERS", "publication_year"=>"2009", "source_facet"=>"The New York Times", "desk_facet"=>"Editorial Desk", "publication_day"=>"02", "day_of_week_facet"=>"Monday"}, {"page_facet"=>"11", "lead_paragraph"=>"", "classifiers_facet"=>["Top/Opinion/Opinion/Editorials", "Top/Opinion", "Top/News/Washington/Campaign 2008/Candidates", "Top/News/Washington/Campaign 2008", "Top/Opinion/Opinion"], "title"=>"OP-ED COLUMNIST; Disgorge, Wall Street Fat Cats", "nytd_title"=>"Disgorge, Wall Street Fat Cats", "byline"=>"By MAUREEN DOWD", "body"=>"The president's disgust at Wall Street looters was good. But we need more. We need disgorgement. Disgorgement is when courts force wrongdoers to repay ill-gotten gains. And I'm ill at the gains gotten by scummy executives acting all Gordon Gekko while they're getting bailed out by us. With the equally laconic Tim Geithner beside him, Mr. Obama", "material_type_facet"=>["Editorial"], "word_count"=>"807", "url"=>"http://www.nytimes.com/2009/02/01/opinion/01dowd.html", "publication_month"=>"02", "date"=>"20090201", "column_facet"=>"OP-ED COLUMNIST", "fee"=>"N", "publication_year"=>"2009", "source_facet"=>"The New York Times", "nytd_lead_paragraph"=>"The president\342\200\231s disgust at Wall Street looters was good. But we need more. We need disgorgement. Disgorgement is when courts force wrongdoers to repay ill-gotten gains.", "desk_facet"=>"Editorial Desk", "publication_day"=>"01", "per_facet"=>["OBAMA, BARACK"], "nytd_des_facet"=>["United States Economy", "Executive Compensation", "Subprime Mortgage Crisis"], "des_facet"=>["UNITED STATES ECONOMY", "EXECUTIVE COMPENSATION", "SUBPRIME MORTGAGE CRISIS"], "day_of_week_facet"=>"Sunday"}, {"page_facet"=>"1", "lead_paragraph"=>"", "geo_facet"=>["WALL STREET (NYC)"], "classifiers_facet"=>["Top/News/Front Page", "Top/Features/Travel/Guides/Destinations/North America", "Top/News/Business", "Top/News/Business/Markets", "Top/News/Washington/Campaign 2008", "Top/News", "Top/Classifieds/Job Market/Job Categories/Banking, Finance and Insurance", "Top/News/Washington/Campaign 2008/Candidates", "Top/Features/Travel/Guides/Destinations/North America/United States/New York", "Top/Features/Travel/Guides/Destinations/North America/United States"], "title"=>"What Red Ink? Wall St. Paid Hefty Bonuses", "nytd_title"=>"What Red Ink? Wall Street Paid Hefty Bonuses", "byline"=>"By BEN WHITE; Paul J. Sullivan contributed reporting.", "body"=>"By almost any measure, 2008 was a complete disaster for Wall Street -- except, that is, when the bonuses arrived. Despite crippling losses, multibillion-dollar bailouts and the passing of some of the most prominent names in the business, employees at financial companies in New York, the now-diminished world capital of capital, collected an", "material_type_facet"=>["News"], "word_count"=>"992", "url"=>"http://www.nytimes.com/2009/01/29/business/29bonus.html", "publication_month"=>"01", "abstract"=>"Wall Street paid out estimated $18.4 billion in bonuses last year, despite crippling losses, multi-billion-dollar bailouts and veritable banishment of well-known heads of financial firms; New York State Comptroller Thomas DiNapoli reports that it is sixth largest amount on record, matching 2004 when Dow Jones industrial average was above 10,000; estimate is based on personal income-tax collections and excludes stock option awards that could push figure even higher; DiNapoli says it is unclear whether bailouts helped make bonuses possible and urges Obama administration to examine issue; graphs (M)", "date"=>"20090129", "fee"=>"N", "publication_year"=>"2009", "source_facet"=>"The New York Times", "nytd_lead_paragraph"=>"Despite crippling losses in 2008, financial employees in New York collected an estimated $18.4 billion in bonuses for the year.", "desk_facet"=>"Business/Financial Desk", "publication_day"=>"29", "nytd_geo_facet"=>["Wall Street (NYC)"], "per_facet"=>["OBAMA, BARACK", "DINAPOLI, THOMAS P"], "nytd_des_facet"=>["Executive Compensation", "High Net Worth Individuals", "Bonuses", "Subprime Mortgage Crisis", "Brokers and Brokerage Firms"], "des_facet"=>["STOCK OPTIONS AND PURCHASE PLANS", "SUBPRIME MORTGAGE CRISIS", "HIGH NET WORTH INDIVIDUALS", "BONUSES", "STOCKS AND BONDS", "WAGES AND SALARIES", "EXECUTIVE COMPENSATION", "ETHICS", "BROKERS AND BROKERAGE FIRMS"], "day_of_week_facet"=>"Thursday"}], "total"=>3292, "tokens"=>["title:wall", "street"], "offset"=>"0"}
|
4
|
+
|
5
|
+
class TestNytimes::TestArticles::TestResultSet < Test::Unit::TestCase
|
6
|
+
include Nytimes::Articles
|
7
|
+
|
8
|
+
context "ResultSet.init_from_api" do
|
9
|
+
setup do
|
10
|
+
@result_set = ResultSet.init_from_api(RESULT_SET_HASH)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "read the offset from the results hash @offset field" do
|
14
|
+
assert_equal RESULT_SET_HASH["offset"].to_i, @result_set.offset
|
15
|
+
end
|
16
|
+
|
17
|
+
should "read the total_results from the results_hash total field" do
|
18
|
+
assert_equal RESULT_SET_HASH["total"].to_i, @result_set.total_results
|
19
|
+
end
|
20
|
+
|
21
|
+
should "return results as an array of Article objects" do
|
22
|
+
assert_kind_of(Array, @result_set.results)
|
23
|
+
assert @result_set.results.all? {|r| r.is_a? Article }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "page_number" do
|
28
|
+
setup do
|
29
|
+
@result_set = ResultSet.init_from_api(RESULT_SET_HASH)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "be offset + 1" do
|
33
|
+
assert_equal 1, @result_set.page_number
|
34
|
+
end
|
35
|
+
|
36
|
+
should "be 0 if total_results is 0" do
|
37
|
+
result_set = ResultSet.new(:total_results => 0)
|
38
|
+
assert_equal 0, result_set.page_number
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "total_pages" do
|
43
|
+
should "be 1 if less than BATCH_SIZE results" do
|
44
|
+
result_set = ResultSet.new(:total_results => 7)
|
45
|
+
assert_equal 1, result_set.total_pages
|
46
|
+
end
|
47
|
+
|
48
|
+
should "be 0 if 0 results returned" do
|
49
|
+
result_set = ResultSet.new(:total_results => 0)
|
50
|
+
assert_equal 0, result_set.total_pages
|
51
|
+
end
|
52
|
+
|
53
|
+
should "be ceil(total_results / batch_size)" do
|
54
|
+
result_set = ResultSet.new(:total_results => 23)
|
55
|
+
assert_equal 3, result_set.total_pages
|
56
|
+
|
57
|
+
result_set = ResultSet.new(:total_results => 20)
|
58
|
+
assert_equal 2, result_set.total_pages
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
gem 'chrisk-fakeweb'
|
6
|
+
require 'fake_web'
|
7
|
+
|
8
|
+
FakeWeb.allow_net_connect = false
|
9
|
+
|
10
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
11
|
+
require 'nytimes_articles'
|
12
|
+
|
13
|
+
API_KEY = '13e234323232222'
|
14
|
+
|
15
|
+
def init_test_key
|
16
|
+
Nytimes::Articles::Base.api_key = API_KEY
|
17
|
+
end
|
18
|
+
|
19
|
+
def api_url_for(params = {})
|
20
|
+
full_params = params.merge 'api-key' => API_KEY
|
21
|
+
Nytimes::Articles::Base.build_request_url(full_params).to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
module TestNytimes
|
25
|
+
module TestArticles
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Test::Unit::TestCase
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harrisj-nytimes-articles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-14 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: htmlentities
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: A gem for accessing the New York Times Article Search API
|
26
|
+
email: jharris@nytimes.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- VERSION.yml
|
35
|
+
- lib/nytimes_articles
|
36
|
+
- lib/nytimes_articles/article.rb
|
37
|
+
- lib/nytimes_articles/base.rb
|
38
|
+
- lib/nytimes_articles/exceptions.rb
|
39
|
+
- lib/nytimes_articles/facet.rb
|
40
|
+
- lib/nytimes_articles/result_set.rb
|
41
|
+
- lib/nytimes_articles.rb
|
42
|
+
- test/nytimes
|
43
|
+
- test/nytimes/articles
|
44
|
+
- test/nytimes/articles/test_article.rb
|
45
|
+
- test/nytimes/articles/test_base.rb
|
46
|
+
- test/nytimes/articles/test_facet.rb
|
47
|
+
- test/nytimes/articles/test_result_set.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/harrisj/nytimes-articles
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --inline-source
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements:
|
70
|
+
- Unicode
|
71
|
+
- The htmlentities gem
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.2.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: A gem for accessing the NYTimes Article Search API
|
77
|
+
test_files: []
|
78
|
+
|