https://github.com/markbates/faraday_bang
Adds error raising ! methods to Faraday
https://github.com/markbates/faraday_bang
Last synced: about 1 year ago
JSON representation
Adds error raising ! methods to Faraday
- Host: GitHub
- URL: https://github.com/markbates/faraday_bang
- Owner: markbates
- License: mit
- Created: 2014-04-18T19:46:36.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2015-02-06T21:27:06.000Z (over 11 years ago)
- Last Synced: 2025-05-12T18:09:16.119Z (about 1 year ago)
- Language: Ruby
- Homepage: http://www.markbates.com
- Size: 167 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Faraday!
[](https://travis-ci.org/markbates/faraday_bang) [](https://codeclimate.com/github/markbates/faraday_bang)
[Faraday](https://github.com/lostisland/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).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'faraday_bang'
```
And then execute:
```bash
$ bundle
```
Or install it yourself as:
```bash
$ gem install faraday_bang
```
## Usage
Using Faraday! is just as easy as using Faraday, except all you have to do is add a `!` to the Faraday methods.
```ruby
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:
```ruby
Faraday::Bang::Response401Error
Faraday::Bang::Response404Error
Faraday::Bang::Response500Error
Faraday::Bang::Response503Error
```
## But Wait! There's More!
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.
```ruby
response = Faraday.get!('http://example.com/example.json')
json = response.as_json
puts json # => {"name"=>"Mark"}
```
## Contributing
1. Fork it ( http://github.com//faraday_bang/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request