Class: Tom::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatcher.rb

Class Method Summary collapse

Class Method Details

.dispatch(env) ⇒ Array

Dispatches this request to all adapters that registered for the route and then calls the merger for this route to compose a response

Parameters:

  • env (Array)

    A rack env object

Returns:

  • (Array)

    Whatever merge had to say



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dispatcher.rb', line 13

def self.dispatch(env)

  # 1. hit APIs. All at the same time. Oh, mygodd!
  Tom::Log.logger.info "#{env['REQUEST_METHOD'].upcase} #{env['REQUEST_URI']}"
  responses = parallel_adapter_dispatch(env)

  # 2. merge
  merged = merge(env, responses)

  # 3. ???
  Tom::Log.logger.info "-------------------------------------------------------"

  # 4. profit
  merged
end

.merge(env, responses) ⇒ Array

Takes a request (rack env) and a couple of responses generated by api adapters and composes a response for the client.

The merger used depends on the route.

Parameters:

  • env (Array)

    A rack env object

Returns:

  • (Array)

    The merged result of all requests made as an array of status code, headers and body, e.g.

    200, {}, “Hi!”


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dispatcher.rb', line 40

def self.merge(env, responses)
  route, method = route_and_method(env)
  if merger = Tom::Routes.merger_for_route(route, method)
    Tom::Log.logger.info "Merging with:"
    Tom::Log.logger.info "  -> #{merger}"
    merged = merger.new.merge(env, responses)
  else
    merged = [404, {}, ""]
  end
  merged[1]["Adapters-Used"] = responses.keys.join(",")
  merged
end

.route_and_method(env) ⇒ Array

Extract the route/request uri and the method from a rack env

Parameters:

  • env (Hash)

    A rack env

Returns:

  • (Array)

    Contains request_path, request_method as symbols



61
62
63
64
# File 'lib/dispatcher.rb', line 61

def self.route_and_method(env)
  [env["REQUEST_PATH"],
   env["REQUEST_METHOD"].downcase.to_sym]
end