Class: Sequence::ResponseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/sequence/response_object.rb

Defined Under Namespace

Classes: DetranslateError, TranslateError

Instance Method Summary collapse

Constructor Details

#initialize(raw_attribs) ⇒ ResponseObject

Returns a new instance of ResponseObject.



6
7
8
9
10
11
# File 'lib/sequence/response_object.rb', line 6

def initialize(raw_attribs)
  raw_attribs.each do |k, v|
    next unless self.class.has_attrib?(k)
    self[k] = self.class.translate(k, v) unless v.nil?
  end
end

Instance Method Details

#[](attrib_name) ⇒ Object

Raises:

  • (KeyError)


29
30
31
32
33
34
# File 'lib/sequence/response_object.rb', line 29

def [](attrib_name)
  attrib_name = attrib_name.to_sym
  raise KeyError.new("key not found: #{attrib_name}") unless self.class.attrib_opts.key?(attrib_name)

  instance_variable_get "@#{attrib_name}"
end

#[]=(attrib_name, value) ⇒ Object

Raises:

  • (KeyError)


36
37
38
39
40
41
# File 'lib/sequence/response_object.rb', line 36

def []=(attrib_name, value)
  attrib_name = attrib_name.to_sym
  raise KeyError.new("key not found: #{attrib_name}") unless self.class.attrib_opts.key?(attrib_name)

  instance_variable_set "@#{attrib_name}", value
end

#to_hObject



13
14
15
16
17
18
# File 'lib/sequence/response_object.rb', line 13

def to_h
  self.class.attrib_opts.keys.reduce({}) do |memo, name|
    memo[name] = instance_variable_get("@#{name}")
    memo
  end
end

#to_json(opts = nil) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/sequence/response_object.rb', line 20

def to_json(opts = nil)
  h = to_h.reduce({}) do |memo, (k, v)|
    memo[k] = self.class.detranslate(k, v)
    memo
  end

  h.to_json
end