class EndiciaLabelServer::Connection

The {Connection} class acts as the main entry point to performing rate and ship operations against the Endicia API.

@author Paul Trippett @abstract @since 0.1.0 @attr [String] url The base url to use either TEST_URL or LIVE_URL

Constants

CHANGE_PASS_PHRASE_ENDPOINT
DEFAULT_PARAMS
GET_POSTAGE_LABEL_ENDPOINT
GET_USER_SIGNUP_ENDPOINT
HEADERS
LIVE_URL
REQUEST_RATES_ENDPOINT
REQUEST_RATE_ENDPOINT
ROOT_PATH
TEST_URL

Attributes

url[RW]

Public Class Methods

new(params = {}) click to toggle source

Initializes a new {Connection} object

@param [Hash] params The initialization options @option params [Boolean] :test_mode If TEST_URL should be used for

requests to the Endicia Label Server URL
# File lib/endicia_label_server/connection.rb, line 43
def initialize(params = {})
  params = DEFAULT_PARAMS.merge(params)
  self.url = (params[:test_mode]) ? TEST_URL : LIVE_URL
end

Public Instance Methods

change_pass_phrase(builder = nil, &block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 68
def change_pass_phrase(builder = nil, &block)
  builder_proxy(builder, CHANGE_PASS_PHRASE_ENDPOINT, ChangePassPhraseBuilder,
                ChangePassPhraseParser, block)
end
get_label(builder = nil, &block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 58
def get_label(builder = nil, &block)
  builder_proxy(builder, GET_POSTAGE_LABEL_ENDPOINT, PostageLabelBuilder,
                PostageLabelParser, block)
end
rate(builder = nil, &block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 48
def rate(builder = nil, &block)
  builder_proxy(builder, REQUEST_RATE_ENDPOINT, PostageRateBuilder,
                PostageRateParser, block)
end
rates(builder = nil, &block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 53
def rates(builder = nil, &block)
  builder_proxy(builder, REQUEST_RATES_ENDPOINT, PostageRatesBuilder,
                PostageRatesParser, block)
end
sign_up(builder = nil, &block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 63
def sign_up(builder = nil, &block)
  builder_proxy(builder, GET_USER_SIGNUP_ENDPOINT, UserSignUpBuilder,
                UserSignUpParser, block)
end

Private Instance Methods

build_url(endpoint) click to toggle source
# File lib/endicia_label_server/connection.rb, line 75
def build_url(endpoint)
  "#{url}#{ROOT_PATH}#{endpoint}"
end
builder_proxy(builder, path, builder_type, parser, block) click to toggle source
# File lib/endicia_label_server/connection.rb, line 84
def builder_proxy(builder, path, builder_type, parser, block)
  if builder.nil? && block
    builder = builder_type.new
    block.call builder
  end

  response = get_response_stream path, builder.to_http_post
  parser.new.tap do |p|
    Ox.sax_parse(p, response)
  end
end
get_response_stream(path, body) click to toggle source
# File lib/endicia_label_server/connection.rb, line 79
def get_response_stream(path, body)
  response = Excon.post(build_url(path), body: body, headers: HEADERS)
  StringIO.new(response.body)
end