Class: Ultron::Entities

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ultron/entities.rb

Direct Known Subclasses

Characters, Comics, Creators, Events, Series, Stories

Constant Summary collapse

PLURALS =
{
    'character' => 'characters',
    'comic'     => 'comics',
    'creator'   => 'creators',
    'event'     => 'events',
    'series'    => 'series',
    'story'     => 'stories'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results_set) ⇒ Entities

Returns a new instance of Entities.



55
56
57
# File 'lib/ultron/entities.rb', line 55

def initialize results_set
  @results_set = results_set
end

Class Method Details

.by_something(something, id) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/ultron/entities.rb', line 40

def self.by_something something, id
  args = [
      PLURALS[something],
      id,
      self.name_for_path
  ].join '/'
  self.new self.perform(args)['data']['results']
end

.connectionObject



22
23
24
# File 'lib/ultron/entities.rb', line 22

def self.connection
  Ultron::Connection.new
end

.find(id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ultron/entities.rb', line 26

def self.find id
  args = [
      self.name_for_path,
      id
  ]
  OpenStruct.new self.perform(args)['data']['results'][0]
end

.method_missing(method_name, *args) ⇒ Object



34
35
36
37
38
# File 'lib/ultron/entities.rb', line 34

def self.method_missing method_name, *args
  if method_name.to_s =~ /by_(.*)/
    self.send(:by_something, $1, args)
  end
end

.name_for_pathObject



14
15
16
17
18
19
20
# File 'lib/ultron/entities.rb', line 14

def self.name_for_path
  name = self.name
  name_parts = name.split('::')
  basename = name_parts[-1]
  downcased = basename.downcase
  downcased
end

.perform(*args) ⇒ Object



49
50
51
52
53
# File 'lib/ultron/entities.rb', line 49

def self.perform *args
  c = self.connection
  c.path = args.join '/'
  c.perform
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/ultron/entities.rb', line 59

def [] key
  OpenStruct.new @results_set[key]
end

#eachObject



63
64
65
66
67
# File 'lib/ultron/entities.rb', line 63

def each
  @results_set.each do |item|
    yield OpenStruct.new item
  end
end