open_uri_w_redirect_to_https 0.1.0 → 0.1.1

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: b9deff2ecc9ce387ffff9e58e0363488e73a4a56
4
- data.tar.gz: 762930ae967325ada38469f82b6860b0522ad5e8
3
+ metadata.gz: 3e440a3dccb0a62785e545546b560476f37ccd57
4
+ data.tar.gz: 38c85885bc54e08028e1cebdd0d9212fad57cec8
5
5
  SHA512:
6
- metadata.gz: 67810beb05e32c9b442846230827695ce16d80c5a241736c241c42d5d8bae5af7fecb21f70e8b91d408e06f73dda5a31e826af8f60a1bd9cf560c9f733c07cdd
7
- data.tar.gz: ac6489110783d2bb0ec7f11c27dde135da3928890000eac965cc0dedd1cac98f3058d2d4c5de9317c911e7dea241256350cce011dc52f8dc33a37ecc9bf4df00
6
+ metadata.gz: b390de4b5d5a12d7d15fa0aaa049d859c07291077ff37f09d0dd8ba89da3daf770ee4128393c2da0d91c94d2f591c44190a3ab9786faec75e1587d5ba7c53aba
7
+ data.tar.gz: cccf201bec13a2d174429fe4706845c05576985b46985e98895f074badb35d81d03c9d0397c32bcdc504d9f3603a5e5b235399f7607594603190a8568ee4a309
data/README.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  File : README.md
4
4
  Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- Date : 2014-11-22
5
+ Date : 2014-11-23
6
6
 
7
7
  Copyright : Copyright (C) 2014 Felix C. Stegerman
8
- Version : v0.1.0
8
+ Version : v0.1.1
9
9
 
10
10
  []: }}}1
11
11
 
@@ -42,7 +42,17 @@ $ pry
42
42
  ```bash
43
43
  $ pry
44
44
  > require 'open_uri_w_redirect_to_https'
45
- > OpenURI.redirect_to_https = true # set default
45
+ > OpenURI.w_redirect_to_https { open 'http://github.com' }
46
+ # dynamically scoped in current thread
47
+ => #<File:/tmp/open-uri...>
48
+ ```
49
+
50
+ or:
51
+
52
+ ```bash
53
+ $ pry
54
+ > require 'open_uri_w_redirect_to_https'
55
+ > OpenURI.redirect_to_https = true # set global default
46
56
  > open 'http://github.com'
47
57
  => #<File:/tmp/open-uri...>
48
58
  ```
@@ -67,10 +77,14 @@ gem install open_uri_w_redirect_to_https
67
77
  Monkey-patching is not a very robust way to fix bugs. Use at your
68
78
  own risk.
69
79
 
