mymoip 0.2.6 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,32 +1,80 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
- **0.2.6**
4
+ 0.3.1
5
+ -----
6
+
7
+ * Re-releasing 0.3.0 after some Rubygems issues.
8
+
9
+ 0.3.0
10
+ -----
11
+
12
+ * Add dependency of active_model gem for validations.
13
+ * Try always to store the plain value of attributes. While the previous version would require you to provide phones in the `"(51)93040-5060"` format, now works even with `"051930405060"`.
14
+ * Payer accepts address_state and address_country downcased.
15
+ * CreditCard accept a valid string date as owner_birthday.
16
+ * Extract conversions of attributes' formats to a new Formatter class.
17
+ * Prevent use of CreditCardPayment#to_json with a invalid CreditCard.
18
+ * Prevent use of Instruction#to_xml with invalid attributes by itself or a invalid Payer.
19
+
20
+ New validations:
21
+ * **CreditCard**
22
+ * Require a logo and a security_code.
23
+ * Validate length of owner_phone (accepts 8 and 9 digit phones with its DDD code).
24
+ * Validate length of security_code (American Express has 4 digits, others 3).
25
+ * Validate format of expiration_date using `%m/%y`.
26
+ * Limit logos in the available at AVAILABLE_LOGOS constant.
27
+ * **Instruction**
28
+ * Require an id, payment_reason, values and a payer.
29
+ * **Payer**
30
+ * Require and id, name, email, address_street, address_street_number, address_neighbourhood, address_city, address_state, address_country, address_cep and an address_phone.
31
+ * Validate length of address_state in 2 chars.
32
+ * Validate length of address_country in 3 chars.
33
+ * Validate length of address_cep in 8 chars.
34
+ * Validate length of address_phone (accepts 8 and 9 digit phones with its DDD code).
35
+
36
+ 0.2.6
37
+ -----
38
+
5
39
  * Deprecate owner_rg attribute of MyMoip::CreditCard; you should provide a owner_cpf from now on. Should explain issues with Visa's risk analysis.
6
40
 
7
- **0.2.5**
41
+ 0.2.5
42
+ -----
43
+
8
44
  * Request's log messages moved to debug level.
9
45
  * Make CreditCard class accept string and symbol logos.
10
46
  * Create MyMoip::CreditCard::AVAILABLE_LOGOS constant.
11
47
  * Standardise Request#api_call parameters.
12
48
 
13
- **0.2.4**
49
+ 0.2.4
50
+ -----
51
+
14
52
  * Fix American Express logo format expected by Moip.
15
53
 
16
- **0.2.3**
54
+ 0.2.3
55
+ -----
56
+
17
57
  * Remove .rvmrc
18
58
  * CreditCardPayment's initialization can now receive a hash of options.
19
59
  * lib/requests folder created.
20
60
  * Requests has methods to return its response id.
21
61
 
22
- **0.2.2**
62
+ 0.2.2
63
+ -----
64
+
23
65
  * Explicitly require order for Requests classes.
24
66
 
25
- **0.2.1**
67
+ 0.2.1
68
+ -----
69
+
26
70
  * Bugfix related to explicitly require MyMoip class being needed.
27
71
 
28
- **0.2.0**
72
+ 0.2.0
73
+ -----
74
+
29
75
  * Update production url from `https://desenvolvedor.moip.com.br` to `https://www.moip.com.br`.
30
76
 
31
- **0.1.0**
77
+ 0.1.0
78
+ -----
79
+
32
80
  * First version of the gem.
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ source "http://rubygems.org"
8
8
 
9
9
  gem "builder"
10
10
  gem "httparty"
11
+ gem "activemodel"
11
12
 
12
13
  group :development do
13
14
  gem "rdoc", "~> 3.12"
data/Gemfile.lock CHANGED
@@ -1,6 +1,12 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.8)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
4
10
  addressable (2.3.2)
5
11
  ansi (1.4.3)
6
12
  builder (3.0.0)
@@ -9,6 +15,7 @@ GEM
9
15
  httparty (0.8.3)
10
16
  multi_json (~> 1.0)
11
17
  multi_xml
18
+ i18n (0.6.1)
12
19
  jeweler (1.8.4)
13
20
  bundler (~> 1.0)
14
21
  git (>= 1.2.5)
@@ -34,6 +41,7 @@ PLATFORMS
34
41
  ruby
35
42
 
36
43
  DEPENDENCIES
44
+ activemodel
37
45
  builder
38
46
  bundler
39
47
  httparty
data/README.md CHANGED
@@ -47,9 +47,9 @@ payer = MyMoip::Payer.new(
47
47
  address_city: "Porto Alegre",
48
48
  address_state: "RS",
49
49
  address_country: "BRA",
50
- address_cep: "90440-150",
51
- address_phone: "(51)3040-5060"
52
- ) # 9 digit phones must be in "(11)93040-5060" format
50
+ address_cep: "90440150",
51
+ address_phone: "5130405060"
52
+ )
53
53
 
