ruby-nuggets 0.7.7 → 0.7.8

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.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.7.7
5
+ This documentation refers to ruby-nuggets version 0.7.8
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -25,32 +25,29 @@
25
25
  ###############################################################################
26
26
  #++
27
27
 
28
+ require 'nuggets/uri/redirect_mixin'
28
29
  require 'nuggets/net/success'
29
30
 
30
31
  module Nuggets
31
32
  module URI
32
33
  module ExistMixin
33
34
 
34
- URI_EXIST_HTTP_CACHE = ::Hash.new { |h, k| h[k] = ::Net::HTTP.new(*k) }
35
+ def self.extended(base)
36
+ base.extend Nuggets::URI::RedirectMixin
37
+ end
35
38
 
36
39
  # call-seq:
37
- # URI.exist?(uri) => +true+ or +false+
40
+ # URI.exist?(uri) => +true+, +false+ or +nil+
41
+ # URI.exist?(uri) { |res| ... } => anObject, +false+ or +nil+
38
42
  #
39
- # Return +true+ if the URI +uri+ exists.
40
- def exist?(uri, cache = URI_EXIST_HTTP_CACHE, steps = 20, &block)
41
- uri = ::URI.parse(uri.to_s)
42
- return unless uri.is_a?(::URI::HTTP)
43
-
44
- res = cache[[uri.host, uri.port]].head(uri.request_uri)
45
-
46
- if res.is_a?(::Net::HTTPRedirection)
47
- exist?(res['Location'], cache, steps - 1, &block) if steps > 0
48
- elsif res.success?
49
- block ? block[res] : true
50
- else
51
- false
52
- end
53
- rescue ::SocketError, ::Errno::EHOSTUNREACH
43
+ # Checks whether the URI +uri+ exists by performing a +HEAD+ request. If
44
+ # successful, yields the response to the given block and returns its return
45
+ # value, or +true+ if no block was given. Returns +false+ in case of failure,
46
+ # or +nil+ if the redirect limit has been exceeded.
47
+ #
48
+ # See Nuggets::URI::RedirectMixin#follow_redirect for more information.
49
+ def exist?(uri)
50
+ head_redirect(uri) { |res| res.success? && (!block_given? || yield(res)) }
54
51
  end
55
52
 
56
53
  alias_method :exists?, :exist?
