Class: Sequence::ResponseObject

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

Defined Under Namespace

Classes: DetranslateError, Snapshot, TranslateError

Instance Method Summary collapse

Constructor Details

#initialize(raw_attribs) ⇒ ResponseObject

Returns a new instance of ResponseObject.



8
9
10
11
12
13
# File 'lib/sequence/response_object.rb', line 8

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



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

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

  instance_variable_get "@#{attrib_name}"
end

#[]=(attrib_name, value) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/sequence/response_object.rb', line 38

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

  instance_variable_set "@#{attrib_name}", value
end

#to_hObject



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

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

#to_json(_opts = nil) ⇒ Object



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

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

  h.to_json
end