Class: Gwooks::Payload

Inherits:
Hash
  • Object
show all
Defined in:
lib/gwooks/payload.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(payload) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/gwooks/payload.rb', line 9

def new(payload)
  new_hash = new_without_indifferent_access do |hash, key|
    hash[key.to_s] if key.is_a? Symbol
  end
  payload = JSON.parse(payload) if payload.is_a? String
  set_branch payload
  new_hash.update(payload)
end

.new_without_indifferent_accessObject



7
# File 'lib/gwooks/payload.rb', line 7

alias_method :new_without_indifferent_access, :new

Instance Method Details

#resolve(key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gwooks/payload.rb', line 27

def resolve(key)
  key.split(".").inject(self) do |obj, segment|
    break nil if obj.nil?
    if obj.is_a? Array
      obj.map do |item|
        item[segment] 
      end.flatten
    else
      obj[segment]
    end
  end
end

#update(hash) ⇒ Object



41
42
43
44
45
46
# File 'lib/gwooks/payload.rb', line 41

def update(hash)
  hash.each do |key, obj|
    self[key] = make_indifferent(obj)
  end
  self
end

#update_without_indifferent_accessObject



40
# File 'lib/gwooks/payload.rb', line 40

alias_method :update_without_indifferent_access, :update