class Endicia::Request

Constants

ENDICIA_API_HOSTS

Public Class Methods

new(options = {}) click to toggle source

Pass a hash of options with: {

credentials: {
  AccountID: '2500334',
  RequesterID: 'lxxx',
  PassPhrase: 'endicia.com',
},
environment: 'test' # or 'production' or 'sandbox'

}

Or if using Rails, create an endicia.yml in {Rails.root}/config/ in this format: development: &development

credentials:
  AccountID: youraccountid
  RequesterID: lxxx
  PassPhrase: endicia.com
environment: sandbox

test:

<<: *development

staging:

<<: *development

production:

credentials:
  AccountID: youraccountid
  RequesterID: lsfx
  PassPhrase: yourpassword
environment: production

If both an endicia.yml file is present and a hash of options is passed, any values in the hash take precedence

# File lib/endicia_ruby/request.rb, line 61
def initialize(options = {})
  @options = load_options_from_config_file.deep_merge(options)
end

Protected Instance Methods

api_host() click to toggle source

Most requests use this url

# File lib/endicia_ruby/request.rb, line 76
def api_host
  if ENDICIA_API_HOSTS.key?(environment)
    ENDICIA_API_HOSTS[environment]
  else
    raise "Invalid environment value: '#{environment}'"
  end
end
api_test_mode_value() click to toggle source

The Test attribute for the root node of the XML request Looks like:

<LabelRequest Test="YES" ...>

or:

<LabelRequest Test="NO" ...>
# File lib/endicia_ruby/request.rb, line 134
def api_test_mode_value
  @options[:environment] == 'production' ? 'NO' : 'YES'
end
api_url_base() click to toggle source
# File lib/endicia_ruby/request.rb, line 84
def api_url_base
  @api_url_base ||= "#{api_host}/LabelService/EwsLabelService.asmx"
end
default_options() click to toggle source
# File lib/endicia_ruby/request.rb, line 97
def default_options
  {
    log_requests:   false,
    log_responses:  false,
  }
end
els_service_url(params = {}) click to toggle source

Some requests use the ELS service url. This URL is used for requests that can accept GET, and have params passed via URL instead of a POST body. Pass a hash of params to have them converted to a &key=value string and appended to the URL.

# File lib/endicia_ruby/request.rb, line 92
def els_service_url(params = {})
  params = params.to_a.map { |i| "#{i[0]}=#{i[1]}"}.join('&')
  "http://www.endicia.com/ELS/ELSServices.cfc?wsdl&#{params}"
end
environment() click to toggle source

Separated so you can easily stub in tests with something like the following (in rspec):

Endicia::Request.any_instance.stub(:environment).and_return(:sandbox)
Endicia::Label.request_label(values)

Note: since api_url_base is memoized you want to do this before you make the first API call on that object

# File lib/endicia_ruby/request.rb, line 71
def environment
  @options[:environment].try(:to_sym)
end
format_xml_for_logging(xml_string) click to toggle source

Make a one line string with image data stripped for use in logging

# File lib/endicia_ruby/request.rb, line 158
def format_xml_for_logging(xml_string)
  xml_string.to_s.gsub("\r\n", '').gsub("\n", '').gsub("\t", '').gsub("\s{2,}", ' ')
    .sub(/<([[:alnum:]]*Image)>.+?<\/[[:alnum:]]*Image>/, '<\1>[data]</\1>')
end
load_options_from_config_file() click to toggle source

If using Rails, load default config options from {Rails.root}/config/endicia.yml

# File lib/endicia_ruby/request.rb, line 139
def load_options_from_config_file
  configuration_values = {}
  if defined?(Rails)
    config_file = File.join(Rails.root, 'config', 'endicia.yml')
    if File.exist?(config_file)
      configuration_values = YAML.load(ERB.new(File.read(config_file)).result)[Rails.env]
      recursive_symbolize_keys!(configuration_values)
    end
  end
  configuration_values
end
log(message, level: :info) click to toggle source

Helper for logging messages via Rails.logger if available or just puts if not using Rails

# File lib/endicia_ruby/request.rb, line 164
def log(message, level: :info)
  if defined?(Rails)
    Rails.logger.send(level, message)
  else
    puts message
  end
end
recursive_build_xml_nodes!(xml, nodes) click to toggle source

Build nodes for the given xml builder recursively from a Hash

# File lib/endicia_ruby/request.rb, line 105
def recursive_build_xml_nodes!(xml, nodes)
  nodes.each do |key, value|
    node_name = key.to_s.sub(/^./,&:upcase) # convert "fooBar" to "FooBar"
    case value
      when Hash
        if node_name == 'ResponseOptions'
          xml.ResponseOptions(value)
        else
          xml.send(node_name) do
            recursive_build_xml_nodes!(xml, value)
          end
        end
      when Array
        xml.send(node_name) do
          value.each do |v|
            recursive_build_xml_nodes!(xml, v)
          end
        end
      else
        xml.send(node_name, value)
    end
  end
end
recursive_symbolize_keys!(hash) click to toggle source

Used by load_options_from_config_file

# File lib/endicia_ruby/request.rb, line 152
def recursive_symbolize_keys! hash
  hash.symbolize_keys!
  hash.values.select{|v| v.is_a? Hash}.each{|h| recursive_symbolize_keys!(h)}
end