lib/mangadex/api/response.rb in mangadex-5.8.0 vs lib/mangadex/api/response.rb in mangadex-5.9.0
- old
+ new
@@ -34,10 +34,12 @@
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
@@ -107,11 +109,14 @@
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)
+ unless Object.const_defined?(class_from_data)
+ warn("Expected class #{class_from_data} to be defined")
+ return
+ end
klass = Object.const_get(class_from_data)
new(
result: data['result'],
response: data['response'],
@@ -138,9 +143,16 @@
klass.from_data(entity_data)
end
)
),
raw_data: data,
+ )
+ end
+
+ def self.coerce_statistics(data)
+ new(
+ result: data['result'],
+ data: Mangadex::Statistic.from_data(data['statistics']),
)
end
end
end
end