Class: VerticalResponse::API::Resource
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Client
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
Returns all the objects of the current class.
- .class_for_resource(original_class, id = nil) ⇒ Object
- .class_name ⇒ Object
-
.create(params) ⇒ Object
Creates a new object of the current class with the parameters provided.
-
.exclude_methods(*methods) ⇒ Object
Exclude methods that are not supported by the current resource API.
-
.find(id, options = {}) ⇒ Object
Find and return an object of the current class based on its ID.
- .id_attribute_name ⇒ Object
- .id_regexp ⇒ Object
-
.object_collection(response) ⇒ Object
Returns a collection of current class objects.
-
.resource_id_from_url(url) ⇒ Object
Get the ID of a resource based on a given URL.
- .resource_name ⇒ Object
- .validate_supported_method!(method) ⇒ Object
Instance Method Summary collapse
- #delete(params = {}) ⇒ Object
-
#id ⇒ Object
Returns the ID of the current object based on the response.
-
#stats(options = {}) ⇒ Object
Returns the summary stats for the current object.
- #update(params) ⇒ Object
- #url ⇒ Object
Methods inherited from Client
add_default_query_param, assign_headers, base_uri, build_params, build_query_params, config, default_query_params, embed_resource, #initialize, resource_uri
Constructor Details
This class inherits a constructor from VerticalResponse::API::Client
Class Method Details
.all(options = {}) ⇒ Object
Returns all the objects of the current class
72 73 74 75 76 77 78 |
# File 'lib/resource.rb', line 72 def all( = {}) validate_supported_method!(:all) response = Response.new get(resource_uri, build_query_params()) object_collection(response) end |
.class_for_resource(original_class, id = nil) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/resource.rb', line 11 def class_for_resource(original_class, id = nil) # Create a new anonymous class to prevent conflicts with other classes new_class = Class.new(original_class) new_class.(resource_name, id) new_class end |
.class_name ⇒ Object
33 34 35 36 37 |
# File 'lib/resource.rb', line 33 def class_name # Use the superclass name if the current class name is not defined # (e.g. for anonymous classes) name || superclass.name end |
.create(params) ⇒ Object
Creates a new object of the current class with the parameters provided
90 91 92 93 94 95 96 97 98 |
# File 'lib/resource.rb', line 90 def create(params) validate_supported_method!(:create) response = Response.new post( resource_uri, build_params(params) ) self.new(response) end |
.exclude_methods(*methods) ⇒ Object
Exclude methods that are not supported by the current resource API. This applies for common methods that are shared across all classes:
- Class methods: all, find, create
- Instance methods: update, delete, stats
22 23 24 |
# File 'lib/resource.rb', line 22 def exclude_methods(*methods) @excluded_methods = methods end |
.find(id, options = {}) ⇒ Object
Find and return an object of the current class based on its ID
81 82 83 84 85 86 87 |
# File 'lib/resource.rb', line 81 def find(id, = {}) validate_supported_method!(:find) response = Response.new get(resource_uri(id), build_query_params()) self.new(response) end |
.id_attribute_name ⇒ Object
49 50 51 |
# File 'lib/resource.rb', line 49 def id_attribute_name 'id' end |
.id_regexp ⇒ Object
45 46 47 |
# File 'lib/resource.rb', line 45 def id_regexp /\d+/ end |
.object_collection(response) ⇒ Object
Returns a collection of current class objects. Useful for when we need to have an object oriented way to handle a list of items from an API response
65 66 67 68 69 |
# File 'lib/resource.rb', line 65 def object_collection(response) response.handle_collection do |response_item| self.new(response_item) end end |
.resource_id_from_url(url) ⇒ Object
Get the ID of a resource based on a given URL
54 55 56 |
# File 'lib/resource.rb', line 54 def resource_id_from_url(url) url.match(/#{ resource_name }\/(#{ id_regexp })/)[1] end |
.resource_name ⇒ Object
39 40 41 42 43 |
# File 'lib/resource.rb', line 39 def resource_name # Manually pluralize just by adding an 's' and underscore manually # We want this wrapper to be Rails-independent "#{ class_name.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase }s" end |
.validate_supported_method!(method) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/resource.rb', line 26 def validate_supported_method!(method) if @excluded_methods && @excluded_methods.include?(method.to_sym) raise NotImplementedError, "This method is not supported for the #{ class_name } class" end end |
Instance Method Details
#delete(params = {}) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/resource.rb', line 129 def delete(params = {}) self.class.validate_supported_method!(:delete) Response.new self.class.delete( self.class.resource_uri(id), self.class.build_params(params) ) end |
#id ⇒ Object
Returns the ID of the current object based on the response
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/resource.rb', line 102 def id if @id.nil? && response if response.attributes && response.attributes.has_key?(self.class::id_attribute_name) @id = response.attributes[self.class::id_attribute_name] elsif url # This case is useful if we need the ID right after a call # that does not return any atributes, like create @id = self.class.resource_id_from_url(url) end end @id end |
#stats(options = {}) ⇒ Object
Returns the summary stats for the current object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/resource.rb', line 139 def stats( = {}) self.class.validate_supported_method!(:stats) if response.links && response.links.has_key?('stats') uri = response.links['stats']['url'] else uri = self.class.resource_uri(id, 'stats') end Response.new self.class.get(uri, self.class.build_query_params()) end |
#update(params) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/resource.rb', line 119 def update(params) self.class.validate_supported_method!(:update) response = Response.new self.class.put( self.class.resource_uri(id), self.class.build_params(params) ) self.class.new(response) end |
#url ⇒ Object
115 116 117 |
# File 'lib/resource.rb', line 115 def url response.url end |