Class: Rack::Queries::App::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/queries/app.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Thought about refactoring this to split it out into multiple objects, but then thought better of it. If we end up adding more API endpoints then we can do something smart about it, but for now it’s fine. rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/queries/app.rb', line 12

def call(env)
  return not_found unless env[REQUEST_METHOD]

  case Utils.unescape(env[PATH_INFO])
  when %r{\A/queries/(.+)/opts/(.+)\z}i
    values = Cache.opts_for($1, $2)
    values ? json(values: values) : not_found
  when %r{\A/queries/(.+)\z}i
    query = Cache.query_for($1)
    return not_found unless query

    params = Request.new(env).params
    json(results: query.new.run(params))
  when '/queries'
    json(queries: Cache.queries)
  else
    not_found
  end
end