Class: Ac::AcObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ac/ac_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_json) ⇒ AcObject

Returns a new instance of AcObject.



23
24
25
26
27
28
# File 'lib/ac/ac_object.rb', line 23

def initialize(parsed_json)
  @values = parsed_json.transform_keys(&:to_s)
  @values.keys.each do |key|
    define_singleton_method(key) { self.[](key) } unless respond_to? key
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/ac/ac_object.rb', line 3

def response
  @response
end

Class Method Details

.from_response(response) ⇒ Object



5
6
7
8
9
10
# File 'lib/ac/ac_object.rb', line 5

def self.from_response(response)
  parsed_json = JSON.parse(response.body) rescue {}
  ac_object = new(parsed_json)
  ac_object.instance_variable_set("@response", response)
  ac_object
end

.new(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/ac/ac_object.rb', line 12

def self.new value
  case value
  when Hash
    super(value)
  when Array
    value.map { new(_1) }
  else
    value
  end
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
47
# File 'lib/ac/ac_object.rb', line 44

def ==(other)
  other.is_a? AcObject
  @values == other.instance_variable_get('@values')
end

#[](key) ⇒ Object



30
31
32
# File 'lib/ac/ac_object.rb', line 30

def [](key)
  wrap(@values[key.to_s])
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ac/ac_object.rb', line 49

def eql?(other)
  self == other
end

#inspectObject



38
39
40
41
42
# File 'lib/ac/ac_object.rb', line 38

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

#jsonObject



61
62
63
# File 'lib/ac/ac_object.rb', line 61

def json
  @values
end

#keysObject



53
54
55
# File 'lib/ac/ac_object.rb', line 53

def keys
  @values.keys
end

#to_s(*_args) ⇒ Object



34
35
36
# File 'lib/ac/ac_object.rb', line 34

def to_s(*_args)
  JSON.pretty_generate(@values)
end

#valuesObject



57
58
59
# File 'lib/ac/ac_object.rb', line 57

def values
  @values.values
end