micky 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/micky/errors.rb +24 -4
- data/lib/micky/request.rb +27 -13
- data/lib/micky/version.rb +1 -1
- data/lib/micky.rb +8 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d13c64e9e5f8191118021ff693f21ea68f72f81
|
4
|
+
data.tar.gz: 7a37f05f5f04731b34082f38ea3f1b2e3ee0f24c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c55274c85383c743de964a36974a2508e6610f747cbed85a71556d9b75df408f8c6b6e8df12f7899070b1f6a93abb9908780030ad4b48130d4415d96a5d9bb53
|
7
|
+
data.tar.gz: 42e54932ac4de572954c07caca24605cb1483c724dacb20cd6f194c0fbb5c6520e044331fa5e860a2f70a4d539315bcd3773090d94e3c080cb1b5a27d4df658c
|
data/README.md
CHANGED
data/lib/micky/errors.rb
CHANGED
@@ -1,13 +1,23 @@
|
|
1
1
|
module Micky
|
2
2
|
class Error < StandardError
|
3
|
-
attr_reader :response
|
3
|
+
attr_reader :exception, :response
|
4
4
|
|
5
|
-
def initialize(
|
5
|
+
def initialize(message = nil, exception: nil, response: nil)
|
6
|
+
@exception = exception
|
6
7
|
@response = response
|
8
|
+
@message = message
|
7
9
|
end
|
8
10
|
|
9
|
-
def
|
10
|
-
|
11
|
+
def message
|
12
|
+
if response
|
13
|
+
"#{response_code} #{response_message} at #{request_uri}"
|
14
|
+
elsif exception
|
15
|
+
exception.inspect
|
16
|
+
elsif @message
|
17
|
+
@message
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
11
21
|
end
|
12
22
|
|
13
23
|
def request_uri
|
@@ -23,9 +33,19 @@ module Micky
|
|
23
33
|
end
|
24
34
|
end
|
25
35
|
|
36
|
+
# Client errors
|
26
37
|
class ClientError < Error
|
27
38
|
end
|
39
|
+
class HTTPClientError < ClientError
|
40
|
+
end
|
41
|
+
class InvalidURIError < ClientError
|
42
|
+
end
|
28
43
|
|
44
|
+
# Server errors
|
29
45
|
class ServerError < Error
|
30
46
|
end
|
47
|
+
class HTTPServerError < ServerError
|
48
|
+
end
|
49
|
+
class TooManyRedirects < ServerError
|
50
|
+
end
|
31
51
|
end
|
data/lib/micky/request.rb
CHANGED
@@ -24,7 +24,11 @@ module Micky
|
|
24
24
|
private
|
25
25
|
|
26
26
|
def request_with_redirect_handling(uri, redirect_count = 0)
|
27
|
-
|
27
|
+
if redirect_count >= @max_redirects
|
28
|
+
raise Micky::TooManyRedirects, "Max redirects reached (#{@max_redirects})" if @raise_errors
|
29
|
+
log "Max redirects reached (#{@max_redirects})"
|
30
|
+
return nil
|
31
|
+
end
|
28
32
|
|
29
33
|
case response = request(uri)
|
30
34
|
when Net::HTTPSuccess
|
@@ -34,40 +38,45 @@ module Micky
|
|
34
38
|
log "Redirect to #{uri}"
|
35
39
|
request_with_redirect_handling(uri, redirect_count + 1)
|
36
40
|
else
|
37
|
-
log response
|
38
|
-
log response.body
|
39
|
-
|
40
41
|
if @raise_errors
|
41
42
|
case response
|
42
43
|
when Net::HTTPClientError
|
43
|
-
raise Micky::
|
44
|
+
raise Micky::HTTPClientError, response: response
|
44
45
|
when Net::HTTPServerError
|
45
|
-
raise Micky::
|
46
|
+
raise Micky::HTTPServerError, response: response
|
46
47
|
end
|
47
48
|
else
|
49
|
+
log response
|
50
|
+
log response.body
|
48
51
|
nil
|
49
52
|
end
|
50
53
|
end
|
51
54
|
end
|
52
55
|
|
53
56
|
def request(uri)
|
54
|
-
@uri = Micky::URI(uri) or
|
57
|
+
@uri = Micky::URI(uri) or begin
|
58
|
+
raise Micky::InvalidURIError, uri if @raise_errors
|
59
|
+
warn "Micky.#{@request_class_name.downcase}('#{uri}'): Invalid URI"
|
60
|
+
return nil
|
61
|
+
end
|
55
62
|
|
56
63
|
unless @skip_resolve == true
|
57
|
-
# Resolv is the only
|
64
|
+
# Resolv is the only host validity check that can be wrapped with Timeout.
|
58
65
|
# Net::HTTP and OpenURI use TCPSocket.open which isn’t timeoutable.
|
59
66
|
require 'resolv' unless defined? Resolv
|
60
67
|
begin
|
61
68
|
Timeout.timeout(@resolve_timeout) do
|
62
69
|
begin
|
63
70
|
Resolv::DNS.new.getaddress(@uri.host)
|
64
|
-
rescue Resolv::ResolvError
|
65
|
-
|
71
|
+
rescue Resolv::ResolvError => e
|
72
|
+
raise Micky::ClientError, exception: e if @raise_errors
|
73
|
+
log 'Host resolution error'
|
66
74
|
return nil
|
67
75
|
end
|
68
76
|
end
|
69
|
-
rescue Timeout::Error
|
70
|
-
|
77
|
+
rescue Timeout::Error => e
|
78
|
+
raise Micky::ClientError, "Host resolution timeout: #{@uri}" if @raise_errors
|
79
|
+
log 'Host resolution timeout'
|
71
80
|
return nil
|
72
81
|
end
|
73
82
|
end
|
@@ -111,7 +120,12 @@ module Micky
|
|
111
120
|
@headers.each { |k,v| request[k] = v }
|
112
121
|
|
113
122
|
http.request(request)
|
114
|
-
rescue
|
123
|
+
rescue Errno::ECONNREFUSED, OpenSSL::SSL::SSLError, SocketError => e
|
124
|
+
raise Micky::ClientError, exception: e if @raise_errors
|
125
|
+
log e
|
126
|
+
nil
|
127
|
+
rescue SystemCallError, IOError, Timeout::Error => e
|
128
|
+
raise Micky::ServerError, exception: e if @raise_errors
|
115
129
|
log e
|
116
130
|
nil
|
117
131
|
end
|
data/lib/micky/version.rb
CHANGED
data/lib/micky.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
require_relative 'micky/version'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
require_relative 'micky/uri'
|
4
|
+
require_relative 'micky/request'
|
5
|
+
require_relative 'micky/response'
|
6
|
+
require_relative 'micky/errors'
|
7
7
|
|
8
8
|
module Micky
|
9
9
|
class << self
|
@@ -20,10 +20,10 @@ module Micky
|
|
20
20
|
|
21
21
|
# Reasonable defaults
|
22
22
|
@raise_errors = false
|
23
|
-
@max_redirects =
|
24
|
-
@timeout =
|
23
|
+
@max_redirects = 20
|
24
|
+
@timeout = 10
|
25
25
|
@skip_resolve = false
|
26
|
-
@resolve_timeout =
|
26
|
+
@resolve_timeout = 5
|
27
27
|
@oauth = {}
|
28
28
|
@query = {}
|
29
29
|
@headers = {}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafaël Blais Masson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
81
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.2.
|
82
|
+
rubygems_version: 2.2.0
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: Lightweight and worry-free HTTP client
|