braintree 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,8 @@
1
- require 'bigdecimal'
1
+ require "bigdecimal"
2
2
  require "cgi"
3
3
  require "date"
4
4
  require "digest/sha1"
5
+ require "enumerator"
5
6
  require "logger"
6
7
  require "net/http"
7
8
  require "net/https"
@@ -66,25 +66,17 @@ module Braintree
66
66
 
67
67
  # Returns a ResourceCollection of expired credit cards.
68
68
  def self.expired(options = {})
69
- page_number = options[:page] || 1
70
- response = Http.get("/payment_methods/all/expired?page=#{page_number}")
71
- attributes = response[:payment_methods]
72
- attributes[:items] = Util.extract_attribute_as_array(attributes, :credit_card).map do |payment_method_attributes|
73
- new payment_method_attributes
74
- end
75
- ResourceCollection.new(attributes) { |page_number| CreditCard.expired(:page => page_number) }
69
+ response = Http.post("/payment_methods/all/expired_ids")
70
+ ResourceCollection.new(response) { |ids| _fetch_expired(ids) }
76
71
  end
77
72
 
78
73
  # Returns a ResourceCollection of credit cards expiring between +start_date+ and +end_date+ inclusive.
79
74
  # Only the month and year of the start and end dates are used.
80
75
  def self.expiring_between(start_date, end_date, options = {})
81
- page_number = options[:page] || 1
82
- response = Http.get("/payment_methods/all/expiring?page=#{page_number}&start=#{start_date.strftime('%m%Y')}&end=#{end_date.strftime('%m%Y')}")
83
- attributes = response[:payment_methods]
84
- attributes[:items] = Util.extract_attribute_as_array(attributes, :credit_card).map do |payment_method_attributes|
85
- new payment_method_attributes
86
- end
87
- ResourceCollection.new(attributes) { |page_number| CreditCard.expiring_between(start_date, end_date, :page => page_number) }
76
+ formatted_start_date = start_date.strftime('%m%Y')
77
+ formatted_end_date = end_date.strftime('%m%Y')
78
+ response = Http.post("/payment_methods/all/expiring_ids?start=#{formatted_start_date}&end=#{formatted_end_date}")
79
+ ResourceCollection.new(response) { |ids| _fetch_expiring_between(formatted_start_date, formatted_end_date, ids) }
88
80
  end
89
81
 
90
82
  # Finds the credit card with the given +token+. Raises a NotFoundError if it cannot be found.
@@ -124,6 +116,21 @@ module Braintree
124
116
  "#{Braintree::Configuration.base_merchant_url}/payment_methods/all/update_via_transparent_redirect_request"
125
117
  end
126
118
 
119
+ def self._fetch_expired(ids)
120
+ response = Http.post("/payment_methods/all/expired", :search => {:ids => ids})
121
+ attributes = response[:payment_methods]
122
+ Util.extract_attribute_as_array(attributes, :credit_card).map { |attrs| _new(attrs) }
123
+ end
124
+
125
+ def self._fetch_expiring_between(formatted_start_date, formatted_end_date, ids)
126
+ response = Http.post(
127
+ "/payment_methods/all/expiring?start=#{formatted_start_date}&end=#{formatted_end_date}",
128
+ :search => {:ids => ids}
129
+ )
130
+ attributes = response[:payment_methods]
131
+ Util.extract_attribute_as_array(attributes, :credit_card).map { |attrs| _new(attrs) }
132
+ end
133
+
127
134
  def initialize(attributes) # :nodoc:
128
135
  _init attributes
129
136
  @subscriptions = (@subscriptions || []).map { |subscription_hash| Subscription._new(subscription_hash) }
@@ -8,25 +8,21 @@ module Braintree
8
8
  attr_reader :addresses, :company, :created_at, :credit_cards, :email, :fax, :first_name, :id, :last_name,
9
9
  :phone, :updated_at, :website, :custom_fields
10
10
 
11
- # Returns a ResourceCollection of all customers stored in the vault. Due to race conditions, this method
12
- # may not reliably return all customers stored in the vault.
11
+ # Returns a ResourceCollection of all customers stored in the vault.
13
12
  #
14
- # page = Braintree::Customer.all
15
- # loop do
16
- # page.each do |customer|
17
- # puts "Customer #{customer.id} email is #{customer.email}"
18
- # end
19
- # break if page.last_page?
20
- # page = page.next_page
13
+ # customers = Braintree::Customer.all
14
+ # customers.each do |customer|
15
+ # puts "Customer #{customer.id} email is #{customer.email}"
21
16
  # end
22
- def self.all(options = {})
23
- page_number = options[:page] || 1
24
- response = Http.get("/customers?page=#{page_number}")
17
+ def self.all
18
+ response = Http.post "/customers/advanced_search_ids"
19
+ ResourceCollection.new(response) { |ids| _fetch_customers(ids) }
20
+ end
21
+
22
+ def self._fetch_customers(ids)
23
+ response = Http.post "/customers/advanced_search", {:search => {:ids => ids}}
25
24
  attributes = response[:customers]
26
- attributes[:items] = Util.extract_attribute_as_array(attributes, :customer).map do |customer_attributes|
27
- new customer_attributes
28
- end
29
- ResourceCollection.new(attributes) { |page_number| Customer.all(:page => page_number) }
25
+ Util.extract_attribute_as_array(attributes, :customer).map { |attrs| _new(attrs) }
30
26
  end
31
27
 
32
28
  # Creates a customer using the given +attributes+. If <tt>:id</tt> is not passed,
@@ -100,13 +96,16 @@ module Braintree
100
96
 
