nap 0.7.0 → 0.8.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55ffcab5a8b5cdab1783bed5bd25c770557fd0a6
4
- data.tar.gz: 1261b212ae72939d3fa6ed746c3316eca3197108
3
+ metadata.gz: 3b399814b5fbc094bb5c2a5d620034ac4476ff58
4
+ data.tar.gz: 78e178d833e8229ed7171ca4fd67c27723b3372e
5
5
  SHA512:
6
- metadata.gz: fcd3842a4c5009bbee634839e2555c4366218d86013251824e1235838251bb349de8fc89daeb76ebe2512616c629ddc67cda26c24a66a10d63170021f8789aaa
7
- data.tar.gz: a6651461595d44851bb36f5659a005ec3249800d70244672fb3e535c28c295763ba7b8e9a9fb7bebbada9299c2a52974340eab73b32bff47d58afaf2f8a2b933
6
+ metadata.gz: 3186e5ea15c41e9d5300b7fe30f9e9cdda734672a6266a8ccb31b9277f1720126627eb0961da2b3d2257c0f1df69703ffd7c39d73e42fea362cabf32191ae7e0
7
+ data.tar.gz: 15f7fbd148fbf9e294cae5b75e7af2532e4830ea93576c01468d580bff3b4e7b26d5c8cffe8aea12dd82cf36510fe079658b1dfdb8109c741cb65d13bf1ea2ec
data/README.md CHANGED
@@ -26,8 +26,7 @@ fire off HTTP requests without having to research net/http internals.
26
26
 
27
27
  ## Advanced request configuration
28
28
 
29
- If you need more control over the Net::HTTP request you can pass a block to
30
- all of the request methods.
29
+ If you need more control over the Net::HTTP request you can pass a block to all of the request methods.
31
30
 
32
31
  response = REST.get('http://google.com') do |http_request|
33
32
  http_request.open_timeout = 15
@@ -40,6 +39,23 @@ To enable the proxy settings in Nap, you can either use the HTTP\_PROXY or http\
40
39
 
41
40
  $ env HTTP_PROXY=http://rob:secret@192.167.1.254:665 ruby app.rb
42
41
 
42
+ ## Exceptions
43
+
44
+ Nap defines one top-level and three main error types which allow you to catch a whole range of exceptions thrown by underlying protocol implementations.
45
+
46
+ * *REST::Error*: Any type of error
47
+ * *REST::Timeout*: Read timeouts of various sorts
48
+ * *REST::Connection*: Connection errors caused by dropped sockets
49
+ * *REST::Protocol*: Request failed because of a problem when handling the HTTP request or response
50
+
51
+ In the most basic case you can rescue from the top-level type to warn about fetching problems.
52
+
53
+ begin
54
+ REST.get('http://example.com/pigeons/12')
55
+ rescue REST::Error
56
+ puts "[!] Failed to fetch Pigeon number 12."
57
+ end
58
+
43
59
  ## Contributions
44
60
 
45
61
  Nap couldn't be the shining beacon in the eternal darkness without help from:
@@ -1,10 +1,13 @@
1
1
  require 'uri'
2
2
 
3
- # REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style
4
- # HTTP calls.
3
+ # REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API
4
+ # for doing REST-style HTTP calls.
5
+ #
6
+ # In addition it provides wrappers for the many error classes that can be raised while making
7
+ # requests. See REST::Error for a complete discussion of options.
5
8
  module REST
6
9
  # Library version
7
- VERSION = '0.7.0'
10
+ VERSION = '0.8.0.pre'
8
11
 
9
12
  # Raised when the remote server disconnects when reading the response
10
13
  class DisconnectedError < StandardError; end
@@ -101,5 +104,6 @@ module REST
101
104
  end
102
105
  end
103
106
 
107
+ require File.expand_path('../rest/error', __FILE__)
104
108
  require File.expand_path('../rest/request', __FILE__)
