class Essential::Resource
Attributes
schema[R]
Public Class Methods
new(sid: nil, attrs: nil, headers: nil)
click to toggle source
# File lib/essential/resource.rb, line 45 def initialize(sid: nil, attrs: nil, headers: nil) init_from(attrs) @attributes['sid'] = sid if sid @headers = headers self end
request(method, url: nil, params: nil, headers: nil)
click to toggle source
# File lib/essential/resource.rb, line 14 def request(method, url: nil, params: nil, headers: nil) if headers headers = headers.clone sid = headers.delete(:sid) token = headers.delete(:token) end Essential.request(method, url, sid: sid, token: token, params: params, headers: headers) end
url()
click to toggle source
# File lib/essential/resource.rb, line 33 def self.url if self == Resource raise NotImplementedError, 'Resource is an abstract class' end "/v2/account/#{CGI.escape(class_name.downcase)}s" end
Protected Class Methods
class_name()
click to toggle source
# File lib/essential/resource.rb, line 26 def class_name self.name.split('::').last end
Public Instance Methods
==(other)
click to toggle source
# File lib/essential/resource.rb, line 84 def ==(other) case other when Essential::Resource self.sid == other.sid else false end end
as_json(opts={})
click to toggle source
# File lib/essential/resource.rb, line 76 def as_json(opts={}) @attributes.dup end
fetch(headers: @headers)
click to toggle source
# File lib/essential/resource.rb, line 52 def fetch(headers: @headers) json = JSON.parse self.class.request(:get, url: self.url, headers: headers) init_from json self end
init_from(attributes)
click to toggle source
# File lib/essential/resource.rb, line 58 def init_from(attributes) attributes = (attributes || {}).clone # reject reject keys that aren't actually our attrs @attributes = filter_attrs(attributes) if self.class.schema self.class.schema.each do |key,type| apply_schema(key, type) end end self end
inspect()
click to toggle source
# File lib/essential/resource.rb, line 93 def inspect format( '#<%s:0x%s @attributes=%s>', self.class.name, (self.object_id << 1).to_s(16), JSON.pretty_generate(@attributes) ) end
loaded?()
click to toggle source
# File lib/essential/resource.rb, line 72 def loaded? @attributes.size > 1 end
to_json(opts={})
click to toggle source
# File lib/essential/resource.rb, line 80 def to_json(opts={}) as_json.to_json(opts) end
url()
click to toggle source
# File lib/essential/resource.rb, line 40 def url raise ArgumentError, 'sid required' if sid.nil? || sid.empty? format('%s/%s', self.class.url, CGI.escape(self.sid)) end
Protected Instance Methods
[](key)
click to toggle source
# File lib/essential/resource.rb, line 103 def [](key) # realize attributes upon access self.fetch unless :sid == key.to_sym || loaded? @attributes[key] end
apply_schema(key, type)
click to toggle source
# File lib/essential/resource.rb, line 109 def apply_schema(key, type) if val = @attributes[key] if type === val return elsif type.respond_to?(:parse) val = type.parse(@attributes[key]) if type == Time && Essential.utc_offset # coerce into local time val = val.getlocal(Essential.utc_offset) end @attributes[key] = val else raise ArgumentError, format('unsure how to apply schema "%s"', type) end end end