Permalink
Browse files

Update GovKit::Resource.parse case statement to return error expected…

… by spec. Minor changes to specs to reflect changed code; specs all pass.
  • Loading branch information...
1 parent 438b177 commit b31d2a168d017f3f37b351b33cdf6acb014f5fa1 @jeff-r jeff-r committed Mar 25, 2011
Showing with 27 additions and 13 deletions.
  1. +4 −2 lib/gov_kit/resource.rb
  2. +4 −0 spec/follow_the_money_spec.rb
  3. +19 −11 spec/open_states_spec.rb
View
@@ -24,12 +24,12 @@ def self.parse(response)
# This method handles the basic responses we might get back from
# Net::HTTP. But if a service returns something other than a 404 when an object is not found,
# you'll need to handle that in the subclass.
- raise ResourceNotFound, "Resource not found" unless !response.blank?
-
if response.class == HTTParty::Response
case response.response
when Net::HTTPNotFound
raise ResourceNotFound, "404 Not Found"
+ when Net::HTTPGone
+ raise ResourceNotFound, "404 Not Found"
when Net::HTTPUnauthorized
raise NotAuthorized, "401 Not Authorized; have you set up your API key?"
when Net::HTTPServerError
@@ -39,6 +39,8 @@ def self.parse(response)
end
end
+ raise ResourceNotFound, "Resource not found" unless !response.blank?
+
instantiate(response)
end
@@ -1,5 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+# Provides "String.singularize"
+# which is used by resource_for_collection, in resource.rb
+require 'active_support/inflector'
+
module GovKit::FollowTheMoney
describe GovKit::FollowTheMoney do
View
@@ -1,19 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+# Provides "String.singularize"
+# which is used by resource_for_collection, in resource.rb
+require 'active_support/inflector'
+
+# Provides string.last()
+# which is used by method_missing in resource.rb
+require 'active_support/core_ext/string'
+
module GovKit::OpenStates
describe GovKit::OpenStates do
before(:all) do
base_uri = GovKit::OpenStatesResource.base_uri.gsub(/\./, '\.')
urls = [
- ['/ca/\?', 'state.response'],
- ['/bills/ca/20092010/AB667/', 'bill.response'],
- ['/bills/search/\?', 'bill_query.response'],
+ ['/metadata/ca/\?', 'state.response'],
+ ['/bills/ca/20092010/AB667/', 'bill.response'],
+ ['/bills/\?', 'bill_query.response'],
['/bills/latest/\?', 'bill_query.response'],
['/legislators/2462/\?', 'legislator.response'],
['/legislators/410/\?', '410.response'],
['/legislators/401/\?', '401.response'],
- ['/legislators/search/\?', 'legislator_query.response']
+ ['/legislators/\?', 'legislator_query.response']
]
urls.each do |u|
@@ -23,15 +31,15 @@ module GovKit::OpenStates
it "should have the base uri set properly" do
[State, Bill, Legislator].each do |klass|
- klass.base_uri.should == "http://fiftystates-dev.sunlightlabs.com/api"
+ klass.base_uri.should == "http://openstates.sunlightlabs.com/api/v1"
end
end
- it "should raise NotAuthorizedError if the api key is not valid" do
+ it "should raise NotAuthorized if the api key is not valid" do
# The Open States API returns a 401 Not Authorized if the API key is invalid.
lambda do
@legislator = Legislator.find(401)
- end.should raise_error(GovKit::NotAuthorizedError)
+ end.should raise_error(GovKit::NotAuthorized)
@legislator.should be_nil
end
@@ -55,9 +63,9 @@ module GovKit::OpenStates
describe Bill do
context "#find" do
- it "should find a bill by stat abbreviation, session, chamber, bill_id" do
+ it "should find a bill by state abbreviation, session, chamber, bill_id" do
lambda do
- @bill = Bill.find('ca', 20092010, 'lower', 'AB667')
+ @bill = Bill.find('ca', '20092010', 'lower', 'AB667')
end.should_not raise_error
@bill.should be_an_instance_of(Bill)
@@ -80,7 +88,7 @@ module GovKit::OpenStates
context "#latest" do
it "should get the latest bills by given criteria" do
lambda do
- @latest = Bill.latest('2010-01-01','tx')
+ @latest = Bill.latest('2010-01-01', :state => 'tx')
end.should_not raise_error
@latest.should be_an_instance_of(Array)
@@ -107,7 +115,7 @@ module GovKit::OpenStates
it "should raise a GovKitError if the legislator is not found" do
lambda do
@legislator = Legislator.find(410)
- end.should raise_error(GovKit::ResourceNotFoundError)
+ end.should raise_error(GovKit::ResourceNotFound)
@legislator.should be_nil
end

0 comments on commit b31d2a1

Please sign in to comment.