105
109
  require File.expand_path('../rest/response', __FILE__)
@@ -0,0 +1,91 @@
1
+ require 'net/http'
2
+
3
+ module REST
4
+ # This constant can be used to rescue any of the known `Timeout`, `Connection`, and `Protocol`
5
+ # error classes.
6
+ #
7
+ # For instance, to rescue _any_ type of error that could be raise while making a request:
8
+ #
9
+ # begin
10
+ # REST.get('http://example.com/pigeons/12')
11
+ # rescue REST::Error => e
12
+ # p e # => Timeout::Error
13
+ # end
14
+ #
15
+ # If you want to rescue only `Timeout` related error classes, however, you can limit the scope:
16
+ #
17
+ # begin
18
+ # REST.get('http://example.com/pigeons/12')
19
+ # rescue REST::Error::Timeout => e
20
+ # p e # => Timeout::Error
21
+ # end
22
+ module Error
23
+ # This constant can be used to rescue only the known `Timeout` error classes.
24
+ module Timeout
25
+ def self.class_names
26
+ %w(
27
+ Errno::ETIMEDOUT
28
+ Timeout::Error
29
+ Net::OpenTimeout
30
+ Net::ReadTimeout
31
+ )
32
+ end
33
+ end
34
+
35
+ # This constant can be used to rescue only the known `Connection` error classes.
36
+ module Connection
37
+ def self.class_names
38
+ %w(
39
+ EOFError
40
+ Errno::ECONNABORTED
41
+ Errno::ECONNREFUSED
42
+ Errno::ECONNRESET
43
+ Errno::EHOSTUNREACH
44
+ Errno::EINVAL
45
+ Errno::ENETUNREACH
46
+ SocketError
47
+ OpenSSL::SSL::SSLError
48
+ )
49
+ end
50
+ end
51
+
52
+ # This constant can be used to rescue only the known `Protocol` error classes.
53
+ module Protocol
54
+ def self.class_names
55
+ %w(
56
+ Net::HTTPBadResponse
57
+ Net::HTTPHeaderSyntaxError
58
+ Net::ProtocolError
59
+ Zlib::GzipFile::Error
60
+ )
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ [Timeout, Connection, Protocol].each do |mod|
67
+ mod.send(:include, Error)
68
+
69
+ # Collect all the error classes that exist at runtime.
70
+ def mod.classes
71
+ class_names.map do |name|
72
+ begin
73
+ # MRI < 2 does not support full constant paths for `Object.const_get`.
74
+ name.split('::').inject(Object) do |current, const|
75
+ current.const_get(const)
76
+ end
77
+ rescue NameError
78
+ nil
79
+ end
80
+ end.compact
81
+ end
82
+
83
+ # Include the `mod` into the classes.
84
+ def mod.extend_classes!
85
+ classes.each { |klass| klass.send(:include, self) }
86
+ end
87
+
88
+ mod.extend_classes!
89
+ end
90
+ end
91
+ end
@@ -111,6 +111,7 @@ module REST
111
111
  if url.scheme == 'https'
112
112
  require 'net/https'
113
113
  require 'openssl'
114
+ Error::Connection.extend_classes!
114
115
 
115
116
  http_request.use_ssl = true
116
117
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manfred Stienstra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-05 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -51,6 +51,7 @@ files:
51
51
  - LICENSE
52
52
  - README.md
53
53
  - lib/rest.rb
54
+ - lib/rest/error.rb
54
55
  - lib/rest/request.rb
55
56
  - lib/rest/response.rb
56
57
  - support/cacert.pem
@@ -70,9 +71,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
71
  version: '0'
71
72
  required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  requirements:
73
- - - ">="
74
+ - - ">"
74
75
  - !ruby/object:Gem::Version
75
- version: '0'
76
+ version: 1.3.1
76
77
  requirements: []
77
78
  rubyforge_project:
78
79
  rubygems_version: 2.2.0