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
-
#accepted ⇒ Object
(also: #accepted?)
readonly
Returns the value of attribute accepted.
-
#data ⇒ Hash
readonly
JSON parsed response.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#raw ⇒ Hash
readonly
JSON parsed response.
Class Method Summary collapse
-
.initialize_from(_response = '') ⇒ 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.
-
#keys ⇒ Array
All the keys.
-
#to_hash ⇒ Hash
Values to hash.
-
#to_json ⇒ JSON
Values to JSON.
-
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes.
Constructor Details
#initialize(values = {}) ⇒ LiveQAObject
Initialize and create accessor for values
40 41 42 43 44 45 |
# File 'lib/liveqa/liveqa_object.rb', line 40 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 (protected)
102 103 104 105 106 107 108 109 |
# File 'lib/liveqa/liveqa_object.rb', line 102 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
#accepted ⇒ Object (readonly) Also known as: accepted?
Returns the value of attribute accepted.
15 16 17 |
# File 'lib/liveqa/liveqa_object.rb', line 15 def accepted @accepted end |
#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.
18 19 20 |
# File 'lib/liveqa/liveqa_object.rb', line 18 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 |
Class Method Details
.initialize_from(_response = '') ⇒ LiveQA::LiveQAObject
Initialize from the API response
26 27 28 29 30 31 32 |
# File 'lib/liveqa/liveqa_object.rb', line 26 def initialize_from(_response = '') object = new object.successful = true object end |
Instance Method Details
#[](key) ⇒ Object
get attribute value
49 50 51 |
# File 'lib/liveqa/liveqa_object.rb', line 49 def [](key) @values[key.to_sym] end |
#[]=(key, value) ⇒ Object
set attribute value
55 56 57 |
# File 'lib/liveqa/liveqa_object.rb', line 55 def []=(key, value) send(:"#{key}=", value) end |
#add_data(data) ⇒ Object
Add data for sub-object
91 92 93 |
# File 'lib/liveqa/liveqa_object.rb', line 91 def add_data(data) @data << data end |
#keys ⇒ Array
Returns all the keys.
61 62 63 |
# File 'lib/liveqa/liveqa_object.rb', line 61 def keys @values.keys end |
#to_hash ⇒ Hash
Returns values to hash.
67 68 69 |
# File 'lib/liveqa/liveqa_object.rb', line 67 def to_hash @values end |
#to_json ⇒ JSON
Returns values to JSON.
73 74 75 |
# File 'lib/liveqa/liveqa_object.rb', line 73 def to_json JSON.generate(@values) end |
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes
81 82 83 84 85 |
# File 'lib/liveqa/liveqa_object.rb', line 81 def update_attributes(attributes) attributes.each do |(key, value)| add_accessor(key, value) end end |