Class: CF::UAA::Util
- Inherits:
-
Object
- Object
- CF::UAA::Util
- Defined in:
- lib/uaa/util.rb
Overview
Helper functions useful to the UAA client APIs
Class Method Summary collapse
-
.arglist(arg, default_arg = nil) ⇒ Array<String>
Converts common input formats into array of strings.
-
.decode64(str) ⇒ String
Decodes a URL-safe base 64 encoded string.
-
.decode_form(url_encoded_pairs, style = nil) ⇒ Hash
Takes an x-www-form-urlencoded string and returns a hash of utf-8 key/value pairs.
-
.default_logger(level = nil, sink = nil) ⇒ Logger
Set the default logger used by the higher level classes.
-
.encode64(obj) ⇒ String
Encodes
obj
as a URL-safe base 64 encoded string, with trailing padding removed. -
.encode_form(obj) ⇒ Object
Encode an object into x-www-form-urlencoded string suitable for oauth2.
-
.hash_key(key, style) ⇒ String, Symbol
General method to transform a hash key to a given style.
-
.hash_keys(obj, style = nil) ⇒ Object
Makes a new copy of obj with hash keys to style.
-
.hash_keys!(obj, style = nil) ⇒ Object
Modifies obj in place changing any hash keys to style.
-
.json(obj) ⇒ String
Converts
obj
to JSON. -
.json_decode64(str, style = nil) ⇒ Hash
Decodes base64 encoding of JSON data.
-
.json_encode64(obj = {}) ⇒ String
Converts
obj
to a URL-safe base 64 encoded string. -
.json_parse(str, style = nil) ⇒ Hash
Parses a JSON string.
-
.json_pretty(obj) ⇒ String
Converts
obj
to nicely formatted JSON. -
.strlist(arg, delim = ' ') ⇒ String
Joins arrays of strings into a single string.
-
.truncate(obj, limit = 50) ⇒ String
Converts obj to a string and truncates if over limit.
Class Method Details
.arglist(arg, default_arg = nil) ⇒ Array<String>
Converts common input formats into array of strings. Many parameters in these classes can be given as arrays, or as a list of arguments separated by spaces or commas. This method handles the possible inputs and returns an array of strings.
204 205 206 207 208 209 |
# File 'lib/uaa/util.rb', line 204 def self.arglist(arg, default_arg = nil) arg = default_arg unless arg return arg if arg.nil? || arg.respond_to?(:join) raise ArgumentError, "arg must be Array or space|comma delimited strings" unless arg.respond_to?(:split) arg.split(/[\s\,]+/).reject { |e| e.empty? } end |
.decode64(str) ⇒ String
Decodes a URL-safe base 64 encoded string. Adds padding if necessary.
171 172 173 174 175 176 177 178 179 |
# File 'lib/uaa/util.rb', line 171 def self.decode64(str) return unless str pad = str.length % 4 str = str + '=' * (4 - pad) if pad > 0 Base64.respond_to?(:urlsafe_decode64) ? Base64.urlsafe_decode64(str) : Base64.decode64(str.tr('-_', '+/')) rescue ArgumentError raise DecodeError, "invalid base64 encoding" end |
.decode_form(url_encoded_pairs, style = nil) ⇒ Hash
Takes an x-www-form-urlencoded string and returns a hash of utf-8 key/value pairs. Useful for OAuth parameters. Raises ArgumentError if a key occurs more than once, which is a restriction of OAuth query strings. OAuth parameters are case sensitive, scim parameters are case-insensitive.
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/uaa/util.rb', line 121 def self.decode_form(url_encoded_pairs, style = nil) pairs = {} url_encoded_pairs.split(/[&;]/).each do |pair| k, v = pair.split('=', 2).collect { |v| decode_component(v) } raise "duplicate keys in form parameters" if pairs.key?(k = hash_key(k, style)) pairs[k] = v end pairs rescue Exception => e raise ArgumentError, e. end |
.default_logger(level = nil, sink = nil) ⇒ Logger
Set the default logger used by the higher level classes.
223 224 225 226 227 228 229 230 231 |
# File 'lib/uaa/util.rb', line 223 def self.default_logger(level = nil, sink = nil) if sink || !@default_logger @default_logger = Logger.new(sink || $stdout) level = :info unless level @default_logger.formatter = Proc.new { |severity, time, pname, msg| msg } end @default_logger.level = Logger::Severity.const_get(level.to_s.upcase) if level @default_logger end |
.encode64(obj) ⇒ String
Encodes obj
as a URL-safe base 64 encoded string, with trailing padding removed.
162 163 164 165 166 167 |
# File 'lib/uaa/util.rb', line 162 def self.encode64(obj) str = Base64.respond_to?(:urlsafe_encode64)? Base64.urlsafe_encode64(obj): [obj].pack("m").tr("+/", "-_") str.gsub!(/(\n|=*$)/, '') str end |
.encode_form(obj) ⇒ Object
that ruby 1.9.3 converts form components to utf-8. Ruby 1.8.7 users must ensure all data is in utf-8 format before passing to form encode.
Encode an object into x-www-form-urlencoded string suitable for oauth2.
138 139 140 |
# File 'lib/uaa/util.rb', line 138 def self.encode_form(obj) obj.map {|k, v| encode_component(k) << '=' << encode_component(v)}.join('&') end |
.hash_key(key, style) ⇒ String, Symbol
General method to transform a hash key to a given style. Useful when dealing with HTTP headers and various protocol tags that tend to contain ‘-’ characters and are case-insensitive and want to use them as keys in ruby hashes. Useful for dealing with / SCIM case-insensitive attribute names to normalize all attribute names (downcase).
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/uaa/util.rb', line 54 def self.hash_key(key, style) case style when nil, :none then key when :downsym then key.to_s.downcase.to_sym when :sym then key.to_sym when :str then key.to_s when :down then key.to_s.downcase when :dash then key.to_s.downcase.tr('_', '-') when :undash then key.to_s.downcase.tr('-', '_').to_sym when :uncamel then key.to_s.gsub(/([A-Z])([^A-Z]*)/,'_\1\2').downcase.to_sym when :camel then key.to_s.gsub(/(_[a-z])([^_]*)/) { $1[1].upcase + $2 } else raise ArgumentError, "unknown hash key style: #{style}" end end |
.hash_keys(obj, style = nil) ⇒ Object
Makes a new copy of obj with hash keys to style. Recursively modifies subordinate hashes.
90 91 92 93 94 95 96 |
# File 'lib/uaa/util.rb', line 90 def self.hash_keys(obj, style = nil) return obj.collect {|o| hash_keys(o, style)} if obj.is_a? Array return obj unless obj.is_a? Hash obj.each_with_object({}) {|(k, v), h| h[hash_key(k, style)] = hash_keys(v, style) } end |
.hash_keys!(obj, style = nil) ⇒ Object
Modifies obj in place changing any hash keys to style. Recursively modifies subordinate hashes.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/uaa/util.rb', line 73 def self.hash_keys!(obj, style = nil) return obj if style == :none return obj.each {|o| hash_keys!(o, style)} if obj.is_a? Array return obj unless obj.is_a? Hash newkeys, nk = {}, nil obj.delete_if { |k, v| hash_keys!(v, style) newkeys[nk] = v unless (nk = hash_key(k, style)) == k nk != k } obj.merge!(newkeys) end |
.json(obj) ⇒ String
Converts obj
to JSON
144 |
# File 'lib/uaa/util.rb', line 144 def self.json(obj) JSON.dump(obj) end |
.json_decode64(str, style = nil) ⇒ Hash
Decodes base64 encoding of JSON data.
158 |
# File 'lib/uaa/util.rb', line 158 def self.json_decode64(str, style = nil) json_parse(decode64(str), style) end |
.json_encode64(obj = {}) ⇒ String
Converts obj
to a URL-safe base 64 encoded string
152 |
# File 'lib/uaa/util.rb', line 152 def self.json_encode64(obj = {}) encode64(json(obj)) end |
.json_parse(str, style = nil) ⇒ Hash
Parses a JSON string.
184 185 186 187 188 |
# File 'lib/uaa/util.rb', line 184 def self.json_parse(str, style = nil) hash_keys!(JSON.parse(str), style) if str && !str.empty? rescue Exception raise DecodeError, "json decoding error" end |
.json_pretty(obj) ⇒ String
Converts obj
to nicely formatted JSON
148 |
# File 'lib/uaa/util.rb', line 148 def self.json_pretty(obj) JSON.pretty_generate(obj) end |
.strlist(arg, delim = ' ') ⇒ String
Joins arrays of strings into a single string. Reverse of arglist.
215 216 217 |
# File 'lib/uaa/util.rb', line 215 def self.strlist(arg, delim = ' ') arg.respond_to?(:join) ? arg.join(delim) : arg.to_s if arg end |
.truncate(obj, limit = 50) ⇒ String
Converts obj to a string and truncates if over limit.
192 193 194 195 196 197 |
# File 'lib/uaa/util.rb', line 192 def self.truncate(obj, limit = 50) return obj.to_s if limit == 0 limit = limit < 5 ? 1 : limit - 4 str = obj.to_s[0..limit] str.length > limit ? str + '...': str end |