diff --git a/lib/gov_kit/resource.rb b/lib/gov_kit/resource.rb index a574891..08b7d65 100644 --- a/lib/gov_kit/resource.rb +++ b/lib/gov_kit/resource.rb @@ -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 diff --git a/spec/follow_the_money_spec.rb b/spec/follow_the_money_spec.rb index 6f2a7b3..3604fce 100644 --- a/spec/follow_the_money_spec.rb +++ b/spec/follow_the_money_spec.rb @@ -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 diff --git a/spec/open_states_spec.rb b/spec/open_states_spec.rb index acaba51..6f9841e 100644 --- a/spec/open_states_spec.rb +++ b/spec/open_states_spec.rb @@ -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