Class: Mountebank::Imposter

Inherits:
Object
  • Object
show all
Defined in:
lib/mountebank/imposter.rb

Constant Summary collapse

PROTOCOL_HTTP =
'http'
PROTOCOL_HTTPS =
'https'
PROTOCOL_SMTP =
'smtp'
PROTOCOL_TCP =
'tcp'
PROTOCOLS =
[
  PROTOCOL_HTTP,
  PROTOCOL_HTTPS,
  PROTOCOL_SMTP,
  PROTOCOL_TCP
]
CREATE_PARAMS_HTTP =
[:protocol, :port, :name, :stubs]
CREATE_PARAMS_HTTPS =
[:protocol, :port, :name, :stubs]
CREATE_PARAMS_TCP =
[:protocol, :mode, :mode, :name, :stubs]
CREATE_PARAMS_SMTP =
[:protocol, :port, :name]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Imposter

Returns a new instance of Imposter.



22
23
24
# File 'lib/mountebank/imposter.rb', line 22

def initialize(data={})
  set_attributes(data)
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def matches
  @matches
end

#modeObject (readonly)

Returns the value of attribute mode.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def protocol
  @protocol
end

#record_requestsObject (readonly)

Returns the value of attribute record_requests.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def record_requests
  @record_requests
end

#requestsObject (readonly)

Returns the value of attribute requests.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def requests
  @requests
end

#stubsObject (readonly)

Returns the value of attribute stubs.



3
4
5
# File 'lib/mountebank/imposter.rb', line 3

def stubs
  @stubs
end

Class Method Details

.build(port, protocol = PROTOCOL_HTTP, options = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/mountebank/imposter.rb', line 26

def self.build(port, protocol=PROTOCOL_HTTP, options={})
  raise 'Invalid port number' unless port.is_a? Integer
  raise 'Invalid protocol' unless PROTOCOLS.include?(protocol)

  data = {port: port, protocol: protocol}.merge(options)
  Mountebank::Imposter.new(data)
end

.create(port, protocol = PROTOCOL_HTTP, options = {}) ⇒ Object



42
43
44
# File 'lib/mountebank/imposter.rb', line 42

def self.create(port, protocol=PROTOCOL_HTTP, options={})
  self.build(port, protocol, options).save!
end

.delete(port) ⇒ Object



59
60
61
62
# File 'lib/mountebank/imposter.rb', line 59

def self.delete(port)
  response = Network.delete("/imposters/#{port}")
  response.success? && !response.body.empty?
end

.find(port) ⇒ Object



52
53
54
55
56
57
# File 'lib/mountebank/imposter.rb', line 52

def self.find(port)
  imposter_data = Imposter.get_imposter_config(port)
  return Mountebank::Imposter.new(imposter_data) unless imposter_data.empty?

  false
end

.find_allObject



46
47
48
49
50
# File 'lib/mountebank/imposter.rb', line 46

def self.find_all
  response = Network.get("/imposters")
  list = response.success? ? response.body[:imposters] : []
  list.map { |i| Mountebank::Imposter.new(i) }
end

Instance Method Details

#add_stub(response = nil, predicate = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mountebank/imposter.rb', line 75

def add_stub(response=nil, predicate=nil)
  responses, predicates = [], []

  if response.is_a? Array
    responses = response
  elsif response.is_a? Mountebank::Stub::Response
    responses << response
  end

  if predicate.is_a? Array
    predicates = predicate
  elsif predicate.is_a? Mountebank::Stub::Predicate
    predicates << predicate
  end

  @stubs << Mountebank::Stub.create(responses, predicates)
end

#delete!Object



64
65
66
# File 'lib/mountebank/imposter.rb', line 64

def delete!
  Imposter.delete(@port)
end

#reloadObject



68
69
70
71
72
73
# File 'lib/mountebank/imposter.rb', line 68

def reload
  data = Imposter.get_imposter_config(@port)
  set_attributes(data) unless data.empty?

  self
end

#replayable_dataObject



93
94
95
96
97
98
99
# File 'lib/mountebank/imposter.rb', line 93

def replayable_data
  data = serializable_hash
  data.delete(:requests)
  data.delete(:matches)

  data
end

#save!Object



34
35
36
37
38
39
40
# File 'lib/mountebank/imposter.rb', line 34

def save!
  delete!
  response = Network.post('/imposters', replayable_data)
  return reload if response.success?

  false
end

#to_json(*args) ⇒ Object



101
102
103
# File 'lib/mountebank/imposter.rb', line 101

def to_json(*args)
  serializable_hash.to_json(*args)
end