class Mangadex::Api::Response
Attributes
data[RW]
errors[RW]
limit[RW]
offset[RW]
raw_data[RW]
response[RW]
result[RW]
total[RW]
Public Class Methods
attributes_to_inspect()
click to toggle source
# File lib/mangadex/api/response.rb, line 12 def self.attributes_to_inspect %i(result errors limit offset total data) end
coerce(data)
click to toggle source
# File lib/mangadex/api/response.rb, line 32 def self.coerce(data) if data['errors'] coerce_errors(data) elsif data['response'] == 'entity' coerce_entity(data) elsif data['response'] == 'collection' coerce_collection(data) else data end end
Private Class Methods
coerce_collection(data)
click to toggle source
# File lib/mangadex/api/response.rb, line 123 def self.coerce_collection(data) new( result: data['result'], response: data['response'], limit: data['limit'], offset: data['offset'], total: data['total'], data: ( Collection.new( data['data'].map do |entity_data| object_type = entity_data['type'] class_from_data = "Mangadex::#{object_type.split('_').collect(&:capitalize).join}" return unless Object.const_defined?(class_from_data) klass = Object.const_get(class_from_data) klass.from_data(entity_data) end ) ), raw_data: data, ) end
coerce_entity(data)
click to toggle source
# File lib/mangadex/api/response.rb, line 107 def self.coerce_entity(data) object_type = data['type'] || data.dig('data', 'type') # Derive the class name from the type. "Convention over configuration" class_from_data = "Mangadex::#{object_type.split('_').collect(&:capitalize).join}" return unless Object.const_defined?(class_from_data) klass = Object.const_get(class_from_data) new( result: data['result'], response: data['response'], data: klass.from_data(data['data'] || data), raw_data: data, ) end
coerce_errors(data)
click to toggle source
# File lib/mangadex/api/response.rb, line 89 def self.coerce_errors(data) new( result: data['result'], response: data['response'], errors: ( data['errors'].map do |error_data| Error.new( id: error_data['id'], status: error_data['status'], title: error_data['title'], detail: error_data['detail'], ) end ), raw_data: data, ) end
Public Instance Methods
as_json(*)
click to toggle source
# File lib/mangadex/api/response.rb, line 83 def as_json(*) Hash(raw_data) end
count()
click to toggle source
# File lib/mangadex/api/response.rb, line 57 def count data.is_a?(Array) ? data.count : nil end
each(&block)
click to toggle source
# File lib/mangadex/api/response.rb, line 63 def each(&block) if data.is_a?(Array) data.each(&block) else raise ArgumentError, "Expect data to be Array, but got #{data.class}" end end
errored?(status=nil)
click to toggle source
# File lib/mangadex/api/response.rb, line 44 def errored?(status=nil) errored = Array(errors).any? return errored if status.nil? errors.select { |error| error.status.to_s == status.to_s }.any? end
first()
click to toggle source
# File lib/mangadex/api/response.rb, line 75 def first to_a.first end
last()
click to toggle source
# File lib/mangadex/api/response.rb, line 79 def last to_a.last end
more_results?()
click to toggle source
# File lib/mangadex/api/response.rb, line 51 def more_results? return unless data.is_a?(Array) total > data.count end
to_a()
click to toggle source
# File lib/mangadex/api/response.rb, line 71 def to_a each.to_a end