Class: Zanders::Address

Inherits:
SoapClient show all
Defined in:
lib/zanders/address.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Address

Public: Initializes a new Address with username and password

username - A String username password - A String password

Returns a new Address service interface



14
15
16
17
18
19
# File 'lib/zanders/address.rb', line 14

def initialize(options = {})
  requires!(options, :username, :password)

  @username = options[:username]
  @password = options[:password]
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/zanders/address.rb', line 5

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



4
5
6
# File 'lib/zanders/address.rb', line 4

def username
  @username
end

Class Method Details

.ship_to_number(address, options = {}) ⇒ Object



21
22
23
# File 'lib/zanders/address.rb', line 21

def self.ship_to_number(address, options = {})
  new(options).ship_to_number(address)
end

Instance Method Details

#ship_to_number(address) ⇒ Object

Public: Get a shipToNumber for the address provided in ‘address`

address - Hash of the address

Returns Hash containing the ship to number, or an error code if the call failed



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zanders/address.rb', line 32

def ship_to_number(address)
  request_data = build_request_data(address)

  response = soap_client(ADDRESS_API_URL).call(:use_ship_to, message: request_data)
  response = response.body[:use_ship_to_response][:return][:item]

  # Successful call return_code is 0
  if response.first[:value] == "0"
    ship_to_number = Hash.new

    # Let's dig to get to data we actually need. Yay, digging...
    parts = ship_to_number[:ship_to_number] = response.last[:value].first[1]

    # We only need the ship_to_number out of the data received
    ship_to_number[:ship_to_number] = parts.find { |i| i[:key] == "ShipToNo" }[:value]
    ship_to_number[:success] = true

    ship_to_number
  else
    { success: false, error_code: response.first[:value], error_message: response.last[:value] }
  end
end