Class: Mangadex::Api::Response

Inherits:
MangadexObject show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/mangadex/api/response.rb

Defined Under Namespace

Classes: Collection, Error

Instance Attribute Summary collapse

Attributes included from Internal::WithAttributes

#attributes, #id, #related_type, #relationships, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MangadexObject

#eq?, #hash, #initialize, #inspect

Methods included from Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

This class inherits a constructor from Mangadex::MangadexObject

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/mangadex/api/response.rb', line 8

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



8
9
10
# File 'lib/mangadex/api/response.rb', line 8

def errors
  @errors
end

#limitObject

Returns the value of attribute limit.



9
10
11
# File 'lib/mangadex/api/response.rb', line 9

def limit
  @limit
end

#offsetObject

Returns the value of attribute offset.



9
10
11
# File 'lib/mangadex/api/response.rb', line 9

def offset
  @offset
end

#raw_dataObject

Returns the value of attribute raw_data.



10
11
12
# File 'lib/mangadex/api/response.rb', line 10

def raw_data
  @raw_data
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/mangadex/api/response.rb', line 8

def response
  @response
end

#resultObject

Returns the value of attribute result.



8
9
10
# File 'lib/mangadex/api/response.rb', line 8

def result
  @result
end

#totalObject

Returns the value of attribute total.



9
10
11
# File 'lib/mangadex/api/response.rb', line 9

def total
  @total
end

Class Method Details

.attributes_to_inspectObject



12
13
14
# File 'lib/mangadex/api/response.rb', line 12

def self.attributes_to_inspect
  %i(result errors limit offset total data)
end

.coerce(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# 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)
  elsif data.keys.include?('statistics')
    coerce_statistics(data)
  else
    data
  end
end

Instance Method Details

#as_jsonObject



85
86
87
# File 'lib/mangadex/api/response.rb', line 85

def as_json(*)
  Hash(raw_data)
end

#countObject Also known as: size, length



59
60
61
# File 'lib/mangadex/api/response.rb', line 59

def count
  data.is_a?(Array) ? data.count : nil
end

#each(&block) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/mangadex/api/response.rb', line 65

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) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/mangadex/api/response.rb', line 46

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

#firstObject



77
78
79
# File 'lib/mangadex/api/response.rb', line 77

def first
  to_a.first
end

#lastObject



81
82
83
# File 'lib/mangadex/api/response.rb', line 81

def last
  to_a.last
end

#more_results?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/mangadex/api/response.rb', line 53

def more_results?
  return unless data.is_a?(Array)

  total > data.count
end

#to_aObject



73
74
75
# File 'lib/mangadex/api/response.rb', line 73

def to_a
  each.to_a
end