class Place::APIResource
Attributes
default_params[RW]
object_type[R]
resource[R]
_obj[RW]
Public Class Methods
create( obj, **params)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Calls superclass method
# 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
new(client: nil, obj: nil, **params)
click to toggle source
# 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
request(method, path: nil, id: nil, client: nil, json: nil, params: nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Public Instance Methods
_set_obj(obj)
click to toggle source
# 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()
click to toggle source
# File lib/place/api_resource.rb, line 182 def delete() self.class.request('Delete', id: self.id) end
json()
click to toggle source
# File lib/place/api_resource.rb, line 102 def json() return JSON.pretty_generate(Place::conv_object(@_obj, inverse: true)) end
method_missing(name, *args)
click to toggle source
Calls superclass method
# 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
update(**updates)
click to toggle source
# File lib/place/api_resource.rb, line 178 def update(**updates) self.class.request('Put', id: self.id, json: updates) end