Module: MasterCard::Core::Util

Extended by:
Util
Included in:
Util, Security::OAuth::OAuthAuthentication
Defined in:
lib/mastercard/core/util.rb

Instance Method Summary collapse

Instance Method Details

#base64Encode(text) ⇒ Object



125
126
127
128
129
# File 'lib/mastercard/core/util.rb', line 125

def base64Encode(text)
  #
  #Base 64 encodes the text
  return Base64.strict_encode64(text)
end

#getReplacedPath(path, inputMap) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mastercard/core/util.rb', line 99

def getReplacedPath(path,inputMap)
  ##
  #Replaces the {var} variables in path with value from inputMap
  #The replaced values are removed from inputPath

  #>>> getReplacedPath("http://localhost:8080/{var1}/car",{"var1" => 1})
  #    "http://localhost:8080/1/car"
  #
  #

  pathRegex = /{(.*?)}/
  matches = path.to_enum(:scan, pathRegex).map { Regexp.last_match }


  matches.each do |match|
    begin
      path["#{match[0]}"] = "%s"%inputMap.fetch(match[1])
      inputMap.delete(match[1])
    rescue
      raise KeyError, "Key \"#{match[1]}\" is not present in the input."
    end
  end

  return path
end

#normalizeParams(url, params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mastercard/core/util.rb', line 46

def normalizeParams(url,params)
  #
  # Combines the query parameters of url and extra params into a single queryString.
  # All the query string parameters are lexicographically sorted
  query = URI.parse(url).query

  unless query.nil?
    query = CGI.parse(URI.parse(url).query)
  end

  unless query.nil?
    query = params.merge(query)
  else
    query = params
  end

  normalizedParams = Hash.new
  #Sort the parameters and
  query.sort.map do |key,value|
    if value.is_a?(Array)
      value = value.join(",")
    end

    normalizedParams[key] = value
  end
  normalizedParams = normalizedParams.map{ |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
  return normalizedParams.gsub("+","%20")
end

#normalizeUrl(url) ⇒ Object



76
77
78
79
80
81
# File 'lib/mastercard/core/util.rb', line 76

def normalizeUrl(url)
  #Removes the query parameters from the URL
  url = URI.parse(url)
  return "#{url.scheme}://#{url.host}#{url.path}"

end

#sha1Base64Encode(text) ⇒ Object



131
132
133
134
135
# File 'lib/mastercard/core/util.rb', line 131

def sha1Base64Encode(text)
  #
  #Base 64 encodes the SHA1 digest of text
  return base64Encode(Digest::SHA1.digest text)
end

#sha256Base64Encode(text) ⇒ Object



137
138
139
140
141
# File 'lib/mastercard/core/util.rb', line 137

def sha256Base64Encode(text)
  #
  #Base 64 encodes the SHA1 digest of text
  return base64Encode(Digest::SHA256.digest text)
end

#subMap(inputMap, keyList) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mastercard/core/util.rb', line 83

def subMap(inputMap,keyList)
  #
  #Returns a dict containing key, value from inputMap for keys in keyList
  #Matched keys are removed from inputMap
  ##
  subMap = Hash.new
  keyList.each do |key|
    if inputMap.key?(key)
      subMap[key] = inputMap[key]
      inputMap.delete(key)
    end
  end
  return subMap
end

#uriRfc3986Encode(value) ⇒ Object



143
144
145
146
147
# File 'lib/mastercard/core/util.rb', line 143

def uriRfc3986Encode(value)
  #
  #RFC 3986 encodes the value
  return CGI.escape(value)
end

#validateURL(url) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/mastercard/core/util.rb', line 36

def validateURL(url)
  #
  # Validates that the given string is a valid URL
  if url =~ URI::DEFAULT_PARSER.make_regexp
    return true
  else
    return false
  end
end