Class: Place::APIResource
- Inherits:
-
Object
- Object
- Place::APIResource
- Defined in:
- lib/place/api_resource.rb
Direct Known Subclasses
AccessToken, Account, Address, AutopayEnrollment, DepositAccount, Event, Invoice, InvoiceItem, InvoiceItemAllocation, InvoicePayer, PaymentMethod, RecurringInvoice, Transaction, TransactionAllocation
Constant Summary collapse
- @@object_index =
{}
Class Attribute Summary collapse
-
.default_params ⇒ Object
Returns the value of attribute default_params.
-
.object_type ⇒ Object
readonly
Returns the value of attribute object_type.
-
.resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Attribute Summary collapse
-
#_obj ⇒ Object
Returns the value of attribute _obj.
Class Method Summary collapse
- .create(obj, **params) ⇒ Object
- .delete_all(objects) ⇒ Object
- .descendants ⇒ Object
- .get(id, update: nil, **params) ⇒ Object
- .new(client: nil, obj: nil, **params) ⇒ Object
- .request(method, path: nil, id: nil, client: nil, json: nil, params: nil) ⇒ Object
- .select(update_all: nil, delete_all: false, **filter_by) ⇒ Object
- .update_all(updates, **params) ⇒ Object
Instance Method Summary collapse
- #_set_obj(obj) ⇒ Object
- #delete ⇒ Object
-
#initialize(client: nil, obj: nil, **params) ⇒ APIResource
constructor
A new instance of APIResource.
- #json ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #update(**updates) ⇒ Object
Constructor Details
#initialize(client: nil, obj: nil, **params) ⇒ APIResource
Returns a new instance of APIResource.
80 81 82 83 84 |
# File 'lib/place/api_resource.rb', line 80 def initialize(client: nil, obj: nil, **params) obj = Place::merge_obj( obj: obj, **params ) @client = client || Place.default_client self._set_obj(obj) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/place/api_resource.rb', line 94 def method_missing(name, *args) attr = name.to_s if @_obj.key?(attr) return @_obj[attr] end super end |
Class Attribute Details
.default_params ⇒ Object
Returns the value of attribute default_params.
65 66 67 |
# File 'lib/place/api_resource.rb', line 65 def default_params @default_params end |
.object_type ⇒ Object (readonly)
Returns the value of attribute object_type.
64 65 66 |
# File 'lib/place/api_resource.rb', line 64 def object_type @object_type end |
.resource ⇒ Object (readonly)
Returns the value of attribute resource.
64 65 66 |
# File 'lib/place/api_resource.rb', line 64 def resource @resource end |
Instance Attribute Details
#_obj ⇒ Object
Returns the value of attribute _obj.
51 52 53 |
# File 'lib/place/api_resource.rb', line 51 def _obj @_obj end |
Class Method Details
.create(obj, **params) ⇒ Object
206 207 208 209 210 211 212 213 214 |
# File 'lib/place/api_resource.rb', line 206 def self.create( obj, **params) if obj.class == Array obj = {"object"=>"list", "values"=>obj} else obj = Place::merge_obj( obj: obj, **params ) end obj = Place::conv_object(obj, inverse: true) return self.request('Post', json: obj) end |
.delete_all(objects) ⇒ Object
222 223 224 225 |
# File 'lib/place/api_resource.rb', line 222 def self.delete_all(objects) deletes = objects.map {|o| o.id }.join('|') return self.request('Delete', params: {'id'=>deletes}) end |
.descendants ⇒ Object
53 54 55 |
# File 'lib/place/api_resource.rb', line 53 def self.descendants ObjectSpace.each_object(Class).select { |klass| klass < self } end |
.get(id, update: nil, **params) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/place/api_resource.rb', line 186 def self.get(id,update:nil, **params) if id.empty? || id.nil? raise Place::APIException.new('id cannot be blank') end if update return self.request('Put', id: id, json: update, params: params ) end return self.request('Get', id: id, params: params) end |
.new(client: nil, obj: nil, **params) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/place/api_resource.rb', line 68 def self.new(client: nil, obj: nil, **params) obj = Place::merge_obj( obj: obj, **params ) if obj["id"] if @@object_index[obj["id"]] @@object_index[obj["id"]]._set_obj(obj) return @@object_index[obj["id"]] end end super end |
.request(method, path: nil, id: nil, client: nil, json: nil, params: nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/place/api_resource.rb', line 106 def self.request(method, path: nil, id: nil, client: nil, json: nil, params: nil) path = path || @resource client = client || Place.default_client if id; path = File.join(path, id) end if path[0] == '/'; path = path[1..-1] end uri = URI.join(client.api_url, path) if @default_params if !params; params = {} end @default_params.each do |key, param| if !params.key?(key.to_sym); params[key.to_sym] = param end end end if params; uri.query = URI.encode_www_form(params) end http = Net::HTTP.new(uri.host, uri.port) if uri.port == 443; http.use_ssl = true end request = Net::HTTP.const_get(method).new(uri.request_uri) request.basic_auth(client.api_key, "") request.add_field("X-API-Version", "v2.5") if json request.body = json.to_json request.add_field("Content-Type", "application/json") end response = http.request(request) status_code = response.code begin obj = JSON.parse(response.body) rescue JSON::ParserError if status_code == '500' raise Place::InternalError.new end raise Place::InvalidResponse.new end if obj.class != Hash raise Place::InvalidResponse.new end object_type = obj["object"] if !object_type raise Place::InvalidResponse.new('Response missing "object" attribute') end if status_code != '200' if object_type != 'error' raise Place::InvalidResponse.new('Expected error object') end for exc in Place::APIException.descendants if exc.status_code != status_code; next end if exc.error_type && exc.error_type != obj["error_type"]; next end raise exc.new(obj["error_description"],obj) end raise Place::APIException.new(obj["error_description"],obj) end if object_type == 'list' return obj["values"].map {|o| self.new(:client=>client, :obj=>o) } end return self.new(:client=>client, :obj=>obj) end |
.select(update_all: nil, delete_all: false, **filter_by) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/place/api_resource.rb', line 196 def self.select( update_all: nil, delete_all: false, **filter_by) if update_all return self.request('Put', params: filter_by, json: update_all) elsif delete_all return self.request('Delete', params: filter_by) else return self.request('Get', params: filter_by) end end |
.update_all(updates, **params) ⇒ Object
216 217 218 219 220 |
# File 'lib/place/api_resource.rb', line 216 def self.update_all(updates, **params) updates = updates.map {|o, upd| Hash(id: o.id, **upd) } return self.request('Put', json: {"object"=>"list", "values"=>updates}, params: params) end |
Instance Method Details
#_set_obj(obj) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/place/api_resource.rb', line 86 def _set_obj(obj) @_obj = obj @_obj = Place::conv_object(@_obj, client: @_client) if obj["id"] @@object_index[obj["id"]] = self end end |
#delete ⇒ Object
182 183 184 |
# File 'lib/place/api_resource.rb', line 182 def delete() self.class.request('Delete', id: self.id) end |
#json ⇒ Object
102 103 104 |
# File 'lib/place/api_resource.rb', line 102 def json() return JSON.pretty_generate(Place::conv_object(@_obj, inverse: true)) end |
#update(**updates) ⇒ Object
178 179 180 |
# File 'lib/place/api_resource.rb', line 178 def update(**updates) self.class.request('Put', id: self.id, json: updates) end |