fastimage 1.2.9 → 1.2.10
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 +6 -1
- data/README.textile +3 -1
- data/VERSION.yml +1 -1
- data/lib/fastimage.rb +22 -2
- data/test/test.rb +33 -0
- metadata +3 -3
data/README
CHANGED
|
@@ -16,6 +16,11 @@ You only need supply the uri, and FastImage will do the rest.
|
|
|
16
16
|
|
|
17
17
|
Fastimage can also read local (and other) files, and uses the open-uri library to do so.
|
|
18
18
|
|
|
19
|
+
And new in v1.2.9, FastImage will automatically read from any object that responds to :read - for
|
|
20
|
+
instance an IO object if that is passed instead of a URI.
|
|
21
|
+
|
|
22
|
+
New in v1.2.10 FastImage will follow up to 4 HTTP redirects to get the image.
|
|
23
|
+
|
|
19
24
|
Examples
|
|
20
25
|
|
|
21
26
|
require 'fastimage'
|
|
@@ -65,7 +70,7 @@ You'll need to 'sudo gem install fakeweb' to be able to run the tests.
|
|
|
65
70
|
|
|
66
71
|
|
|
67
72
|
References
|
|
68
|
-
* "http://pennysmalls.com/
|
|
73
|
+
* "http://pennysmalls.com/find-jpeg-dimensions-fast-in-pure-ruby-no-ima":http://pennysmalls.com/find-jpeg-dimensions-fast-in-pure-ruby-no-ima
|
|
69
74
|
* "http://snippets.dzone.com/posts/show/805":http://snippets.dzone.com/posts/show/805
|
|
70
75
|
* "http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/":http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
|
|
71
76
|
* "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
|
data/README.textile
CHANGED
|
@@ -19,6 +19,8 @@ Fastimage can also read local (and other) files, and uses the open-uri library t
|
|
|
19
19
|
And new in v1.2.9, FastImage will automatically read from any object that responds to :read - for
|
|
20
20
|
instance an IO object if that is passed instead of a URI.
|
|
21
21
|
|
|
22
|
+
New in v1.2.10 FastImage will follow up to 4 HTTP redirects to get the image.
|
|
23
|
+
|
|
22
24
|
h2. Examples
|
|
23
25
|
|
|
24
26
|
<pre>
|
|
@@ -82,7 +84,7 @@ Finished in 0.059392 seconds.
|
|
|
82
84
|
|
|
83
85
|
|
|
84
86
|
h2. References
|
|
85
|
-
* "http://pennysmalls.com/
|
|
87
|
+
* "http://pennysmalls.com/find-jpeg-dimensions-fast-in-pure-ruby-no-ima":http://pennysmalls.com/find-jpeg-dimensions-fast-in-pure-ruby-no-ima
|
|
86
88
|
* "http://snippets.dzone.com/posts/show/805":http://snippets.dzone.com/posts/show/805
|
|
87
89
|
* "http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/":http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
|
|
88
90
|
* "http://imagesize.rubyforge.org/":http://imagesize.rubyforge.org/
|
data/VERSION.yml
CHANGED
data/lib/fastimage.rb
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
# New in v1.2.9, FastImage will automatically read from any object that responds to :read - for
|
|
17
17
|
# instance an IO object if that is passed instead of a URI.
|
|
18
18
|
#
|
|
19
|
+
# New in v1.2.10 FastImage will follow up to 4 HTTP redirects to get the image.
|
|
20
|
+
#
|
|
19
21
|
# === Examples
|
|
20
22
|
# require 'fastimage'
|
|
21
23
|
#
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
# === References
|
|
32
34
|
# * http://snippets.dzone.com/posts/show/805
|
|
33
35
|
# * http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
|
|
34
|
-
# * http://pennysmalls.com/
|
|
36
|
+
# * http://pennysmalls.com/find-jpeg-dimensions-fast-in-pure-ruby-no-ima
|
|
35
37
|
# * http://imagesize.rubyforge.org/
|
|
36
38
|
#
|
|
37
39
|
require 'net/https'
|
|
@@ -143,7 +145,7 @@ class FastImage
|
|
|
143
145
|
@property = options[:type_only] ? :type : :size
|
|
144
146
|
@timeout = options[:timeout] || DefaultTimeout
|
|
145
147
|
@uri = uri
|
|
146
|
-
|
|
148
|
+
|
|
147
149
|
if uri.respond_to?(:read)
|
|
148
150
|
fetch_using_read(uri)
|
|
149
151
|
else
|
|
@@ -172,9 +174,27 @@ class FastImage
|
|
|
172
174
|
private
|
|
173
175
|
|
|
174
176
|
def fetch_using_http
|
|
177
|
+
@redirect_count = 0
|
|
178
|
+
|
|
179
|
+
fetch_using_http_from_parsed_uri
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def fetch_using_http_from_parsed_uri
|
|
175
183
|
setup_http
|
|
176
184
|
@http.request_get(@parsed_uri.request_uri) do |res|
|
|
185
|
+
if res.is_a?(Net::HTTPRedirection) && @redirect_count < 4
|
|
186
|
+
@redirect_count += 1
|
|
187
|
+
begin
|
|
188
|
+
@parsed_uri = URI.parse(res['Location'])
|
|
189
|
+
rescue URI::InvalidURIError
|
|
190
|
+
else
|
|
191
|
+
fetch_using_http_from_parsed_uri
|
|
192
|
+
break
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
177
196
|
raise ImageFetchFailure unless res.is_a?(Net::HTTPSuccess)
|
|
197
|
+
|
|
178
198
|
res.read_body do |str|
|
|
179
199
|
break if parse_packet(str)
|
|
180
200
|
end
|
data/test/test.rb
CHANGED
|
@@ -139,4 +139,37 @@ class FastImageTest < Test::Unit::TestCase
|
|
|
139
139
|
FastImage.size(File.join(FixturePath, "faulty.jpg"), :raise_on_failure=>true)
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
|
+
|
|
143
|
+
def test_should_handle_permanent_redirect
|
|
144
|
+
url = "http://example.com/foo.jpeg"
|
|
145
|
+
register_redirect(url, TestUrl + GoodFixtures.keys.first)
|
|
146
|
+
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(url, :raise_on_failure=>true)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def test_should_handle_permanent_redirect_4_times
|
|
150
|
+
first_url = "http://example.com/foo.jpeg"
|
|
151
|
+
register_redirect(first_url, "http://example.com/foo2.jpeg")
|
|
152
|
+
register_redirect("http://example.com/foo2.jpeg", "http://example.com/foo3.jpeg")
|
|
153
|
+
register_redirect("http://example.com/foo3.jpeg", "http://example.com/foo4.jpeg")
|
|
154
|
+
register_redirect("http://example.com/foo4.jpeg", TestUrl + GoodFixtures.keys.first)
|
|
155
|
+
assert_equal GoodFixtures[GoodFixtures.keys.first][1], FastImage.size(first_url, :raise_on_failure=>true)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def test_should_raise_on_permanent_redirect_5_times
|
|
159
|
+
first_url = "http://example.com/foo.jpeg"
|
|
160
|
+
register_redirect(first_url, "http://example.com/foo2.jpeg")
|
|
161
|
+
register_redirect("http://example.com/foo2.jpeg", "http://example.com/foo3.jpeg")
|
|
162
|
+
register_redirect("http://example.com/foo3.jpeg", "http://example.com/foo4.jpeg")
|
|
163
|
+
register_redirect("http://example.com/foo4.jpeg", "http://example.com/foo5.jpeg")
|
|
164
|
+
register_redirect("http://example.com/foo5.jpeg", TestUrl + GoodFixtures.keys.first)
|
|
165
|
+
assert_raises(FastImage::ImageFetchFailure) do
|
|
166
|
+
FastImage.size(first_url, :raise_on_failure=>true)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def register_redirect(from, to)
|
|
171
|
+
resp = Net::HTTPMovedPermanently.new(1.0, 302, "Moved")
|
|
172
|
+
resp['Location'] = to
|
|
173
|
+
FakeWeb.register_uri(:get, from, :response=>resp)
|
|
174
|
+
end
|
|
142
175
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fastimage
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 11
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
8
|
- 2
|
|
9
|
-
-
|
|
10
|
-
version: 1.2.
|
|
9
|
+
- 10
|
|
10
|
+
version: 1.2.10
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Stephen Sykes
|