class Hawkei::HawkeiObject
Hawkei
Object¶ ↑
Define the API objects
Attributes
data[R]
@return [Hash] JSON parsed response
errors[R]
raw[R]
@return [Hash] JSON parsed response
successful[R]
successful?[R]
Public Class Methods
initialize_from(response, object = new)
click to toggle source
Initialize from the API response
@return [Hawkei::HawkeiObject]
# File lib/hawkei/hawkei_object.rb, line 26 def initialize_from(response, object = new) object.load_response_api(response.is_a?(Hash) ? response : Util.safe_json_parse(response)) object.update_attributes(Hawkei::Util.except_keys(object.raw, :data)) if object.raw[:object] == 'list' object.raw[:data].each do |response_object| data = object.type_from_string_object(response_object[:object]).initialize_from(response_object) object.add_data(data) end end object end
new(values = {})
click to toggle source
Initialize and create accessor for values
@params [Hash] values
# File lib/hawkei/hawkei_object.rb, line 61 def initialize(values = {}) @data = [] @values = {} update_attributes(values) end
Public Instance Methods
[](key)
click to toggle source
get attribute value
# File lib/hawkei/hawkei_object.rb, line 70 def [](key) @values[key.to_sym] end
[]=(key, value)
click to toggle source
set attribute value
# File lib/hawkei/hawkei_object.rb, line 76 def []=(key, value) send(:"#{key}=", value) end
add_data(data)
click to toggle source
Add data for sub-object
@param [Object] data
# File lib/hawkei/hawkei_object.rb, line 121 def add_data(data) @data << data end
inspect()
click to toggle source
# File lib/hawkei/hawkei_object.rb, line 137 def inspect id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : '' "#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values) end
keys()
click to toggle source
@return [Array] all the keys
# File lib/hawkei/hawkei_object.rb, line 82 def keys @values.keys end
load_response_api(response)
click to toggle source
Load the root components for the API response
@param [Hash] values
# File lib/hawkei/hawkei_object.rb, line 112 def load_response_api(response) @raw = Hawkei::Util.deep_underscore_key(response) @successful = true end
method_missing(name, *args)
click to toggle source
Calls superclass method
# File lib/hawkei/hawkei_object.rb, line 142 def method_missing(name, *args) super unless name.to_s.end_with?('=') attribute = name.to_s[0...-1].to_sym value = args.first add_accessor(attribute, value) end
to_hash()
click to toggle source
@return [Hash] values to hash
# File lib/hawkei/hawkei_object.rb, line 88 def to_hash @values end
to_json(_object = nil)
click to toggle source
@return [JSON] values to JSON
# File lib/hawkei/hawkei_object.rb, line 94 def to_json(_object = nil) JSON.generate(@values) end
type_from_string_object(string_object)
click to toggle source
Create an object from a string
@param [String] object to be build
@return [Object]
# File lib/hawkei/hawkei_object.rb, line 131 def type_from_string_object(string_object) klass_name = Hawkei::Util.camelize(string_object.to_s) Object.const_get("Hawkei::#{klass_name}") end
update_attributes(attributes)
click to toggle source
Update the attribute and add accessor for new attributes
@param [Hash] values
# File lib/hawkei/hawkei_object.rb, line 102 def update_attributes(attributes) attributes.each do |(key, value)| add_accessor(key, value) end end
update_from(response)
click to toggle source
Update the object based on the response from the API Remove and new accessor
@param [Hash] response
@return [Hawkei::HawkeiObject]
# File lib/hawkei/hawkei_object.rb, line 49 def update_from(response) self.class.initialize_from(response, self) (@values.keys - raw.keys).each { |key| remove_accessor(key) } self end
Private Instance Methods
add_accessor(name, value)
click to toggle source
# File lib/hawkei/hawkei_object.rb, line 159 def add_accessor(name, value) @values[name] = value define_singleton_method(name) { @values[name] } define_singleton_method(:"#{name}=") do |v| @values[name] = v end define_singleton_method(:"#{name}?") { value } if [FalseClass, TrueClass].include?(value.class) end
add_accessors(keys, payload = raw)
click to toggle source
# File lib/hawkei/hawkei_object.rb, line 153 def add_accessors(keys, payload = raw) keys.each do |key| add_accessor(key, payload[key]) end end
remove_accessor(name)
click to toggle source
# File lib/hawkei/hawkei_object.rb, line 170 def remove_accessor(name) @values.delete(name) singleton_class.class_eval { remove_method name.to_sym } if singleton_methods.include?(name.to_sym) singleton_class.class_eval { remove_method "#{name}=".to_sym } if singleton_methods.include?("#{name}=".to_sym) singleton_class.class_eval { remove_method "#{name}?".to_sym } if singleton_methods.include?("#{name}?".to_sym) end