Class: Mountebank::Imposter
- Inherits:
-
Object
- Object
- Mountebank::Imposter
- 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
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
-
#stubs ⇒ Object
readonly
Returns the value of attribute stubs.
Class Method Summary collapse
- .build(port, protocol = PROTOCOL_HTTP, options = {}) ⇒ Object
- .create(port, protocol = PROTOCOL_HTTP, options = {}) ⇒ Object
- .delete(port) ⇒ Object
- .find(port) ⇒ Object
Instance Method Summary collapse
- #add_stub(response = nil, predicate = nil) ⇒ Object
- #delete! ⇒ Object
-
#initialize(data = {}) ⇒ Imposter
constructor
A new instance of Imposter.
- #reload ⇒ Object
- #replayable_data ⇒ Object
- #save! ⇒ Object
- #to_json(*args) ⇒ Object
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
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def matches @matches end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def mode @mode end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def name @name end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def port @port end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def protocol @protocol end |
#requests ⇒ Object (readonly)
Returns the value of attribute requests.
3 4 5 |
# File 'lib/mountebank/imposter.rb', line 3 def requests @requests end |
#stubs ⇒ Object (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, ={}) raise 'Invalid port number' unless port.is_a? Integer raise 'Invalid protocol' unless PROTOCOLS.include?(protocol) data = {port: port, protocol: protocol}.merge() 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, ={}) self.build(port, protocol, ).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 |
#reload ⇒ Object
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_data ⇒ Object
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 |