Module: Boomerang::Utilities
Instance Method Summary collapse
- #build_query(parameters) ⇒ Object
- #camel_case(hash_or_string, first_cap = true) ⇒ Object (also: #CamelCase)
- #camelCase(hash_or_string, first_cap = false) ⇒ Object
- #snake_case(hash_or_string) ⇒ Object
- #url_encode(string) ⇒ Object
- #utf8(string) ⇒ Object
Instance Method Details
#build_query(parameters) ⇒ Object
41 42 43 |
# File 'lib/boomerang/utilities.rb', line 41 def build_query(parameters) parameters.map { |k, v| "#{url_encode k}=#{url_encode v}" }.join("&") end |
#camel_case(hash_or_string, first_cap = true) ⇒ Object Also known as: CamelCase
5 6 7 8 9 10 11 12 13 |
# File 'lib/boomerang/utilities.rb', line 5 def camel_case(hash_or_string, first_cap = true) if hash_or_string.is_a? Hash Hash[hash_or_string.map { |k, v| [camel_case(k, first_cap), v]}] else string = hash_or_string.to_s string = string.gsub(/(\A|\.)[a-z]/) { $&.upcase } if first_cap string.gsub(/_([a-z])/) { $1.upcase } end end |
#camelCase(hash_or_string, first_cap = false) ⇒ Object
15 16 17 |
# File 'lib/boomerang/utilities.rb', line 15 def camelCase(hash_or_string, first_cap = false) camel_case(hash_or_string, first_cap) end |
#snake_case(hash_or_string) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/boomerang/utilities.rb', line 19 def snake_case(hash_or_string) if hash_or_string.is_a? Hash Hash[hash_or_string.map { |k, v| [snake_case(k), v]}] else hash_or_string.to_s .gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2.downcase}" } .downcase end end |
#url_encode(string) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/boomerang/utilities.rb', line 33 def url_encode(string) string.chars .map { |char| char =~ /\A[-A-Za-z0-9_.~]\z/ ? char : char.bytes.map { |byte| "%%%02X" % byte }.join } .join end |
#utf8(string) ⇒ Object
29 30 31 |
# File 'lib/boomerang/utilities.rb', line 29 def utf8(string) string.to_s.encode(Encoding::UTF_8) end |