class UPS::Connection

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

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

Constants

ADDRESS_PATH
DEFAULT_PARAMS
LIVE_URL
RATE_PATH
SHIP_ACCEPT_PATH
SHIP_CONFIRM_PATH
TEST_URL
TRACK_PATH

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 UPS URL
# File lib/ups/connection.rb, line 35
def initialize(params = {})
  params = DEFAULT_PARAMS.merge(params)
  self.url = (params[:test_mode]) ? TEST_URL : LIVE_URL
end

Public Instance Methods

rates(rate_builder = nil) { |rate_builder| ... } click to toggle source

Makes a request to fetch Rates for a shipment.

A pre-configured {Builders::RateBuilder} object can be passed as the first option or a block yielded to configure a new {Builders::RateBuilder} object.

@param [Builders::RateBuilder] rate_builder A pre-configured

{Builders::RateBuilder} object to use

@yield [rate_builder] A RateBuilder object for configuring

the shipment information sent
# File lib/ups/connection.rb, line 50
def rates(rate_builder = nil)
  if rate_builder.nil? && block_given?
    rate_builder = UPS::Builders::RateBuilder.new
    yield rate_builder
  end

  response = get_response(RATE_PATH, rate_builder.to_xml)
  UPS::Parsers::RatesParser.new(response.body)
end
ship(confirm_builder = nil) { |confirm_builder| ... } click to toggle source

Makes a request to ship a package

A pre-configured {Builders::ShipConfirmBuilder} object can be passed as the first option or a block yielded to configure a new {Builders::ShipConfirmBuilder} object.

@param [Builders::ShipConfirmBuilder] confirm_builder A pre-configured

{Builders::ShipConfirmBuilder} object to use

@yield [ship_confirm_builder] A ShipConfirmBuilder object for configuring

the shipment information sent
# File lib/ups/connection.rb, line 70
def ship(confirm_builder = nil)
  if confirm_builder.nil? && block_given?
    confirm_builder = Builders::ShipConfirmBuilder.new
    yield confirm_builder
  end

  confirm_response = make_confirm_request(confirm_builder)
  return confirm_response unless confirm_response.success?

  accept_builder = build_accept_request_from_confirm(confirm_builder, confirm_response)
  make_accept_request(accept_builder)
end
track(track_builder = nil) { |track_builder| ... } click to toggle source

Makes a request to Track the status for a shipment.

A pre-configured {Builders::TrackBuilder} object can be passed as the first option or a block yielded to configure a new {Builders::TrackBuilder} object.

@param [Builders::TrackBuilder] track_builder A pre-configured

{Builders::TrackBuilder} object to use

@yield [track_builder] A TrackBuilder object for configuring

the shipment information sent
# File lib/ups/connection.rb, line 93
def track(track_builder = nil)
  if track_builder.nil? && block_given?
    track_builder = UPS::Builders::TrackBuilder.new
    yield track_builder
  end

  response = get_response(TRACK_PATH, track_builder.to_xml)

  UPS::Parsers::TrackParser.new(response.body)
end

Private Instance Methods

build_accept_request_from_confirm(confirm_builder, confirm_response) click to toggle source
# File lib/ups/connection.rb, line 127
def build_accept_request_from_confirm(confirm_builder, confirm_response)
  UPS::Builders::ShipAcceptBuilder.new.tap do |builder|
    builder.add_access_request confirm_builder.license_number,
                               confirm_builder.user_id,
                               confirm_builder.password
    builder.add_shipment_digest confirm_response.shipment_digest
  end
end
build_url(path) click to toggle source
# File lib/ups/connection.rb, line 106
def build_url(path)
  "#{url}#{path}"
end
get_response(path, body) click to toggle source
# File lib/ups/connection.rb, line 110
def get_response(path, body)
  Excon.post(build_url(path), body: body)
end
make_accept_request(accept_builder) click to toggle source
# File lib/ups/connection.rb, line 118
def make_accept_request(accept_builder)
  make_ship_request(accept_builder, SHIP_ACCEPT_PATH, Parsers::ShipAcceptParser)
end
make_confirm_request(confirm_builder) click to toggle source
# File lib/ups/connection.rb, line 114
def make_confirm_request(confirm_builder)
  make_ship_request(confirm_builder, SHIP_CONFIRM_PATH, Parsers::ShipConfirmParser)
end
make_ship_request(builder, path, ship_parser) click to toggle source
# File lib/ups/connection.rb, line 122
def make_ship_request(builder, path, ship_parser)
  response = get_response(path, builder.to_xml)
  ship_parser.new(response.body)
end