Class: Uatu::Resource

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/uatu/resource.rb

Direct Known Subclasses

Character, Comic, Creator, Event, Serie, Story

Instance Method Summary collapse

Constructor Details

#initialize(original_hash) ⇒ Resource

Returns a new instance of Resource.



4
5
6
# File 'lib/uatu/resource.rb', line 4

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

Instance Method Details

#improve_values(hash) ⇒ Object

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/uatu/resource.rb', line 25

def improve_values(hash)
  _hash = {}
  hash.each do |k,v|
    _hash[k] = if k.to_s=='thumbnail'
      [v['path'],v['extension']].join('.')
    elsif v.is_a?(Hash)
      Hashie::Mash.new(v)
    else
      v
    end
  end
  _hash
end

#underscore_keys(hash) ⇒ Object

Underscore names of the Hash. I mean… this is Ruby, right?.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/uatu/resource.rb', line 9

def underscore_keys(hash)
  _hash = {}
  hash.each do |k, v|
    _hash[k.to_s.underscore] = if v.is_a?(Hash)
      underscore_keys(v)
    elsif v.is_a?(Array) and v.first.is_a?(Hash)
      v.map{|h| underscore_keys(h)}
    else
      v
    end
  end
  _hash
end