Class: Barometer::Utils::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/barometer/utils/payload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, query = nil) ⇒ Payload

Returns a new instance of Payload.



6
7
8
9
# File 'lib/barometer/utils/payload.rb', line 6

def initialize(hash, query=nil)
  @hash = hash
  @query = query
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



4
5
6
# File 'lib/barometer/utils/payload.rb', line 4

def hash
  @hash
end

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'lib/barometer/utils/payload.rb', line 4

def query
  @query
end

#regexObject (readonly)

Returns the value of attribute regex.



4
5
6
# File 'lib/barometer/utils/payload.rb', line 4

def regex
  @regex
end

Instance Method Details

#each(*paths, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/barometer/utils/payload.rb', line 30

def each(*paths, &block)
  path = fetch(*paths)
  if path
    path.each do |result|
      result_payload = Barometer::Utils::Payload.new(result)
      block.call(result_payload)
    end
  end
end

#each_with_index(*paths, &block) ⇒ Object



40
41
42
43
44
45
# File 'lib/barometer/utils/payload.rb', line 40

def each_with_index(*paths, &block)
  fetch(*paths).each_with_index do |result, index|
    result_payload = Barometer::Utils::Payload.new(result)
    block.call(result_payload, index)
  end
end

#fetch(*paths) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/barometer/utils/payload.rb', line 16

def fetch(*paths)
  if hash
    result = fetch_value_or_attribute(paths)
  else
    result = nil
  end

  result = _apply_regex(result)
  result = _cleanup(result)
  result = _convert_alternate_nil_values(result)

  result
end

#fetch_each(*paths, &block) ⇒ Object



47
48
49
# File 'lib/barometer/utils/payload.rb', line 47

def fetch_each(*paths, &block)
  each(*paths, &block)
end

#fetch_each_with_index(*paths, &block) ⇒ Object



51
52
53
# File 'lib/barometer/utils/payload.rb', line 51

def fetch_each_with_index(*paths, &block)
  each_with_index(*paths, &block)
end

#unitsObject



55
56
57
# File 'lib/barometer/utils/payload.rb', line 55

def units
  query.units if query
end

#using(regex) ⇒ Object



11
12
13
14
# File 'lib/barometer/utils/payload.rb', line 11

def using(regex)
  @regex = regex
  self
end