Class: Ultron::Entities

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

Direct Known Subclasses

Characters, Comics, Creators, Events, Series, Stories

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, url) ⇒ Entities

Returns a new instance of Entities.



81
82
83
84
85
# File 'lib/ultron/models/entities.rb', line 81

def initialize data, url
  @results_set = data['results']
  @metadata    = OpenStruct.new data
  @url         = url
end

Instance Attribute Details

#metadataObject (readonly)

instance methods



79
80
81
# File 'lib/ultron/models/entities.rb', line 79

def 
  @metadata
end

#urlObject (readonly)

instance methods



79
80
81
# File 'lib/ultron/models/entities.rb', line 79

def url
  @url
end

Class Method Details

.by_params(params) ⇒ Object



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

def self.by_params params
  params.map { |k, v| '%s=%s&' % [k, v] }.join
end

.by_something(something, id) ⇒ Object



61
62
63
# File 'lib/ultron/models/entities.rb', line 61

def self.by_something something, id
  [something.pluralize, id, self.name_for_path].join '/'
end

.get_url(path, query = nil) ⇒ Object



69
70
71
# File 'lib/ultron/models/entities.rb', line 69

def self.get_url path, query = nil
  Ultron::URL.new path, query
end

.method_missing(method_name, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ultron/models/entities.rb', line 5

def self.method_missing method_name, *args
  mname = method_name.to_s
  query = nil
  path  = self.name_for_path

  mname.split(/_and_/).each do |part|
    case part
      when 'get'
        true # just so this method actually exists

      when 'sample'
        true

      when 'find'
        path = '%s/%s' % [path, args.shift]

      when /by_(.*)/
        path = self.send(:by_something, $1, args.shift)

      when 'with', 'where'
        query = self.send(:by_params, args.shift)

      when 'vanilla_comics'
        query = self.send(:by_params, format: 'comic', formatType: 'comic', noVariants: true)

      else
        raise NoMethodError

    end
  end

  url      = get_url path, query
  response = self.response url

  set = self.new response['data'], url

  return set.sample if mname == 'sample'
  mname == 'find' ? set.first : set
end

.name_for_pathObject



73
74
75
# File 'lib/ultron/models/entities.rb', line 73

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

.response(url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ultron/models/entities.rb', line 45

def self.response url
  response = Ultron::Connection.perform url
  case response['code'].to_s
    when /^4/, /^5/
      raise MarvelException.new response
    when 'ResourceNotFound'
      raise UltronException.new 'Resource does not exist. Check %s' % Config.instance.config.api_docs
    when 'RequestThrottled'
      raise UltronException.new 'You have exceeded your rate limit. Please try again later'
  end

  raise UltronException.new 'The search returned no results' unless response['data']['results'].any?

  response
end

Instance Method Details

#==(other) ⇒ Object



108
109
110
# File 'lib/ultron/models/entities.rb', line 108

def == other
  other.url == @url
end

#[](key) ⇒ Object



87
88
89
# File 'lib/ultron/models/entities.rb', line 87

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

#eachObject



91
92
93
94
95
# File 'lib/ultron/models/entities.rb', line 91

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

#random_offsetObject



97
98
99
# File 'lib/ultron/models/entities.rb', line 97

def random_offset
  Random.rand @metadata.total
end

#sampleObject



101
102
103
104
105
106
# File 'lib/ultron/models/entities.rb', line 101

def sample
  sample_params = 'offset=%d&limit=1&' % random_offset
  full_url = self.class.get_url @url.path, '%s%s' % [@url.query, sample_params]
  response = Ultron::Connection.perform full_url
  self.class.new(response['data'], full_url).first
end