101
97
  # Returns a ResourceCollection of transactions for the customer with the given +customer_id+.
102
98
  def self.transactions(customer_id, options = {})
103
- page_number = options[:page] || 1
104
- response = Http.get "/customers/#{customer_id}/transactions?page=#{page_number}"
99
+ response = Http.post "/customers/#{customer_id}/transaction_ids"
100
+ ResourceCollection.new(response) { |ids| _fetch_transactions(customer_id, ids) }
101
+ end
102
+
103
+ def self._fetch_transactions(customer_id, ids)
104
+ response = Http.post "/customers/#{customer_id}/transactions", :search => {:ids => ids}
105
105
  attributes = response[:credit_card_transactions]
106
- attributes[:items] = Util.extract_attribute_as_array(attributes, :transaction).map do |transaction_attributes|
106
+ Util.extract_attribute_as_array(attributes, :transaction).map do |transaction_attributes|
107
107
  Transaction._new transaction_attributes
108
108
  end
109
- ResourceCollection.new(attributes) { |page_number| Customer.transactions(customer_id, :page => page_number) }
110
109
  end
111
110
 
112
111
  def self.update(customer_id, attributes)
@@ -38,6 +38,9 @@ module Braintree # :nodoc:
38
38
  # This shouldn't happen.
39
39
  class UnexpectedError < BraintreeError; end
40
40
 
41
+ # Raised when a client library that has been End of Life'd is being used.
42
+ class UpgradeRequiredError < BraintreeError; end
43
+
41
44
  # Raised from bang methods when validations fail.
42
45
  class ValidationsFailed < BraintreeError
43
46
  attr_reader :error_result
@@ -1,55 +1,35 @@
1
1
  module Braintree
2
2
  class ResourceCollection
3
- include BaseModule
4
3
  include Enumerable
5
4
 
6
- def initialize(attributes, &block) # :nodoc:
7
- set_instance_variables_from_hash attributes
5
+ def initialize(response, &block) # :nodoc:
6
+ @ids = Util.extract_attribute_as_array(response[:search_results], :ids)
7
+ @page_size = response[:search_results][:page_size]
8
8
  @paging_block = block
9
9
  end
10
10
 
11
- # Yields each item on the current page.
11
+ # Yields each item
12
12
  def each(&block)
13
- @items.each(&block)
14
-
15
- _next_page.each(&block) unless _last_page?
13
+ @ids.each_slice(@page_size) do |page_of_ids|
14
+ resources = @paging_block.call(page_of_ids)
15
+ resources.each(&block)
16
+ end
16
17
  end
17
18
 
18
19
  def empty?
19
- @items.empty?
20
+ @ids.empty?
20
21
  end
21
22
 
22
- # Returns the first item from the current page.
23
+ # Returns the first item in the collection or nil if the collection is empty
23
24
  def first
24
- @items.first
25
- end
26
-
27
- # Returns true if the page is the last page. False otherwise.
28
- def _last_page?
29
- @current_page_number == _total_pages
30
- end
31
-
32
- # Retrieves the next page of records.
33
- def _next_page
34
- if _last_page?
35
- return nil
36
- end
37
- @paging_block.call(@current_page_number + 1)
25
+ @paging_block.call([@ids.first]).first
38
26
  end
39
27
 
40
- # The size of a resource collection is only approximate due to race conditions when pulling back results. This method
41
- # should be avoided.
42
- def _approximate_size
43
- @total_items
44
- end
45
-
46
- # Returns the total number of pages.
47
- def _total_pages
48
- total = @total_items / @page_size
49
- if @total_items % @page_size != 0
50
- total += 1
51
- end
52
- total
28
+ # Only the maximum size of a resource collection can be determined since the data on the server can change while
29
+ # fetching blocks of results for iteration. For example, customers can be deleted while iterating, so the number
30
+ # of results iterated over may be less than the maximum_size. In general, this method should be avoided.
31
+ def maximum_size
32
+ @ids.size
53
33
  end
54
34
  end
55
35
  end
@@ -97,14 +97,19 @@ module Braintree
97
97
  # s.days_past_due.is "30"
98
98
  # s.status.in [Subscription::Status::PastDue]
99
99
  # end
100
- def self.search(page=1, &block)
100
+ def self.search(&block)
101
101
  search = SubscriptionSearch.new
102
- block.call(search)
102
+ block.call(search) if block
103
103
 
104
- response = Http.post "/subscriptions/advanced_search?page=#{page}", {:search => search.to_hash}
104
+ response = Http.post "/subscriptions/advanced_search_ids", {:search => search.to_hash}
105
+ ResourceCollection.new(response) { |ids| _fetch_subscriptions(search, ids) }
106
+ end
107
+
108
+ def self._fetch_subscriptions(search, ids)
109
+ search.ids.in ids
110
+ response = Http.post "/subscriptions/advanced_search", {:search => search.to_hash}
105
111
  attributes = response[:subscriptions]
106
- attributes[:items] = Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| _new(attrs) }
107
- ResourceCollection.new(attributes) { |page_number| Subscription.search(page_number, &block) }
112
+ Util.extract_attribute_as_array(attributes, :subscription).map { |attrs| _new(attrs) }
108
113
  end
109
114
 
110
115
  def self.update(subscription_id, attributes)
@@ -1,5 +1,6 @@
1
1
  module Braintree
2
2
  class SubscriptionSearch < AdvancedSearch
3
+ multiple_value_field :ids
3
4
  search_fields :plan_id, :days_past_due
