class BlockScore::Base
Public Class Methods
api_url()
click to toggle source
# File lib/blockscore/base.rb, line 61 def self.api_url 'https://api.blockscore.com/' end
endpoint()
click to toggle source
# File lib/blockscore/base.rb, line 65 def self.endpoint fail NotImplementedError, 'Base is an abstract class, not an API resource' if equal?(Base) "#{api_url}#{Util.to_plural(resource)}" end
new(options = {}, &block)
click to toggle source
# File lib/blockscore/base.rb, line 7 def initialize(options = {}, &block) @loaded = !(block) @proc = block @attributes = options end
resource()
click to toggle source
# File lib/blockscore/base.rb, line 57 def self.resource @resource ||= Util.to_underscore(to_s.split('::').last) end
Public Instance Methods
attributes()
click to toggle source
# File lib/blockscore/base.rb, line 13 def attributes return @attributes if @loaded force! @attributes end
force!()
click to toggle source
# File lib/blockscore/base.rb, line 19 def force! res = @proc.call @attributes = res.attributes.merge(@attributes) @loaded = true self end
id()
click to toggle source
# File lib/blockscore/base.rb, line 26 def id @attributes.fetch(:id, nil) end
inspect()
click to toggle source
# File lib/blockscore/base.rb, line 30 def inspect str_attr = "JSON:#{JSON.pretty_generate(attributes)}" "#<#{self.class}:#{format('%#016x', object_id << 1)} #{str_attr}>" end
persisted?()
click to toggle source
# File lib/blockscore/base.rb, line 71 def persisted? !id.nil? && !attributes[:deleted] end
refresh()
click to toggle source
# File lib/blockscore/base.rb, line 35 def refresh res = self.class.retrieve(id) @attributes = res.attributes true rescue Error false end
save()
click to toggle source
# File lib/blockscore/base.rb, line 44 def save save! rescue false end
save!()
click to toggle source
# File lib/blockscore/base.rb, line 50 def save! response = self.class.post(self.class.endpoint, attributes) @attributes = response.attributes true end
Protected Instance Methods
add_accessor(symbol, *_args)
click to toggle source
# File lib/blockscore/base.rb, line 77 def add_accessor(symbol, *_args) singleton_class.instance_eval do define_method(symbol) do wrap_attribute(attributes[symbol]) end end end
add_setter(symbol, *_args)
click to toggle source
# File lib/blockscore/base.rb, line 85 def add_setter(symbol, *_args) singleton_class.instance_eval do define_method(symbol) do |value| attributes[symbol.to_s.chop.to_sym] = value end end end
Private Instance Methods
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/blockscore/base.rb, line 95 def method_missing(method, *args, &block) if respond_to_missing?(method) if setter?(method) add_setter(method, args) else add_accessor(method, args) end send(method, *args) else super end end
respond_to_missing?(symbol, include_private = false)
click to toggle source
Calls superclass method
# File lib/blockscore/base.rb, line 108 def respond_to_missing?(symbol, include_private = false) setter?(symbol) || attributes && attributes.key?(symbol) || super end
setter?(symbol)
click to toggle source
# File lib/blockscore/base.rb, line 112 def setter?(symbol) symbol.to_s.end_with?('=') end
wrap_array(arr)
click to toggle source
# File lib/blockscore/base.rb, line 127 def wrap_array(arr) arr.map { |item| wrap_attribute(item) } end
wrap_attribute(attribute)
click to toggle source
# File lib/blockscore/base.rb, line 116 def wrap_attribute(attribute) case attribute when Array wrap_array(attribute) when Hash wrap_hash(attribute) else attribute end end
wrap_hash(hsh)
click to toggle source
# File lib/blockscore/base.rb, line 131 def wrap_hash(hsh) hsh.each { |key, value| hsh[key] = wrap_attribute(value) } OpenStruct.new(hsh) end