Class: LiveQA::LiveQAObject

Inherits:
Object
  • Object
show all
Defined in:
lib/liveqa/liveqa_object.rb

Overview

LiveQA Object

Define the API objects

Direct Known Subclasses

APIResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#acceptedObject (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

#dataHash (readonly)

Returns JSON parsed response.

Returns:

  • (Hash)

    JSON parsed response



13
14
15
# File 'lib/liveqa/liveqa_object.rb', line 13

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/liveqa/liveqa_object.rb', line 18

def errors
  @errors
end

#rawHash (readonly)

Returns JSON parsed response.

Returns:

  • (Hash)

    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

Parameters:

  • data (Object)


91
92
93
# File 'lib/liveqa/liveqa_object.rb', line 91

def add_data(data)
  @data << data
end

#keysArray

Returns all the keys.

Returns:

  • (Array)

    all the keys



61
62
63
# File 'lib/liveqa/liveqa_object.rb', line 61

def keys
  @values.keys
end

#to_hashHash

Returns values to hash.

Returns:

  • (Hash)

    values to hash



67
68
69
# File 'lib/liveqa/liveqa_object.rb', line 67

def to_hash
  @values
end

#to_jsonJSON

Returns values to JSON.

Returns:

  • (JSON)

    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

Parameters:

  • values (Hash)


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