class Ultron::Entities
Attributes
metadata[R]
instance methods
url[R]
instance methods
Public Class Methods
by_params(params)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_path()
click to toggle source
# File lib/ultron/models/entities.rb, line 73 def self.name_for_path self.name.split('::')[-1].downcase end
new(data, url)
click to toggle source
# File lib/ultron/models/entities.rb, line 81 def initialize data, url @results_set = data['results'] @metadata = OpenStruct.new data @url = url end
response(url)
click to toggle source
# 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
Public Instance Methods
==(other)
click to toggle source
# File lib/ultron/models/entities.rb, line 108 def == other other.url == @url end
[](key)
click to toggle source
# File lib/ultron/models/entities.rb, line 87 def [] key OpenStruct.new @results_set[key] end
each() { |open_struct item| ... }
click to toggle source
# File lib/ultron/models/entities.rb, line 91 def each @results_set.each do |item| yield OpenStruct.new item end end
random_offset()
click to toggle source
# File lib/ultron/models/entities.rb, line 97 def random_offset Random.rand @metadata.total end
sample()
click to toggle source
# 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