Class: Killbill::Plugin::ActiveMerchant::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/killbill/helpers/active_merchant/utils.rb

Defined Under Namespace

Classes: BoundedLRUCache, KBWiredumpDevice, LazyEvaluator

Constant Summary collapse

BASE62 =

Use base 62 to be safe

('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a

Class Method Summary collapse

Class Method Details

.compact_uuid(uuid) ⇒ Object



10
11
12
13
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 10

def self.compact_uuid(uuid)
  uuid = uuid.gsub(/-/, '')
  uuid.hex.base(62).map { |i| BASE62[i].chr } * ''
end

.first_private_ipv4Object



26
27
28
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 26

def self.first_private_ipv4
  Socket.ip_address_list.detect { |intf| intf.ipv4_private? }
end

.first_public_ipv4Object



30
31
32
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 30

def self.first_public_ipv4
  Socket.ip_address_list.detect { |intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private? }
end

.ipObject



22
23
24
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 22

def self.ip
  first_public_ipv4 ? first_public_ipv4.ip_address : first_private_ipv4.ip_address
end

.normalize(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 45

def self.normalize(value)
  case value.respond_to?(:strip) ? value.strip : value
    when 'true' then true
    when :true then true
    when 'yes' then true
    when :yes then true
    when 'false' then false
    when :false then false
    when 'no' then false
    when :no then false
    when '' then nil
    when 'null' then nil
    else value
  end
end

.normalize_property(properties, key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 61

def self.normalize_property(properties, key)
  return if key.nil?

  (properties || []).each do |property|
    # Be friendly with Java-style conventions
    if property.key == key || property.key == key.to_s.camelize(false)
      property.value = normalize(property.value)
    end
  end
end

.normalized(options, key) ⇒ Object

Note: we assume options only contains keys as symbols



35
36
37
38
39
40
41
42
43
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 35

def self.normalized(options, key)
  if !options.has_key?(key)
    # Be friendly with Java-style conventions
    camelized_key = key.to_s.camelize(false).to_sym
    options.has_key?(camelized_key) ? normalize(options[camelized_key]) : nil
  else
    normalize(options[key])
  end
end

.unpack_uuid(base62_uuid) ⇒ Object



15
16
17
18
19
20
# File 'lib/killbill/helpers/active_merchant/utils.rb', line 15

def self.unpack_uuid(base62_uuid)
  as_hex     = base62_uuid.split(//).inject(0) { |i, e| i*62 + BASE62.index(e[0]) }
  no_hyphens = "%x" % as_hex
  no_hyphens = '0' * (32 - no_hyphens.size) + no_hyphens
  no_hyphens.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-")
end