lib/pdfmonkey/adapter.rb in pdfmonkey-0.1.0 vs lib/pdfmonkey/adapter.rb in pdfmonkey-0.2.0
- old
+ new
@@ -8,11 +8,18 @@
@config = config
end
def call(method, resource)
response = send_request(method, resource)
- extract_attributes(response, resource)
+
+ if response.is_a?(Net::HTTPSuccess)
+ extract_attributes(response, resource)
+ else
+ extract_errors(response)
+ end
+ rescue StandardError => e
+ { errors: [e.message], status: 'error' }
end
private def build_get_request(uri, _resource)
Net::HTTP::Get.new(uri, headers)
end
@@ -24,9 +31,14 @@
end
private def extract_attributes(response, resource)
member = resource.class.const_get('MEMBER')
JSON.parse(response.body).fetch(member)
+ end
+
+ private def extract_errors(response)
+ payload = JSON.parse(response.body)
+ { errors: payload['errors'], status: 'error' }
end
private def headers
{
'Authorization' => "Bearer #{config.private_key}",