class DPD::Resource

Base endpoint resources class

Public Class Methods

credentials() click to toggle source

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
error_response?(response, parsed) click to toggle source

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

Calls superclass method
# 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) click to toggle source

Extracts the error message from the response

@param response [HTTP::Response] the server response @param parsed [Object] the parsed server response

@return [String]

Calls superclass method
# File lib/dpd.rb, line 55
def self.extract_error(response, parsed)
  parsed&.dig('error', 'message') || super
end
parse_response(response) click to toggle source

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

Calls superclass method
# 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 = {}) click to toggle source

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]

Calls superclass method
# File lib/dpd.rb, line 31
def self.request(verb, uri, options = {})
  options[:json] ||= {}
  options[:json].merge!(credentials)
  super(verb, uri, options)
end