Sha256: 12f21e42b5ca354a0d076c6c65e21a69e0e918818371d640f3ac86c38f5317e7

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# Utility Hash methods
class Hash
  def process_rows
    case self['rowset']
    when Hash
      normalize_hash_rowset
    when Array
      normalize_array_rowset
    end
    collapse_key
  end

  def collapse_key
    length == 1 ? self.merge!(delete(keys.first)) : self
  end

  def normalize_hash_rowset
    self.merge!(self['rowset']['name'] => self['rowset']['row'])
    delete('rowset')
  end

  def normalize_array_rowset
    self['rowset'].each do |rowset|
      self[rowset['name']] = rowset['row']
    end
    delete('rowset')
  end
end

# Utility String methods
class String
  def camelize
    split('_').each(&:capitalize!).join('')
  end

  # Stolen from ActiveSupport::Inflector
  def underscore
    return self unless self =~ /[A-Z-]|::/
    word = to_s.gsub(/::/, '/')
    word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
    word.tr!('-', '_')
    word.downcase!
    word
  end
end

module EVEApi
  # Utility methods
  module Util
    def json_get(url)
      Crack::JSON.parse Excon.get(url).body
    end

    def underscore_key(k)
      k.to_s.underscore.to_sym
    end

    def convert_hash_keys(value)
      case value
      when Array
        value.map { |v| convert_hash_keys(v) }
      when Hash
        Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
      else
        value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eveapi-0.1.2 lib/eveapi/util.rb