4
5
  multiple_value_field :status, :allows => [
5
6
  Subscription::Status::Active,
@@ -223,14 +223,13 @@ module Braintree
223
223
  # Returns a ResourceCollection of transactions matching the search query.
224
224
  # If <tt>query</tt> is a string, the search will be a basic search.
225
225
  # If <tt>query</tt> is a hash, the search will be an advanced search.
226
- def self.search(query = nil, page=1, &block)
227
- if block_given?
228
- _advanced_search page, &block
229
- elsif query.is_a?(String)
230
- _basic_search query, page
231
- else
232
- raise ArgumentError, "expected search to be a string or a block"
233
- end
226
+ # See: http://www.braintreepaymentsolutions.com/gateway/transaction-api#searching
227
+ def self.search(&block)
228
+ search = TransactionSearch.new
229
+ block.call(search) if block
230
+
231
+ response = Http.post "/transactions/advanced_search_ids", {:search => search.to_hash}
232
+ ResourceCollection.new(response) { |ids| _fetch_transactions(search, ids) }
234
233
  end
235
234
 
236
235
  # Submits transaction with +transaction_id+ for settlement.
@@ -395,28 +394,10 @@ module Braintree
395
394
  end
396
395
  end
397
396
 
398
- def self._advanced_search(page, &block) # :nodoc:
399
- search = TransactionSearch.new
400
- block.call(search)
401
-
402
- response = Http.post "/transactions/advanced_search?page=#{page}", {:search => search.to_hash}
403
- attributes = response[:credit_card_transactions]
404
- attributes[:items] = Util.extract_attribute_as_array(attributes, :transaction).map { |attrs| _new(attrs) }
405
-
406
- ResourceCollection.new(attributes) { |page_number| Transaction.search(nil, page_number, &block) }
407
- end
408
-
409
397
  def self._attributes # :nodoc:
410
398
  [:amount, :created_at, :credit_card_details, :customer_details, :id, :status, :type, :updated_at]
411
399
  end
412
400
 
413
- def self._basic_search(query, page) # :nodoc:
414
- response = Http.get "/transactions/all/search?q=#{Util.url_encode(query)}&page=#{Util.url_encode(page)}"
415
- attributes = response[:credit_card_transactions]
416
- attributes[:items] = Util.extract_attribute_as_array(attributes, :transaction).map { |attrs| _new(attrs) }
417
- ResourceCollection.new(attributes) { |page_number| Transaction.search(query, page_number) }
418
- end
419
-
420
401
  def self._create_signature # :nodoc:
421
402
  [
422
403
  :amount, :customer_id, :merchant_account_id, :order_id, :payment_method_token, :type,
@@ -429,6 +410,13 @@ module Braintree
429
410
  ]
430
411
  end
431
412
 
413
+ def self._fetch_transactions(search, ids)
414
+ search.ids.in ids
415
+ response = Http.post "/transactions/advanced_search", {:search => search.to_hash}
416
+ attributes = response[:credit_card_transactions]
417
+ Util.extract_attribute_as_array(attributes, :transaction).map { |attrs| _new(attrs) }
418
+ end
419
+
432
420
  def _init(attributes) # :nodoc:
433
421
  set_instance_variables_from_hash(attributes)
434
422
  @amount = Util.to_big_decimal(amount)
@@ -47,6 +47,7 @@ module Braintree
47
47
  CreditCard::CustomerLocation::International,
48
48
  CreditCard::CustomerLocation::US
49
49
  ]
50
+ multiple_value_field :ids
50
51
  multiple_value_field :merchant_account_id
51
52
  multiple_value_field :status, :allows => Transaction::Status::All
