Module: Braintree::Util
- Defined in:
- lib/braintree/util.rb
Defined Under Namespace
Modules: IdEquality, TokenEquality
Class Method Summary
collapse
-
._flatten_hash_keys(element, namespace = nil) ⇒ Object
-
._flatten_valid_keys(valid_keys, namespace = nil) ⇒ Object
-
._get_invalid_keys(valid_keys, hash) ⇒ Object
-
._remove_wildcard_keys(valid_keys, invalid_keys) ⇒ Object
-
.extract_attribute_as_array(hash, attribute) ⇒ Object
-
.hash_to_query_string(hash, namespace = nil) ⇒ Object
-
.inspect_amount(amount) ⇒ Object
-
.keys_valid?(valid_keys, hash) ⇒ Boolean
-
.parse_query_string(qs) ⇒ Object
-
.raise_exception_for_graphql_error(response) ⇒ Object
-
.raise_exception_for_status_code(status_code, message = nil) ⇒ Object
-
.replace_key(hash, target_key, replacement_key) ⇒ Object
-
.symbolize_keys(hash) ⇒ Object
-
.to_big_decimal(decimal) ⇒ Object
-
.url_decode(text) ⇒ Object
-
.url_encode(text) ⇒ Object
-
.verify_keys(valid_keys, hash) ⇒ Object
Class Method Details
._flatten_hash_keys(element, namespace = nil) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/braintree/util.rb', line 162
def self._flatten_hash_keys(element, namespace = nil)
element = [element] if element.is_a?(String)
element.inject([]) do |result, (key, value)|
full_key = (namespace ? "#{namespace}[#{key}]" : key.to_s)
if value.is_a?(Hash)
result += _flatten_hash_keys(value, full_key)
elsif value.is_a?(Array)
value.each do |item|
result += _flatten_hash_keys(item, full_key)
end
else
result << full_key
end
result
end.sort
end
|
._flatten_valid_keys(valid_keys, namespace = nil) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/braintree/util.rb', line 144
def self._flatten_valid_keys(valid_keys, namespace = nil)
valid_keys.inject([]) do |result, key|
if key.is_a?(Hash)
full_key = key.keys[0]
full_key = (namespace ? "#{namespace}[#{full_key}]" : full_key)
nested_keys = key.values[0]
if nested_keys.is_a?(Array)
result += _flatten_valid_keys(nested_keys, full_key)
else
result << "#{full_key}[#{nested_keys}]"
end
else
result << (namespace ? "#{namespace}[#{key}]" : key.to_s)
end
result
end.sort
end
|
._get_invalid_keys(valid_keys, hash) ⇒ Object
191
192
193
194
195
|
# File 'lib/braintree/util.rb', line 191
def self._get_invalid_keys(valid_keys, hash)
flattened_valid_keys = _flatten_valid_keys(valid_keys)
keys = _flatten_hash_keys(hash) - flattened_valid_keys
_remove_wildcard_keys(flattened_valid_keys, keys)
end
|
._remove_wildcard_keys(valid_keys, invalid_keys) ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/braintree/util.rb', line 180
def self._remove_wildcard_keys(valid_keys, invalid_keys)
wildcard_keys = valid_keys.select { |k| k.include? "[_any_key_]" }
return invalid_keys if wildcard_keys.empty?
wildcard_keys.map! { |wk| wk.sub "[_any_key_]", "" }
invalid_keys.select do |invalid_key|
wildcard_keys.all? do |wildcard_key|
invalid_key.index(wildcard_key) != 0
end
end
end
|
3
4
5
6
7
|
# File 'lib/braintree/util.rb', line 3
def self.(hash, attribute)
raise UnexpectedError.new("Unprocessable entity due to an invalid request") if hash.nil?
value = hash.has_key?(attribute) ? hash.delete(attribute) : []
value.is_a?(Array) ? value : [value]
end
|
.hash_to_query_string(hash, namespace = nil) ⇒ Object
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/braintree/util.rb', line 9
def self.hash_to_query_string(hash, namespace = nil)
hash.collect do |key, value|
full_key = namespace ? "#{namespace}[#{key}]" : key
if value.is_a?(Hash)
hash_to_query_string(value, full_key)
else
url_encode(full_key) + "=" + url_encode(value)
end
end.sort * "&"
end
|
.inspect_amount(amount) ⇒ Object
115
116
117
|
# File 'lib/braintree/util.rb', line 115
def self.inspect_amount(amount)
amount ? "amount: #{amount.to_s("F").inspect}" : "amount: nil"
end
|
.keys_valid?(valid_keys, hash) ⇒ Boolean
127
128
129
130
131
|
# File 'lib/braintree/util.rb', line 127
def self.keys_valid?(valid_keys, hash)
invalid_keys = _get_invalid_keys(valid_keys, hash)
!invalid_keys.any?
end
|
.parse_query_string(qs) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/braintree/util.rb', line 20
def self.parse_query_string(qs)
qs.split("&").inject({}) do |result, couplet|
pair = couplet.split("=")
result[CGI.unescape(pair[0]).to_sym] = CGI.unescape(pair[1] || "")
result
end
end
|
.raise_exception_for_graphql_error(response) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/braintree/util.rb', line 73
def self.raise_exception_for_graphql_error(response)
return if !response[:errors]
for error in response[:errors]
if error[:extensions] && error[:extensions][:errorClass]
case error[:extensions][:errorClass]
when "VALIDATION"
next when "AUTHENTICATION"
raise AuthenticationError
when "AUTHORIZATION"
raise AuthorizationError, error[:message]
when "NOT_FOUND"
raise NotFoundError
when "UNSUPPORTED_CLIENT"
raise UpgradeRequiredError, "Please upgrade your client library."
when "RESOURCE_LIMIT"
raise TooManyRequestsError
when "INTERNAL"
raise ServerError
when "SERVICE_AVAILABILITY"
raise ServiceUnavailableError
else
raise UnexpectedError, "Unexpected Response: #{error[:message]}"
end
else
raise UnexpectedError, "Unexpected Response: #{error[:message]}"
end
end
end
|
.raise_exception_for_status_code(status_code, message = nil) ⇒ Object
.replace_key(hash, target_key, replacement_key) ⇒ Object
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/braintree/util.rb', line 133
def self.replace_key(hash, target_key, replacement_key)
hash.inject({}) do |new_hash, (key, value)|
if value.is_a?(Hash)
value = replace_key(value, target_key, replacement_key)
end
key = replacement_key if key == target_key
new_hash.merge(key => value)
end
end
|
.symbolize_keys(hash) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/braintree/util.rb', line 36
def self.symbolize_keys(hash)
hash.inject({}) do |new_hash, (key, value)|
if value.is_a?(Hash)
value = symbolize_keys(value)
elsif value.is_a?(Array) && value.all? { |v| v.is_a?(Hash) }
value = value.map { |v| symbolize_keys(v) }
end
new_hash.merge(key.to_sym => value)
end
end
|
.to_big_decimal(decimal) ⇒ Object
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/braintree/util.rb', line 104
def self.to_big_decimal(decimal)
case decimal
when BigDecimal, NilClass
decimal
when String
BigDecimal(decimal)
else
raise ArgumentError, "Argument must be a String or BigDecimal"
end
end
|
.url_decode(text) ⇒ Object
28
29
30
|
# File 'lib/braintree/util.rb', line 28
def self.url_decode(text)
CGI.unescape text.to_s.to_str
end
|
.url_encode(text) ⇒ Object
32
33
34
|
# File 'lib/braintree/util.rb', line 32
def self.url_encode(text)
CGI.escape text.to_s.to_str
end
|
.verify_keys(valid_keys, hash) ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/braintree/util.rb', line 119
def self.verify_keys(valid_keys, hash)
invalid_keys = _get_invalid_keys(valid_keys, hash)
if invalid_keys.any?
sorted = invalid_keys.sort_by { |k| k.to_s }.join(", ")
raise ArgumentError, "invalid keys: #{sorted}"
end
end
|