Class: PinchHitter::Service::ReplayWs

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Message::ContentType
Defined in:
lib/pinch_hitter/service/replay_ws.rb

Instance Method Summary collapse

Methods included from Message::ContentType

#determine_content_type

Methods included from Message::Json

#json_message, #valid_json?

Instance Method Details

#normalize(endpoint) ⇒ Object



120
121
122
123
# File 'lib/pinch_hitter/service/replay_ws.rb', line 120

def normalize(endpoint)
  return endpoint if endpoint =~ /^\//
  "/#{endpoint}"
end

#register_module(endpoint = '/', mod = '') ⇒ Object



98
99
100
101
# File 'lib/pinch_hitter/service/replay_ws.rb', line 98

def register_module(endpoint='/', mod='')
  endpoint = normalize(endpoint)
  @@handlers.register_module endpoint, Marshal.load(mod)
end

#request_headersObject



116
117
118
# File 'lib/pinch_hitter/service/replay_ws.rb', line 116

def request_headers
  env.select { |key, value| key.upcase == key }
end

#requests(endpoint) ⇒ Object



103
104
105
106
107
# File 'lib/pinch_hitter/service/replay_ws.rb', line 103

def requests endpoint
  endpoint = normalize(endpoint)
  content_type 'application/json'
  { requests: @@recorder.requests(endpoint) }.to_json
end

#respond(endpoint = '/', request = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pinch_hitter/service/replay_ws.rb', line 86

def respond(endpoint='/', request=nil)
  endpoint = normalize(endpoint)
  body, request = wrap(request)
  @@recorder.record(endpoint, request)
  message = @@handlers.respond_to(endpoint, body, request, response)
  if message.is_a? String
    content_type determine_content_type message
    puts "No message found for #{endpoint}" unless message
  end
  message
end

#store(endpoint = '/', message = nil) ⇒ Object



81
82
83
84
# File 'lib/pinch_hitter/service/replay_ws.rb', line 81

def store(endpoint='/', message=nil)
  endpoint = normalize(endpoint)
  @@handlers.store_message endpoint, message
end

#wrap(request) ⇒ Object



109
110
111
112
113
114
# File 'lib/pinch_hitter/service/replay_ws.rb', line 109

def wrap(request)
  return [nil, nil] unless request

  body = request.body.read
  [body, { headers: request_headers, body: body }]
end