Extra Faraday Middleware! Geared towards a service oriented architecture.
Here is an overview of my favorite stack. More information about each middleware is below.
APP_VERSION = IO.popen(["git", "rev-parse", "HEAD", :chdir => Rails.root]).read.chomp connection = .new(url: "http://widgets.yourapp.com") do |faraday| # provided by Faraday itself faraday.token_auth "secret" # provided by this gem faraday.use :extended_logging, logger: Rails.logger # provided by Faraday faraday.request :multipart faraday.request :url_encoded # provided by this gem faraday.request :user_agent, app: "MarketingSite", version: APP_VERSION faraday.request :request_id # provided by faraday_middleware faraday.response :json, content_type: /\bjson$/ faraday.adapter .default_adapter end
You should also take a look at faraday_middleware.
More middleware to come!
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install faraday-conductivity
Here is an overview of the middleware included in this gem.
Provides pretty logging, allowing you to inspect every detail of the request and response.
connection = .new(url: "http://widgets.yourapp.com") do |faraday| faraday.use :extended_logging, logger: Rails.logger end
Pass on a request ID from your frontend applications to your backend services. This allows for tracking requests over multiple services. Use this in combination with something like the Rails tagged logger and you'll always know what triggered something to happen in your application.
It works by trying to find the request id in
connection = .new(url: "http://widgets.yourapp.com") do |faraday| faraday.request :request_id end
In order for this to work, you need to make the Request ID globally available. To do this in Rails:
class Application < ActionController::Base before_filter :::: end
It's a hack, but it works really well.
Which application, on which server, made this request? With this middleware you know! It sets the User-Agent string based on the user, pid and hostname.
connection = .new(url: "http://widgets.yourapp.com") do |faraday| faraday.request :user_agent, app: "MarketingSite", version: "1.1" end
The User-Agent will looks like this on my machine:
MarketingSite/1.1 (iain.local; iain; 30360) ruby/1.9.3 (327; x86_64-darwin12.2.0)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request