All Projects → CultureHQ → rack-queries

CultureHQ / rack-queries

Licence: MIT license
A page in your rack-based application that allows quick execution of pre-built queries

Programming Languages

ruby
36898 projects - #4 most used programming language
typescript
32286 projects
CSS
56736 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Labels

Projects that are alternatives of or similar to rack-queries

Prax.cr
Rack proxy server for development (Crystal port)
Stars: ✭ 142 (+468%)
Mutual labels:  rack
Split
📈 The Rack Based A/B testing framework
Stars: ✭ 2,539 (+10056%)
Mutual labels:  rack
rack-reproxy
Transparently proxy Rack responses from a backend URL. Great for private access to an internal service or to route authenticated requests through intermediate caching layers.
Stars: ✭ 20 (-20%)
Mutual labels:  rack
Sham rack
run Rack applications in-process, without a server
Stars: ✭ 169 (+576%)
Mutual labels:  rack
Bugsnag Ruby
Bugsnag error monitoring & reporting software for rails, sinatra, rack and ruby
Stars: ✭ 211 (+744%)
Mutual labels:  rack
Rack Reducer
Declaratively filter data via URL params, in any Rack app, with any ORM.
Stars: ✭ 241 (+864%)
Mutual labels:  rack
Crepe
The thin API stack.
Stars: ✭ 120 (+380%)
Mutual labels:  rack
rack-cargo
🚚 Batch requests for Rack apps (works with Rails, Sinatra, etc)
Stars: ✭ 17 (-32%)
Mutual labels:  rack
Controller
Complete, fast and testable actions for Rack and Hanami
Stars: ✭ 221 (+784%)
Mutual labels:  rack
tipi
Tipi - the All-in-one Web Server for Ruby Apps
Stars: ✭ 214 (+756%)
Mutual labels:  rack
Rack csrf
Anti-CSRF Rack middleware
Stars: ✭ 169 (+576%)
Mutual labels:  rack
Trashed
Tell StatsD about request time, GC, objects and more. Latest Rails 4 and Ruby 2.1 support, and ancient Rails 2 and Ruby 1.8 support.
Stars: ✭ 188 (+652%)
Mutual labels:  rack
rack-session-smart cookie
Slightly smarter session cookies for Rack 2 apps
Stars: ✭ 43 (+72%)
Mutual labels:  rack
Sinatra
Classy web-development dressed in a DSL (official / canonical repo)
Stars: ✭ 11,497 (+45888%)
Mutual labels:  rack
dyndnsd
A small, lightweight and extensible DynDNS server written with Ruby and Rack.
Stars: ✭ 69 (+176%)
Mutual labels:  rack
Vite ruby
⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
Stars: ✭ 112 (+348%)
Mutual labels:  rack
Plezi
Plezi - the Ruby framework for realtime web-apps, websockets and RESTful HTTP
Stars: ✭ 239 (+856%)
Mutual labels:  rack
itop-datacenter-view
Extension for iTop: Easily manage & visualize your racks, enclosures and datacenter devices.
Stars: ✭ 24 (-4%)
Mutual labels:  rack
grape-jwt-authentication
A reusable Grape JWT authentication concern
Stars: ✭ 31 (+24%)
Mutual labels:  rack
Slide
MasahiroSaito's Slides Repository
Stars: ✭ 12 (-52%)
Mutual labels:  rack

Rack::Queries

Build Status Gem Version

This gem provides a page in your rack-based (e.g., Rails, Sinatra) application that allows quick execution of pre-built queries. The goal is to allow quick insights into the state of your application without needing to update the main UI. Consider it a backdoor admin page that you can use before you decide to truly expose query results.

Screenshot

Usage

First, add rack-queries to your Gemfile and bundle install. Then, mount the Rack::Queries::App application within your app.

Within Rails, that will look like (within config/routes.rb):

Rails.application.routes.draw do
  mount Rack::Queries::App, at: '/queries'
end

Then, you can go to /queries within your application to view the empty queries page.

Second, define the queries that you want included on your query page. Queries are classes that respond to run(opts). The run method should return either a single value or an array of arrays (in the UI it will either display one value or a table). The following example returns an overall count:

class UserCountQuery
  def run(_opts)
    User.count
  end
end

If you want your queries to have arguments (say for instance that users can be scoped to organizations), you define public instance methods on your query class:

class UserPerOrgCountQuery
  def org
    Org.order(:name).pluck(:name)
  end

  def run(opts)
    Org.where(name: opts['org']).users.count
  end
end

Each public instance method is expected to return an array of options (they get transformed into select tags in the UI). They are then given to the run method through the opts hash which contains the value within a string key corresponding to the method name.

Finally, inform rack-queries that you want to include the query on your query page by adding it to the list:

Rack::Queries.add(
  UserCountQuery,
  UserPerOrgCountQuery
)

Customization

Query objects can optionally have a bit of customization with regard to display through the ::name and ::desc methods. Overriding the ::name method will change the display on the right-hand side, and adding a ::desc method will add a small paragraph with a description below the name.

Query DSL

You can create queries manually as described above, or you can use a minimal DSL through the Rack::Queries::create method. To recreate the UserPerOrgCountQuery as above, you could:

Rack::Queries.create do
  name 'UserPerOrgCountQuery'
  desc 'The count of users in each organization'

  opt :org do
    Org.order(:name).pluck(:name)
  end

  run do |opts|
    Org.where(name: opts['org']).users.count
  end
end

With the DSL, you can additionally provide a type for your options that allows them to be specified on the client side. By default, the type is select, which will perform a query to the server to get the list of options. However, if you specify one of the other types (string or text), it will instead allow the user to provide input. For example, to allow a text area field, you would:

Rack::Queries.create do
  name 'Some CSV parsing'
  desc 'Parse some CSV input!'

  opt :csv, type: :text

  run do |opts|
    require 'csv'

    CSV.foreach(opts['csv']) do
      ...
    end

    ...
  end
end

Middleware

Since Rack::Queries is a rack application, you can add whatever middleware you like into its stack before the request hits the application. For instance, to integrate HTTP basic auth around it to protect the query results, you can use the Rack::Queries::App::use method as in:

Rack::Queries::App.use(Rack::Auth::Basic) do |username, password|
  compare = lambda { |left, right|
    ActiveSupport::SecurityUtils.secure_compare(
      ::Digest::SHA256.hexdigest(left),
      ::Digest::SHA256.hexdigest(right)
    )
  }

  credentials = Rails.application.credentials

  compare[username, credentials.rack_queries_username] &
    compare[password, credentials.rack_queries_password]
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/CultureHQ/rack-queries.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].