Class: LiveQA::LiveQAObject
- Inherits:
-
Object
- Object
- LiveQA::LiveQAObject
- Defined in:
- lib/liveqa/liveqa_object.rb
Overview
LiveQA Object
Define the API objects
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
JSON parsed response.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#raw ⇒ Hash
readonly
JSON parsed response.
-
#successful ⇒ Object
(also: #successful?)
readonly
Returns the value of attribute successful.
Class Method Summary collapse
-
.initialize_from(response, object = new) ⇒ LiveQA::LiveQAObject
Initialize from the API response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
get attribute value.
-
#[]=(key, value) ⇒ Object
set attribute value.
-
#add_data(data) ⇒ Object
Add data for sub-object.
-
#initialize(values = {}) ⇒ LiveQAObject
constructor
Initialize and create accessor for values.
- #inspect ⇒ Object
-
#keys ⇒ Array
All the keys.
-
#load_response_api(response) ⇒ Object
Load the root components for the API response.
- #method_missing(name, *args) ⇒ Object
-
#to_hash ⇒ Hash
Values to hash.
-
#to_json(_object = nil) ⇒ JSON
Values to JSON.
-
#type_from_string_object(string_object) ⇒ Object
Create an object from a string.
-
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes.
-
#update_from(response) ⇒ LiveQA::LiveQAObject
Update the object based on the response from the API Remove and new accessor.
Constructor Details
#initialize(values = {}) ⇒ LiveQAObject
Initialize and create accessor for values
61 62 63 64 65 66 |
# File 'lib/liveqa/liveqa_object.rb', line 61 def initialize(values = {}) @data = [] @values = {} update_attributes(values) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/liveqa/liveqa_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 |
Instance Attribute Details
#data ⇒ Hash (readonly)
Returns JSON parsed response.
13 14 15 |
# File 'lib/liveqa/liveqa_object.rb', line 13 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
15 16 17 |
# File 'lib/liveqa/liveqa_object.rb', line 15 def errors @errors end |
#raw ⇒ Hash (readonly)
Returns JSON parsed response.
9 10 11 |
# File 'lib/liveqa/liveqa_object.rb', line 9 def raw @raw end |
#successful ⇒ Object (readonly) Also known as: successful?
Returns the value of attribute successful.
17 18 19 |
# File 'lib/liveqa/liveqa_object.rb', line 17 def successful @successful end |
Class Method Details
.initialize_from(response, object = new) ⇒ LiveQA::LiveQAObject
Initialize from the API response
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/liveqa/liveqa_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(LiveQA::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 |
Instance Method Details
#[](key) ⇒ Object
get attribute value
70 71 72 |
# File 'lib/liveqa/liveqa_object.rb', line 70 def [](key) @values[key.to_sym] end |
#[]=(key, value) ⇒ Object
set attribute value
76 77 78 |
# File 'lib/liveqa/liveqa_object.rb', line 76 def []=(key, value) send(:"#{key}=", value) end |
#add_data(data) ⇒ Object
Add data for sub-object
121 122 123 |
# File 'lib/liveqa/liveqa_object.rb', line 121 def add_data(data) @data << data end |
#inspect ⇒ Object
137 138 139 140 |
# File 'lib/liveqa/liveqa_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 ⇒ Array
Returns all the keys.
82 83 84 |
# File 'lib/liveqa/liveqa_object.rb', line 82 def keys @values.keys end |
#load_response_api(response) ⇒ Object
Load the root components for the API response
112 113 114 115 |
# File 'lib/liveqa/liveqa_object.rb', line 112 def load_response_api(response) @raw = LiveQA::Util.deep_underscore_key(response) @successful = true end |
#to_hash ⇒ Hash
Returns values to hash.
88 89 90 |
# File 'lib/liveqa/liveqa_object.rb', line 88 def to_hash @values end |
#to_json(_object = nil) ⇒ JSON
Returns values to JSON.
94 95 96 |
# File 'lib/liveqa/liveqa_object.rb', line 94 def to_json(_object = nil) JSON.generate(@values) end |
#type_from_string_object(string_object) ⇒ Object
Create an object from a string
131 132 133 134 135 |
# File 'lib/liveqa/liveqa_object.rb', line 131 def type_from_string_object(string_object) klass_name = LiveQA::Util.camelize(string_object.to_s) Object.const_get("LiveQA::#{klass_name}") end |
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes
102 103 104 105 106 |
# File 'lib/liveqa/liveqa_object.rb', line 102 def update_attributes(attributes) attributes.each do |(key, value)| add_accessor(key, value) end end |
#update_from(response) ⇒ LiveQA::LiveQAObject
Update the object based on the response from the API Remove and new accessor
49 50 51 52 53 54 55 |
# File 'lib/liveqa/liveqa_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 |