Class: GovKit::Resource
- Inherits:
-
Object
- Object
- GovKit::Resource
- Includes:
- HTTParty
- Defined in:
- lib/gov_kit/resource.rb
Overview
This is the parent class for the classes that wrap the data returned to govkit.
Subclasses are responsible for fetching the data from different web services; Resource will then parse the returned data, converting returned fields to instance methods.
Initialize a Resource with a hash of attributes, or an array of hashes. For each attribute, add a getter and setter to this instance. Includes HTTParty, which provides convenience methods like get().
Direct Known Subclasses
FollowTheMoneyResource, OpenStatesResource, TransparencyDataResource, VoteSmartResource
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The attributes data returned by the service.
-
#raw_response ⇒ Object
readonly
The response returned by the service.
Class Method Summary collapse
-
.instantiate(record) ⇒ Resource
Instantiate new GovKit::Resources.
-
.instantiate_collection(collection) ⇒ Array
Instantiate a set of records.
-
.parse(response) ⇒ Resource
Handles the basic responses we might get back from a web service.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#to_md5 ⇒ Hash
The response object, potentially useful for comparison on sync.
-
#unload(attributes) ⇒ Object
Given a hash of attributes, assign it to the @attributes member.
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
29 30 31 32 33 34 |
# File 'lib/gov_kit/resource.rb', line 29 def initialize(attributes = {}) @attributes = {} @raw_response = attributes unload(attributes) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object (private)
:nodoc:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/gov_kit/resource.rb', line 174 def method_missing(method_symbol, * arguments) #:nodoc: method_name = method_symbol.to_s case method_name.last when "=" attributes[method_name.first(-1)] = arguments.first when "?" !attributes[method_name.first(-1)].blank? when "]" attributes[arguments.first.to_s] else attributes.has_key?(method_name) ? attributes[method_name] : super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
The attributes data returned by the service.
24 25 26 |
# File 'lib/gov_kit/resource.rb', line 24 def attributes @attributes end |
#raw_response ⇒ Object (readonly)
The response returned by the service.
27 28 29 |
# File 'lib/gov_kit/resource.rb', line 27 def raw_response @raw_response end |
Class Method Details
.instantiate(record) ⇒ Resource
Instantiate new GovKit::Resources.
If record
is a hash, return a single GovKit::Resource. If it is an array, return an array of GovKit::Resources.
82 83 84 85 86 87 88 |
# File 'lib/gov_kit/resource.rb', line 82 def self.instantiate(record) if record.is_a?(Array) instantiate_collection(record) else new(record) end end |
.instantiate_collection(collection) ⇒ Array
Instantiate a set of records.
94 95 96 |
# File 'lib/gov_kit/resource.rb', line 94 def self.instantiate_collection(collection) collection.collect! { |record| new(record) } end |
.parse(response) ⇒ Resource
Handles the basic responses we might get back from a web service.
On failure, throws an error.
If a service returns something other than a 404 when an object is not found, you’ll need to handle that in the subclass.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/gov_kit/resource.rb', line 52 def self.parse(response) 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 raise ServerError, '5xx server error' when Net::HTTPClientError raise ClientError, '4xx client error' end end return [] unless !response.blank? instantiate(response) end |
Instance Method Details
#to_md5 ⇒ Hash
Returns the response object, potentially useful for comparison on sync.
38 39 40 |
# File 'lib/gov_kit/resource.rb', line 38 def to_md5 Digest::MD5.hexdigest(@raw_response.body) end |
#unload(attributes) ⇒ Object
Given a hash of attributes, assign it to the @attributes member. Then for each attribute, create or set a pair of member accessors with the name of the attribute’s key.
If the value of the attribute is itself an array or a hash, then create a new class with the (singularized) key as a name, and with a parent class of Resource, and initialize it with the hash.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gov_kit/resource.rb', line 108 def unload(attributes) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) attributes.each do |key, value| @attributes[key.to_s] = case value when Array resource = resource_for_collection(key) value.map do |attrs| if attrs.is_a?(String) || attrs.is_a?(Numeric) attrs.duplicable? ? attrs.dup : attrs else resource.new(attrs) end end when Hash resource = find_or_create_resource_for(key) resource.new(value) else value.dup rescue value end end self end |