Class: Braintree::AddressGateway

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

Overview

:nodoc

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AddressGateway) initialize(gateway)



3
4
5
6
# File 'lib/braintree/address_gateway.rb', line 3

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

Class Method Details

+ (Object) _create_signature

:nodoc:



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

def self._create_signature # :nodoc:
  _shared_signature + [:customer_id]
end

+ (Object) _shared_signature

:nodoc:



66
67
68
69
70
# File 'lib/braintree/address_gateway.rb', line 66

def self._shared_signature # :nodoc:
  [:company, :country_code_alpha2, :country_code_alpha3, :country_code_numeric,
    :country_name, :extended_address, :first_name,
    :last_name, :locality, :postal_code, :region, :street_address]
end

+ (Object) _update_signature

:nodoc:



72
73
74
# File 'lib/braintree/address_gateway.rb', line 72

def self._update_signature # :nodoc:
  _create_signature
end

Instance Method Details

- (Object) _determine_customer_id(customer_or_customer_id)

:nodoc:



54
55
56
57
58
59
60
# File 'lib/braintree/address_gateway.rb', line 54

def _determine_customer_id(customer_or_customer_id) # :nodoc:
  customer_id = customer_or_customer_id.is_a?(Customer) ? customer_or_customer_id.id : customer_or_customer_id
  unless customer_id =~ /\A[\w_-]+\z/
    raise ArgumentError, "customer_id contains invalid characters"
  end
  customer_id
end

- (Object) create(attributes)



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

def create(attributes)
  Util.verify_keys(AddressGateway._create_signature, attributes)
  unless attributes[:customer_id]
    raise ArgumentError, "Expected hash to contain a :customer_id"
  end
  unless attributes[:customer_id] =~ /\A[0-9A-Za-z_-]+\z/
    raise ArgumentError, ":customer_id contains invalid characters"
  end
  response = @config.http.post "/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

- (Object) delete(customer_or_customer_id, address_id)



26
27
28
29
30
# File 'lib/braintree/address_gateway.rb', line 26

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

- (Object) find(customer_or_customer_id, address_id)



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

def find(customer_or_customer_id, address_id)
  customer_id = _determine_customer_id(customer_or_customer_id)
  raise ArgumentError if address_id.nil? || address_id.to_s.strip == ""
  response = @config.http.get("/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

- (Object) update(customer_or_customer_id, address_id, attributes)



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/braintree/address_gateway.rb', line 41

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)
  response = @config.http.put "/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