faraday-raise-errors 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 773fb7c5a1d23e615652cb8e134d7ed5fe8fb91b
4
+ data.tar.gz: 32c70ccbce201d376c895c37e2efa6325a86207b
5
+ SHA512:
6
+ metadata.gz: ae48c3207eb6470f998804fcceacd6c222f609cc89d2d2742ca7bc004d8d1d079347152579ee83d4a3470c656723b4372069ddce328d4b016dec2a6903b96904
7
+ data.tar.gz: 801377761e6b606b0261d13b06ab03c0520cb1325f394c9528d1e358a7ae13b56bf18dcef16c8c14a75e2d29f7137fa7d9369fa604de28646bebb5032a3875b0
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday-raise_errors.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bob Lail
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.
@@ -0,0 +1,34 @@
1
+ # Faraday::RaiseErrors
2
+
3
+ Faraday middleware that raises exceptions for HTTP errors in the 400..599 range.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "faraday-raise_errors"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install faraday-raise_errors
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ connection = Faraday.new
25
+ connection.use Faraday::RaiseErrors
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( https://github.com/boblail/faraday-raise_errors/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday/raise_errors/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faraday-raise-errors"
8
+ spec.version = Faraday::RaiseErrors::VERSION
9
+ spec.authors = ["Bob Lail"]
10
+ spec.email = ["bob.lailfamily@gmail.com"]
11
+ spec.summary = %q{Raises exceptions corresponding to HTTP responses}
12
+ spec.description = %q{Raises exceptions corresponding to HTTP responses}
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
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1 @@
1
+ require "faraday/raise_errors"
@@ -0,0 +1,49 @@
1
+ module Faraday
2
+ module HTTP
3
+ class Error < RuntimeError
4
+ attr_reader :env
5
+
6
+ def initialize(env, message=nil)
7
+ @env = env
8
+ super message || default_message(env)
9
+ end
10
+
11
+ def default_message(env)
12
+ "#{status} from #{env[:url].host}#{env[:url].path}"
13
+ end
14
+
15
+ def status
16
+ env[:status]
17
+ end
18
+ end
19
+
20
+ class ClientError < Error; end
21
+ class ServerError < Error; end
22
+ class UnrecognizedResponse < Error; end
23
+
24
+ CLIENT_ERRORS = {
25
+ 400 => :BadRequest,
26
+ 401 => :Unauthorized,
27
+ 403 => :Forbidden,
28
+ 406 => :NotAcceptable,
29
+ 410 => :Gone,
30
+ 422 => :UnprocessableEntity }.freeze
31
+
32
+ SERVER_ERRORS = {
33
+ 500 => :InternalError,
34
+ 502 => :BadGateway,
35
+ 503 => :ServiceUnavailable,
36
+ 504 => :GatewayTimeout }.freeze
37
+
38
+ ERRORS = CLIENT_ERRORS.merge(SERVER_ERRORS).freeze
39
+
40
+ CLIENT_ERRORS.each do |code, error|
41
+ const_set error, Class.new(Faraday::HTTP::ClientError)
42
+ end
43
+
44
+ SERVER_ERRORS.each do |code, error|
45
+ const_set error, Class.new(Faraday::HTTP::ServerError)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+ require "faraday"
2
+ require "faraday/http_errors"
3
+ require "faraday/raise_errors/version"
4
+
5
+ module Faraday
6
+
7
+ class RaiseErrors < ::Faraday::Response::Middleware
8
+ def on_complete(env)
9
+ case env[:status]
10
+ when 404
11
+ raise Faraday::Error::ResourceNotFound, response_values(env)
12
+ when 407
13
+ # mimic the behavior that we get with proxy requests with HTTPS
14
+ raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
15
+ when 400..599
16
+ error = Faraday::HTTP::ERRORS.fetch(env[:status], :UnrecognizedResponse)
17
+ exception = Faraday::HTTP.const_get error
18
+ raise exception.new(env)
19
+ end
20
+ end
21
+
22
+ def response_values(env)
23
+ { status: env[:status], headers: env[:response_headers], body: env[:body] }
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,7 @@
1
+ require "faraday"
2
+
3
+ module Faraday
4
+ class RaiseErrors < ::Faraday::Response::Middleware
5
+ VERSION = "0.2.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-raise-errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Bob Lail
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-27 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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Raises exceptions corresponding to HTTP responses
56
+ email:
57
+ - bob.lailfamily@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - faraday-raise-errors.gemspec
68
+ - lib/faraday-raise-errors.rb
69
+ - lib/faraday/http_errors.rb
70
+ - lib/faraday/raise_errors.rb
71
+ - lib/faraday/raise_errors/version.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Raises exceptions corresponding to HTTP responses
96
+ test_files: []