mechanize_clip 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/mechanize_clip/version.rb +1 -1
- data/lib/mechanize_clip.rb +26 -4
- data/spec/mechanize_clip_spec.rb +1 -1
- metadata +7 -7
data/README.md
CHANGED
@@ -21,6 +21,13 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
```ruby
|
23
23
|
user.avatar = MechanizeClip.get 'http://www.google.ru/images/nav_logo99.png'
|
24
|
+
|
25
|
+
# it follows redirects
|
26
|
+
user.avatar = MechanizeClip.get 'http://bit.ly/cHbjf'
|
27
|
+
|
28
|
+
# it creates temp files from raw requests
|
29
|
+
# useful for ajax uploaders like https://github.com/valums/file-uploader
|
30
|
+
user.avatar = MechanizeClie.from_raw request
|
24
31
|
```
|
25
32
|
|
26
33
|
## Contributing
|
data/lib/mechanize_clip.rb
CHANGED
@@ -4,14 +4,14 @@ require "mechanize"
|
|
4
4
|
|
5
5
|
module MechanizeClip
|
6
6
|
|
7
|
-
#
|
8
|
-
class
|
7
|
+
# Tempfiles capatible with paperclip
|
8
|
+
class PageTmpFile < Tempfile
|
9
9
|
attr :content_type
|
10
10
|
attr :page
|
11
11
|
|
12
12
|
def initialize page
|
13
13
|
@page = page
|
14
|
-
ext
|
14
|
+
ext = File.extname(self.original_filename)
|
15
15
|
name = File.basename(self.original_filename, ext)
|
16
16
|
super([name, ext])
|
17
17
|
self.binmode
|
@@ -36,6 +36,23 @@ module MechanizeClip
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
class RawTmpFile < Tempfile
|
40
|
+
attr :content_type
|
41
|
+
attr :original_filename
|
42
|
+
|
43
|
+
def initialize request, filename_param
|
44
|
+
@original_filename = request.params[filename_param] || request.headers['X-File-Name']
|
45
|
+
ext = File.extname(self.original_filename)
|
46
|
+
name = File.basename(self.original_filename, ext)
|
47
|
+
super([name, ext])
|
48
|
+
self.binmode
|
49
|
+
self.write request.raw_post
|
50
|
+
self.flush
|
51
|
+
self.rewind
|
52
|
+
@content_type = request.content_type
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
39
56
|
# Downloads url contents to temp file using Mechanize
|
40
57
|
# returns nil if something bad happend
|
41
58
|
def get url
|
@@ -51,9 +68,14 @@ module MechanizeClip
|
|
51
68
|
def get! url
|
52
69
|
agent = Mechanize.new
|
53
70
|
# agent.max_file_buffer = 0 # always download to temp file
|
54
|
-
|
71
|
+
PageTmpFile.new agent.get(url)
|
72
|
+
end
|
73
|
+
|
74
|
+
def from_raw request, filename_param = 'qqfile'
|
75
|
+
RawTmpFile.new request, filename_param
|
55
76
|
end
|
56
77
|
|
57
78
|
module_function :get
|
58
79
|
module_function :get!
|
80
|
+
module_function :from_raw
|
59
81
|
end
|
data/spec/mechanize_clip_spec.rb
CHANGED
@@ -26,7 +26,7 @@ describe MechanizeClip do
|
|
26
26
|
file.original_filename.should == "logo.png"
|
27
27
|
end
|
28
28
|
|
29
|
-
it "
|
29
|
+
it "detects file names" do
|
30
30
|
file = subject.get! "https://a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x-hover.png?1324325376"
|
31
31
|
file.original_filename.should == "logov7@4x-hover.png"
|
32
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mechanize_clip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mechanize
|
16
|
-
requirement: &
|
16
|
+
requirement: &70253091346040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70253091346040
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70253091345540 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '2.8'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70253091345540
|
36
36
|
description: Downloads files to temp files with Mechanize gem
|
37
37
|
email:
|
38
38
|
- yury.korolev@gmail.com
|
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
70
|
version: '0'
|
71
71
|
requirements: []
|
72
72
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.8.
|
73
|
+
rubygems_version: 1.8.17
|
74
74
|
signing_key:
|
75
75
|
specification_version: 3
|
76
76
|
summary: Mechanize url fetcher for paperclip
|