Class: TNT::Resource
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- TNT::Resource
- Extended by:
- HTTP::RestClient::DSL
- Defined in:
- lib/tnt.rb
Overview
Base endpoint resources class
Constant Summary collapse
- XML_HEADER =
'<?xml version="1.0" encoding="UTF-8"?>'.freeze
- XML_RENDER_OPTIONS =
{ unwrap: false, key_converter: :upcase, pretty_print: true }
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
Parses response (from XML).
-
.to_xml(data) ⇒ String
Renders a dictionary to XML.
Class Method Details
.credentials ⇒ Hash
Returns a payload with service credentials
22 23 24 25 26 27 28 29 |
# File 'lib/tnt.rb', line 22 def self.credentials { company: ENV['TNT_USERNAME'], password: ENV['TNT_PASSWORD'], appid: :EC, appversion: 3.0 } end |
.error_response?(response, parsed) ⇒ TrueClass
Validate error response
Looks at the response code by default.
47 48 49 50 51 52 53 |
# File 'lib/tnt.rb', line 47 def self.error_response?(response, parsed) parsed.is_a?(Hash) && ( parsed.dig('parse_error') || parsed.dig('runtime_error') || parsed.dig('document', 'error') ) || super end |
.extract_error(response, parsed) ⇒ String
Extracts the error message from the response
61 62 63 64 65 66 |
# File 'lib/tnt.rb', line 61 def self.extract_error(response, parsed) parsed&.dig('runtime_error') || parsed&.dig('parse_error') || parsed&.dig('document', 'error')&.first || super end |
.parse_response(response) ⇒ Object
Parses response (from XML)
72 73 74 75 76 |
# File 'lib/tnt.rb', line 72 def self.parse_response(response) Hash.from_xml(response.body.to_s).deep_transform_keys(&:downcase) rescue StandardError [response.body.to_s.split(':')].to_h.deep_transform_keys(&:downcase) end |
.to_xml(data) ⇒ String
Renders a dictionary to XML
35 36 37 |
# File 'lib/tnt.rb', line 35 def self.to_xml(data) XML_HEADER + Gyoku.xml({ eshipper: data }, XML_RENDER_OPTIONS) end |