52
53
  multiple_value_field :source, :allows => [
@@ -49,6 +49,8 @@ module Braintree
49
49
  raise AuthorizationError, message
50
50
  when 404
51
51
  raise NotFoundError
52
+ when 426
53
+ raise UpgradeRequiredError, "Please upgrade your client library."
52
54
  when 500
53
55
  raise ServerError
54
56
  when 503
@@ -1,7 +1,7 @@
1
1
  module Braintree
2
2
  module Version
3
3
  Major = 2
4
- Minor = 1
4
+ Minor = 2
5
5
  Tiny = 0
6
6
 
7
7
  String = "#{Major}.#{Minor}.#{Tiny}"
@@ -23,7 +23,7 @@ module Braintree
23
23
  when 'Hash'
24
24
  if value['type'] == 'array'
25
25
  child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway
26
- if entries.nil? || (c = value[CONTENT_ROOT] && c.blank?)
26
+ if entries.nil? || ((c = value[CONTENT_ROOT]) && c.strip.empty?)
27
27
  []
28
28
  else
29
29
  case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
@@ -518,7 +518,7 @@ describe Braintree::CreditCard do
518
518
  updated_credit_card.last_4.should == Braintree::Test::CreditCardNumbers::MasterCard[-4..-1]
519
519
  updated_credit_card.expiration_date.should == "06/2013"
520
520
  updated_credit_card.cardholder_name.should == "New Holder"
521
- updated_credit_card.updated_at.between?(Time.now - 5, Time.now).should == true
521
+ updated_credit_card.updated_at.between?(Time.now - 60, Time.now).should == true
522
522
  end
523
523
 
524
524
  it "raises a ValidationsFailed if invalid" do
@@ -686,19 +686,55 @@ describe Braintree::CreditCard do
686
686
  describe "self.expired" do
687
687
  it "finds expired payment methods, paginated" do
688
688
  collection = Braintree::CreditCard.expired
689
- collection._approximate_size.should > 0
689
+ collection.maximum_size.should > 0
690
690
  collection.all? { |pm| pm.expired?.should == true }
691
691
  end
692
+
693
+ it "can iterate over all items" do
694
+ customer = Braintree::Customer.all.first
695
+
696
+ (110 - Braintree::CreditCard.expired.maximum_size).times do
697
+ Braintree::CreditCard.create!(
698
+ :customer_id => customer.id,
699
+ :number => Braintree::Test::CreditCardNumbers::Visa,
700
+ :expiration_date => "01/2010"
701
+ )
702
+ end
703
+
704
+ collection = Braintree::CreditCard.expired
705
+ collection.maximum_size.should > 100
706
+
707
+ credit_card_ids = collection.map {|c| c.token }.uniq.compact
708
+ credit_card_ids.size.should == collection.maximum_size
709
+ end
692
710
  end
693
711
 
694
712
  describe "self.expiring_between" do
695
713
  it "finds payment methods expiring between the given dates" do
696
714
  next_year = Time.now.year + 1
697
715
  collection = Braintree::CreditCard.expiring_between(Time.mktime(next_year, 1), Time.mktime(next_year, 12))
698
- collection._approximate_size.should > 0
716
+ collection.maximum_size.should > 0
699
717
  collection.all? { |pm| pm.expired?.should == false }
700
718
  collection.all? { |pm| pm.expiration_year.should == next_year.to_s }
701
719
  end
720
+
721
+ it "can iterate over all items" do
722
+ customer = Braintree::Customer.all.first
723
+
724
+ (110 - Braintree::CreditCard.expiring_between(Time.mktime(2010, 1, 1), Time.mktime(2010,3, 1)).maximum_size).times do
725
+ Braintree::CreditCard.create!(
726
+ :customer_id => customer.id,
727
+ :number => Braintree::Test::CreditCardNumbers::Visa,
728
+ :expiration_date => "01/2010"
729
+ )
730
+ end
731
+
732
+ collection = Braintree::CreditCard.expiring_between(Time.mktime(2010, 1, 1), Time.mktime(2010,3, 1))
733
+ collection.maximum_size.should > 100
734
+
735
+ credit_card_ids = collection.map {|c| c.token }.uniq.compact
736
+ credit_card_ids.size.should == collection.maximum_size
737
+ end
702
738
  end
703
739
 
704
740
  describe "self.find" do
@@ -967,7 +1003,7 @@ describe Braintree::CreditCard do
967
1003
  credit_card.last_4.should == Braintree::Test::CreditCardNumbers::MasterCard[-4..-1]
968
1004
  credit_card.expiration_date.should == "06/2013"
969
1005
  credit_card.cardholder_name.should == "New Holder"
970
- credit_card.updated_at.between?(Time.now - 5, Time.now).should == true
1006
+ credit_card.updated_at.between?(Time.now - 60, Time.now).should == true
971
1007
  end
972
1008
 
973
1009
  it "raises a ValidationsFailed if invalid" do
@@ -4,10 +4,10 @@ describe Braintree::Customer do
4
4
  describe "self.all" do
5
5
  it "gets more than a page of customers" do
6
6
  customers = Braintree::Customer.all
7
- customers._approximate_size.should > 100
7
+ customers.maximum_size.should > 100
8
8
 
9
9
  customer_ids = customers.map {|c| c.id }.uniq.compact
10
- customer_ids.size.should == customers._approximate_size
10
+ customer_ids.size.should == customers.maximum_size
11
11
  end
12
12
  end
13
13
 
@@ -578,7 +578,7 @@ describe Braintree::Customer do
578
578
  )
579
579
  updated_customer.first_name.should == "Mr. Joe"
580
580
  updated_customer.last_name.should == "Super Cool"
581
- updated_customer.updated_at.between?(Time.now - 5, Time.now).should == true
581
+ updated_customer.updated_at.between?(Time.now - 60, Time.now).should == true
582
582
  end
583
583
 
584
584
  it "raises an error if unsuccessful" do
@@ -630,7 +630,7 @@ describe Braintree::Customer do
630
630
  ).should == customer
631
631
  customer.first_name.should == "Mr. Joe"
632
632
  customer.last_name.should == "Super Cool"
633
- customer.updated_at.between?(Time.now - 5, Time.now).should == true
633
+ customer.updated_at.between?(Time.now - 60, Time.now).should == true
634
634
  end
635
635
 
636
636
  it "raises an error if unsuccessful" do
@@ -32,7 +32,7 @@ describe Braintree::Http do
32
32
  Braintree::Configuration.logger.level = Logger::INFO
33
33
  Braintree::Customer.all
34
34
  utc_or_gmt = Time.now.utc.strftime("%Z")
35
- output.string.should include("[Braintree] [10/Oct/2009 13:55:36 #{utc_or_gmt}] GET /customers?page=1 200")
35
+ output.string.should include("[Braintree] [10/Oct/2009 13:55:36 #{utc_or_gmt}] POST /customers/advanced_search_ids 200")
36
36
  end
37
37
  ensure
38
38
  Braintree::Configuration.logger = old_logger
@@ -416,7 +416,7 @@ describe Braintree::Subscription do
416
416
  search.plan_id.is "not_a_real_plan_id"
417
417
  end
418
418
 
419
- collection._approximate_size.should == 0
419
+ collection.maximum_size.should == 0
420
420
  end
421
421
 
422
422
  context "is statement" do
@@ -612,6 +612,19 @@ describe Braintree::Subscription do
612
612
  end
613
613
  end
614
614
  end
