class DPD::Resource
Base endpoint resources class
Public Class Methods
Returns a payload with service credentials
@return [Hash]
# File lib/dpd.rb, line 18 def self.credentials { userName: ENV['DPD_USER'], password: ENV['DPD_PASSWORD'] } end
Validate error response
Looks at the response code by default.
@param response [HTTP::Response] the server response @param parsed [Object] the parsed server response
@return [TrueClass] if status code is not a successful standard value
# File lib/dpd.rb, line 45 def self.error_response?(response, parsed) super || parsed.is_a?(Hash) && parsed.dig('error') end
Extracts the error message from the response
@param response [HTTP::Response] the server response @param parsed [Object] the parsed server response
@return [String]
# File lib/dpd.rb, line 55 def self.extract_error(response, parsed) parsed&.dig('error', 'message') || super end
Will try to parse the response or return an IO if it's a PDF
Will return nothing on failure.
@param response [HTTP::Response] the server response
@return [Object] upon success
# 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
Patched request handler to include the credentials
@param verb [String] the HTTP method. @param uri [URI] the HTTP URI. @param options [Hash] the params/json-payload/form to include. @return [DPD::Response]
# File lib/dpd.rb, line 31 def self.request(verb, uri, options = {}) options[:json] ||= {} options[:json].merge!(credentials) super(verb, uri, options) end