Class: VerticalResponse::API::Resource
- Defined in:
- lib/verticalresponse/api/resource.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Client
Class Method Summary collapse
-
.all(options = {}, path_prefix = []) ⇒ Object
Returns all the objects of the current class The prefix is more or less a hack, to allow viewing contacts inside lists with one call.
- .class_for_resource(original_class, id = nil) ⇒ Object
- .class_name ⇒ Object
-
.create(params, path_prefix = []) ⇒ 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, access_token = nil) ⇒ 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_service_uri, base_uri, base_uri_with_prefix, build_params, build_query_params, config, default_query_params, embed_resource, #initialize, resource_uri, resource_uri_suffix, resource_uri_with_prefix, resource_uri_with_token
Constructor Details
This class inherits a constructor from VerticalResponse::API::Client
Class Method Details
.all(options = {}, path_prefix = []) ⇒ Object
Returns all the objects of the current class The prefix is more or less a hack, to allow viewing contacts inside lists with one call
74 75 76 77 78 79 80 81 |
# File 'lib/verticalresponse/api/resource.rb', line 74 def all( = {}, path_prefix = []) validate_supported_method!(:all) uri = resource_uri_with_prefix(path_prefix) response = Response.new(get(uri, build_query_params()), [:access_token]) object_collection(response, [:access_token]) end |
.class_for_resource(original_class, id = nil) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/verticalresponse/api/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/verticalresponse/api/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, path_prefix = []) ⇒ Object
Creates a new object of the current class with the parameters provided
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/verticalresponse/api/resource.rb', line 92 def create(params, path_prefix = []) access_token = params[:access_token] params.delete :access_token validate_supported_method!(:create) uri = resource_uri_with_prefix(path_prefix) # VerticalResponse doesn't like it when we pass the access_token via POST uri += (uri.include? "?") ? "&" : "?" uri += "access_token=#{access_token}" response = Response.new( post(uri, build_params(params)) ) self.new(response, access_token) 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/verticalresponse/api/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
84 85 86 87 88 89 |
# File 'lib/verticalresponse/api/resource.rb', line 84 def find(id, = {}) validate_supported_method!(:find) response = Response.new(get(resource_uri(id),build_query_params())) self.new(response, [:access_token]) end |
.id_attribute_name ⇒ Object
49 50 51 |
# File 'lib/verticalresponse/api/resource.rb', line 49 def id_attribute_name 'id' end |
.id_regexp ⇒ Object
45 46 47 |
# File 'lib/verticalresponse/api/resource.rb', line 45 def id_regexp /\d+/ end |
.object_collection(response, access_token = nil) ⇒ 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/verticalresponse/api/resource.rb', line 65 def object_collection(response, access_token = nil) response.handle_collection do |response_item| self.new(response_item, access_token) end end |
.resource_id_from_url(url) ⇒ Object
Get the ID of a resource based on a given URL
54 55 56 |
# File 'lib/verticalresponse/api/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/verticalresponse/api/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/verticalresponse/api/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
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/verticalresponse/api/resource.rb', line 143 def delete(params = {}) @access_token = params[:access_token] params.delete :access_token self.class.validate_supported_method!(:delete) Response.new( self.class.delete( self.class.resource_uri_with_token(@access_token, id), self.class.build_params(params) ) ) end |
#id ⇒ Object
Returns the ID of the current object based on the response
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/verticalresponse/api/resource.rb', line 112 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
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/verticalresponse/api/resource.rb', line 157 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
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/verticalresponse/api/resource.rb', line 129 def update(params) @access_token = params[:access_token] params.delete :access_token self.class.validate_supported_method!(:update) response = Response.new( self.class.put( self.class.resource_uri_with_token(@access_token, id), self.class.build_params(params) ) ) self.class.new(response, @access_token) end |
#url ⇒ Object
125 126 127 |
# File 'lib/verticalresponse/api/resource.rb', line 125 def url response.url end |