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

#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



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

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

.find(port) ⇒ Object



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

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

  false
end

Instance Method Details

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mountebank/imposter.rb', line 69

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



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

def delete!
  Imposter.delete(@port)
end

#reloadObject



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

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

  self
end

#replayable_dataObject



87
88
89
90
91
92
93
# File 'lib/mountebank/imposter.rb', line 87

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



95
96
97
# File 'lib/mountebank/imposter.rb', line 95

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