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



125
126
127
128
# File 'lib/pinch_hitter/service/replay_ws.rb', line 125

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

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



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

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

#request_headersObject



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

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

#requests(endpoint) ⇒ Object



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

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
97
98
99
100
101
# 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)
  if @@handlers.handler_for?(endpoint)
    message = @@handlers.respond_to(endpoint, body, request, response)
    if message
      content_type determine_content_type message
      message
    else
      status 404
    end
  else
    status 404
  end
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



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

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

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