Module: Braintree::Util

Defined in:
lib/braintree/util.rb

Overview

:nodoc:

Class Method Summary (collapse)

Class Method Details

+ (Object) _flatten_hash_keys(element, namespace = nil)



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/braintree/util.rb', line 106

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

+ (Object) _flatten_valid_keys(valid_keys, namespace = nil)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/braintree/util.rb', line 88

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

+ (Object) _remove_wildcard_keys(valid_keys, invalid_keys)



124
125
126
127
128
129
130
131
132
133
# File 'lib/braintree/util.rb', line 124

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

+ (Object) extract_attribute_as_array(hash, attribute)

Raises:



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

def self.extract_attribute_as_array(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

+ (Object) hash_to_query_string(hash, namespace = nil)



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

+ (Object) parse_query_string(qs)



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

+ (Object) raise_exception_for_status_code(status_code, message = nil)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/braintree/util.rb', line 48

def self.raise_exception_for_status_code(status_code, message=nil)
  case status_code.to_i
  when 401
    raise AuthenticationError
  when 403
    raise AuthorizationError, message
  when 404
    raise NotFoundError
  when 426
    raise UpgradeRequiredError, "Please upgrade your client library."
  when 500
    raise ServerError
  when 503
    raise DownForMaintenanceError
  else
    raise UnexpectedError, "Unexpected HTTP_RESPONSE #{status_code.to_i}"
  end
end

+ (Object) symbolize_keys(hash)



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

+ (Object) to_big_decimal(decimal)



67
68
69
70
71
72
73
74
75
76
# File 'lib/braintree/util.rb', line 67

def self.to_big_decimal(decimal)
  case decimal
  when BigDecimal, NilClass
    decimal
  when String
    BigDecimal.new(decimal)
  else
    raise ArgumentError, "Argument must be a String or BigDecimal"
  end
end

+ (Object) url_decode(text)



28
29
30
# File 'lib/braintree/util.rb', line 28

def self.url_decode(text)
  CGI.unescape text.to_s.to_str
end

+ (Object) url_encode(text)



32
33
34
# File 'lib/braintree/util.rb', line 32

def self.url_encode(text)
  CGI.escape text.to_s.to_str
end

+ (Object) verify_keys(valid_keys, hash)



78
79
80
81
82
83
84
85
86
# File 'lib/braintree/util.rb', line 78

def self.verify_keys(valid_keys, hash)
  flattened_valid_keys = _flatten_valid_keys(valid_keys)
  invalid_keys = _flatten_hash_keys(hash) - flattened_valid_keys
  invalid_keys = _remove_wildcard_keys(flattened_valid_keys, invalid_keys)
  if invalid_keys.any?
    sorted = invalid_keys.sort_by { |k| k.to_s }.join(", ")
    raise ArgumentError, "invalid keys: #{sorted}"
  end
end