Skip to content

Commit

Permalink
Update GovKit::Resource.parse case statement to return error expected…
Browse files Browse the repository at this point in the history
… by spec. Minor changes to specs to reflect changed code; specs all pass.
  • Loading branch information
jeff-r committed Mar 25, 2011
1 parent 438b177 commit b31d2a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lib/gov_kit/resource.rb
Expand Up @@ -24,12 +24,12 @@ def self.parse(response)
# This method handles the basic responses we might get back from # 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, # 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. # you'll need to handle that in the subclass.
raise ResourceNotFound, "Resource not found" unless !response.blank?

if response.class == HTTParty::Response if response.class == HTTParty::Response
case response.response case response.response
when Net::HTTPNotFound when Net::HTTPNotFound
raise ResourceNotFound, "404 Not Found" raise ResourceNotFound, "404 Not Found"
when Net::HTTPGone
raise ResourceNotFound, "404 Not Found"
when Net::HTTPUnauthorized when Net::HTTPUnauthorized
raise NotAuthorized, "401 Not Authorized; have you set up your API key?" raise NotAuthorized, "401 Not Authorized; have you set up your API key?"
when Net::HTTPServerError when Net::HTTPServerError
Expand All @@ -39,6 +39,8 @@ def self.parse(response)
end end
end end


raise ResourceNotFound, "Resource not found" unless !response.blank?

instantiate(response) instantiate(response)
end end


Expand Down
4 changes: 4 additions & 0 deletions spec/follow_the_money_spec.rb
@@ -1,5 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') 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 module GovKit::FollowTheMoney
describe GovKit::FollowTheMoney do describe GovKit::FollowTheMoney do


Expand Down
30 changes: 19 additions & 11 deletions spec/open_states_spec.rb
@@ -1,19 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') 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 module GovKit::OpenStates
describe GovKit::OpenStates do describe GovKit::OpenStates do
before(:all) do before(:all) do
base_uri = GovKit::OpenStatesResource.base_uri.gsub(/\./, '\.') base_uri = GovKit::OpenStatesResource.base_uri.gsub(/\./, '\.')


urls = [ urls = [
['/ca/\?', 'state.response'], ['/metadata/ca/\?', 'state.response'],
['/bills/ca/20092010/AB667/', 'bill.response'], ['/bills/ca/20092010/AB667/', 'bill.response'],
['/bills/search/\?', 'bill_query.response'], ['/bills/\?', 'bill_query.response'],
['/bills/latest/\?', 'bill_query.response'], ['/bills/latest/\?', 'bill_query.response'],
['/legislators/2462/\?', 'legislator.response'], ['/legislators/2462/\?', 'legislator.response'],
['/legislators/410/\?', '410.response'], ['/legislators/410/\?', '410.response'],
['/legislators/401/\?', '401.response'], ['/legislators/401/\?', '401.response'],
['/legislators/search/\?', 'legislator_query.response'] ['/legislators/\?', 'legislator_query.response']
] ]


urls.each do |u| urls.each do |u|
Expand All @@ -23,15 +31,15 @@ module GovKit::OpenStates


it "should have the base uri set properly" do it "should have the base uri set properly" do
[State, Bill, Legislator].each do |klass| [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
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. # The Open States API returns a 401 Not Authorized if the API key is invalid.
lambda do lambda do
@legislator = Legislator.find(401) @legislator = Legislator.find(401)
end.should raise_error(GovKit::NotAuthorizedError) end.should raise_error(GovKit::NotAuthorized)


@legislator.should be_nil @legislator.should be_nil
end end
Expand All @@ -55,9 +63,9 @@ module GovKit::OpenStates


describe Bill do describe Bill do
context "#find" 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 lambda do
@bill = Bill.find('ca', 20092010, 'lower', 'AB667') @bill = Bill.find('ca', '20092010', 'lower', 'AB667')
end.should_not raise_error end.should_not raise_error


@bill.should be_an_instance_of(Bill) @bill.should be_an_instance_of(Bill)
Expand All @@ -80,7 +88,7 @@ module GovKit::OpenStates
context "#latest" do context "#latest" do
it "should get the latest bills by given criteria" do it "should get the latest bills by given criteria" do
lambda do lambda do
@latest = Bill.latest('2010-01-01','tx') @latest = Bill.latest('2010-01-01', :state => 'tx')
end.should_not raise_error end.should_not raise_error


@latest.should be_an_instance_of(Array) @latest.should be_an_instance_of(Array)
Expand All @@ -107,7 +115,7 @@ module GovKit::OpenStates
it "should raise a GovKitError if the legislator is not found" do it "should raise a GovKitError if the legislator is not found" do
lambda do lambda do
@legislator = Legislator.find(410) @legislator = Legislator.find(410)
end.should raise_error(GovKit::ResourceNotFoundError) end.should raise_error(GovKit::ResourceNotFound)


@legislator.should be_nil @legislator.should be_nil
end end
Expand Down

0 comments on commit b31d2a1

Please sign in to comment.