Class: Holoserve::Interface::Fake

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/holoserve/interface/fake.rb

Defined Under Namespace

Classes: NoResponseError

Instance Method Summary collapse

Instance Method Details

#response(env) ⇒ Object



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
45
46
47
48
49
50
51
52
53
54
# File 'lib/holoserve/interface/fake.rb', line 19

def response(env)
  request = Holoserve::Request::Decomposer.new(env, env["parameters"]).hash
  finder = Holoserve::Pair::Finder.new(pairs, request)
  pair = finder.pair
  if pair
    id, request_variant, responses = finder.id, finder.variant, pair[:responses]

    selector = Holoserve::Response::Selector.new responses, state.merge(:request_variant => request_variant), logger
    response_variant = selector.selection

    response = if response_variant == :default
      responses[:default] || { }
    elsif response_variant
      Holoserve::Tool::Merger.new(responses[:default] || { }, responses[response_variant]).result
    else
      raise NoResponseError, id, request_variant
    end
    Holoserve::State::Updater.new(state, response[:transitions]).perform
    history << {:id => id, :request_variant => request_variant, :response_variant => response_variant}

    Holoserve::Interface::Event.send_pair_event id
    logger.info "handled request [#{id}] with request variant [#{request_variant}] and response variant [#{response_variant}]"

    Holoserve::Response::Composer.new(response).response_array
  else
    bucket << request
    Holoserve::Interface::Event.send_bucket_event request
    logger.error "received unhandled request\n" + request.pretty_inspect

    not_found
  end
rescue NoResponseError => error
  logger.warn "could not select any response for request [#{error.id}] with request variant [#{error.request_variant}]"
rescue Object => error
  p error
end