Class: DPD::Resource
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- DPD::Resource
- Extended by:
- HTTP::RestClient::CRUD, HTTP::RestClient::DSL
- Defined in:
- lib/dpd.rb
Overview
Base endpoint resources class
Class Method Summary collapse
-
.credentials ⇒ Hash
Returns a payload with service credentials.
-
.error_response?(response, parsed) ⇒ TrueClass
Validate error response.
-
.extract_error(response, parsed) ⇒ String
Extracts the error message from the response.
-
.parse_response(response) ⇒ Object
Will try to parse the response or return an IO if it’s a PDF.
-
.request(verb, uri, options = {}) ⇒ DPD::Response
Patched request handler to include the credentials.
Class Method Details
.credentials ⇒ Hash
Returns a payload with service credentials
18 19 20 21 22 23 |
# File 'lib/dpd.rb', line 18 def self.credentials { userName: ENV['DPD_USER'], password: ENV['DPD_PASSWORD'] } end |
.error_response?(response, parsed) ⇒ TrueClass
Validate error response
Looks at the response code by default.
45 46 47 |
# File 'lib/dpd.rb', line 45 def self.error_response?(response, parsed) super || parsed.is_a?(Hash) && parsed.dig('error') end |
.extract_error(response, parsed) ⇒ String
Extracts the error message from the response
55 56 57 |
# File 'lib/dpd.rb', line 55 def self.extract_error(response, parsed) parsed&.dig('error', 'message') || super end |
.parse_response(response) ⇒ Object
Will try to parse the response or return an IO if it’s a PDF
Will return nothing on failure.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dpd.rb', line 66 def self.parse_response(response) if response.mime_type == 'application/pdf' io = Tempfile.new([name.underscore, '.pdf']) io.write(response.body) io.seek(0) { data: io } else super end end |
.request(verb, uri, options = {}) ⇒ DPD::Response
Patched request handler to include the credentials
31 32 33 34 35 |
# File 'lib/dpd.rb', line 31 def self.request(verb, uri, = {}) [:json] ||= {} [:json].merge!(credentials) super(verb, uri, ) end |