Module: Fog::Mock

Defined in:
lib/fog/core/mock.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.delayObject

Returns the value of attribute delay.



23
24
25
# File 'lib/fog/core/mock.rb', line 23

def delay
  @delay
end

Class Method Details

.not_implemented(message = "Contributions welcome!") ⇒ Object



31
32
33
# File 'lib/fog/core/mock.rb', line 31

def self.not_implemented(message = "Contributions welcome!")
  raise Fog::Errors::MockNotImplemented, message
end

.random_base64(length) ⇒ Object



51
52
53
54
55
56
# File 'lib/fog/core/mock.rb', line 51

def self.random_base64(length)
  random_selection(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    length
  )
end

.random_hex(length) ⇒ Object



58
59
60
61
# File 'lib/fog/core/mock.rb', line 58

def self.random_hex(length)
  max = ("f" * length).to_i(16)
  rand(max).to_s(16).rjust(length, "0")
end

.random_ip(opts = { :version => :v4 }) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/core/mock.rb', line 35

def self.random_ip(opts = { :version => :v4 })
  version = opts[:version]
  if version == :v6
    bit_length = 128
    family = Socket::AF_INET6
  elsif version == :v4
    bit_length = 32
    family = Socket::AF_INET
  else
    raise ArgumentError, "Unknown IP version: #{version}"
  end

  seed = 1 + rand((2**bit_length) - 1)
  IPAddr.new(seed, family).to_s
end

.random_letters(length) ⇒ Object



63
64
65
66
67
68
# File 'lib/fog/core/mock.rb', line 63

def self.random_letters(length)
  random_selection(
    "abcdefghijklmnopqrstuvwxyz",
    length
  )
end

.random_letters_and_numbers(length) ⇒ Object



75
76
77
78
79
80
# File 'lib/fog/core/mock.rb', line 75

def self.random_letters_and_numbers(length)
  random_selection(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    length
  )
end

.random_numbers(length) ⇒ Object



70
71
72
73
# File 'lib/fog/core/mock.rb', line 70

def self.random_numbers(length)
  max = ("9" * length).to_i
  rand(max).to_s
end

.random_selection(characters, length) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/fog/core/mock.rb', line 82

def self.random_selection(characters, length)
  selection = ""
  length.times do
    position = rand(characters.length)
    selection << characters[position..position]
  end
  selection
end

.resetObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/core/mock.rb', line 91

def self.reset
  mocked_services = []
  Fog.constants.map do |x|
    next if Fog.autoload?(x)
    x_const = Fog.const_get(x)
    x_const.respond_to?(:constants) && x_const.constants.map do |y|
      next if x_const.autoload?(y)
      y_const = x_const.const_get(y)
      y_const.respond_to?(:constants) && y_const.constants.map do |z|
        next if y_const.autoload?(z)
        mocked_services << y_const.const_get(z) if z.to_sym == :Mock
      end
    end
  end

  mocked_services.each do |mocked_service|
    next unless mocked_service.respond_to?(:reset)
    mocked_service.reset
  end
end