Class: Helio::APIResource
- Inherits:
-
HelioObject
- Object
- HelioObject
- Helio::APIResource
- Includes:
- Helio::APIOperations::Request
- Defined in:
- lib/helio/api_resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#save_with_parent ⇒ Object
A flag that can be set a behavior that will cause this resource to be encoded and sent up along with an update of its parent resource.
Class Method Summary collapse
- .class_name ⇒ Object
- .resource_url ⇒ Object
- .retrieve(id, opts = {}) ⇒ Object
-
.save_nested_resource(name) ⇒ Object
A metaprogramming call that specifies that a field of a resource can be its own type of API resource (say a nested card under an account for example), and if that resource is set, it should be transmitted to the API on a create or update.
Instance Method Summary collapse
Methods included from Helio::APIOperations::Request
Methods inherited from HelioObject
#==, #[], #[]=, #as_json, construct_from, #deleted?, #dirty!, #each, #initialize, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #refresh_from, #serialize_params, serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values
Constructor Details
This class inherits a constructor from Helio::HelioObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Helio::HelioObject
Instance Attribute Details
#save_with_parent ⇒ Object
A flag that can be set a behavior that will cause this resource to be encoded and sent up along with an update of its parent resource. This is usually not desirable because resources are updated individually on their own endpoints, but there are certain cases, replacing a customer’s source for example, where this is allowed.
12 13 14 |
# File 'lib/helio/api_resource.rb', line 12 def save_with_parent @save_with_parent end |
Class Method Details
.class_name ⇒ Object
14 15 16 |
# File 'lib/helio/api_resource.rb', line 14 def self.class_name name.split("::")[-1] end |
.resource_url ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/helio/api_resource.rb', line 18 def self.resource_url if self == APIResource raise NotImplementedError, "APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)" end resource_path = class_name.gsub(/([^\^])([A-Z])/,'\1_\2').downcase "/#{CGI.escape(resource_path)}s" end |
.retrieve(id, opts = {}) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/helio/api_resource.rb', line 62 def self.retrieve(id, opts = {}) opts = Util.normalize_opts(opts) instance = new(id, opts) instance.refresh instance end |
.save_nested_resource(name) ⇒ Object
A metaprogramming call that specifies that a field of a resource can be its own type of API resource (say a nested card under an account for example), and if that resource is set, it should be transmitted to the API on a create or update. Doing so is not the default behavior because API resources should normally be persisted on their own RESTful endpoints.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/helio/api_resource.rb', line 32 def self.save_nested_resource(name) define_method(:"#{name}=") do |value| super(value) # The parent setter will perform certain useful operations like # converting to an APIResource if appropriate. Refresh our argument # value to whatever it mutated to. value = send(name) # Note that the value may be subresource, but could also be a scalar # (like a tokenized card's ID for example), so we check the type before # setting #save_with_parent here. value.save_with_parent = true if value.is_a?(APIResource) value end end |
Instance Method Details
#refresh ⇒ Object
57 58 59 60 |
# File 'lib/helio/api_resource.rb', line 57 def refresh resp, opts = request(:get, resource_url, @retrieve_params) initialize_from(resp.data, opts) end |
#resource_url ⇒ Object
50 51 52 53 54 55 |
# File 'lib/helio/api_resource.rb', line 50 def resource_url unless (id = self["id"]) raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", "id") end "#{self.class.resource_url}/#{CGI.escape(id.to_s)}" end |