Class: Braintree::AddressGateway

Inherits:
Object
  • Object
show all
Includes:
BaseModule
Defined in:
lib/braintree/address_gateway.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseModule

included

Methods included from BaseModule::Methods

#copy_instance_variables_from_object, #return_object_or_raise, #set_instance_variables_from_hash, #singleton_class

Constructor Details

#initialize(gateway) ⇒ AddressGateway

Returns a new instance of AddressGateway.



5
6
7
8
9
# File 'lib/braintree/address_gateway.rb', line 5

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
  @config.assert_has_access_token_or_keys
end

Class Method Details

._address_attributesObject



98
99
100
101
# File 'lib/braintree/address_gateway.rb', line 98

def self._address_attributes
  [:street_address, :extended_address, :locality, :region, :postal_code, :country_code_alpha2,
   {:international_phone => [:country_code, :national_number]}]
end

._create_signatureObject



83
84
85
# File 'lib/braintree/address_gateway.rb', line 83

def self._create_signature
  _shared_signature + [:customer_id]
end

._shared_signatureObject



87
88
89
90
91
92
# File 'lib/braintree/address_gateway.rb', line 87

def self._shared_signature
  [:company, :country_code_alpha2, :country_code_alpha3, :country_code_numeric,
   :country_name, :extended_address, :first_name,
   {:international_phone => [:country_code, :national_number]},
   :last_name, :locality, :phone_number, :postal_code, :region, :street_address]
end

._update_signatureObject



94
95
96
# File 'lib/braintree/address_gateway.rb', line 94

def self._update_signature
  _create_signature
end

Instance Method Details

#_determine_address_id(address_id) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/braintree/address_gateway.rb', line 75

def _determine_address_id(address_id)
  address_id = address_id.to_s
  if Util.invalid_path_segment?(address_id)
    raise ArgumentError, "address_id contains invalid characters"
  end
  address_id
end

#_determine_customer_id(customer_or_customer_id) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/braintree/address_gateway.rb', line 67

def _determine_customer_id(customer_or_customer_id)
  customer_id = customer_or_customer_id.is_a?(Customer) ? customer_or_customer_id.id : customer_or_customer_id
  if Util.invalid_path_segment?(customer_id)
    raise ArgumentError, "customer_id contains invalid characters"
  end
  customer_id
end

#create(attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/braintree/address_gateway.rb', line 11

def create(attributes)
  Util.verify_keys(AddressGateway._create_signature, attributes)
  unless attributes[:customer_id]
    raise ArgumentError, "Expected hash to contain a :customer_id"
  end
  if Util.invalid_path_segment?(attributes[:customer_id])
    raise ArgumentError, ":customer_id contains invalid characters"
  end
  response = @config.http.post("#{@config.base_merchant_path}/customers/#{attributes.delete(:customer_id)}/addresses", :address => attributes)
  if response[:address]
    SuccessfulResult.new(:address => Address._new(@gateway, response[:address]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise UnexpectedError, "expected :address or :api_error_response"
  end
end

#create!(*args) ⇒ Object



29
30
31
# File 'lib/braintree/address_gateway.rb', line 29

def create!(*args)
  return_object_or_raise(:address) { create(*args) }
end

#delete(customer_or_customer_id, address_id) ⇒ Object



33
34
35
36
37
38
# File 'lib/braintree/address_gateway.rb', line 33

def delete(customer_or_customer_id, address_id)
  customer_id = _determine_customer_id(customer_or_customer_id)
  address_id = _determine_address_id(address_id)
  @config.http.delete("#{@config.base_merchant_path}/customers/#{customer_id}/addresses/#{address_id}")
  SuccessfulResult.new
end

#find(customer_or_customer_id, address_id) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/braintree/address_gateway.rb', line 40

def find(customer_or_customer_id, address_id)
  customer_id = _determine_customer_id(customer_or_customer_id)
  address_id = _determine_address_id(address_id)
  response = @config.http.get("#{@config.base_merchant_path}/customers/#{customer_id}/addresses/#{address_id}")
  Address._new(@gateway, response[:address])
rescue NotFoundError
  raise NotFoundError, "address for customer #{customer_id.inspect} with id #{address_id.inspect} not found"
end

#update(customer_or_customer_id, address_id, attributes) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/braintree/address_gateway.rb', line 49

def update(customer_or_customer_id, address_id, attributes)
  Util.verify_keys(AddressGateway._update_signature, attributes)
  customer_id = _determine_customer_id(customer_or_customer_id)
  address_id = _determine_address_id(address_id)
  response = @config.http.put("#{@config.base_merchant_path}/customers/#{customer_id}/addresses/#{address_id}", :address => attributes)
  if response[:address]
    SuccessfulResult.new(:address => Address._new(@gateway, response[:address]))
  elsif response[:api_error_response]
    ErrorResult.new(@gateway, response[:api_error_response])
  else
    raise UnexpectedError, "expected :address or :api_error_response"
  end
end

#update!(*args) ⇒ Object



63
64
65
# File 'lib/braintree/address_gateway.rb', line 63

def update!(*args)
  return_object_or_raise(:address) { update(*args) }
end