Class: UPS::Connection Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ups/connection.rb

Overview

This class is abstract.

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

Author:

  • Paul Trippett

Since:

  • 0.1.0

Constant Summary collapse

TEST_URL =

Since:

  • 0.1.0

'https://wwwcie.ups.com'
LIVE_URL =

Since:

  • 0.1.0

'https://onlinetools.ups.com'
RATE_PATH =

Since:

  • 0.1.0

'/ups.app/xml/Rate'
SHIP_CONFIRM_PATH =

Since:

  • 0.1.0

'/ups.app/xml/ShipConfirm'
SHIP_ACCEPT_PATH =

Since:

  • 0.1.0

'/ups.app/xml/ShipAccept'
ADDRESS_PATH =

Since:

  • 0.1.0

'/ups.app/xml/XAV'
DEFAULT_PARAMS =

Since:

  • 0.1.0

{
  test_mode: false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Initializes a new UPS::Connection object

Options Hash (params):

  • :test_mode (Boolean)

    If TEST_URL should be used for requests to the UPS URL

Since:

  • 0.1.0


34
35
36
37
# File 'lib/ups/connection.rb', line 34

def initialize(params = {})
  params = DEFAULT_PARAMS.merge(params)
  self.url = (params[:test_mode]) ? TEST_URL : LIVE_URL
end

Instance Attribute Details

#urlString

The base url to use either TEST_URL or LIVE_URL

Since:

  • 0.1.0


14
15
16
# File 'lib/ups/connection.rb', line 14

def url
  @url
end

Instance Method Details

#rates(rate_builder = nil) {|rate_builder| ... } ⇒ Object

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.

Yields:

  • (rate_builder)

    A RateBuilder object for configuring the shipment information sent

Since:

  • 0.1.0


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ups/connection.rb', line 49

def rates(rate_builder = nil)
  if rate_builder.nil? && block_given?
    rate_builder = UPS::Builders::RateBuilder.new
    yield rate_builder
  end

  response = get_response_stream RATE_PATH, rate_builder.to_xml
  UPS::Parsers::RatesParser.new.tap do |parser|
    Ox.sax_parse(parser, response)
  end
end

#ship(confirm_builder = nil) {|ship_confirm_builder| ... } ⇒ Object

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.

Yields:

  • (ship_confirm_builder)

    A ShipConfirmBuilder object for configuring the shipment information sent

Since:

  • 0.1.0


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ups/connection.rb', line 71

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