Faraday is a great HTTP library, but it would be even better if had ! versions of all it's methods to raise errors when the request does not receive a successful response. Enter Farady! (pronounced Faraday Bang).
Add this line to your application's Gemfile:
gem 'faraday_bang'
And then execute:
$ bundle
Or install it yourself as:
$ gem install faraday_bang
Using Faraday! is just as easy as using Faraday, except all you have to do is add a !
to the Faraday methods.
response = Faraday.get!('http://sushi.com/nigiri/sake.json')
If a response is not successful, then a Faraday::Bang::ResponseError
error will be raised.
Each of the HTTP error status codes have been given their own sub-class to make catching them even easier. Here are a few examples:
Faraday::Bang::Response401Error
Faraday::Bang::Response404Error
Faraday::Bang::Response500Error
Faraday::Bang::Response503Error
As a special added bonus an as_json
method has been added to Faraday::Response
to make it easier to JSON bodies back as a Ruby Hash.
response = Faraday.get!('http://example.com/example.json')
json = response.as_json
puts json # => {"name"=>"Mark"}
- Fork it ( http://github.com//faraday_bang/fork )
- 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