Sha256: 7526d2230d713f880c10895b6f3436aa42bce034b5471e03ef024647fac38b2a

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Uatu
  class Resource < OpenStruct

    def initialize(original_hash)
      super(improve_values(underscore_keys(original_hash)))
    end

  private

    # Underscore names of the Hash. I mean... this is Ruby, right?.
    def underscore_keys(hash)
      _hash = {}
      hash.each do |key, value|
        _hash[key.to_s.underscore] = if value.is_a?(Hash)
          underscore_keys(value)
        elsif value.is_a?(Array) and value.first.is_a?(Hash)
          value.map{|h| underscore_keys(h)}
        else
          value
        end
      end
      _hash
    end

    # We change the hashes to mashes (Hashie::Mash) so it's easier to manipulate
    # The 'thumbnail' hash drives me crazy, we convert it to a single value.
    def improve_values(hash)
      _hash = {}
      hash.each do |key, value|
        _hash[key] = if key.to_s=='thumbnail' && !value.nil?
          [value['path'],value['extension']].join('.')
        elsif value.is_a?(Hash)
          Hashie::Mash.new(value)
        else
          value
        end
      end
      _hash
    end

  end
end

class Uatu::Character < Uatu::Resource ; end
class Uatu::Event < Uatu::Resource     ; end
class Uatu::Comic < Uatu::Resource     ; end
class Uatu::Story < Uatu::Resource     ; end
class Uatu::Serie < Uatu::Resource     ; end
class Uatu::Creator < Uatu::Resource   ; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uatu-marvel-0.1.0 lib/uatu/resource.rb