Module: Adzerk::Util

Instance Method Summary collapse

Instance Method Details

#camelize_data(data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/adzerk/util.rb', line 5

def camelize_data(data)
  return data unless data.respond_to?(:reduce)
  data.reduce({}) do |acc, (sym, val)|
    sym = sym.to_s.camelize if sym.class == Symbol
    acc[sym] = case val
                 when Hash then camelize_data(val)
                 when Array then val.map { |elem| camelize_data(elem) }
                 else val
               end
    acc
  end
end

#parse_response(response) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/adzerk/util.rb', line 31

def parse_response(response)
  parsed_body = JSON.parse(response.body)
  case parsed_body
    when Hash then uncamelize_data(parsed_body)
    when Array then parsed_body.map {|elem| uncamelize_data(elem)}
    else parsed_body
  end
end

#uncamelize_data(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/adzerk/util.rb', line 18

def uncamelize_data(data)
  # stop condition for the recursion
  return data unless data.respond_to?(:reduce)
  data.reduce({}) do |acc, (key, val)|
    acc[/[a-zA-Z]/.match?(key) ? key.underscore.to_sym : key] = case val
                                   when Hash then uncamelize_data(val)
                                   when Array then val.map {|elem| uncamelize_data(elem) }
                                   else val
                                 end
    acc
  end
end