Sharing is Caring – Sharing filters between applications

We’ve been creating a set of Rack applications that we are tying together using Padrino.

Each application represents an API endpoint for a set of related services on a distinct resource, e.g. /customers or /invoices etc.

This is nice. But we still need to share some common API-level functions, such as request timing etc. between the all applications. This means for every API call in every application we want to log the time the request started at, store the time in the request and then log the elapsed time when the request ended. So:

  • Before filter: Log time, store in request as @start_time
  • After filter: Read @start_time from request and log the elapsed time between it and now

Now that we had this, how could we apply this to all applications? In other words, how do you avoid plastering these before and after filters into every application?

Well we were already creating all applications as a sub-class of BaseApp – mostly in order to share settings. This was working great, we could set lots of configuration up in the BaseApp class and the CustomerApp class would just inherit it, and override it if required. But you can’t use it to share the filters, because filters are not inherited.

I posted this to the Padrino Google Groups mailing list, and one of the Padrino maintainers, Davide D’Agostino, kindly helped me out via this StackOverflow post.

It worked great. The Ruby community to the rescue again!

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s