@@ -0,0 +1,5 @@
1
+ require 'nuggets/uri/redirect_mixin'
2
+
3
+ module URI
4
+ extend Nuggets::URI::RedirectMixin
5
+ end
@@ -0,0 +1,102 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2011 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU Affero General Public License as published by #
14
+ # the Free Software Foundation; either version 3 of the License, or (at your #
15
+ # option) any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
+ # for more details. #
21
+ # #
22
+ # You should have received a copy of the GNU Affero General Public License #
23
+ # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ require 'net/https'
29
+
30
+ module Nuggets
31
+ module URI
32
+ module RedirectMixin
33
+
34
+ # Maximum number of redirects to follow.
35
+ URI_REDIRECT_MAX_STEPS = 20
36
+
37
+ # Cache for HTTP objects to reuse.
38
+ URI_REDIRECT_HTTP_CACHE = ::Hash.new { |h, k| h[k] = ::Net::HTTP.new(*k) }
39
+
40
+ # call-seq:
41
+ # URI.follow_redirect(uri[, steps]) { |req, path| ... } => aResponse or +nil+
42
+ #
43
+ # Performs any HTTP request on +uri+ while following at most +steps+ redirects.
44
+ # Accepts both strings and URI objects for +uri+. Defers to the given block to
45
+ # perform the actual request; yields the request object and the request URI
46
+ # string.
47
+ #
48
+ # Returns the response object if successful, or +nil+ if +uri+ is not an HTTP
49
+ # URI, the redirect limit has been exceeded, or any connection error occurs.
50
+ def follow_redirect(uri, steps = URI_REDIRECT_MAX_STEPS, cache = URI_REDIRECT_HTTP_CACHE)
51
+ unless uri.is_a?(::URI::HTTP)
52
+ uri = ::URI.parse(uri.to_s)
53
+ return unless uri.is_a?(::URI::HTTP)
54
+ end
55
+
56
+ req = cache[[uri.host, uri.port]]
57
+ req.use_ssl = uri.is_a?(::URI::HTTPS)
58
+
59
+ ctx = req.instance_variable_get(:@ssl_context) if req.use_ssl?
60
+ ctx.verify_mode ||= ::OpenSSL::SSL::VERIFY_NONE if ctx
61
+
62
+ res = yield req, uri.request_uri
63
+ return res unless res.is_a?(::Net::HTTPRedirection)
64
+ return nil unless steps > 0
65
+
66
+ follow_redirect(res['Location'], steps - 1, cache) { |*a| yield(*a) }
67
+ rescue ::SocketError, ::Errno::EHOSTUNREACH
68
+ end
69
+
70
+ # call-seq:
71
+ # URI.head_redirect(uri[, steps]) => aResponse or +nil+
72
+ # URI.head_redirect(uri[, steps]) { |res| ... } => anObject or +nil+
73
+ #
74
+ # Performs a +HEAD+ request on +uri+ while following at most +steps+ redirects.
75
+ # If successful, yields the response to the given block and returns its return
76
+ # value, or the response itself if no block was given. Returns +nil+ in case
77
+ # of failure.
78
+ #
79
+ # See Nuggets::URI::RedirectMixin#follow_redirect for more information.
80
+ def head_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
81
+ res = follow_redirect(uri, steps) { |req, path| req.head(path) }
82
+ res && block_given? ? yield(res) : res
83
+ end
84
+
85
+ # call-seq:
86
+ # URI.get_redirect(uri[, steps]) => aResponse or +nil+
87
+ # URI.get_redirect(uri[, steps]) { |res| ... } => anObject or +nil+
88
+ #
89
+ # Performs a +GET+ request on +uri+ while following at most +steps+ redirects.
90
+ # If successful, yields the response to the given block and returns its return
91
+ # value, or the response itself if no block was given. Returns +nil+ in case
92
+ # of failure.
93
+ #
94
+ # See Nuggets::URI::RedirectMixin#follow_redirect for more information.
95
+ def get_redirect(uri, steps = URI_REDIRECT_MAX_STEPS)
96
+ res = follow_redirect(uri, steps) { |req, path| req.get(path) }
97
+ res && block_given? ? yield(res) : res
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 7
7
- TINY = 7
7
+ TINY = 8
8
8
 
9
9
  class << self
10
10
 
@@ -14,12 +14,17 @@ describe URI, 'when extended by', Nuggets::URI::ContentTypeMixin do
14
14
  %w[
15
15
  htp://www.google.de
16
16
  www.google.de
17
- http://blackwinter.de/bla
18
17
  http://blawinter.de
19
18
  ].each { |u|
20
19
  example { URI.content_type(u).should == nil }
21
20
  }
22
21
 
22
+ %w[
23
+ http://blackwinter.de/bla
24
+ ].each { |u|
25
+ example { URI.content_type(u).should == false }
26
+ }
27
+
23
28
  {
24
29
  'http://blackwinter.de/misc/ww.png' => 'image/png',
25
30
  'http://blackwinter.de/misc/suicide_is_painless.mid' => 'audio/midi',
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 7
10
- version: 0.7.7
9
+ - 8
10
+ version: 0.7.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jens Wille
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-24 00:00:00 Z
18
+ date: 2011-10-28 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Some extensions to the Ruby programming language.
@@ -143,6 +143,8 @@ files:
143
143
  - lib/nuggets/io/null.rb
144
144
  - lib/nuggets/io/agrep.rb
145
145
  - lib/nuggets/uri/content_type.rb
146
+ - lib/nuggets/uri/redirect_mixin.rb
147
+ - lib/nuggets/uri/redirect.rb
146
148
  - lib/nuggets/uri/exist_mixin.rb
147
149
  - lib/nuggets/uri/content_type_mixin.rb
148
150
  - lib/nuggets/uri/exist.rb
@@ -199,7 +201,7 @@ rdoc_options:
199
201
  - UTF-8
200
202
  - --all
201
203
  - --title
202
- - ruby-nuggets Application documentation (v0.7.7)
204
+ - ruby-nuggets Application documentation (v0.7.8)
203
205
  - --line-numbers
204
206
  require_paths:
205
207
  - lib