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(resource_hash) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(resource_hash)
  super(improve_hash(resource_hash))
end

Instance Method Details

#add_shortcuts(args) ⇒ Object

That “thumbnail” hash in the original response drives me crazy



44
45
46
47
48
49
50
51
52
53
# File 'lib/uatu/resource.rb', line 44

def add_shortcuts(args)
  _args = {}
  args.each do |k, v|
    _args[k] = case k.to_s
      when 'thumbnail' then [v['path'],v['extension']].join('.')
      else v
    end
  end
  _args
end

#improve_hash(original_hash) ⇒ Object



8
9
10
11
12
13
# File 'lib/uatu/resource.rb', line 8

def improve_hash(original_hash)
  output_hash = underscore_keys(original_hash)
  output_hash = add_shortcuts(output_hash)
  output_hash = mashify(output_hash)
  output_hash
end

#mashify(hash) ⇒ Object

We change the hasehs to mashes (Hashie::Mash) so it’s easier to manipulate



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/uatu/resource.rb', line 16

def mashify(hash)
  _hash = {}
  hash.each do |k,v|
    _hash[k] = if 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?.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/uatu/resource.rb', line 29

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