module Icasework::Resource::Data

Converts data returned from the iCasework API into a more “Ruby like” hash

Public Class Methods

process(data) click to toggle source
# File lib/icasework/resource/data.rb, line 10
def process(data)
  case data
  when Hash
    convert_keys(array_keys_to_array(flat_keys_to_nested(data)))
  when Array
    data.map { |d| process(d) }
  else
    data
  end
end

Private Class Methods

array_keys_to_array(hash) click to toggle source

converts: { 'n1': 'foo', 'n2': 'bar' } into: { n: ['foo', 'bar'] }

# File lib/icasework/resource/data.rb, line 35
def array_keys_to_array(hash)
  hash.each_with_object({}) do |(key, value), all|
    if key.to_s =~ /^(.*)\d+$/
      key = Regexp.last_match(1)
      all[key] ||= []
      all[key] << process(value)
    else
      all[key] = process(value)
    end
  end
end
convert_keys(hash) click to toggle source

converts: 'FooBar' into: :foo_bar

# File lib/icasework/resource/data.rb, line 49
def convert_keys(hash)
  hash.each_with_object({}) do |(key, value), all|
    converted_key = key.gsub(/([a-z\d])?([A-Z])/) do
      first = Regexp.last_match(1)
      second = Regexp.last_match(2)
      "#{"#{first}_" if first}#{second.downcase}"
    end

    all[converted_key.to_sym] = process(value)
  end
end
flat_keys_to_nested(hash) click to toggle source

converts: { 'foo.bar': 'baz' } into { foo: { bar: 'baz' } }

# File lib/icasework/resource/data.rb, line 25
def flat_keys_to_nested(hash)
  hash.each_with_object({}) do |(key, value), all|
    key_parts = key.to_s.split('.')
    leaf = key_parts[0...-1].inject(all) { |h, k| h[k] ||= {} }
    leaf[key_parts.last] = process(value)
  end
end