615
+
616
+ it "returns multiple results" do
617
+ (110 - Braintree::Subscription.search.maximum_size).times do
618
+ Braintree::Subscription.create(:payment_method_token => @credit_card.token, :plan_id => TriallessPlan[:id])
619
+ end
620
+
621
+ collection = Braintree::Subscription.search
622
+ collection.maximum_size.should > 100
623
+
624
+ subscriptions_ids = collection.map {|t| t.id }.uniq.compact
625
+ subscriptions_ids.size.should == collection.maximum_size
626
+ end
627
+
615
628
  end
616
629
 
617
630
  describe "self.retry_charge" do
@@ -7,7 +7,7 @@ describe Braintree::Transaction, "search" do
7
7
  search.billing_first_name.is "thisnameisnotreal"
8
8
  end
9
9
 
10
- collection._approximate_size.should == 0
10
+ collection.maximum_size.should == 0
11
11
  end
12
12
 
13
13
  it "returns one result for text field search" do
@@ -101,7 +101,7 @@ describe Braintree::Transaction, "search" do
101
101
  search.id.is transaction.id
102
102
  search.send(criterion).is value
103
103
  end
104
- collection._approximate_size.should == 1
104
+ collection.maximum_size.should == 1
105
105
  collection.first.id.should == transaction.id
106
106
 
107
107
  collection = Braintree::Transaction.search do |search|
@@ -198,7 +198,7 @@ describe Braintree::Transaction, "search" do
198
198
  search.id.is transaction.id
199
199
  end
200
200
 
201
- collection._approximate_size.should == 1
201
+ collection.maximum_size.should == 1
202
202
  collection.first.id.should == transaction.id
203
203
  end
204
204
 
@@ -217,21 +217,21 @@ describe Braintree::Transaction, "search" do
217
217
  search.created_using.is Braintree::Transaction::CreatedUsing::FullInformation
218
218
  end
219
219
 
220
- collection._approximate_size.should == 1
220
+ collection.maximum_size.should == 1
221
221
 
222
222
  collection = Braintree::Transaction.search do |search|
223
223
  search.id.is transaction.id
224
224
  search.created_using.in Braintree::Transaction::CreatedUsing::FullInformation, Braintree::Transaction::CreatedUsing::Token
225
225
  end
226
226
 
227
- collection._approximate_size.should == 1
227
+ collection.maximum_size.should == 1
228
228
 
229
229
  collection = Braintree::Transaction.search do |search|
230
230
  search.id.is transaction.id
231
231
  search.created_using.is Braintree::Transaction::CreatedUsing::Token
232
232
  end
233
233
 
234
- collection._approximate_size.should == 0
234
+ collection.maximum_size.should == 0
235
235
  end
236
236
 
237
237
  it "searches on credit_card_customer_location" do
@@ -248,21 +248,21 @@ describe Braintree::Transaction, "search" do
248
248
  search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::US
249
249
  end
250
250
 
251
- collection._approximate_size.should == 1
251
+ collection.maximum_size.should == 1
252
252
 
253
253
  collection = Braintree::Transaction.search do |search|
254
254
  search.id.is transaction.id
255
255
  search.credit_card_customer_location.in Braintree::CreditCard::CustomerLocation::US, Braintree::CreditCard::CustomerLocation::International
256
256
  end
257
257
 
258
- collection._approximate_size.should == 1
258
+ collection.maximum_size.should == 1
259
259
 
260
260
  collection = Braintree::Transaction.search do |search|
261
261
  search.id.is transaction.id
262
262
  search.credit_card_customer_location.is Braintree::CreditCard::CustomerLocation::International
263
263
  end
264
264
 
265
- collection._approximate_size.should == 0
265
+ collection.maximum_size.should == 0
266
266
  end
267
267
 
268
268
  it "searches on merchant_account_id" do
@@ -279,21 +279,21 @@ describe Braintree::Transaction, "search" do
279
279
  search.merchant_account_id.is transaction.merchant_account_id
280
280
  end
281
281
 
282
- collection._approximate_size.should == 1
282
+ collection.maximum_size.should == 1
283
283
 
284
284
  collection = Braintree::Transaction.search do |search|
285
285
  search.id.is transaction.id
286
286
  search.merchant_account_id.in transaction.merchant_account_id, "bogus_merchant_account_id"
287
287
  end
288
288
 
289
- collection._approximate_size.should == 1
289
+ collection.maximum_size.should == 1
290
290
 
291
291
  collection = Braintree::Transaction.search do |search|
292
292
  search.id.is transaction.id
293
293
  search.merchant_account_id.is "bogus_merchant_account_id"
294
294
  end
295
295
 
296
- collection._approximate_size.should == 0
296
+ collection.maximum_size.should == 0
297
297
  end
298
298
 
299
299
  it "searches on credit_card_card_type" do
@@ -310,28 +310,28 @@ describe Braintree::Transaction, "search" do
310
310
  search.credit_card_card_type.is Braintree::CreditCard::CardType::Visa
311
311
  end
312
312
 
313
- collection._approximate_size.should == 1
313
+ collection.maximum_size.should == 1
314
314
 
315
315
  collection = Braintree::Transaction.search do |search|
316
316
  search.id.is transaction.id
317
317
  search.credit_card_card_type.is transaction.credit_card_details.card_type
318
318
  end
319
319
 
320
- collection._approximate_size.should == 1
320
+ collection.maximum_size.should == 1
321
321
 
322
322
  collection = Braintree::Transaction.search do |search|
323
323
  search.id.is transaction.id
324
324
  search.credit_card_card_type.in Braintree::CreditCard::CardType::Visa, Braintree::CreditCard::CardType::MasterCard
325
325
  end
326
326
 
327
- collection._approximate_size.should == 1
327
+ collection.maximum_size.should == 1
328
328
 
329
329
  collection = Braintree::Transaction.search do |search|
