Class: MessageBird::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/messagebird/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Base

Returns a new instance of Base.



19
20
21
# File 'lib/messagebird/base.rb', line 19

def initialize(json)
  map_hash_elements_to_self(json)
end

Instance Method Details

#map_hash_elements_to_self(hash) ⇒ Object

takes each element from the given hash and apply it to ourselves through an assignment method



9
10
11
12
13
14
15
16
17
# File 'lib/messagebird/base.rb', line 9

def map_hash_elements_to_self(hash)
  return if hash.nil?

  hash.each do |key, value|
    method_name = key.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase # convert came case to snake case
    method_name += '='
    send(method_name, value) if respond_to?(method_name)
  end
end

#value_to_time(value) ⇒ Object



23
24
25
# File 'lib/messagebird/base.rb', line 23

def value_to_time(value)
  value ? Time.parse(value) : nil
end