70
- Q: why should I prefer this gem to
71
- [open_uri_redirections](https://github.com/jaimeiniesta/open_uri_redirections)?
80
+ Q: why should I prefer this gem to [open_uri_redirections]
81
+ (https://github.com/jaimeiniesta/open_uri_redirections)?
72
82
  <br/>
73
- A: because this one is thread-safe.
83
+ A: because this one is thread-safe (I hope); other than that, feel
84
+ free to choose either
85
+
86
+ NB: this gem internally uses thread-local variables like
87
+ `Thread.current[:__open_uri_w_redirect_to_https__]`.
74
88
 
75
89
  ## Specs & Docs
76
90
 
@@ -80,6 +94,10 @@ rake coverage
80
94
  rake docs
81
95
  ```
82
96
 
97
+ ## TODO
98
+
99
+ * specs: (can we) test `redirect_to_https=true` w/ threads?
100
+
83
101
  ## License
84
102
 
85
103
  MIT [1].
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # File : open_uri_w_redirect_to_https.rb
4
4
  # Maintainer : Felix C. Stegerman <flx@obfusk.net>
5
- # Date : 2014-11-22
5
+ # Date : 2014-11-23
6
6
  #
7
7
  # Copyright : Copyright (C) 2014 Felix C. Stegerman
8
8
  # Licence : MIT
@@ -20,17 +20,48 @@ module OpenURI
20
20
  alias_method :open_uri_orig , :open_uri
21
21
  alias_method :redirectable_orig?, :redirectable?
22
22
 
23
- # set the `open_uri` `:redirect_to_https` default; thread safe!
23
+ # set the `open_uri` `:redirect_to_https` global default
24
24
  def redirect_to_https=(val)
25
- RedirectHTTPToHTTPS[:mutex].synchronize {
26
- RedirectHTTPToHTTPS[:default] = val
27
- }
25
+ (x = RedirectHTTPToHTTPS)[:mutex].synchronize { x[:default] = val }
28
26
  end
29
27
 
30
- # `redirectable?` patch that uses `caller` to determine whether
31
- # HTTP to HTTPS redirection should be allowed (as well)
28
+ # get the `open_uri` `:redirect_to_https` dynamic or global
29
+ # default
30
+ def redirect_to_https?
31
+ (x = RedirectHTTPToHTTPS)[:mutex].synchronize do
32
+ t = Thread.current[:__open_uri_w_redirect_to_https_default__]
33
+ t.nil? ? x[:default] : t
34
+ end
35
+ end
36
+
37
+ # dynamically thread-scoped `open_uri` `:redirect_to_https`
38
+ # default; for example:
39
+ #
40
+ # ```
41
+ # w_redirect_to_https { open('http://github.com' }
42
+ # ```
43
+ def w_redirect_to_https(val = true, &b)
44
+ old = Thread.current[:__open_uri_w_redirect_to_https_default__]
45
+ Thread.current[:__open_uri_w_redirect_to_https_default__] = val
46
+ begin
47
+ b[]
48
+ ensure
49
+ Thread.current[:__open_uri_w_redirect_to_https_default__] = old
50
+ end
51
+ end
52
+
53
+ # `redirectable?` patch that uses a thread-local variable to
54
+ # determine whether HTTP to HTTPS redirection should be allowed
55
+ # (as well)
56
+ #
57
+ # unless the dynamic or global `:redirect_to_https` setting is set
58
+ # to `:always`, only the behaviour of calls through `open_uri`
59
+ # will be changed (as per argument or dynamic or global setting)
32
60
  def redirectable?(uri1, uri2)
33
- if caller.find { |x| x =~ /redirect_to_https/ } =~ /__WITH__/
61
+ if redirect_to_https? == :always ||
62
+ Thread.current[:__open_uri_w_redirect_to_https__]
63
+ # clear to prevent leaking (e.g. to block)
64
+ Thread.current[:__open_uri_w_redirect_to_https__] = nil
34
65
  redirectable_w_redirect_to_https? uri1, uri2
35
66
  else
36
67
  redirectable_orig? uri1, uri2
@@ -42,20 +73,16 @@ module OpenURI
42
73
  # allowed; for example:
43
74
  #
44
75
  # ```
45
- # open('http://github.com', redirect_to_https: true) # works!
76
+ # open('http://github.com', redirect_to_https: true)
46
77
  # ```
47
78
  #
48
- # you can set the default using `redirect_to_https=`
79
+ # you can set the dynamic or global default using
80
+ # `redirect_to_https=` or `w_redirect_to_https`
49
81
  def open_uri(name, *rest, &b)
50
82
  r = (o = rest.find { |x| Hash === x }) && o.delete(:redirect_to_https)
51
- d = RedirectHTTPToHTTPS[:mutex].synchronize {
52
- RedirectHTTPToHTTPS[:default]
53
- }
54
- if (r.nil? ? d : r)
55
- open_uri__WITH__redirect_to_https name, *rest, &b
56
- else
57
- open_uri__WITHOUT__redirect_to_https name, *rest, &b
58
- end
83
+ Thread.current[:__open_uri_w_redirect_to_https__] = \
84
+ r.nil? ? redirect_to_https? : r
85
+ open_uri_orig name, *rest, &b
59
86
  end
60
87
 
61
88
  private
@@ -67,15 +94,6 @@ module OpenURI
67
94
  (uri1.scheme.downcase == 'http' && uri2.scheme.downcase == 'https')
68
95
  end
69
96
 
70
- # just calls open_uri_orig; redirectable? matches __WITH__
71
- def open_uri__WITH__redirect_to_https(name, *rest, &b)
72
- open_uri_orig name, *rest, &b
73
- end
74
-
75
- # just calls open_uri_orig; redirectable? does't match __WITH__
76
- def open_uri__WITHOUT__redirect_to_https(name, *rest, &b)
77
- open_uri_orig name, *rest, &b
78
- end
79
97
  end
80
98
  end
81
99
 
@@ -1,4 +1,4 @@
1
1
  module OpenURIWithRedirectToHttps
2
- VERSION = '0.1.0'
3
- DATE = '2014-11-22'
2
+ VERSION = '0.1.1'
3
+ DATE = '2014-11-23'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_uri_w_redirect_to_https
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix C. Stegerman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-22 00:00:00.000000000 Z
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fakeweb