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'
TRACK_PATH =

Since:

  • 0.1.0

'/ups.app/xml/Track'
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

Parameters:

  • params (Hash) (defaults to: {})

    The initialization options

Options Hash (params):

  • :test_mode (Boolean)

    If TEST_URL should be used for requests to the UPS URL

Since:

  • 0.1.0


35
36
37
38
# 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

Instance Attribute Details

#urlString

The base url to use either TEST_URL or LIVE_URL

Returns:

  • (String)

    the current value of 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.

Parameters:

Yields:

  • (rate_builder)

    A RateBuilder object for configuring the shipment information sent

Since:

  • 0.1.0


50
51
52
53
54
55
56
57
58
# 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) {|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.

Parameters:

Yields:

  • (ship_confirm_builder)

    A ShipConfirmBuilder object for configuring the shipment information sent

Since:

  • 0.1.0


70
71
72
73
74
75
76
77
78
79
80
81
# 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| ... } ⇒ Object

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.

Parameters:

Yields:

  • (track_builder)

    A TrackBuilder object for configuring the shipment information sent

Since:

  • 0.1.0


93
94
95
96
97
98
99
100
101
102
# 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