Class: Colloquy::Server
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- Colloquy::Server
- Defined in:
- lib/colloquy/server.rb
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Server
constructor
Create an instance of Colloquy::Renderer with given options and set’s it up.
-
#response(env) ⇒ Array
This methods overrides #response of Goliath::API.
Constructor Details
Instance Method Details
#response(env) ⇒ Array
This methods overrides #response of Goliath::API. So this is where parameters are validated and fed to an instance of Renderer. This methods returns response array with a http 200 status and body on successful execution.
It validates the incoming request by checking presence of flow, msisdn and session_id. It sanitizes the parameters and obtains a hash containing flow, msisdn, session_id and input. Any exception raised at this point is rescued and a default error message is stored in response.
After validation params are passed on the #apply method of instance of Renderer which was created and configured in Server constructor.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/colloquy/server.rb', line 33 def response(env) response = Colloquy::Response.new begin parameters = {} parameters = validate_request(env) parameters = sanitize_parameters(parameters) logger.debug "REQUEST flow: #{parameters[:flow_name]}, msisdn: #{parameters[:msisdn]}, \ session_id: #{parameters[:session_id]}, input: #{parameters[:input]}, other: #{parameters[:params].inspect}" rescue Exception => e logger.error "Exception #{e.inspect} when trying to validate request flow: #{parameters[:flow_name]}, \ msisdn: #{parameters[:msisdn]}, session_id: #{parameters[:session_id]}, input: #{parameters[:input]}" logger.debug "#{e.backtrace.inspect}" logger.info 'Responding with default error message' response = Colloquy::Response.new(Colloquy::Renderer::DEFAULT_ERROR_MESSAGE) response.flow_state = :notify end response = @renderer.apply(parameters[:flow_name], parameters[:msisdn], parameters[:session_id], parameters[:input], parameters[:params]) if response.empty? body = case parameters[:params][:accept] when 'text/plain' response.to_s else Yajl.dump({ response: response, flow_state: response.flow_state }) end [200, {}, body] end |