Permalink
Browse files

Resource should respond to a 404 with an exception. Also, return [] for

empty query results for OpenStates queries.
  • Loading branch information...
1 parent c66954d commit 49409ad19ee024ec796efb26e111ef267f26f688 @jeff-r jeff-r committed May 11, 2011
View
@@ -9,6 +9,24 @@ class OpenStatesResource < Resource
# http://rubydoc.info/gems/httparty/0.7.4/HTTParty/ClassMethods#default_params-instance_method
default_params :output => 'json', :apikey => GovKit::configuration.sunlight_apikey
base_uri GovKit::configuration.openstates_base_url
+
+ # Do a GET query, with optional parameters.
+ #
+ # OpenStates returns a 404 error when a query
+ # returns nothing.
+ #
+ # So, if a query result is a resource not found error,
+ # we return an empty set.
+ def self.get_uri(uri, options={})
+ begin
+ response = get(uri, options)
+ result = parse(response)
+ rescue ResourceNotFound
+ return []
+ end
+ result
+ end
+
end
# Ruby module for interacting with the Open States Project API
@@ -32,8 +50,7 @@ module OpenStates
#
class State < OpenStatesResource
def self.find_by_abbreviation(abbreviation)
- response = get("/metadata/#{abbreviation}/")
- parse(response)
+ get_uri("/metadata/#{abbreviation}/")
end
end
@@ -47,13 +64,12 @@ class Bill < OpenStatesResource
def self.find(state_abbrev, session, bill_id, chamber = '')
escaped_bill_id = bill_id.gsub(/ /, '%20')
escaped_session = session.gsub(/ /, '%20')
- response = get("/bills/#{state_abbrev.downcase}/#{escaped_session}/#{chamber.blank? ? '' : chamber + '/'}#{escaped_bill_id}/")
- parse(response)
+
+ get_uri("/bills/#{state_abbrev.downcase}/#{escaped_session}/#{chamber.blank? ? '' : chamber + '/'}#{escaped_bill_id}/")
end
def self.search(query, options = {})
- response = get('/bills/', :query => {:q => query}.merge(options))
- parse(response)
+ get_uri('/bills/', :query => {:q => query}.merge(options))
end
def self.latest(updated_since, ops = {})
@@ -69,13 +85,11 @@ def self.latest(updated_since, ops = {})
#
class Legislator < OpenStatesResource
def self.find(legislator_id)
- response = get("/legislators/#{legislator_id}/")
- parse(response)
+ get_uri("/legislators/#{legislator_id}/")
end
def self.search(options = {})
- response = get('/legislators/', :query => options)
- parse(response)
+ get_uri('/legislators/', :query => options)
end
end
@@ -86,13 +100,11 @@ def self.search(options = {})
#
class Committee < OpenStatesResource
def self.find(committee_id)
- response = get("/committees/#{committee_id}/")
- parse(response)
+ get_uri("/committees/#{committee_id}/")
end
def self.search(options = {})
- response = get('/committees/', :query => options)
- parse(response)
+ get_uri('/committees/', :query => options)
end
end
View
@@ -53,8 +53,7 @@ def self.parse(response)
if response.class == HTTParty::Response
case response.response
when Net::HTTPNotFound
- return []
- # raise ResourceNotFound, "404 Not Found"
+ raise ResourceNotFound, "404 Not Found"
when Net::HTTPGone
raise ResourceNotFound, "404 Not Found"
when Net::HTTPUnauthorized
@@ -76,14 +75,14 @@ def self.parse(response)
# +record+ is a hash of values returned by a service,
# or an array of hashes.
#
- # If +record+ is a hash, return a single GovKit::Resource.
+ # If +record+ is a hash, return an array containing a single GovKit::Resource.
# If it is an array, return an array of GovKit::Resources.
#
def self.instantiate(record)
if record.is_a?(Array)
instantiate_collection(record)
else
- new(record)
+ [new(record)]
end
end
@@ -0,0 +1,53 @@
+HTTP/1.1 200 OK
+Server: nginx/1.0.0
+Date: Fri, 29 Apr 2011 21:05:59 GMT
+Content-Type: application/json; charset=utf-8
+Connection: keep-alive
+Authorization:
+
+{
+ "members": [
+ {
+ "leg_id": "MDL000194",
+ "role": "member",
+ "name": "Joan Carter Conway"
+ },
+ {
+ "leg_id": "MDL000226",
+ "role": "member",
+ "name": "Paul G. Pinsky"
+ },
+ {
+ "leg_id": "MDL000379",
+ "role": "member",
+ "name": "Bill Ferguson"
+ },
+ {
+ "leg_id": "MDL000381",
+ "role": "member",
+ "name": "J. B. Jennings"
+ },
+ {
+ "leg_id": "MDL000384",
+ "role": "member",
+ "name": "Karen S. Montgomery"
+ },
+ {
+ "leg_id": "MDL000387",
+ "role": "member",
+ "name": "Ronald N. Young"
+ }
+ ],
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "sources": [
+ {
+ "url": "http://www.msa.md.gov/msa/mdmanual/05sen/html/com/02eco.html"
+ }
+ ],
+ "subcommittee": "ENVIRONMENT SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000012"
+}
@@ -0,0 +1,190 @@
+HTTP/1.1 200 OK
+Server: nginx/1.0.0
+Date: Fri, 29 Apr 2011 20:52:00 GMT
+Content-Type: application/json; charset=utf-8
+Connection: keep-alive
+
+[
+ {
+ "updated_at": "2011-04-26 01:20:17",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "ALCOHOLIC BEVERAGES SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000009"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000003",
+ "state": "md",
+ "subcommittee": "CAPITAL BUDGET SUBCOMMITTEE",
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000004"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "ENVIRONMENT SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000012"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "ETHICS & ELECTION LAW SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000013"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "FINANCE COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000015"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000015",
+ "state": "md",
+ "subcommittee": "HEALTH SUBCOMMITTEE",
+ "committee": "FINANCE COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000016"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:53",
+ "parent_id": "MDC000015",
+ "state": "md",
+ "subcommittee": "TRANSPORTATION SUBCOMMITTEE",
+ "committee": "FINANCE COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000017"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:53",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "EXECUTIVE NOMINATIONS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000018"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000003",
+ "state": "md",
+ "subcommittee": "PUBLIC SAFETY, TRANSPORTATION & ENVIRONMENT SUBCOMMITTEE",
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000007"
+ },
+ {
+ "created_at": "2011-01-29 00:13:19",
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "HEALTH OCCUPATIONS SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000068"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000003"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:53",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "SPECIAL COMMITTEE ON SUBSTANCE ABUSE",
+ "chamber": "upper",
+ "id": "MDC000019"
+ },
+ {
+ "created_at": "2011-01-29 00:13:19",
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "LABOR LICENSING, & REGULATION SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000069"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000003",
+ "state": "md",
+ "subcommittee": "HEALTH & HUMAN SERVICES SUBCOMMITTEE",
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000067"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000003",
+ "state": "md",
+ "subcommittee": "EDUCATION, BUSINESS & ADMINISTRATION SUBCOMMITTEE",
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000066"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000003",
+ "state": "md",
+ "subcommittee": "PENSIONS SUBCOMMITTEE",
+ "committee": "BUDGET & TAXATION COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000006"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000008"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": "MDC000009",
+ "state": "md",
+ "subcommittee": "EDUCATION SUBCOMMITTEE",
+ "committee": "EDUCATION, HEALTH & ENVIRONMENTAL AFFAIRS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000011"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "RULES COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000001"
+ },
+ {
+ "updated_at": "2011-04-29 01:19:52",
+ "parent_id": null,
+ "state": "md",
+ "subcommittee": null,
+ "committee": "JUDICIAL PROCEEDINGS COMMITTEE",
+ "chamber": "upper",
+ "id": "MDC000002"
+ }
+]
Oops, something went wrong.

0 comments on commit 49409ad

Please sign in to comment.