Class: Zaius::APIResource
Class Method Summary
collapse
Instance Method Summary
collapse
included
Methods inherited from ZaiusObject
#==, #[], #[]=, #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
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Zaius::ZaiusObject
Class Method Details
.class_name ⇒ Object
7
8
9
|
# File 'lib/zaius/api_resource.rb', line 7
def self.class_name
name.split("::")[-1]
end
|
.resource_url ⇒ Object
11
12
13
14
15
16
17
18
|
# File 'lib/zaius/api_resource.rb', line 11
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
"/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
end
|
.retrieve(id, opts = {}) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/zaius/api_resource.rb', line 56
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.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/zaius/api_resource.rb', line 26
def self.save_nested_resource(name)
define_method(:"#{name}=") do |value|
super(value)
value = send(name)
value.save_with_parent = true if value.is_a?(APIResource)
value
end
end
|
Instance Method Details
#refresh ⇒ Object
51
52
53
54
|
# File 'lib/zaius/api_resource.rb', line 51
def refresh
resp, opts = request(:get, resource_url, @retrieve_params)
initialize_from(resp.data, opts)
end
|
#resource_url ⇒ Object
44
45
46
47
48
49
|
# File 'lib/zaius/api_resource.rb', line 44
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)}"
end
|