class Rack::Queries::App::Controller

Public Instance Methods

call(env) click to toggle source

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

# 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

Private Instance Methods

json(body) click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength

# File lib/rack/queries/app.rb, line 36
def json(body)
  [200, { 'Content-Type' => 'application/json' }, [JSON.dump(body)]]
end
not_found() click to toggle source
# File lib/rack/queries/app.rb, line 40
def not_found
  [404, { 'Content-Type' => 'text/plain' }, ['Not Found']]
end