Class: Zaypay::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/zaypay/util.rb

Overview

A class containing some utility methods.

Class Method Summary collapse

Class Method Details

.arrayify_if_not_an_array(obj) ⇒ Object

Wraps something in an array if it is not an array



46
47
48
# File 'lib/zaypay/util.rb', line 46

def self.arrayify_if_not_an_array(obj)
  obj.is_a?(Array) ? obj : [obj]
end

.stringify_locale_hash(locale_hash) ⇒ String

Turns a hash with :language and :country keys to a string that represents the locale

Example:

Zaypay::Util.stringify_locale_hash( { :country=>"NL", :language=>"nl" } ) 
=> 'nl-NL'

Parameters:

  • locale_hash (Hash)

    a hash that represents the locale

Returns:

  • (String)

    a string representing the locale in the format “language-country”



41
42
43
# File 'lib/zaypay/util.rb', line 41

def self.stringify_locale_hash(locale_hash)
  locale_hash[:language] << '-' << locale_hash[:country]
end

.uber_symbolize(data) ⇒ Object

Symbolize keys recursively - Not just for hashes within a Hash, but also for hashes within an Array.

Example:

hashie = { 'a' => 'A', 'b' => ['B'], 'c' => [ {'cc' => 'CC'} ], 'd' => { 'e' => 'E', :f => { :ff => 'FF' } } }
Zaypay::Util.uber_symbolize(hashie)

=> { :a => "A", :b => ["B"], :c => [ {:cc=>"CC"} ], :d => { :e => "E", :f => { :ff => "FF" } } }

Parameters:

  • data

    A hash or an array that you want to symbolize recursively

Returns:

  • A hash or an array that has been symbolized recursively for you



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zaypay/util.rb', line 17

def self.uber_symbolize(data)
  if data.is_a?(Hash)
    data.keys.each do |key|
      data[(key.to_sym rescue key) || key] = data.delete(key)
    end
    data.values.each do |v|
      Zaypay::Util.uber_symbolize(v)
    end
  end
  if data.is_a?(Array)
    data.each{|e| Zaypay::Util.uber_symbolize(e)}
  end
  data
end