54
54
  instruction = MyMoip::Instruction.new(
55
55
  id: "instruction_id_defined_by_you",
@@ -71,8 +71,8 @@ credit_card = MyMoip::CreditCard.new(
71
71
  security_code: "000",
72
72
  owner_name: "Juquinha da Rocha",
73
73
  owner_birthday: Date.new(1984, 11, 3),
74
- owner_phone: "(51)3040-5060",
75
- owner_cpf: "522.116.706-95"
74
+ owner_phone: "5130405060",
75
+ owner_cpf: "52211670695"
76
76
  )
77
77
 
78
78
  credit_card_payment = MyMoip::CreditCardPayment.new(credit_card, installments: 1)
@@ -85,6 +85,11 @@ payment_request.api_call(credit_card_payment, token: transparent_request.token)
85
85
  payment_request.success?
86
86
  ```
87
87
 
88
+ Documentation
89
+ -------------
90
+
91
+ For more information you can access the [wiki page](https://github.com/Irio/mymoip/wiki).
92
+
88
93
  License
89
94
  -------
90
95
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.3.1
@@ -1,5 +1,7 @@
1
1
  module MyMoip
2
2
  class CreditCard
3
+ include ActiveModel::Validations
4
+
3
5
  attr_accessor :logo, :card_number, :expiration_date, :security_code,
4
6
  :owner_name, :owner_birthday, :owner_phone, :owner_cpf
5
7
 
@@ -7,22 +9,59 @@ module MyMoip
7
9
  :american_express, :diners, :hipercard, :mastercard, :visa
8
10
  ]
9
11
 
10
- def initialize(params)
11
- @logo = params[:logo] if params.has_key? :logo
12
- @card_number = params[:card_number] if params.has_key? :card_number
13
- @expiration_date = params[:expiration_date] if params.has_key? :expiration_date
14
- @security_code = params[:security_code] if params.has_key? :security_code
15
- @owner_name = params[:owner_name] if params.has_key? :owner_name
16
- @owner_birthday = params[:owner_birthday] if params.has_key? :owner_birthday
17
- @owner_phone = params[:owner_phone] if params.has_key? :owner_phone
18
- @owner_cpf = params[:owner_cpf] if params.has_key? :owner_cpf
19
-
20
- self.owner_rg = params[:owner_rg] if params.has_key? :owner_rg
12
+ validates_presence_of :logo, :security_code
13
+ validates_length_of :owner_phone, within: 10..11, allow_nil: true
14
+ validates_length_of :security_code, within: 3..4
15
+ validates_format_of :expiration_date, with: /\A(?:(?:0[1-9])|(?:1[02]))\/\d{2}\Z/ # %m/%y
16
+ validates_inclusion_of :logo, in: AVAILABLE_LOGOS
17
+
18
+ def initialize(attrs)
19
+ self.logo = attrs[:logo] if attrs.has_key?(:logo)
20
+ self.card_number = attrs[:card_number] if attrs.has_key?(:card_number)
21
+ self.expiration_date = attrs[:expiration_date] if attrs.has_key?(:expiration_date)
22
+ self.security_code = attrs[:security_code] if attrs.has_key?(:security_code)
23
+ self.owner_name = attrs[:owner_name] if attrs.has_key?(:owner_name)
24
+ self.owner_birthday = attrs[:owner_birthday] if attrs.has_key?(:owner_birthday)
25
+ self.owner_phone = attrs[:owner_phone] if attrs.has_key?(:owner_phone)
26
+ self.owner_cpf = attrs[:owner_cpf] if attrs.has_key?(:owner_cpf)
27
+
28
+ # Deprecated attributes
29
+ self.owner_rg = attrs[:owner_rg] if attrs.has_key?(:owner_rg)
30
+ end
31
+
32
+ def logo=(value)
33
+ value = value.to_sym unless value.nil?
34
+ @logo = value
35
+ end
36
+
37
+ def owner_birthday=(value)
38
+ unless value.nil?
39
+ value = Date.parse(value.to_s)
40
+ end
41
+ @owner_birthday = value
42
+ end
43
+
44
+ def owner_phone=(value)
45
+ unless value.nil?
46
+ # Removes non-digits
47
+ value.gsub!(/\D*/, '')
48
+ # Removes zeros in the beginning
49
+ value.gsub!(/\A0*/, '')
50
+ end
51
+ @owner_phone = value
21
52
  end
22
53
 
23
54
  def owner_rg=(value)
24
55
  warn "[DEPRECATION] `owner_rg` is deprecated. Please use `owner_cpf` instead."
25
56
  self.owner_cpf = value
26
57
  end
58
+
59
+ def owner_cpf=(value)
60
+ unless value.nil?
61
+ # Removes dashes and dots
62
+ value.gsub!(/\-|\./, '')
63
+ end
64
+ @owner_cpf = value
65
+ end
27
66
  end
28
67
  end
@@ -3,17 +3,18 @@ module MyMoip
3
3
  attr_accessor :credit_card, :installments
4
4
 
5
5
  def initialize(credit_card, opts = {})
6
- @credit_card = credit_card
6
+ self.credit_card = credit_card
7
7
  # Backward compatibility. See 0.2.3 CHANGELOG
8
- @installments = if opts.kind_of?(Integer)
9
- opts
10
- else
11
- opts[:installments] || 1
12
- end
8
+ self.installments = if opts.kind_of?(Integer)
9
+ opts
10
+ else
11
+ opts[:installments] || 1
12
+ end
13
13
  end
14
14
 
15
- def to_json
15
+ def to_json(formatter = MyMoip::Formatter)
16
16
  raise "No CreditCard provided" if credit_card.nil?
17
+ raise ArgumentError, 'Invalid credit card' if credit_card.invalid?
17
18
 
18
19
  json = {
19
20
  Forma: "CartaoCredito",
@@ -21,23 +22,27 @@ module MyMoip
21
22
  CartaoCredito: {
22
23
  Numero: credit_card.card_number,
23
24
  Expiracao: credit_card.expiration_date,
24
- CodigoSeguranca: credit_card.security_code,
25
- Portador: {
26
- Nome: credit_card.owner_name,
27
- DataNascimento: credit_card.owner_birthday.strftime("%d/%m/%Y"),
28
- Telefone: credit_card.owner_phone,
29
- Identidade: credit_card.owner_cpf
30
- }
25
+ CodigoSeguranca: credit_card.security_code
31
26
  }
32
27
  }
33
28
 
29
+ json[:CartaoCredito][:Portador] = {
30
+ Nome: credit_card.owner_name,
31
+ DataNascimento: (credit_card.owner_birthday and
32
+ formatter.date(credit_card.owner_birthday)),
33
+ Telefone: (credit_card.owner_phone and
34
+ formatter.phone(credit_card.owner_phone)),
35
+ Identidade: (credit_card.owner_cpf and
36
+ formatter.cpf(credit_card.owner_cpf))
37
+ }
38
+
34
39
  json[:Instituicao] = {
35
40
  american_express: "AmericanExpress",
36
41
  diners: "Diners",
37
42
  hipercard: "Hipercard",
38
43
  mastercard: "Mastercard",
39
44
  visa: "Visa"
40
- }.fetch(credit_card.logo.to_sym)
45
+ }.fetch(credit_card.logo)
41
46
 
42
47
  if cash?
43
48
  json[:Recebimento] = "AVista"
@@ -0,0 +1,23 @@
1
+ module MyMoip
2
+ class Formatter
3
+ def self.cep(plain_cep)
4
+ raise ArgumentError, 'Cannot format CEP nil' if plain_cep.nil?
5
+ plain_cep.gsub(/(\d{5})/, '\1-')
6
+ end
7
+
8
+ def self.phone(plain_phone)
9
+ raise ArgumentError, 'Cannot format phone nil' if plain_phone.nil?
10
+ plain_phone.gsub(/(\d{2})(\d)?(\d{4})(\d{4})/, '(\1)\2\3-\4')
11
+ end
12
+
13
+ def self.date(plain_date)
14
+ raise ArgumentError, 'Cannot format date nil' if plain_date.nil?
15
+ plain_date.strftime("%d/%m/%Y")
16
+ end
17
+
18
+ def self.cpf(plain_cpf)
19
+ raise ArgumentError, 'Cannot format cpf nil' if plain_cpf.nil?
20
+ plain_cpf.gsub(/(\d{3})(\d{3})(\d{3})(\d{2})/, '\1.\2.\3-\4')
21
+ end
22
+ end
23
+ end
@@ -1,15 +1,21 @@
1
1
  module MyMoip
2
2
  class Instruction
3
+ include ActiveModel::Validations
4
+
3
5
  attr_accessor :id, :payment_reason, :values, :payer
4
6
 
7
+ validates_presence_of :id, :payment_reason, :values, :payer
8
+
5
9
  def initialize(attrs)
6
- @id = attrs[:id] if attrs.has_key?(:id)
7
- @payment_reason = attrs[:payment_reason] if attrs.has_key?(:payment_reason)
8
- @values = attrs[:values] if attrs.has_key?(:values)
9
- @payer = attrs[:payer] if attrs.has_key?(:payer)
10
+ self.id = attrs[:id] if attrs.has_key?(:id)
11
+ self.payment_reason = attrs[:payment_reason] if attrs.has_key?(:payment_reason)
12
+ self.values = attrs[:values] if attrs.has_key?(:values)
13
+ self.payer = attrs[:payer] if attrs.has_key?(:payer)
10
14
  end
11
15
 
12
16
  def to_xml(root = nil)
17
+ raise ArgumentError, 'Invalid payer' if payer.invalid?
18
+ raise ArgumentError, 'Invalid params for instruction' if self.invalid?
13
19
 
14
20
  xml = ""
15
21
  root = Builder::XmlMarkup.new(target: xml)
data/lib/mymoip/payer.rb CHANGED
@@ -1,26 +1,62 @@
1
1
  module MyMoip
2
2
  class Payer
3
+ include ActiveModel::Validations
4
+
3
5
  attr_accessor :id, :name, :email,
4
6
  :address_street, :address_street_number, :address_street_extra,
5
7
  :address_neighbourhood, :address_city, :address_state,
6
8
  :address_country, :address_cep, :address_phone
7
9
 
10
+ validates_presence_of :id, :name, :email, :address_street,
11
+ :address_street_number, :address_neighbourhood,
12
+ :address_city, :address_state, :address_country,
13
+ :address_cep, :address_phone
14
+ validates_length_of :address_state, is: 2
15
+ validates_length_of :address_country, is: 3
16
+ validates_length_of :address_cep, is: 8
17
+ validates_length_of :address_phone, within: 10..11
18
+
8
19
  def initialize(attrs)
9
- @id = attrs[:id] if attrs.has_key?(:id)
10
- @name = attrs[:name] if attrs.has_key?(:name)
11
- @email = attrs[:email] if attrs.has_key?(:email)
12
- @address_street = attrs[:address_street] if attrs.has_key?(:address_street)
13
- @address_street_number = attrs[:address_street_number] if attrs.has_key?(:address_street_number)
14
- @address_street_extra = attrs[:address_street_extra] if attrs.has_key?(:address_street_extra)
15
- @address_neighbourhood = attrs[:address_neighbourhood] if attrs.has_key?(:address_neighbourhood)
16
- @address_city = attrs[:address_city] if attrs.has_key?(:address_city)
17
- @address_state = attrs[:address_state] if attrs.has_key?(:address_state)
18
- @address_country = attrs[:address_country] if attrs.has_key?(:address_country)
19
- @address_cep = attrs[:address_cep] if attrs.has_key?(:address_cep)
20
- @address_phone = attrs[:address_phone] if attrs.has_key?(:address_phone)
20
+ self.id = attrs[:id] if attrs.has_key?(:id)
21
+ self.name = attrs[:name] if attrs.has_key?(:name)
22
+ self.email = attrs[:email] if attrs.has_key?(:email)
23
+ self.address_street = attrs[:address_street] if attrs.has_key?(:address_street)
24
+ self.address_street_number = attrs[:address_street_number] if attrs.has_key?(:address_street_number)
25
+ self.address_street_extra = attrs[:address_street_extra] if attrs.has_key?(:address_street_extra)
26
+ self.address_neighbourhood = attrs[:address_neighbourhood] if attrs.has_key?(:address_neighbourhood)
27
+ self.address_city = attrs[:address_city] if attrs.has_key?(:address_city)
28
+ self.address_state = attrs[:address_state] if attrs.has_key?(:address_state)
29
+ self.address_country = attrs[:address_country] if attrs.has_key?(:address_country)
30
+ self.address_cep = attrs[:address_cep] if attrs.has_key?(:address_cep)
31
+ self.address_phone = attrs[:address_phone] if attrs.has_key?(:address_phone)
32
+ end
33
+
34
+ def address_cep=(value)
35
+ value.gsub!(/\D*/, '') unless value.nil?
36
+ @address_cep = value
37
+ end
38
+
39
+ def address_state=(value)
40
+ value = value.upcase unless value.nil?
41
+ @address_state = value
42
+ end
43
+
44
+ def address_country=(value)
45
+ value = value.upcase unless value.nil?
46
+ @address_country = value
47
+ end
48
+
49
+ def address_phone=(value)
50
+ unless value.nil?
51
+ # Removes non-digits
52
+ value.gsub!(/\D*/, '')
53
+ # Removes zeros in the beginning
54
+ value.gsub!(/\A0*/, '')
55
+ end
56
+ @address_phone = value
21
57
  end
22
58
 
23
- def to_xml(root = nil)
59
+ def to_xml(root = nil, formatter = MyMoip::Formatter)
24
60
  if root.nil?
25
61
  xml = ""
26
62
  root ||= Builder::XmlMarkup.new(target: xml)
@@ -37,8 +73,8 @@ module MyMoip
37
73
  n1.Cidade(@address_city)
38
74
  n1.Estado(@address_state)
39
75
  n1.Pais(@address_country)
40
- n1.CEP(@address_cep)
41
- n1.TelefoneFixo(@address_phone)
76
+ n1.CEP(formatter.cep(@address_cep))
77
+ n1.TelefoneFixo(formatter.phone(@address_phone))
42
78
  end
43
79
 
44
80
  xml
data/lib/mymoip.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'active_model'
1
2
  require 'builder'
2
3
  require 'logger'
3
4
  require 'httparty'
data/mymoip.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "mymoip"
8
- s.version = "0.2.6"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Irio Irineu Musskopf Junior"]
12
- s.date = "2012-11-13"
12
+ s.date = "2012-11-17"
13
13
  s.description = "Provides a implementation of MoIP's transparent checkout."
14
14
  s.email = "irio.musskopf@caixadeideias.com.br"
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/mymoip.rb",
29
29
  "lib/mymoip/credit_card.rb",
30
30
  "lib/mymoip/credit_card_payment.rb",
31
+ "lib/mymoip/formatter.rb",
31
32
  "lib/mymoip/instruction.rb",
32
33
  "lib/mymoip/json_parser.rb",
33
34
  "lib/mymoip/payer.rb",
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  "test/live_test.rb",
43
44
  "test/test_credit_card_payment.rb",
44
45
  "test/test_creditcard.rb",
46
+ "test/test_formatter.rb",
45
47
  "test/test_instruction.rb",
46
48
  "test/test_mymoip.rb",
47
49
  "test/test_payer.rb",
@@ -61,6 +63,7 @@ Gem::Specification.new do |s|
61
63
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
62
64
  s.add_runtime_dependency(%q<builder>, [">= 0"])
63
65
  s.add_runtime_dependency(%q<httparty>, [">= 0"])
66
+ s.add_runtime_dependency(%q<activemodel>, [">= 0"])
64
67
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
65
68
  s.add_development_dependency(%q<bundler>, [">= 0"])
66
69
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
@@ -71,6 +74,7 @@ Gem::Specification.new do |s|
71
74
  else
72
75
  s.add_dependency(%q<builder>, [">= 0"])
73
76
  s.add_dependency(%q<httparty>, [">= 0"])
77
+ s.add_dependency(%q<activemodel>, [">= 0"])
74
78
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
75
79
  s.add_dependency(%q<bundler>, [">= 0"])
76
80
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
@@ -82,6 +86,7 @@ Gem::Specification.new do |s|
82
86
  else
83
87
  s.add_dependency(%q<builder>, [">= 0"])
84
88
  s.add_dependency(%q<httparty>, [">= 0"])
89
+ s.add_dependency(%q<activemodel>, [">= 0"])
85
90
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
86
91
  s.add_dependency(%q<bundler>, [">= 0"])
87
92
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
@@ -1,6 +1,6 @@
1
1
  class Fixture
2
- def self.payer
3
- MyMoip::Payer.new(
2
+ def self.payer(params = {})
3
+ params = {
4
4
  id: "your_own_payer_id",
5
5
  name: "Juquinha da Rocha",
6
6
  email: "juquinha@rocha.com",
@@ -13,16 +13,18 @@ class Fixture
13
13
  address_country: "BRA",
14
14
  address_cep: "90440-150",
15
15
  address_phone: "(51)3040-5060"
16
- )
16
+ }.merge(params)
17
+ MyMoip::Payer.new(params)
17
18
  end
18
19
 
19
- def self.instruction(payer)
20
- MyMoip::Instruction.new(
20
+ def self.instruction(params={})
21
+ params = {
21
22
  id: "your_own_instruction_id",
22
23
  payment_reason: "some payment_reason",
23
24
  values: [100.0, 200.0],
24
25
  payer: payer
25
- )
26
+ }.merge(params)
27
+ MyMoip::Instruction.new(params)
26
28
  end
27
29
 
28
30
  def self.credit_card(params = {})
@@ -1,7 +1,6 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestCreditCardPayment < Test::Unit::TestCase
4
-
5
4
  def test_initialization_and_getters
6
5
  credit_card = Fixture.credit_card
7
6
  subject = MyMoip::CreditCardPayment.new(credit_card, 1)
@@ -46,8 +45,8 @@ class TestCreditCardPayment < Test::Unit::TestCase
46
45
  assert_match /\A((0[1-9])|(1[02]))\/\d{2}\z/, payment.to_json[:CartaoCredito][:Expiracao]
47
46
  assert_match /\A\d{3}\z/, payment.to_json[:CartaoCredito][:CodigoSeguranca]
48
47
  original_date = Date.new(1980, 11, 3)
49
- MyMoip::CreditCard.any_instance.stubs(:owner_birthday).returns(original_date)
50
- assert_equal original_date.strftime("%d/%m/%Y"), payment.to_json[:CartaoCredito][:Portador][:DataNascimento]
48
+ MyMoip::Formatter.stubs(:date).returns('03/11/1980')
49
+ assert_equal '03/11/1980', payment.to_json[:CartaoCredito][:Portador][:DataNascimento]
51
50
  assert_match /\A\(\d{2}\)\d{4,5}-\d{4}/, payment.to_json[:CartaoCredito][:Portador][:Telefone]
52
51
  assert_match /\A\d{3}\.\d{3}\.\d{3}\-\d{2}\z/, payment.to_json[:CartaoCredito][:Portador][:Identidade]
53
52
  end
@@ -59,11 +58,57 @@ class TestCreditCardPayment < Test::Unit::TestCase
59
58
  end
60
59
  end
61
60
 
62
- def test_to_json_should_accept_any_creditcard_logo_as_string
63
- MyMoip::CreditCard::AVAILABLE_LOGOS.each do |logo|
64
- payment = MyMoip::CreditCardPayment.new(Fixture.credit_card(logo: logo.to_s))
65
- assert_nothing_raised(KeyError) { payment.to_json }
61
+ def test_to_json_method_uses_the_formatted_version_of_the_credit_cards_owner_birthday
62
+ date = Date.new(2040, 10, 30)
63
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_birthday: date))
64
+ formatter = stub_everything('formatter')
65
+ formatter.expects(:date).with(date)
66
+ subject.to_json(formatter)
67
+ end
68
+
69
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_birthday_is_nil
70
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_birthday: nil))
71
+ formatter = stub_everything('formatter')
72
+ formatter.stubs(:date).with(nil).returns('should not be here')
73
+ json = subject.to_json(formatter)
74
+ assert_nil json[:CartaoCredito][:Portador][:DataNascimento]
75
+ end
76
+
77
+ def test_to_json_method_uses_the_formatted_version_of_the_credit_cards_owner_phone
78
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_phone: '5130405060'))
79
+ formatter = stub_everything('formatter')
80
+ formatter.expects(:phone).with('5130405060')
81
+ subject.to_json(formatter)
82
+ end
83
+
84
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_phone_is_nil
85
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_phone: nil))
86
+ formatter = stub_everything('formatter')
87
+ formatter.stubs(:phone).with(nil).returns('should not be here')
88
+ json = subject.to_json(formatter)
89
+ assert_nil json[:CartaoCredito][:Portador][:Telefone]
90
+ end
91
+
92
+ def test_to_json_method_raises_an_exception_when_called_without_a_credit_card
93
+ subject = MyMoip::CreditCardPayment.new(nil)
94
+ assert_raise RuntimeError do
95
+ subject.to_json
66
96
  end
67
97
  end
68
98
 
99
+ def test_to_json_method_skip_formatting_when_credit_cards_owner_cpf_is_nil
100
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card(owner_cpf: nil))
101
+ formatter = stub_everything('formatter')
102
+ formatter.stubs(:cpf).with(nil).returns('should not be here')
103
+ json = subject.to_json(formatter)
104
+ assert_nil json[:CartaoCredito][:Portador][:Identidade]
105
+ end
106
+
107
+ def test_to_json_method_raises_an_exception_when_called_with_a_invalid_credit_card
108
+ subject = MyMoip::CreditCardPayment.new(Fixture.credit_card)
109
+ MyMoip::CreditCard.any_instance.stubs(:invalid?).returns(true)
110
+ assert_raise ArgumentError do
111
+ subject.to_json
112
+ end
113
+ end
69
114
  end
@@ -1,7 +1,6 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestCreditCard < Test::Unit::TestCase
4
-
5
4
  def test_initialization_and_setters
6
5
  subject = MyMoip::CreditCard.new(
7
6
  logo: :visa,
@@ -10,7 +9,7 @@ class TestCreditCard < Test::Unit::TestCase
10
9
  security_code: "000",
11
10
  owner_name: "Juquinha da Rocha",
12
11
  owner_birthday: Date.new(1984, 11, 3),
13
- owner_phone: "(51)3040-5060",
12
+ owner_phone: "5130405060",
14
13
  owner_cpf: "522.116.706-95"
15
14
  )
16
15
 
@@ -20,8 +19,83 @@ class TestCreditCard < Test::Unit::TestCase
20
19
  assert_equal "000", subject.security_code
21
20
  assert_equal "Juquinha da Rocha", subject.owner_name
22
21
  assert_equal Date.new(1984, 11, 3), subject.owner_birthday
23
- assert_equal "(51)3040-5060", subject.owner_phone
24
- assert_equal "522.116.706-95", subject.owner_cpf
22
+ assert_equal "5130405060", subject.owner_phone
23
+ assert_equal "52211670695", subject.owner_cpf
24
+ end
25
+
26
+ def test_validate_presence_of_logo_attribute
27
+ subject = Fixture.credit_card
28
+ subject.logo = nil
29
+ assert subject.invalid? && subject.errors[:logo].present?,
30
+ 'should be invalid without a logo'
31
+ end
32
+
33
+ def test_owner_birthday_accepts_string_version_of_dates
34
+ subject = Fixture.credit_card
35
+ subject.owner_birthday = '20/12/1980'
36
+ assert_equal Date.new(1980, 12, 20), subject.owner_birthday
37
+ end
38
+
39
+ def test_validate_presence_of_security_code_attribute
40
+ subject = Fixture.credit_card
41
+ subject.security_code = nil
42
+ assert subject.invalid? && subject.errors[:security_code].present?,
43
+ 'should be invalid without an security_code'
44
+ end
45
+
46
+ def test_accept_nil_owner_phone
47
+ subject = Fixture.credit_card(owner_phone: nil)
48
+ assert subject.valid?, 'should be valid'
49
+ end
50
+
51
+ def test_validate_length_of_owner_phone_attribute_in_10_or_11_chars
52
+ subject = Fixture.credit_card
53
+ subject.owner_phone = '5130405060'
54
+ assert subject.valid?, 'should accept 10 chars'
55
+ subject.owner_phone = '51930405060'
56
+ assert subject.valid?, 'should accept 11 chars'
57
+ subject.owner_phone = '215130405060'
58
+ assert subject.invalid? && subject.errors[:owner_phone].present?,
59
+ 'should not accept strings with other than 10 or 11 chars'
60
+ end
61
+
62
+ def test_remove_left_zeros_from_owner_phone
63
+ subject = Fixture.credit_card
64
+ subject.owner_phone = '05130405060'
65
+ assert_equal '5130405060', subject.owner_phone
66
+ end
67
+
68
+ def test_remove_dashes_from_owner_phone
69
+ subject = Fixture.credit_card
70
+ subject.owner_phone = '513040-5060'
71
+ assert_equal '5130405060', subject.owner_phone
72
+ subject.owner_phone = '5193040-5060'
73
+ assert_equal '51930405060', subject.owner_phone
74
+ end
75
+
76
+ def test_remove_parenthesis_from_owner_phone
77
+ subject = Fixture.credit_card
78
+ subject.owner_phone = '(51)30405060'
79
+ assert_equal '5130405060', subject.owner_phone
80
+ subject.owner_phone = '(51)930405060'
81
+ assert_equal '51930405060', subject.owner_phone
82
+ end
83
+
84
+ def test_remove_dashes_from_owner_cpf
85
+ subject = Fixture.credit_card
86
+ subject.owner_cpf = '522116706-95'
87
+ assert_equal '52211670695', subject.owner_cpf
88
+ end
89
+
90
+ def test_remove_dots_from_owner_cpf
91
+ subject = Fixture.credit_card
92
+ subject.owner_cpf = '522.116.70695'
93
+ assert_equal '52211670695', subject.owner_cpf
94
+ end
95
+
96
+ def test_accept_nil_owner_cpf
97
+ subject = Fixture.credit_card(owner_cpf: nil)
98
+ assert subject.valid?, 'should be valid'
25
99
  end
26
100
 
27
101
  def test_warns_about_owner_rg_attribute_deprecation_on_initialization
@@ -34,4 +108,43 @@ class TestCreditCard < Test::Unit::TestCase
34
108
  subject.expects(:warn).with(regexp_matches(/is deprecated/))
35
109
  subject.owner_rg = '1010202030'
36
110
  end
111
+
112
+ def test_accepts_security_codes_of_3_digits
113
+ subject = Fixture.credit_card(security_code: "180")
114
+ assert subject.valid?, 'should be valid'
115
+ end
116
+
117
+ def test_accepts_security_codes_of_4_digits
118
+ subject = Fixture.credit_card(security_code: "1809")
119
+ assert subject.valid?, 'should be valid'
120
+ end
121
+
122
+ def test_dont_accept_security_codes_of_neither_3_or_4_digits
123
+ subject = Fixture.credit_card(security_code: "1")
124
+ assert subject.invalid? && subject.errors[:security_code].present?, 'should not be valid'
125
+ end
126
+
127
+ def test_validate_format_of_expiration_date
128
+ subject = Fixture.credit_card(expiration_date: "12/2018")
129
+ assert subject.invalid? && subject.errors[:expiration_date].present?, 'should not accept other formats'
130
+ subject = Fixture.credit_card(expiration_date: "12/18")
131
+ assert subject.valid? && subject.errors[:expiration_date].empty?, 'should accept "%m/%y" format'
132
+ end
133
+
134
+ def test_converts_creditcard_string_logos_to_symbol
135
+ subject = Fixture.credit_card(logo: "visa")
136
+ assert_equal :visa, subject.logo
137
+ end
138
+
139
+ def test_accepts_any_creditcard_from_available_logos_constant
140
+ MyMoip::CreditCard::AVAILABLE_LOGOS.each do |logo|
141
+ subject = Fixture.credit_card(logo: logo)
142
+ assert subject.valid?, 'should be valid'
143
+ end
144
+ end
145
+
146
+ def test_dont_accept_logos_out_of_available_logos_constant
147
+ subject = Fixture.credit_card(logo: :unavailable_card)
148
+ assert subject.invalid? && subject.errors[:logo].present?, 'should not be valid'
149
+ end
37
150
  end
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class TestFormatter < Test::Unit::TestCase
4
+ def test_cep_method_returns_the_given_cep_with_section_separator
5
+ assert_equal '92400-123', MyMoip::Formatter.cep('92400123')
6
+ end
7
+
8
+ def test_cep_method_raises_exception_with_nil_cep_given
9
+ assert_raise ArgumentError do
10
+ MyMoip::Formatter.cep(nil)
11
+ end
12
+ end
13
+
14
+ def test_phone_method_returns_the_given_8_digit_phone_with_section_separators
15
+ assert_equal '(51)3040-5060', MyMoip::Formatter.phone('5130405060')
16
+ end
17
+
18
+ def test_phone_method_returns_the_given_9_digit_phone_with_section_separators
19
+ assert_equal '(51)93040-5060', MyMoip::Formatter.phone('51930405060')
20
+ end
21
+
22
+ def test_phone_method_raises_exception_with_nil_phone_given
23
+ assert_raise ArgumentError do
24
+ MyMoip::Formatter.phone(nil)
25
+ end
26
+ end
27
+
28
+ def test_date_method_returns_the_given_date_in_the_format_expected
29
+ date = Date.new(2040, 10, 30)
30
+ assert_equal '30/10/2040', MyMoip::Formatter.date(date)
31
+ end
32
+
33
+ def test_date_method_raises_exception_with_nil_date_given
34
+ assert_raise ArgumentError do
35
+ MyMoip::Formatter.date(nil)
36
+ end
37
+ end
38
+
39
+ def test_cpf_method_returns_the_given_number_with_section_separators
40
+ cpf = '522.116.706-95'
41
+ assert_equal '522.116.706-95', MyMoip::Formatter.cpf('52211670695')
42
+ end
43
+
44
+ def test_cpf_method_raises_exception_with_nil_cpf_given
45
+ assert_raise ArgumentError do
46
+ MyMoip::Formatter.cpf(nil)
47
+ end
48
+ end
49
+ end
@@ -1,9 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestInstruction < Test::Unit::TestCase
4
-
5
4
  def test_getters_for_attributes
6
-
7
5
  payer = Fixture.payer
8
6
  instruction = MyMoip::Instruction.new(
9
7
  id: "some id",
@@ -20,18 +18,80 @@ class TestInstruction < Test::Unit::TestCase
20
18
 
21
19
  def test_should_generate_a_string_when_converting_to_xml
22
20
  payer = Fixture.payer
23
- instruction = Fixture.instruction(payer)
21
+ instruction = Fixture.instruction(payer: payer)
24
22
 
25
23
  assert_equal String, instruction.to_xml.class
26
24
  end
27
25
 
28
26
  def test_xml_format
29
27
  payer = Fixture.payer
30
- instruction = Fixture.instruction(payer)
28
+ instruction = Fixture.instruction(payer: payer)
31
29
  expected_format = <<XML
32
30
  <EnviarInstrucao><InstrucaoUnica TipoValidacao=\"Transparente\"><Razao>some payment_reason</Razao><Valores><Valor moeda=\"BRL\">100.00</Valor><Valor moeda=\"BRL\">200.00</Valor></Valores><IdProprio>your_own_instruction_id</IdProprio><Pagador><Nome>Juquinha da Rocha</Nome><Email>juquinha@rocha.com</Email><IdPagador>your_own_payer_id</IdPagador><EnderecoCobranca><Logradouro>Felipe Neri</Logradouro><Numero>406</Numero><Complemento>Sala 501</Complemento><Bairro>Auxiliadora</Bairro><Cidade>Porto Alegre</Cidade><Estado>RS</Estado><Pais>BRA</Pais><CEP>90440-150</CEP><TelefoneFixo>(51)3040-5060</TelefoneFixo></EnderecoCobranca></Pagador></InstrucaoUnica></EnviarInstrucao>
33
31
  XML
34
32
  assert_equal expected_format.rstrip, instruction.to_xml
35
33
  end
36
34
 
35
+ def test_to_xml_method_raises_exception_when_called_with_invalid_payer
36
+ subject = Fixture.instruction
37
+ MyMoip::Payer.any_instance.stubs(:invalid?).returns(true)
38
+ assert_raise ArgumentError do
39
+ subject.to_xml
40
+ end
41
+ end
42
+
43
+ def test_to_xml_method_dont_raises_exception_when_called_with_valid_payer
44
+ subject = Fixture.instruction
45
+ MyMoip::Payer.any_instance.stubs(:invalid?).returns(false)
46
+ assert_nothing_raised ArgumentError do
47
+ subject.to_xml
48
+ end
49
+ end
50
+
51
+ def test_to_xml_method_raises_exception_when_called_with_invalid_params
52
+ subject = Fixture.instruction
53
+ MyMoip::Instruction.any_instance.stubs(:invalid?).returns(true)
54
+ assert_raise ArgumentError do
55
+ subject.to_xml
56
+ end
57
+ end
58
+
59
+ def test_to_xml_method_dont_raises_exception_when_called_with_valid_params
60
+ subject = Fixture.instruction
61
+ MyMoip::Instruction.any_instance.stubs(:invalid?).returns(false)
62
+ assert_nothing_raised ArgumentError do
63
+ subject.to_xml
64
+ end
65
+ end
66
+
67
+ def test_validate_presence_of_id_attribute
68
+ subject = Fixture.instruction
69
+ subject.id = nil
70
+ assert subject.invalid? && subject.errors[:id].present?,
71
+ 'should be invalid without an id'
72
+ end
73
+
74
+ def test_validate_presence_of_payment_reason_attribute
75
+ subject = Fixture.instruction
76
+ subject.payment_reason = nil
77
+ assert subject.invalid? && subject.errors[:payment_reason].present?,
78
+ 'should be invalid without a payment_reason'
79
+ subject.payment_reason = ''
80
+ assert subject.invalid? && subject.errors[:payment_reason].present?,
81
+ 'should be invalid without a payment_reason'
82
+ end
83
+
84
+ def test_validate_presence_of_values_attribute
85
+ subject = Fixture.instruction
86
+ subject.values = nil
87
+ assert subject.invalid? && subject.errors[:values].present?,
88
+ 'should be invalid without values'
89
+ end
90
+
91
+ def test_validate_presence_of_payer_attribute
92
+ subject = Fixture.instruction
93
+ subject.payer = nil
94
+ assert subject.invalid? && subject.errors[:payer].present?,
95
+ 'should be invalid without a payer'
96
+ end
37
97
  end
data/test/test_payer.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestPayer < Test::Unit::TestCase
4
-
5
4
  def test_getters_for_attributes
6
5
  payer = MyMoip::Payer.new(
7
6
  id: "some id",
@@ -12,10 +11,10 @@ class TestPayer < Test::Unit::TestCase
12
11
  address_street_extra: "some address_street_extra",
13
12
  address_neighbourhood: "some address_neighbourhood",
14
13
  address_city: "some address_city",
15
- address_state: "some address_state",
16
- address_country: "some address_country",
17
- address_cep: "some address_cep",
18
- address_phone: "some address_phone"
14
+ address_state: "RS",
15
+ address_country: "BRA",
16
+ address_cep: "92123456",
17
+ address_phone: "5130405060"
19
18
  )
20
19
 
21
20
  assert_equal "some id", payer.id
@@ -26,10 +25,195 @@ class TestPayer < Test::Unit::TestCase
26
25
  assert_equal "some address_street_extra", payer.address_street_extra
27
26
  assert_equal "some address_neighbourhood", payer.address_neighbourhood
28
27
  assert_equal "some address_city", payer.address_city
29
- assert_equal "some address_state", payer.address_state
30
- assert_equal "some address_country", payer.address_country
31
- assert_equal "some address_cep", payer.address_cep
32
- assert_equal "some address_phone", payer.address_phone
28
+ assert_equal "RS", payer.address_state
29
+ assert_equal "BRA", payer.address_country
30
+ assert_equal "92123456", payer.address_cep
31
+ assert_equal "5130405060", payer.address_phone
32
+ end
33
+
34
+ def test_validate_presence_of_id_attribute
35
+ subject = Fixture.payer
36
+ subject.id = nil
37
+ assert subject.invalid?, 'should be invalid without an id'
38
+ subject.id = ''
39
+ assert subject.invalid?, 'should be invalid without an id'
40
+ end
41
+
42
+ def test_validate_presence_of_name_attribute
43
+ subject = Fixture.payer
44
+ subject.name = nil
45
+ assert subject.invalid?, 'should be invalid without an name'
46
+ subject.name = ''
47
+ assert subject.invalid?, 'should be invalid without an name'
48
+ end
49
+
50
+ def test_validate_presence_of_email_attribute
51
+ subject = Fixture.payer
52
+ subject.email = nil
53
+ assert subject.invalid?, 'should be invalid without an email'
54
+ subject.email = ''
55
+ assert subject.invalid?, 'should be invalid without an email'
56
+ end
57
+
58
+ def test_validate_presence_of_address_street_attribute
59
+ subject = Fixture.payer
60
+ subject.address_street = nil
61
+ assert subject.invalid?, 'should be invalid without an address_street'
62
+ subject.address_street = ''
63
+ assert subject.invalid?, 'should be invalid without an address_street'
64
+ end
65
+
66
+ def test_validate_presence_of_address_street_number_attribute
67
+ subject = Fixture.payer
68
+ subject.address_street_number = nil
69
+ assert subject.invalid?, 'should be invalid without an address_street_number'
70
+ subject.address_street_number = ''
71
+ assert subject.invalid?, 'should be invalid without an address_street_number'
72
+ end
73
+
74
+ def test_validate_presence_of_address_neighbourhood_attribute
75
+ subject = Fixture.payer
76
+ subject.address_neighbourhood = nil
77
+ assert subject.invalid?, 'should be invalid without an address_neighbourhood'
78
+ subject.address_neighbourhood = ''
79
+ assert subject.invalid?, 'should be invalid without an address_neighbourhood'
80
+ end
81
+
82
+ def test_validate_presence_of_address_city_attribute
83
+ subject = Fixture.payer
84
+ subject.address_city = nil
85
+ assert subject.invalid?, 'should be invalid without an address_city'
86
+ subject.address_city = ''
87
+ assert subject.invalid?, 'should be invalid without an address_city'
88
+ end
89
+
90
+ def test_validate_presence_of_address_state_attribute
91
+ subject = Fixture.payer
92
+ subject.address_state = nil
93
+ assert subject.invalid?, 'should be invalid without an address_state'
94
+ subject.address_state = ''
95
+ assert subject.invalid?, 'should be invalid without an address_state'
96
+ end
97
+
98
+ def test_validate_length_of_address_state_attribute_in_2_chars
99
+ subject = Fixture.payer
100
+ subject.address_state = 'RS'
101
+ assert subject.valid?, 'should accept 2 chars'
102
+ subject.address_state = 'RSS'
103
+ assert subject.invalid? && subject.errors[:address_state].present?,
104
+ 'should not accept strings with other than 2 chars'
105
+ end
106
+
107
+ def test_upcase_assigned_address_state
108
+ subject = Fixture.payer
109
+ subject.address_state = 'rs'
110
+ assert_equal 'RS', subject.address_state
111
+ end
112
+
113
+ def test_validate_presence_of_address_country_attribute
114
+ subject = Fixture.payer
115
+ subject.address_country = nil
116
+ assert subject.invalid?, 'should be invalid without an address_country'
117
+ subject.address_country = ''
118
+ assert subject.invalid?, 'should be invalid without an address_country'
119
+ end
120
+
121
+ def test_validate_length_of_address_country_attribute_in_3_chars
122
+ subject = Fixture.payer
123
+ subject.address_country = 'BRA'
124
+ assert subject.valid?, 'should accept 3 chars'
125
+ subject.address_country = 'BR'
126
+ assert subject.invalid? && subject.errors[:address_country].present?,
127
+ 'should not accept strings with other than 3 chars'
128
+ end
129
+
130
+ def test_upcase_assigned_address_country
131
+ subject = Fixture.payer
132
+ subject.address_country = 'bra'
133
+ assert_equal 'BRA', subject.address_country
134
+ end
135
+
136
+ def test_validate_presence_of_address_cep_attribute
137
+ subject = Fixture.payer
138
+ subject.address_cep = nil
139
+ assert subject.invalid?, 'should be invalid without an address_cep'
140
+ subject.address_cep = ''
141
+ assert subject.invalid?, 'should be invalid without an address_cep'
142
+ end
143
+
144
+ def test_validate_length_of_address_cep_attribute_in_2_chars
145
+ subject = Fixture.payer
146
+ subject.address_cep = '92123456'
147
+ assert subject.valid?, 'should accept 8 chars'
148
+ subject.address_cep = '921234560000'
149
+ assert subject.invalid? && subject.errors[:address_cep].present?,
150
+ 'should not accept strings with other than 8 chars'
151
+ end
152
+
153
+ def test_dont_count_dashes_in_the_address_cep_length_validation
154
+ subject = Fixture.payer
155
+ subject.address_cep = '92123-456'
156
+ assert subject.valid?
157
+ end
158
+
159
+ def test_remove_dashes_from_address_cep
160
+ subject = Fixture.payer
161
+ subject.address_cep = '92123-456'
162
+ assert_equal '92123456', subject.address_cep
163
+ end
164
+
165
+ def test_validate_presence_of_address_phone_attribute
166
+ subject = Fixture.payer
167
+ subject.address_phone = nil
168
+ assert subject.invalid?, 'should be invalid without an address_phone'
169
+ subject.address_phone = ''
170
+ assert subject.invalid?, 'should be invalid without an address_phone'
171
+ end
172
+
173
+ def test_validate_length_of_address_phone_attribute_in_10_or_11_chars
174
+ subject = Fixture.payer
175
+ subject.address_phone = '5130405060'
176
+ assert subject.valid?, 'should accept 10 chars'
177
+ subject.address_phone = '51930405060'
178
+ assert subject.valid?, 'should accept 11 chars'
179
+ subject.address_phone = '215130405060'
180
+ assert subject.invalid? && subject.errors[:address_phone].present?,
181
+ 'should not accept strings with other than 10 or 11 chars'
182
+ end
183
+
184
+ def test_remove_left_zeros_from_address_phone
185
+ subject = Fixture.payer
186
+ subject.address_phone = '05130405060'
187
+ assert_equal '5130405060', subject.address_phone
188
+ end
189
+
190
+ def test_remove_dashes_from_address_phone
191
+ subject = Fixture.payer
192
+ subject.address_phone = '513040-5060'
193
+ assert_equal '5130405060', subject.address_phone
194
+ subject.address_phone = '5193040-5060'
195
+ assert_equal '51930405060', subject.address_phone
196
+ end
197
+
198
+ def test_remove_parenthesis_from_address_phone
199
+ subject = Fixture.payer
200
+ subject.address_phone = '(51)30405060'
201
+ assert_equal '5130405060', subject.address_phone
202
+ subject.address_phone = '(51)930405060'
203
+ assert_equal '51930405060', subject.address_phone
33
204
  end
34
205
 
206
+ def test_to_xml_method_uses_the_formatted_version_of_the_address_cep
207
+ subject = Fixture.payer(address_cep: '92000123')
208
+ formatter = stub_everything('formatter')
209
+ formatter.expects(:cep).with('92000123')
210
+ subject.to_xml(nil, formatter)
211
+ end
212
+
213
+ def test_to_xml_method_uses_the_formatted_version_of_the_address_phone
214
+ subject = Fixture.payer(address_phone: '5130405060')
215
+ formatter = stub_everything('formatter')
216
+ formatter.expects(:phone).with('5130405060')
217
+ subject.to_xml(nil, formatter)
218
+ end
35
219
  end
@@ -71,7 +71,7 @@ class TestPaymentRequest < Test::Unit::TestCase
71
71
  end
72
72
 
73
73
  def test_method_to_get_moip_code
74
- instruction = Fixture.instruction(Fixture.payer)
74
+ instruction = Fixture.instruction(payer: Fixture.payer)
75
75
  transparent_request = MyMoip::TransparentRequest.new("your_own_id")
76
76
  VCR.use_cassette('transparent_request') do
77
77
  transparent_request.api_call(instruction)
@@ -85,7 +85,7 @@ class TestPaymentRequest < Test::Unit::TestCase
85
85
  end
86
86
 
87
87
  def test_code_method_should_return_nil_with_blank_response
88
- instruction = Fixture.instruction(Fixture.payer)
88
+ instruction = Fixture.instruction(payer: Fixture.payer)
89
89
  transparent_request = MyMoip::TransparentRequest.new("your_own_id")
90
90
  VCR.use_cassette('transparent_request') do
91
91
  transparent_request.api_call(instruction)
@@ -57,7 +57,7 @@ class TestTransparentRequest < Test::Unit::TestCase
57
57
  def test_should_provide_the_transaction_id_get_by_the_request
58
58
  request = MyMoip::TransparentRequest.new("some_id")
59
59
  VCR.use_cassette('transparent_request') do
60
- request.api_call(Fixture.instruction(Fixture.payer))
60
+ request.api_call(Fixture.instruction(payer: Fixture.payer))
61
61
  end
62
62
  assert_equal "201210171118501100000001102691", request.id
63
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mymoip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-13 00:00:00.000000000 Z
12
+ date: 2012-11-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activemodel
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
63
  name: rdoc
48
64
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +190,7 @@ files:
174
190
  - lib/mymoip.rb
175
191
  - lib/mymoip/credit_card.rb
176
192
  - lib/mymoip/credit_card_payment.rb
193
+ - lib/mymoip/formatter.rb
177
194
  - lib/mymoip/instruction.rb
178
195
  - lib/mymoip/json_parser.rb
179
196
  - lib/mymoip/payer.rb
@@ -188,6 +205,7 @@ files:
188
205
  - test/live_test.rb
189
206
  - test/test_credit_card_payment.rb
190
207
  - test/test_creditcard.rb
208
+ - test/test_formatter.rb
191
209
  - test/test_instruction.rb
192
210
  - test/test_mymoip.rb
193
211
  - test/test_payer.rb
@@ -209,7 +227,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
209
227
  version: '0'
210
228
  segments:
211
229
  - 0
212
- hash: 1339834329601628158
230
+ hash: 3042837852117747253
213
231
  required_rubygems_version: !ruby/object:Gem::Requirement
214
232
  none: false
215
233
  requirements: