Questions tagged [rack-middleware]

Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby.

66 questions
42
votes
8 answers

How to determine if Rails is running from CLI, console or as server?

I have a middleware for announcing my application on the local network app using Bonjour, but it's also announcing the service when Rails is invoked from rake or through the console. I'd like to exclude these cases, and only use the Bonjour…
crishoj
  • 5,660
  • 4
  • 32
  • 31
27
votes
4 answers

Testing Middleware with Rspec

I've written some Rack-Middleware and now I'm trying to test it with Rspec. But all Rack-Middleware is instantiated with an 'app' argument, that represents the Rails app itself. How do you guys mock this up in Rspec? For example, describe…
User314159
  • 7,733
  • 9
  • 39
  • 63
21
votes
5 answers

What is middleware when referenced in the context of Ruby on Rails?

I often hear the term 'middleware' in the context of Ruby on Rails. What exactly is it? Can you provide specific examples?
randombits
  • 47,058
  • 76
  • 251
  • 433
21
votes
1 answer

"use" keyword/word in Ruby/Rails/Rack code

Recently I happened to see this word in Ruby code, use, when I was going through some code related to goliath, middleware etc. Looks like it is different from include/extend, and require. Can somebody explain why this use keyword exists, and how it…
Amol Pujari
  • 2,280
  • 20
  • 42
20
votes
4 answers

Adding a custom middleware to Rails 4

I have a Rails 4 sample project (Blog) and I have created a simple middleware called 'request_timer' in config/initializers/request_timer.rb #config/initializers/request_timer.rb class RequestTimer …
sameera207
  • 16,547
  • 19
  • 87
  • 152
16
votes
2 answers

How to rescue ActionDispatch::ParamsParser::ParseError and return custom API error in rails 5?

Whenever sending malformed JSON against my API-only Rails 5.x application, I get an exception and Rails is returning the entire stack trace as JSON. Obviously I'd like to respond with a nicely, custom, formatted error. => Booting Puma => Rails…
nakwa
  • 1,157
  • 1
  • 13
  • 25
15
votes
1 answer

How to use Middleware from an Engine in an Engine

Because of how the different gems interact in my system, I have an engine mounted onto a rails application. I recently started working on a new gem that provides some middleware functionality. Sort of like this: BaseApp \ Engine \ …
SortingHat
  • 727
  • 3
  • 15
  • 30
12
votes
4 answers

Reloading rails middleware without restarting the server in development

I have a rails 4 app with middleware located at lib/some/middleware.rb which is currently injected into the stack though an initializer like so: MyApp::Application.configure.do |config| config.middleware.use…
10
votes
1 answer

Alter response.body in Rack Middleware

I'm trying to write some Rack Middleware for a Rails 4.2 app that alters the response body using the gsub method. I found older examples that use a pattern like this: class MyMiddleware def initialize(app) @app = app end def call(env) …
rb-
  • 2,315
  • 29
  • 41
10
votes
2 answers

RoR - Which is preferred - Rack Middleware or Active Controller Filters?

For the latest version of Ruby on Rails (4 at the time of asking this question), what is the preferred way to implement code that modifies the request/response such as an authentication mechanism. I see many sites and tutorials advocating Rack…
8
votes
3 answers

ActiveAdmin: How to setup HTTP basic authentication?

I want to set basic authentication for ActiveAdmin, which internal devise solution doesn't apply to my case. For that I would like to be able to add middleware to the ActiveAdmin Engine before this is bundled into my app. What I managed to do…
ChuckE
  • 5,610
  • 4
  • 31
  • 59
7
votes
1 answer

ignore Rack::Timeout middle in specific rails routes

I have installed rack-timeout gem and created rack_timeout.rb inside the initializers.I have set Rack::Timeout.service_timeout = 1 inside rack_timeout.rb.I want to restrict the rack-timeout to halt the execution if timeout exceeded in controllers…
Luna Das
  • 103
  • 5
6
votes
0 answers

How to dynamically change the session cookie name in Rails?

I have a Ruby on Rails 4.2 multi-tenant application where I want each tenant to use it's own session cookie and cookie options. I tried different approaches but I can't get every detail right. To add more context, tenant is selected from the host…
6
votes
2 answers

Hello World rack middleware with rails 3: how to process body of all requests

i want to try out a simple rack middleware "hello world", but i seem to get stuck. it looks like the main sytax changed, since some examples use this code: require 'rack/utils' class FooBar def initialize(app) @app = app end def…
Hank Probes
  • 61
  • 1
  • 3
6
votes
2 answers

How to add middleware in Rails 4.2 application

I am trying to learn Middlewares and been practising how to mount it in the Rails application. I have followed the railscast So far I have implemented these steps: 1) Created a new Rails 4.2 application called: Blog 2) Added a file in the lib folder…
1
2 3 4 5