module DHL::Ecommerce

Constants

VERSION

Attributes

access_token[W]
client_id[RW]
label_format[RW]
password[W]
username[W]

Public Class Methods

access_token() click to toggle source
# File lib/dhl-ecommerce.rb, line 50
def self.access_token
  # TODO This needs better error handling.
  @access_token ||= client.get("https://api.dhlglobalmail.com/v1/auth/access_token", username: @username, password: @password, state: Time.now.to_i).body.response.data[:access_token]
end
configure() { |self| ... } click to toggle source
# File lib/dhl-ecommerce.rb, line 45
def configure
  yield self
end
request(method, url, &block) click to toggle source
# File lib/dhl-ecommerce.rb, line 55
def self.request(method, url, &block)
  client.params = {
    access_token: self.access_token,
    client_id: client_id
  }

  response = client.run_request method.downcase.to_sym, url, nil, nil, &block

  case response.status
  when 400
    case response.body.response.meta.error.error_type
    when "INVALID_CLIENT_ID", "INVALID_KEY", "INVALID_TOKEN", "INACTIVE_KEY"
      raise Errors::AuthenticationError.new response.body.response.meta.error.error_message, response
    when "VALIDATION_ERROR", "INVALID_FACILITY_CODE"
      errors = response.body.response.data.mpu_list.mpu.error_list.error
      errors = [errors] unless errors.is_a? Array

      raise Errors::ValidationError.new response.body.response.meta.error.error_message, response, errors
    else
      raise Errors::BaseError.new response.body.response.meta.error.error_message, response
    end
  end

  response.body.response.data
end

Private Class Methods

client() click to toggle source
# File lib/dhl-ecommerce.rb, line 82
def self.client
  @client ||= Faraday.new url: "https://api.dhlglobalmail.com/v1/", headers: { accept: "application/xml", content_type: "application/xml;charset=UTF-8" } do |c|
    c.response :rashify
    c.response :xml, :content_type => /\bxml$/
    c.adapter :net_http
  end
end