class Shokkenki::Consumer::Stubber::LocalServerStubber

Attributes

host[R]
interactions_path[R]
port[R]
scheme[R]
server[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 14
def initialize attributes
  @port = attributes[:port]
  @scheme = attributes[:scheme] || :http
  @host = attributes[:host] || 'localhost'
  @interactions_path = attributes[:interactions_path] || '/shokkenki/interactions'
  @unmatched_requests_path = '/shokkenki/requests/unmatched'
  @unused_interactions_path = '/shokkenki/interactions/unused'
end

Public Instance Methods

clear_interaction_stubs() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 36
def clear_interaction_stubs
  response = HTTParty.delete interactions_uri
  server.assert_ok!
  raise "Failed to clear interaction stubs: #{response.inspect}" unless successful?(response.code)
end
interactions_uri() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 86
def interactions_uri
  URI::Generic.build(server_properties.merge(:path => @interactions_path.to_s)).to_s
end
server_properties() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 78
def server_properties
  {
    :scheme => @scheme.to_s,
    :host => @host.to_s,
    :port => @port
  }
end
session_closed() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 60
def session_closed
  @server.shutdown
end
session_started() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 46
def session_started
  # Find a port as late as possible to minimise the
  # chance that it starts being used in between finding it
  # and using it.
  @port = FindAPort.available_port unless @port

  @server = Server.new(
    :app => StubServerMiddleware.new,
    :host => @host,
    :port => @port
  )
  @server.start
end
stub_interaction(interaction) click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 27
def stub_interaction interaction
  response = HTTParty.post(interactions_uri,
    :body => interaction.to_hash.to_json,
    :headers => { 'Content-Type' => 'application/json' }
  )
  server.assert_ok!
  raise "Failed to stub interaction: #{response.inspect}" unless successful?(response.code)
end
successful?(response_code) click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 42
def successful? response_code
  (200 <= response_code) &&  (response_code < 300)
end
type() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 23
def type
  :local_server
end
unmatched_requests() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 64
def unmatched_requests
  response = HTTParty.get unmatched_requests_uri
  server.assert_ok!
  raise "Failed to find unmatched requests: #{response.inspect}" unless successful?(response.code)
  JSON.parse response.body
end
unmatched_requests_uri() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 90
def unmatched_requests_uri
  URI::Generic.build(server_properties.merge(:path => @unmatched_requests_path.to_s)).to_s
end
unused_interactions() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 71
def unused_interactions
  response = HTTParty.get unused_interactions_uri
  server.assert_ok!
  raise "Failed to find unused interactions: #{response.inspect}" unless successful?(response.code)
  JSON.parse response.body
end
unused_interactions_uri() click to toggle source
# File lib/shokkenki/consumer/stubber/local_server_stubber.rb, line 94
def unused_interactions_uri
  URI::Generic.build(server_properties.merge(:path => @unused_interactions_path.to_s)).to_s
end