Class: SlidePay::ApiResource
- Inherits:
-
Hash
- Object
- Hash
- SlidePay::ApiResource
- Defined in:
- lib/slidepay/resources/api_resource.rb
Direct Known Subclasses
SlidePay::ACH::Resource, ApiKey, BankAccount, Company, Customer, Location, Payment, UserMaster
Class Attribute Summary collapse
-
.id_attribute ⇒ Object
Returns the value of attribute id_attribute.
-
.url_root ⇒ Object
Returns the value of attribute url_root.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#id_attribute ⇒ Object
Returns the value of attribute id_attribute.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url_root ⇒ Object
Returns the value of attribute url_root.
Class Method Summary collapse
- .api_resource_array_from_response(response) ⇒ Object
- .find(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
- .find_between(start_params, end_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
- .find_greater_than(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
- .find_less_than(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
- .find_where(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
- .retrieve(token = nil, api_key = nil, endpoint = nil) ⇒ Object
Instance Method Summary collapse
- #destroy(options_hash = {}) ⇒ Object
- #id ⇒ Object
-
#initialize(values_hash = {}) ⇒ ApiResource
constructor
A new instance of ApiResource.
- #is_new? ⇒ Boolean
- #populate_from_response(response) ⇒ Object
- #retrieve(options_hash = {}) ⇒ Object
- #save(options_hash = {}) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(values_hash = {}) ⇒ ApiResource
Returns a new instance of ApiResource.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/slidepay/resources/api_resource.rb', line 5 def initialize(values_hash={}) values_clone = values_hash.clone() if values_clone[:url_root] @url_root = values_clone[:url_root] values_clone.delete(:url_root) end if values_clone[:id_attribute] @id_attribute = values_clone[:id_attribute] values_clone.delete(:id_attribute) end if values_clone[:token] @token = values_clone[:token] values_clone.delete(:token) end if values_clone[:api_key] @api_key = values_clone[:api_key] values_clone.delete(:api_key) end if values_clone[:endpoint] @endpoint = values_clone[:endpoint] values_clone.delete(:endpoint) end merge! values_clone end |
Class Attribute Details
.id_attribute ⇒ Object
Returns the value of attribute id_attribute.
120 121 122 |
# File 'lib/slidepay/resources/api_resource.rb', line 120 def id_attribute @id_attribute end |
.url_root ⇒ Object
Returns the value of attribute url_root.
120 121 122 |
# File 'lib/slidepay/resources/api_resource.rb', line 120 def url_root @url_root end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
3 4 5 |
# File 'lib/slidepay/resources/api_resource.rb', line 3 def api_key @api_key end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
3 4 5 |
# File 'lib/slidepay/resources/api_resource.rb', line 3 def endpoint @endpoint end |
#id_attribute ⇒ Object
Returns the value of attribute id_attribute.
3 4 5 |
# File 'lib/slidepay/resources/api_resource.rb', line 3 def id_attribute @id_attribute end |
#token ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/slidepay/resources/api_resource.rb', line 3 def token @token end |
#url_root ⇒ Object
Returns the value of attribute url_root.
3 4 5 |
# File 'lib/slidepay/resources/api_resource.rb', line 3 def url_root @url_root end |
Class Method Details
.api_resource_array_from_response(response) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/slidepay/resources/api_resource.rb', line 122 def api_resource_array_from_response(response) resources_array = [] if response.was_successful? and response.data != nil data = response.data data.each do |resource_hash| resources_array.push self.new(resource_hash) end end resources_array end |
.find(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/slidepay/resources/api_resource.rb', line 140 def find(search_params, token=nil, api_key=nil, endpoint=nil) if search_params.is_a? Hash search_params = [search_params] elsif not search_params.is_a? Array raise Exception.new("Invalid or no search filter supplied.") end response = SlidePay.put(path: @url_root, data: search_params.to_json, token: token, api_key: api_key, endpoint: endpoint) return api_resource_array_from_response(response) end |
.find_between(start_params, end_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/slidepay/resources/api_resource.rb', line 160 def find_between(start_params, end_params, token=nil, api_key=nil, endpoint=nil) sfa = [] start_params.each_pair do |k,v| sfa.push(field: k, condition: "greater_than", value: v) end end_params.each_pair do |k,v| sfa.push(field: k, condition: "less_than", value: v) end return find(sfa, token, api_key, endpoint) end |
.find_greater_than(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/slidepay/resources/api_resource.rb', line 173 def find_greater_than(search_params, token=nil, api_key=nil, endpoint=nil) sfa = [] search_params.each_pair do |k,v| sfa.push(field: k, condition: "greater_than", value: v) end return find(sfa, token, api_key, endpoint) end |
.find_less_than(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/slidepay/resources/api_resource.rb', line 182 def find_less_than(search_params, token=nil, api_key=nil, endpoint=nil) sfa = [] search_params.each_pair do |k,v| sfa.push(field: k, condition: "less_than", value: v) end return find(sfa, token, api_key, endpoint) end |
.find_where(search_params, token = nil, api_key = nil, endpoint = nil) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/slidepay/resources/api_resource.rb', line 151 def find_where(search_params, token=nil, api_key=nil, endpoint=nil) sfa = [] search_params.each_pair do |k,v| sfa.push(field: k, condition: "equals", value: v) end return find(sfa, token, api_key, endpoint) end |
.retrieve(token = nil, api_key = nil, endpoint = nil) ⇒ Object
134 135 136 137 138 |
# File 'lib/slidepay/resources/api_resource.rb', line 134 def retrieve(token=nil, api_key=nil, endpoint=nil) response = SlidePay.get(path: @url_root, token: token, api_key: api_key, endpoint: endpoint) return api_resource_array_from_response(response) end |
Instance Method Details
#destroy(options_hash = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/slidepay/resources/api_resource.rb', line 100 def destroy(={}) token = @token || [:token] api_key = @api_key || [:api_key] endpoint = @endpoint || [:endpoint] if is_new? raise Exception.new("Cannot destroy a resource that has not been saved.") end response = SlidePay.delete(path: self.url(), token: token, api_key: api_key, endpoint: endpoint) if response.was_successful? self[@id_attribute] = nil true else false end end |
#id ⇒ Object
44 45 46 |
# File 'lib/slidepay/resources/api_resource.rb', line 44 def id self[@id_attribute] end |
#is_new? ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/slidepay/resources/api_resource.rb', line 48 def is_new? if id() == nil true else false end end |
#populate_from_response(response) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/slidepay/resources/api_resource.rb', line 56 def populate_from_response(response) if response.data.instance_of? Array self.merge! response.data.first elsif response.data.instance_of? Hash self.merge! response.data else raise Exception.new("Resource with the specified id not found on the server.") end end |
#retrieve(options_hash = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/slidepay/resources/api_resource.rb', line 66 def retrieve(={}) token = @token || [:token] api_key = @api_key || [:api_key] endpoint = @endpoint || [:endpoint] if is_new? raise Exception.new("Cannot retrieve an unsaved object from the server.") end response = SlidePay.get(path: self.url(), token: token, api_key: api_key, endpoint: endpoint) if response.was_successful? self.populate_from_response(response) end end |
#save(options_hash = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/slidepay/resources/api_resource.rb', line 81 def save(={}) token = @token || [:token] api_key = @api_key || [:api_key] endpoint = @endpoint || [:endpoint] if is_new? response = SlidePay.post(path: self.url(), token: token, api_key: api_key, endpoint: endpoint, data: self.to_json) else response = SlidePay.put(path: self.url(), token: token, api_key: api_key, endpoint: endpoint, data: self.to_json) end if response.was_successful? self.populate_from_response(response) true else false end end |
#url ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/slidepay/resources/api_resource.rb', line 36 def url if is_new? @url_root else "#{@url_root}/#{self.id()}" end end |