Class: KingSoa::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/king_soa/rack/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



4
5
6
# File 'lib/king_soa/rack/middleware.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#authenticated?(service, key) ⇒ Boolean

TODO raise and rescue specific error

Returns:

  • (Boolean)


47
48
49
# File 'lib/king_soa/rack/middleware.rb', line 47

def authenticated?(service, key)
  raise "Please provide a valid authentication key" unless service.auth == key
end

#call(env) ⇒ Object

Takes incoming soa requests and calls the passed in method with given params



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/king_soa/rack/middleware.rb', line 9

def call(env)
#      Hoth::Logger.debug "env: #{env.inspect}"
  if env["PATH_INFO"] =~ /^\/soa/
    begin
      req = Rack::Request.new(env)
      # find service
      service = KingSoa.find(req.params["name"])
      #  TODO rescue service class not found
      raise "The service: #{req.params["name"]} could not be found" unless service
      # authenticate
      authenticated?(service, req.params["auth"])
      # perform method with decoded params
      result = service.perform(*service.decode( req.params["args"] ))
      # encode result
      encoded_result = service.encode({"result" => result})
      # and return
      [
        200,
        {'Content-Type' => 'application/json', 'Content-Length' => "#{encoded_result.length}"},
        [encoded_result] 
      ]

    rescue Exception => e
      #Hoth::Logger.debug "e: #{e.message}"
      if service
        encoded_error = service.encode({"error" => e})
        [500, {'Content-Type' => 'application/json', 'Content-Length' => "#{encoded_error.length}"}, [encoded_error]]
      else
        encoded_error = {"error" => "An error occurred => #{e.message}"}.to_json
        [500, {'Content-Type' => "application/json", 'Content-Length' => "#{encoded_error.length}"}, [encoded_error]]
      end
    end
  else
    @app.call(env)
  end
end