Module: OandaAPI::Utils

Defined in:
lib/oanda_api/utils/utils.rb

Overview

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

Class Method Summary collapse

Class Method Details

.camelize(s) ⇒ String

Puts camelHumps where you expect them.

Parameters:

  • s (String)

Returns:

  • (String)


9
10
11
# 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) ⇒ String

Converts a string from snake_case to upper camel case (class name like "MyClass").

Parameters:

  • s (String)

Returns:

  • (String)


16
17
18
# File 'lib/oanda_api/utils/utils.rb', line 16

def self.classify(s)
  s.split('_').collect(&:capitalize).join
end

.pluralize(s) ⇒ String

Naively plops an "s" at the end of a string. If the string is "" or nil, returns "".

Parameters:

  • s (String)

Returns:

  • (String)


24
25
26
27
# 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) ⇒ Hash

Returns a deep copy of a hash with its keys downcased, underscored and symbolized into ruby sweetness.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


40
41
42
# 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) ⇒ String

Returns a string with its trailing "s" vaporized.

Parameters:

  • s (String)

Returns:

  • (String)


32
33
34
# File 'lib/oanda_api/utils/utils.rb', line 32

def self.singularize(s)
  s.to_s.chomp("s")
end

.stringify_keys(hash) ⇒ Hash

Returns a deep copy of a hash with its keys camelized, underscored and symbolized.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


48
49
50
# 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) {|Object| ... } ⇒ Hash

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

Parameters:

  • value (Object)

    can be a Hash, an Array or scalar object type.

  • block (Block)

    transforms the yielded key.

Yields:

  • (Object)

    key the key to be prettied up.

Returns:

  • (Hash)

    a deep copy of the hash with it's keys transformed according to the design of the block.



63
64
65
66
67
68
69
70
71
72
# 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, &block) ⇒ Hash

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

Parameters:

  • value (Object)

    can be a Hash, an Array or scalar object type.

  • key (Object) (defaults to: nil)
  • block (Block)

    transforms the yielded value.

Returns:

  • (Hash)

    a deep copy of the hash with it's values transformed according to the design of the block.



86
87
88
89
90
91
92
93
94
95
# 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) ⇒ String

Converts a string from camelCase to snake_case.

Parameters:

  • s (String)

Returns:

  • (String)


100
101
102
103
104
105
106
# 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