Module: MasterCard::Core::Util
- Extended by:
- Util
- Included in:
- Util, Security::OAuth::OAuthAuthentication
- Defined in:
- lib/mastercard/core/util.rb
Instance Method Summary collapse
- #base64Encode(text) ⇒ Object
- #getReplacedPath(path, inputMap) ⇒ Object
- #normalizeParams(url, params) ⇒ Object
- #normalizeUrl(url) ⇒ Object
- #sha1Base64Encode(text) ⇒ Object
- #subMap(inputMap, keyList) ⇒ Object
- #uriRfc3986Encode(value) ⇒ Object
- #validateURL(url) ⇒ Object
Instance Method Details
#base64Encode(text) ⇒ Object
122 123 124 125 126 |
# File 'lib/mastercard/core/util.rb', line 122 def base64Encode(text) # #Base 64 encodes the text return Base64.strict_encode64(text) end |
#getReplacedPath(path, inputMap) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mastercard/core/util.rb', line 96 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
43 44 45 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 |
# File 'lib/mastercard/core/util.rb', line 43 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 return normalizedParams.map{ |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&") end |
#normalizeUrl(url) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/mastercard/core/util.rb', line 73 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
128 129 130 131 132 |
# File 'lib/mastercard/core/util.rb', line 128 def sha1Base64Encode(text) # #Base 64 encodes the SHA1 digest of text return base64Encode(Digest::SHA1.digest text) end |
#subMap(inputMap, keyList) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mastercard/core/util.rb', line 80 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
134 135 136 137 138 |
# File 'lib/mastercard/core/util.rb', line 134 def uriRfc3986Encode(value) # #RFC 3986 encodes the value return CGI.escape(value) end |
#validateURL(url) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/mastercard/core/util.rb', line 36 def validateURL(url) # # Validates that the given string is a valid URL return !URI.regexp.match(url).nil? end |