faraday_bang 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +61 -0
- data/Rakefile +9 -0
- data/faraday_bang.gemspec +26 -0
- data/lib/faraday_bang/bang.rb +23 -0
- data/lib/faraday_bang/errors.rb +13 -0
- data/lib/faraday_bang/json.rb +7 -0
- data/lib/faraday_bang/version.rb +3 -0
- data/lib/faraday_bang.rb +6 -0
- data/test/lib/faraday_bang/bang_test.rb +61 -0
- data/test/lib/faraday_bang/errors_test.rb +14 -0
- data/test/lib/faraday_bang/json_test.rb +11 -0
- data/test/test_helper.rb +16 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8c86ab07973616e783fc90b028611e2553f8d9cd
|
4
|
+
data.tar.gz: c954035229658542ea9be28c378bac598a405704
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbf870ac82be8668c9bd567385bc64c0ac77615017ecbdb48b0f07f7b0c756d7131bcfedac3d3095d19018dd473d66b7df3b7f19a8ae1fede9c83d5b56f50d45
|
7
|
+
data.tar.gz: 090a6094059669e372f5e8672e8596ec0a1adc036d970f727432f4a669e0ee50c4a030cc739867033c2ed964df2733a6a961ce4ed26467a6f316a15b4a2232d0
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Mark Bates
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Faraday!
|
2
|
+
[](https://travis-ci.org/markbates/faraday_bang) [](https://codeclimate.com/github/markbates/faraday_bang)
|
3
|
+
|
4
|
+
[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).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'faraday_bang'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
```bash
|
23
|
+
$ gem install faraday_bang
|
24
|
+
```
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Using Faraday! is just as easy as using Faraday, except all you have to do is add a `!` to the Faraday methods.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
response = Faraday.get!('http://sushi.com/nigiri/sake.json')
|
32
|
+
```
|
33
|
+
|
34
|
+
If a response is not successful, then a `Faraday::Bang::ResponseError` error will be raised.
|
35
|
+
|
36
|
+
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:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Faraday::Bang::Response401Error
|
40
|
+
Faraday::Bang::Response404Error
|
41
|
+
Faraday::Bang::Response500Error
|
42
|
+
Faraday::Bang::Response503Error
|
43
|
+
```
|
44
|
+
|
45
|
+
## But Wait! There's More!
|
46
|
+
|
47
|
+
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.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
response = Faraday.get!('http://example.com/example.json')
|
51
|
+
json = response.as_json
|
52
|
+
puts json # => {"name"=>"Mark"}
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( http://github.com/<my-github-username>/faraday_bang/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'faraday_bang/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "faraday_bang"
|
8
|
+
spec.version = FaradayBang::VERSION
|
9
|
+
spec.authors = ["Mark Bates"]
|
10
|
+
spec.email = ["mark@markbates.com"]
|
11
|
+
spec.summary = %q{Adds error raising ! methods to Farday.}
|
12
|
+
spec.description = %q{Adds error raising ! methods to Farday.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "faraday"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "mocha"
|
25
|
+
spec.add_development_dependency "minitest", '>=5.2.3'
|
26
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Faraday::Bang
|
2
|
+
ERROR_CODES = [(400..417).to_a, (500..505).to_a].flatten
|
3
|
+
|
4
|
+
Faraday::Connection::METHODS.each do |verb|
|
5
|
+
define_method("#{verb}!") do |*args|
|
6
|
+
response = self.send(verb, *args)
|
7
|
+
if response.status >= 400
|
8
|
+
err_name = "Response#{response.status}Error"
|
9
|
+
if Faraday::Bang.const_defined?(err_name)
|
10
|
+
klass = Faraday::Bang.const_get(err_name)
|
11
|
+
raise klass.new(response)
|
12
|
+
else
|
13
|
+
raise Faraday::Bang::ResponseError.new(response)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
return response
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
Faraday.extend(Faraday::Bang)
|
23
|
+
Faraday::Connection.send(:include, Faraday::Bang)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Faraday::Bang::ResponseError < StandardError
|
2
|
+
attr_reader :response
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
super("Error making request to #{response.env.url} (#{response.status})")
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
Faraday::Bang::ERROR_CODES.each do |code|
|
12
|
+
Faraday::Bang.const_set("Response#{code}Error", Class.new(Faraday::Bang::ResponseError))
|
13
|
+
end
|
data/lib/faraday_bang.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Faraday::Bang do
|
4
|
+
|
5
|
+
let(:url) { 'http://example.com' }
|
6
|
+
let(:args) { {a: 'b'} }
|
7
|
+
let(:body) { {foo: 'bar'}.to_json }
|
8
|
+
|
9
|
+
[Faraday, Faraday.new].each do |klass|
|
10
|
+
|
11
|
+
Faraday::Connection::METHODS.each do |verb|
|
12
|
+
|
13
|
+
describe "##{verb}!" do
|
14
|
+
|
15
|
+
let(:response) { mock(body: body, status: 200) }
|
16
|
+
|
17
|
+
before do
|
18
|
+
klass.expects(verb).with(url, args).returns(response)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'successful response' do
|
22
|
+
|
23
|
+
it 'passes the call onto Faraday' do
|
24
|
+
res = klass.send("#{verb}!", url, args)
|
25
|
+
res.body.must_equal body
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'unsuccessful response' do
|
31
|
+
|
32
|
+
let(:env) { OpenStruct.new(url: 'http://example.com') }
|
33
|
+
let(:response) { OpenStruct.new(body: body, status: 506, env: env) }
|
34
|
+
|
35
|
+
it 'raises an error if its a non-200 response without an explicit exception' do
|
36
|
+
->{klass.send("#{verb}!", url, args)}.must_raise Faraday::Bang::ResponseError, body
|
37
|
+
end
|
38
|
+
|
39
|
+
Faraday::Bang::ERROR_CODES.each do |code|
|
40
|
+
|
41
|
+
context code do
|
42
|
+
|
43
|
+
let(:response) { OpenStruct.new(body: body, status: code, env: env) }
|
44
|
+
|
45
|
+
it "raises a Faraday::Bang::Response#{code}Error" do
|
46
|
+
->{klass.send("#{verb}!", url, args)}.must_raise Faraday::Bang.const_get("Response#{code}Error")
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Faraday::Bang::ResponseError do
|
4
|
+
|
5
|
+
let(:env) { OpenStruct.new(url: 'http://example.com') }
|
6
|
+
let(:response) { OpenStruct.new(status: 500, env: env) }
|
7
|
+
let(:error) { Faraday::Bang::ResponseError.new(response) }
|
8
|
+
|
9
|
+
it 'has a useful error message' do
|
10
|
+
error.response.must_equal response
|
11
|
+
error.message.must_equal "Error making request to http://example.com (500)"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
require 'faraday_bang' # and any other gems you need
|
4
|
+
|
5
|
+
require 'ostruct'
|
6
|
+
require 'minitest/autorun'
|
7
|
+
require "mocha/setup"
|
8
|
+
require 'mocha/mini_test'
|
9
|
+
|
10
|
+
class MiniTest::Spec
|
11
|
+
|
12
|
+
class << self
|
13
|
+
alias :context :describe
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday_bang
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Bates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mocha
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 5.2.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 5.2.3
|
83
|
+
description: Adds error raising ! methods to Farday.
|
84
|
+
email:
|
85
|
+
- mark@markbates.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- faraday_bang.gemspec
|
97
|
+
- lib/faraday_bang.rb
|
98
|
+
- lib/faraday_bang/bang.rb
|
99
|
+
- lib/faraday_bang/errors.rb
|
100
|
+
- lib/faraday_bang/json.rb
|
101
|
+
- lib/faraday_bang/version.rb
|
102
|
+
- test/lib/faraday_bang/bang_test.rb
|
103
|
+
- test/lib/faraday_bang/errors_test.rb
|
104
|
+
- test/lib/faraday_bang/json_test.rb
|
105
|
+
- test/test_helper.rb
|
106
|
+
homepage: ''
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.2.2
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Adds error raising ! methods to Farday.
|
130
|
+
test_files:
|
131
|
+
- test/lib/faraday_bang/bang_test.rb
|
132
|
+
- test/lib/faraday_bang/errors_test.rb
|
133
|
+
- test/lib/faraday_bang/json_test.rb
|
134
|
+
- test/test_helper.rb
|