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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8856a3b9bdf0070da933541f52c3571e5499783
4
- data.tar.gz: fbcd0010c28a491fe267a27d0617774906d30b2a
3
+ metadata.gz: 6d13c64e9e5f8191118021ff693f21ea68f72f81
4
+ data.tar.gz: 7a37f05f5f04731b34082f38ea3f1b2e3ee0f24c
5
5
  SHA512:
6
- metadata.gz: f6f1d6969aa4957ec2983d44b841d3a5515cbadb067858b6b6bf859f785b5e8814e324d06db993cbc5960eda6a1e15d1ecc9bfd228db8f808ac56eadcaa250ee
7
- data.tar.gz: 41d5a0daa0d33f7655082863beea63bc6a233f1f5e43e433ed18f90ff5e7d43a7ea6e8b8f82b10886d922802c2ff262de8126784840ecfcfe90ffec93f74f856
6
+ metadata.gz: c55274c85383c743de964a36974a2508e6610f747cbed85a71556d9b75df408f8c6b6e8df12f7899070b1f6a93abb9908780030ad4b48130d4415d96a5d9bb53
7
+ data.tar.gz: 42e54932ac4de572954c07caca24605cb1483c724dacb20cd6f194c0fbb5c6520e044331fa5e860a2f70a4d539315bcd3773090d94e3c080cb1b5a27d4df658c
data/README.md CHANGED
@@ -175,4 +175,4 @@ end
175
175
 
176
176
  ---
177
177
 
178
- © 2013 [Rafaël Blais Masson](http://rafbm.com). Micky is released under the MIT license.
178
+ © 2014 [Rafaël Blais Masson](http://rafbm.com). Micky is released under the MIT license.
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(response = nil)
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 to_s
10
- "#{response_code} #{response_message} at #{request_uri}"
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
- return log "Max redirects reached (#{@max_redirects})" if redirect_count >= @max_redirects
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::ClientError.new(response)
44
+ raise Micky::HTTPClientError, response: response
44
45
  when Net::HTTPServerError
45
- raise Micky::ServerError.new(response)
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 return nil
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 domain validity check that can be wrapped with Timeout.
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
- log 'Domain resolution error'
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
- log 'Domain resolution timeout'
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 Timeout::Error, OpenSSL::SSL::SSLError, SystemCallError, SocketError => e
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
@@ -1,3 +1,3 @@
1
1
  module Micky
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/micky.rb CHANGED
@@ -1,9 +1,9 @@
1
- require 'micky/version'
1
+ require_relative 'micky/version'
2
2
 
3
- require 'micky/uri'
4
- require 'micky/request'
5
- require 'micky/response'
6
- require 'micky/errors'
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 = 10
24
- @timeout = 5
23
+ @max_redirects = 20
24
+ @timeout = 10
25
25
  @skip_resolve = false
26
- @resolve_timeout = 2
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.3.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-04-09 00:00:00.000000000 Z
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.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