Skip to content

Commit f5f56da

Browse files
author
Ed Lebert
committed
Added support for Ruby 2.4 BigDecimal with empty string.
Starting with Ruby 2.4, BigDecimal.new('') raises an ArgumentError. Previous versions returned 0.
1 parent 3585a70 commit f5f56da

2 files changed

Lines changed: 73 additions & 57 deletions

File tree

lib/authorize_net/authorize_net.rb

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# :title: Authorize.Net Ruby SDK
22
# The core AuthoizeNet module. The entire SDK is name-spaced inside of this module.
33
module AuthorizeNet
4-
4+
55
# Some type conversion routines that will be injected into our Transaction/Response
66
# classes.
77
module TypeConversions
8-
8+
99
API_FIELD_PREFIX = 'x_'
10-
10+
1111
# Coverts a value received from Authorize.Net into a boolean if possible. This
1212
# is designed to handle the wide range of boolean formats that Authorize.Net uses.
1313
def value_to_boolean(value)
@@ -20,7 +20,7 @@ def value_to_boolean(value)
2020
value
2121
end
2222
end
23-
23+
2424
# Converts a boolean into an Authorize.Net boolean value string. This
2525
# is designed to handle the wide range of boolean formats that Authorize.Net
2626
# uses. If bool isn't a Boolean, its converted to a string and passed along.
@@ -32,12 +32,13 @@ def boolean_to_value(bool)
3232
bool.to_s
3333
end
3434
end
35-
35+
3636
# Coverts a value received from Authorize.Net into a BigDecimal.
3737
def value_to_decimal(value)
38+
value = 0 if value == '' # Ruby 2.4+ does not accept ""
3839
BigDecimal.new(value)
3940
end
40-
41+
4142
# Converts a BigDecimal (or Float) into an Authorize.Net float value string. If float isn't
4243
# a BigDecimal (or Float), its converted to a string and passed along.
4344
def decimal_to_value(float)
@@ -50,12 +51,12 @@ def decimal_to_value(float)
5051
float.to_s
5152
end
5253
end
53-
54+
5455
# Coverts a value received from Authorize.Net into a Date.
5556
def value_to_date(value)
5657
Date.strptime(value, '%Y-%m-%d')
5758
end
58-
59+
5960
# Converts a Date (or DateTime, or Time) into an Authorize.Net date value string. If date isn't
6061
# a Date (or DateTime, or Time), its converted to a string and passed along.
6162
def date_to_value(date)
@@ -66,12 +67,12 @@ def date_to_value(date)
6667
date.to_s
6768
end
6869
end
69-
70+
7071
# Coverts a value received from Authorize.Net into a DateTime.
7172
def value_to_datetime(value)
7273
DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
7374
end
74-
75+
7576
# Converts a Date (or DateTime, or Time) into an Authorize.Net datetime value string. If date isn't
7677
# a Date (or DateTime, or Time), its converted to a string and passed along.
7778
def datetime_to_value(datetime)
@@ -84,37 +85,37 @@ def datetime_to_value(datetime)
8485
datetime.to_s
8586
end
8687
end
87-
88+
8889
# Coverts a value received from Authorize.Net into an Integer.
8990
def value_to_integer(value)
9091
value.to_s.to_i
9192
end
92-
93+
9394
# Coverts an Integer into an Authorize.Net integer string.
9495
def integer_to_value(int)
9596
int.to_s
9697
end
97-
98+
9899
# Converts a key value pair into a HTTP POST parameter. The key is prefixed
99100
# with key_prefix when being converted to a parameter name.
100101
def to_param(key, value, key_prefix = API_FIELD_PREFIX)
101102
key_str = "#{key_prefix}#{key}="
102-
if value.kind_of?(Array)
103+
if value.kind_of?(Array)
103104
(value.collect do |v|
104105
key_str + CGI::escape(v.to_s)
105106
end).join('&')
106107
else
107108
key_str + CGI::escape(value.to_s)
108109
end
109110
end
110-
111-
111+
112+
112113
# Converts an internal field name (Symbol) into an external field name (Symbol)
113114
# that can be consumed by the Authorize.Net API.
114115
def to_external_field(key)
115116
(API_FIELD_PREFIX + key.to_s).to_sym
116117
end
117-
118+
118119
# Converts an external field name (Symbol) into an internal field name (Symbol). This
119120
# is the exact inverse of to_external_field. Running to_internal_field(to_external_field(:foo))
120121
# would return :foo back.
@@ -123,10 +124,10 @@ def to_internal_field(key)
123124
k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
124125
end
125126
end
126-
127+
127128
# Provides some basic methods used by the various model classes.
128129
module Model
129-
130+
130131
# The constructor for models. Takes any of the supported attributes
131132
# as key/value pairs.
132133
def initialize(fields = {})
@@ -137,18 +138,18 @@ def initialize(fields = {})
137138
end
138139
end
139140
end
140-
141+
141142
def to_a
142143
[self]
143144
end
144-
145+
145146
#:enddoc:
146147
protected
147-
148+
148149
def handle_multivalue_hashing(obj)
149150
obj.to_a.collect(&:to_hash)
150151
end
151-
152+
152153
end
153154

154-
end
155+
end

0 commit comments

Comments
 (0)