330
330
  search.id.is transaction.id
331
331
  search.credit_card_card_type.is Braintree::CreditCard::CardType::MasterCard
332
332
  end
333
333
 
334
- collection._approximate_size.should == 0
334
+ collection.maximum_size.should == 0
335
335
  end
336
336
 
337
337
  it "searches on status" do
@@ -348,21 +348,21 @@ describe Braintree::Transaction, "search" do
348
348
  search.status.is Braintree::Transaction::Status::Authorized
349
349
  end
350
350
 
351
- collection._approximate_size.should == 1
351
+ collection.maximum_size.should == 1
352
352
 
353
353
  collection = Braintree::Transaction.search do |search|
354
354
  search.id.is transaction.id
355
355
  search.status.in Braintree::Transaction::Status::Authorized, Braintree::Transaction::Status::ProcessorDeclined
356
356
  end
357
357
 
358
- collection._approximate_size.should == 1
358
+ collection.maximum_size.should == 1
359
359
 
360
360
  collection = Braintree::Transaction.search do |search|
361
361
  search.id.is transaction.id
362
362
  search.status.is Braintree::Transaction::Status::ProcessorDeclined
363
363
  end
364
364
 
365
- collection._approximate_size.should == 0
365
+ collection.maximum_size.should == 0
366
366
  end
367
367
 
368
368
  it "searches on source" do
@@ -379,21 +379,21 @@ describe Braintree::Transaction, "search" do
379
379
  search.source.is Braintree::Transaction::Source::Api
380
380
  end
381
381
 
382
- collection._approximate_size.should == 1
382
+ collection.maximum_size.should == 1
383
383
 
384
384
  collection = Braintree::Transaction.search do |search|
385
385
  search.id.is transaction.id
386
386
  search.source.in Braintree::Transaction::Source::Api, Braintree::Transaction::Source::ControlPanel
387
387
  end
388
388
 
389
- collection._approximate_size.should == 1
389
+ collection.maximum_size.should == 1
390
390
 
391
391
  collection = Braintree::Transaction.search do |search|
392
392
  search.id.is transaction.id
393
393
  search.source.is Braintree::Transaction::Source::ControlPanel
394
394
  end
395
395
 
396
- collection._approximate_size.should == 0
396
+ collection.maximum_size.should == 0
397
397
  end
398
398
 
399
399
  it "searches on type" do
@@ -425,7 +425,7 @@ describe Braintree::Transaction, "search" do
425
425
  search.type.is Braintree::Transaction::Type::Credit
426
426
  end
427
427
 
428
- collection._approximate_size.should == 2
428
+ collection.maximum_size.should == 2
429
429
 
430
430
  collection = Braintree::Transaction.search do |search|
431
431
  search.credit_card_cardholder_name.is cardholder_name
@@ -433,7 +433,7 @@ describe Braintree::Transaction, "search" do
433
433
  search.refund.is true
434
434
  end
435
435
 
436
- collection._approximate_size.should == 1
436
+ collection.maximum_size.should == 1
437
437
  collection.first.id.should == refund_transaction.id
438
438
 
439
439
  collection = Braintree::Transaction.search do |search|
@@ -442,7 +442,7 @@ describe Braintree::Transaction, "search" do
442
442
  search.refund.is false
443
443
  end
444
444
 
445
- collection._approximate_size.should == 1
445
+ collection.maximum_size.should == 1
446
446
  collection.first.id.should == credit_transaction.id
447
447
  end
448
448
  end
@@ -463,7 +463,7 @@ describe Braintree::Transaction, "search" do
463
463
  search.amount.between "500.00", "1500.00"
464
464
  end
465
465
 
466
- collection._approximate_size.should == 1
466
+ collection.maximum_size.should == 1
467
467
  collection.first.id.should == transaction.id
468
468
 
469
469
  collection = Braintree::Transaction.search do |search|
@@ -471,7 +471,7 @@ describe Braintree::Transaction, "search" do
471
471
  search.amount >= "500.00"
472
472
  end
473
473
 
474
- collection._approximate_size.should == 1
474
+ collection.maximum_size.should == 1
475
475
  collection.first.id.should == transaction.id
476
476
 
477
477
  collection = Braintree::Transaction.search do |search|
@@ -479,7 +479,7 @@ describe Braintree::Transaction, "search" do
479
479
  search.amount <= "1500.00"
480
480
  end
481
481
 
482
- collection._approximate_size.should == 1
482
+ collection.maximum_size.should == 1
483
483
  collection.first.id.should == transaction.id
484
484
 
485
485
  collection = Braintree::Transaction.search do |search|
@@ -487,7 +487,7 @@ describe Braintree::Transaction, "search" do
487
487
  search.amount.between "500.00", "900.00"
488
488
  end
489
489
 
490
- collection._approximate_size.should == 0
490
+ collection.maximum_size.should == 0
491
491
  end
492
492
 
493
493
  it "can also take BigDecimal for amount" do
@@ -504,7 +504,7 @@ describe Braintree::Transaction, "search" do
504
504
  search.amount <= BigDecimal.new("1000.00")
505
505
  end
506
506
 
507
- collection._approximate_size.should == 1
507
+ collection.maximum_size.should == 1
508
508
  end
509
509
  end
510
510
 
@@ -528,7 +528,7 @@ describe Braintree::Transaction, "search" do
528
528
  )
529
529
  end
530
530
 
531
- collection._approximate_size.should == 1
531
+ collection.maximum_size.should == 1
532
532
  collection.first.id.should == transaction.id
533
533
 
534
534
  collection = Braintree::Transaction.search do |search|
@@ -536,7 +536,7 @@ describe Braintree::Transaction, "search" do
536
536
  search.created_at >= created_at - 1
537
537
  end
538
538
 
539
- collection._approximate_size.should == 1
539
+ collection.maximum_size.should == 1
540
540
  collection.first.id.should == transaction.id
541
541
 
542
542
  collection = Braintree::Transaction.search do |search|
@@ -544,7 +544,7 @@ describe Braintree::Transaction, "search" do
544
544
  search.created_at <= created_at + 1
545
545
  end
546
546
 
547
- collection._approximate_size.should == 1
547
+ collection.maximum_size.should == 1
548
548
  collection.first.id.should == transaction.id
549
549
 
550
550
  collection = Braintree::Transaction.search do |search|
@@ -555,7 +555,7 @@ describe Braintree::Transaction, "search" do
555
555
  )
556
556
  end
557
557
 
558
- collection._approximate_size.should == 0
558
+ collection.maximum_size.should == 0
559
559
  end
560
560
 
561
561
  it "searches on created_at in local time" do
@@ -577,7 +577,7 @@ describe Braintree::Transaction, "search" do
577
577
  )
578
578
  end
579
579
 
580
- collection._approximate_size.should == 1
580
+ collection.maximum_size.should == 1
581
581
  collection.first.id.should == transaction.id
582
582
 
583
583
  collection = Braintree::Transaction.search do |search|
@@ -585,7 +585,7 @@ describe Braintree::Transaction, "search" do
585
585
  search.created_at >= now - 60
586
586
  end
587
587
 
588
- collection._approximate_size.should == 1
588
+ collection.maximum_size.should == 1
589
589
  collection.first.id.should == transaction.id
590
590
 
591
591
  collection = Braintree::Transaction.search do |search|
@@ -593,7 +593,7 @@ describe Braintree::Transaction, "search" do
593
593
  search.created_at <= now + 60
594
594
  end
595
595
 
596
- collection._approximate_size.should == 1
596
+ collection.maximum_size.should == 1
597
597
  collection.first.id.should == transaction.id
598
598
 
599
599
  collection = Braintree::Transaction.search do |search|
@@ -604,18 +604,16 @@ describe Braintree::Transaction, "search" do
604
604
  )
605
605
  end
606
606
 
607
- collection._approximate_size.should == 0
607
+ collection.maximum_size.should == 0
608
608
  end
609
609
  end
610
610
 
611
611
  it "returns multiple results" do
612
- collection = Braintree::Transaction.search do |search|
613
- search.credit_card_number.starts_with "411"
614
- end
615
- collection._approximate_size.should > 100
612
+ collection = Braintree::Transaction.search
613
+ collection.maximum_size.should > 100
616
614
 
617
615
  transaction_ids = collection.map {|t| t.id }.uniq.compact
618
- transaction_ids.size.should == collection._approximate_size
616
+ transaction_ids.size.should == collection.maximum_size
619
617
  end
620
618
 
621
619
  context "text node operations" do
@@ -636,7 +634,7 @@ describe Braintree::Transaction, "search" do
636
634
  search.credit_card_cardholder_name.is "Tom Smith"
637
635
  end
638
636
 
639
- collection._approximate_size.should == 1
637
+ collection.maximum_size.should == 1
640
638
  collection.first.id.should == @transaction.id
641
639
 
642
640
  collection = Braintree::Transaction.search do |search|
@@ -644,7 +642,7 @@ describe Braintree::Transaction, "search" do
644
642
  search.credit_card_cardholder_name.is "Invalid"
645
643
  end
646
644
 
647
- collection._approximate_size.should == 0
645
+ collection.maximum_size.should == 0
648
646
  end
649
647
 
650
648
  it "is_not" do
@@ -653,7 +651,7 @@ describe Braintree::Transaction, "search" do
653
651
  search.credit_card_cardholder_name.is_not "Anybody Else"
654
652
  end
655
653
 
656
- collection._approximate_size.should == 1
654
+ collection.maximum_size.should == 1
657
655
  collection.first.id.should == @transaction.id
658
656
 
659
657
  collection = Braintree::Transaction.search do |search|
@@ -661,7 +659,7 @@ describe Braintree::Transaction, "search" do
661
659
  search.credit_card_cardholder_name.is_not "Tom Smith"
662
660
  end
663
661
 
664
- collection._approximate_size.should == 0
662
+ collection.maximum_size.should == 0
665
663
  end
666
664
 
667
665
  it "ends_with" do
@@ -670,7 +668,7 @@ describe Braintree::Transaction, "search" do
670
668
  search.credit_card_cardholder_name.ends_with "m Smith"
671
669
  end
672
670
 
673
- collection._approximate_size.should == 1
671
+ collection.maximum_size.should == 1
674
672
  collection.first.id.should == @transaction.id
675
673
 
676
674
  collection = Braintree::Transaction.search do |search|
@@ -678,7 +676,7 @@ describe Braintree::Transaction, "search" do
678
676
  search.credit_card_cardholder_name.ends_with "Tom S"
679
677
  end
680
678
 
681
- collection._approximate_size.should == 0
679
+ collection.maximum_size.should == 0
682
680
  end
683
681
 
684
682
  it "starts_with" do
@@ -687,7 +685,7 @@ describe Braintree::Transaction, "search" do
687
685
  search.credit_card_cardholder_name.starts_with "Tom S"
688
686
  end
689
687
 
690
- collection._approximate_size.should == 1
688
+ collection.maximum_size.should == 1
691
689
  collection.first.id.should == @transaction.id
692
690
 
693
691
  collection = Braintree::Transaction.search do |search|
@@ -695,7 +693,7 @@ describe Braintree::Transaction, "search" do
695
693
  search.credit_card_cardholder_name.starts_with "m Smith"
696
694
  end
697
695
 
698
- collection._approximate_size.should == 0
696
+ collection.maximum_size.should == 0
699
697
  end
700
698
 
701
699
  it "contains" do
@@ -704,7 +702,7 @@ describe Braintree::Transaction, "search" do
704
702
  search.credit_card_cardholder_name.contains "m Sm"
705
703
  end
706
704
 
707
- collection._approximate_size.should == 1
705
+ collection.maximum_size.should == 1
708
706
  collection.first.id.should == @transaction.id
709
707
 
710
708
  collection = Braintree::Transaction.search do |search|
@@ -712,23 +710,8 @@ describe Braintree::Transaction, "search" do
712
710
  search.credit_card_cardholder_name.contains "Anybody Else"
713
711
  end
714
712
 
715
- collection._approximate_size.should == 0
713
+ collection.maximum_size.should == 0
716
714
  end
717
715
  end
718
716
  end
719
-
720
- context "basic" do
721
- it "returns transactions matching the given search terms" do
722
- transactions = Braintree::Transaction.search "1111"
723
- transactions._approximate_size.should > 0
724
- end
725
-
726
- it "can iterate over the entire collection" do
727
- transactions = Braintree::Transaction.search "411111"
728
- transactions._approximate_size.should > 100
729
-
730
- transaction_ids = transactions.map {|t| t.id }.uniq.compact
731
- transaction_ids.size.should == transactions._approximate_size
732
- end
733
- end
734
717
  end
@@ -636,7 +636,7 @@ describe Braintree::Transaction do
636
636
  result.success?.should == true
637
637
  result.transaction.amount.should == BigDecimal.new("999.99")
638
638
  result.transaction.status.should == Braintree::Transaction::Status::SubmittedForSettlement
639
- result.transaction.updated_at.between?(Time.now - 5, Time.now).should == true
639
+ result.transaction.updated_at.between?(Time.now - 60, Time.now).should == true
640
640
  end
641
641
 
642
642
  it "returns an error result if settlement is too large" do
@@ -1,68 +1,20 @@
1
1
  require File.dirname(__FILE__) + "/../spec_helper"
2
2
 
3
3
  describe "Braintree::ResourceCollection" do
4
- it "includes enumerable" do
5
- collection = Braintree::ResourceCollection.new(:items => ["a"])
6
- collection.detect { |item| item == "a" }.should == "a"
7
- end
8
-
9
- describe "each" do
10
- it "iterates over the contents" do
11
- expected = ["apples", "bananas", "cherries"]
12
- collection = Braintree::ResourceCollection.new(
13
- :current_page_number => 1,
14
- :items => expected,
15
- :page_size => 5,
16
- :total_items => expected.size
17
- )
18
- actual = []
19
- collection.each do |item|
20
- actual << item
4
+ describe "enumeration" do
5
+ it "iterates over the elements, yielding to the block in pages" do
6
+ values = %w(a b c d e)
7
+ collection = Braintree::ResourceCollection.new(:search_results => {:ids => [0,1,2,3,4], :page_size => 2}) do |ids|
8
+ ids.map {|id| values[id] }
21
9
  end
22
- actual.should == expected
23
- end
24
- end
25
-
26
- describe "empty?" do
27
- it "returns true if there are no items" do
28
- collection = Braintree::ResourceCollection.new(
29
- :current_page_number => 1,
30
- :items => [],
31
- :page_size => 5,
32
- :total_items => 0
33
- )
34
- collection.should be_empty
35
- end
36
-
37
- it "returns false if there are items" do
38
- collection = Braintree::ResourceCollection.new(
39
- :current_page_number => 1,
40
- :items => ["one"],
41
- :page_size => 5,
42
- :total_items => 1
43
- )
44
- collection.should_not be_empty
45
- end
46
- end
47
10
 
48
- describe "first" do
49
- it "returns the first element" do
50
- collection = Braintree::ResourceCollection.new(
51
- :items => ["apples", "bananas", "cherries"]
52
- )
53
- collection.first.should == "apples"
54
- end
55
- end
56
-
57
- describe "_last_page?" do
58
- it "returns true if the page is the last page" do
59
- collection = Braintree::ResourceCollection.new(:current_page_number => 3, :page_size => 50, :total_items => 150)
60
- collection._last_page?.should == true
61
- end
11
+ count = 0
12
+ collection.each_with_index do |item, index|
13
+ item.should == values[index]
14
+ count += 1
15
+ end
62
16
 
63
- it "returns false if the page is not the last page" do
64
- collection = Braintree::ResourceCollection.new(:current_page_number => 3, :page_size => 50, :total_items => 151)
65
- collection._last_page?.should == false
17
+ count.should == 5
66
18
  end
67
19
  end
68
20
  end
@@ -183,6 +183,12 @@ describe Braintree::Util do
183
183
  end.to raise_error(Braintree::AuthorizationError)
184
184
  end
185
185
 
186
+ it "raises an UpgradeRequired if the client library is EOL'd" do
187
+ expect do
188
+ Braintree::Util.raise_exception_for_status_code(426)
189
+ end.to raise_error(Braintree::UpgradeRequiredError, "Please upgrade your client library.")
190
+ end
191
+
186
192
  it "raises a ServerError if the server 500's" do
187
193
  expect do
188
194
  Braintree::Util.raise_exception_for_status_code(500)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braintree
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Braintree Payment Solutions
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-05 00:00:00 -05:00
12
+ date: 2010-05-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency