module OandaAPI::Utils

A few general purpose useful methods. Intentionally not implemented as monkey patches. Some wheel-reinventing to avoid adding extra dependencies.

Public Class Methods

camelize(s) click to toggle source

Puts camelHumps where you expect them. @param [String] s @return [String]

# File lib/oanda_api/utils/utils.rb, line 9
def self.camelize(s)
  s.to_s.gsub(/(?:_)([a-z\d]*)/i) { "#{$1.capitalize}" }.sub(/^(.)/) { $1.downcase }
end
classify(s) click to toggle source

Converts a string from snake_case to upper camel case (class name like “MyClass”). @param [String] s @return [String]

# File lib/oanda_api/utils/utils.rb, line 16
def self.classify(s)
  s.split('_').collect(&:capitalize).join
end
pluralize(s) click to toggle source

Naively plops an “s” at the end of a string. If the string is “” or nil, returns “”. @param [String] s @return [String]

# File lib/oanda_api/utils/utils.rb, line 24
def self.pluralize(s)
  return "" if s.to_s == ""
  s.to_s =~ /s$/ ? s.to_s : "#{s}s"
end
rubyize_keys(hash) click to toggle source

Returns a deep copy of a hash with its keys downcased, underscored and symbolized into ruby sweetness. @param [Hash] hash @return [Hash]

# File lib/oanda_api/utils/utils.rb, line 40
def self.rubyize_keys(hash)
  transform_hash_keys(hash) { |key| underscore(key).to_sym }
end
singularize(s) click to toggle source

Returns a string with its trailing “s” vaporized. @param [String] s @return [String]

# File lib/oanda_api/utils/utils.rb, line 32
def self.singularize(s)
  s.to_s.chomp("s")
end
stringify_keys(hash) click to toggle source

Returns a deep copy of a hash with its keys camelized, underscored and symbolized. @param [Hash] hash @return [Hash]

# File lib/oanda_api/utils/utils.rb, line 48
def self.stringify_keys(hash)
  transform_hash_keys(hash) { |key| camelize key }
end
transform_hash_keys(value, &block) click to toggle source

Yields all keys of a hash, and safely applies whatever transform the block provides. Supports nested hashes.

@param [Object] value can be a `Hash`, an `Array` or scalar object type.

@param [Block] block transforms the yielded key.

@yield [Object] key the key to be prettied up.

@return [Hash] a deep copy of the hash with it's keys transformed

according to the design of the block.
# File lib/oanda_api/utils/utils.rb, line 63
def self.transform_hash_keys(value, &block)
  case
  when value.is_a?(Array)
    value.map { |v| transform_hash_keys(v, &block) }
  when value.is_a?(Hash)
    Hash[value.map { |k, v| [yield(k), transform_hash_keys(v, &block)] }]
  else
    value
  end
end
transform_hash_values(value, key = nil) { |key, value| ... } click to toggle source

Yields all key/value pairs of a hash, and safely applies whatever transform the block provides to the values. Supports nested hashes and arrays.

@param [Object] value can be a `Hash`, an `Array` or scalar object type.

@param [Object] key

@param [Block] block transforms the yielded value.

@return [Hash] a deep copy of the hash with it's values transformed

according to the design of the block.
# File lib/oanda_api/utils/utils.rb, line 86
def self.transform_hash_values(value, key = nil, &block)
  case
  when value.is_a?(Array)
    value.map { |v| transform_hash_values(v, key, &block) }
  when value.is_a?(Hash)
    Hash[value.map { |k, v| [k, transform_hash_values(v, k, &block)] }]
  else
    yield key, value
  end
end
underscore(s) click to toggle source

Converts a string from camelCase to snake_case. @param [String] s @return [String]

# File lib/oanda_api/utils/utils.rb, line 100
def self.underscore(s)
  s.to_s
   .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
   .gsub(/([a-z\d])([A-Z])/, '\1_\2')
   .tr("-", "_")
   .downcase
end