mechanize_clip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mechanize_clip.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yury Korolev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # MechanizeClip
2
+
3
+ Downloads files to temp files with Mechanize gem.
4
+ You can use it with paperclip.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mechanize_clip'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mechanize_clip
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ user.avatar = MechanizeClip.get 'http://www.google.ru/images/nav_logo99.png'
24
+ ```
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,65 @@
1
+ require "mechanize_clip/version"
2
+ require "tempfile"
3
+ require "mechanize"
4
+
5
+ module MechanizeClip
6
+
7
+ # Mechanize::Download v2.1 doesn't define filename accessor
8
+ class DownloadWithFilename < ::Mechanize::Download
9
+ attr :filename
10
+ end
11
+
12
+ # Temfile capatible with paperclip
13
+ class TmpFile < Tempfile
14
+ attr :content_type
15
+ attr :page
16
+
17
+ def initialize page
18
+ @page = page
19
+ ext = File.extname(self.original_filename)
20
+ name = File.basename(self.original_filename, ext)
21
+ super([name, ext])
22
+ self.binmode
23
+ content = @page.content
24
+ if String === content
25
+ self.write content
26
+ else
27
+ until content.eof? do
28
+ self.write content.read 16384
29
+ end
30
+ end
31
+ self.flush
32
+ self.rewind
33
+ @content_type = @page.response['content-type']
34
+ end
35
+
36
+ def original_filename
37
+ @original_filename ||= begin
38
+ match = @page.uri.path.match(/^.*\/(.+\..+)$/)
39
+ match ? match[1] : @page.filename
40
+ end
41
+ end
42
+ end
43
+
44
+ # Downloads url contents to temp file using Mechanize
45
+ # returns nil if something bad happend
46
+ def get url
47
+ begin
48
+ get! url
49
+ rescue
50
+ nil
51
+ end
52
+ end
53
+
54
+ # Downloads url contents to temp file using Mechanize
55
+ # raise exception if something bad happend
56
+ def get! url
57
+ agent = Mechanize.new
58
+ # agent.max_file_buffer = 0 # always download to temp file
59
+ agent.pluggable_parser.default = DownloadWithFilename
60
+ TmpFile.new agent.get(url)
61
+ end
62
+
63
+ module_function :get
64
+ module_function :get!
65
+ end
@@ -0,0 +1,3 @@
1
+ module MechanizeClip
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mechanize_clip/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Yury Korolev"]
6
+ gem.email = ["yury.korolev@gmail.com"]
7
+ gem.description = %q{Downloads files to temp files with Mechanize gem}
8
+ gem.summary = %q{Mechanize url fetcher for paperclip}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "mechanize_clip"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MechanizeClip::VERSION
17
+
18
+ gem.add_dependency "mechanize", ">= 2.1.0"
19
+ gem.add_development_dependency "rspec", ">= 2.8"
20
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe MechanizeClip do
4
+ it "downloads url contents to temp file" do
5
+ file = subject.get! 'http://google.com'
6
+ file.content_type.should match("text\/html")
7
+ file.original_filename.should == "index.html"
8
+ file.should_not be_nil
9
+ end
10
+
11
+ it "downloads images" do
12
+ file = subject.get! 'http://img.yandex.net/i/www/logo.png'
13
+ file.original_filename.should == "logo.png"
14
+ file.content_type.should == "image/png"
15
+
16
+ File.size(file.path).should > 0
17
+ end
18
+
19
+ it "downloads files from https" do
20
+ file = subject.get! 'https://img.yandex.net/i/www/logo.png'
21
+ file.original_filename.should == "logo.png"
22
+ end
23
+
24
+ it "follows redirects" do
25
+ file = subject.get! "http://bit.ly/cHbjf"
26
+ file.original_filename.should == "logo.png"
27
+ end
28
+
29
+ it "detectes file names" do
30
+ file = subject.get! "https://a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x-hover.png?1324325376"
31
+ file.original_filename.should == "logov7@4x-hover.png"
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'mechanize_clip'
5
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mechanize_clip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yury Korolev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: &70105015015640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70105015015640
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70105015015140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2.8'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70105015015140
36
+ description: Downloads files to temp files with Mechanize gem
37
+ email:
38
+ - yury.korolev@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - lib/mechanize_clip.rb
49
+ - lib/mechanize_clip/version.rb
50
+ - mechanize_clip.gemspec
51
+ - spec/mechanize_clip_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: ''
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.10
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Mechanize url fetcher for paperclip
77
+ test_files:
78
+ - spec/mechanize_clip_spec.rb
79
+ - spec/spec_helper.rb