mattfawcett-s3ify 0.1.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 mattfawcett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = s3ify
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 mattfawcett. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "s3ify"
8
+ gem.summary = %Q{Upload HTML emaisl to S3}
9
+ gem.email = "matt@bolseragency.com"
10
+ gem.homepage = "http://github.com/mattfawcett/s3ify"
11
+ gem.authors = ["mattfawcett"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "s3ify #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/s3ify ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'commander'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/s3ify')
5
+
6
+ program :name, 'S3ify'
7
+ program :version, '1.0.0'
8
+ program :description, 'Take a folder containing a html email file(s) and some images and convert the relative image paths to point at absolute paths on S3 and upload the folder to S3'
9
+
10
+
11
+ command '' do |c|
12
+ c.syntax = "s3ify -b mybycketname -p mypathinmybucket /home/Desktop/PromoEmail/"
13
+ c.description = "Take a folder containing a html email file(s) and some images and convert the relative image paths to point at absolute paths on S3 and upload the folder to S3"
14
+ c.option "--bucket STRING"
15
+ c.option "--path STRING"
16
+ c.when_called do |args, options|
17
+ uploaded_html_files = S3ify.s3ify_folder(args.first, options.bucket, options.path)
18
+ uploaded_html_files.each {|file_name| say file_name}
19
+ end
20
+ end
data/lib/s3ify.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'aws/s3'
3
+ require 'nokogiri'
4
+
5
+
6
+ module S3ify
7
+
8
+ AWS::S3::Base.establish_connection!(
9
+ :access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
10
+ :secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY']
11
+ )
12
+
13
+ def self.s3ify_folder(path, bucket, s3_folder)
14
+ s3_folder = Time.now.to_i.to_s if s3_folder.nil?
15
+ final_html_paths = []
16
+ @base_path = path
17
+ Dir.entries( path ).each do |entry|
18
+ if entry =~ /html/ && !entry.match(/^s3\_/)
19
+ output = File.new("#{path}/s3_#{entry}", "w")
20
+ doc = Nokogiri::HTML(open(path + "/" + entry))
21
+ doc.css('img').each do |image|
22
+ unless image['src'] =~ /^http/
23
+ image.set_attribute 'src', "http://#{bucket}.s3.amazonaws.com/#{s3_folder}#{new_image_path(image['src'])}"
24
+ end
25
+ end
26
+ output.puts doc
27
+ output.close
28
+ final_html_paths << "http://#{bucket}.s3.amazonaws.com/#{s3_folder}/#{entry}"
29
+ end
30
+ end
31
+ copy_file_or_directory_to_s3(path, bucket, s3_folder)
32
+ return final_html_paths
33
+ end
34
+
35
+ def self.copy_file_or_directory_to_s3(file_name, bucket, s3_folder)
36
+ fstat = File.stat(file_name)
37
+ copy_file_to_s3(file_name, bucket, s3_folder) if fstat.file? || fstat.symlink?
38
+
39
+ if fstat.directory?
40
+ Dir.entries(file_name).each do |entry|
41
+ copy_file_or_directory_to_s3(file_name + "/" + entry, bucket, s3_folder) unless entry =~ /^\./
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.copy_file_to_s3(file_name, bucket, s3_folder)
47
+ AWS::S3::S3Object.store(s3_folder + file_name.gsub(@base_path, ''), open(file_name), bucket, :access => :public_read)
48
+ end
49
+
50
+ def self.new_image_path(old_path)
51
+ if old_path =~ /^\.\//
52
+ return old_path.gsub(/^\.\//, '/')
53
+ elsif !old_path.match(/^\//)
54
+ return "/" + old_path
55
+ end
56
+ return old_path
57
+ end
58
+
59
+
60
+ end
data/s3ify.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{s3ify}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["mattfawcett"]
9
+ s.date = %q{2009-06-25}
10
+ s.default_executable = %q{s3ify}
11
+ s.email = %q{matt@bolseragency.com}
12
+ s.executables = ["s3ify"]
13
+ s.extra_rdoc_files = [
14
+ "LICENSE",
15
+ "README.rdoc"
16
+ ]
17
+ s.files = [
18
+ ".document",
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "bin/s3ify",
25
+ "lib/s3ify.rb",
26
+ "s3ify.gemspec",
27
+ "spec/fixtures/expected/index.html",
28
+ "spec/fixtures/original/images/image1.jpg",
29
+ "spec/fixtures/original/images/image2.jpg",
30
+ "spec/fixtures/original/images/image3.jpg",
31
+ "spec/fixtures/original/images/nested/image4.jpg",
32
+ "spec/fixtures/original/index.html",
33
+ "spec/fixtures/original/s3_index.html",
34
+ "spec/s3ify_spec.rb",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.has_rdoc = true
38
+ s.homepage = %q{http://github.com/mattfawcett/s3ify}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.1}
42
+ s.summary = %q{Upload HTML emaisl to S3}
43
+ s.test_files = [
44
+ "spec/s3ify_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 2
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ else
54
+ end
55
+ else
56
+ end
57
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2
+ <html>
3
+ <head></head>
4
+ <body>
5
+ <h1>Hello World</h1>
6
+ <h2>Image 1 with no leading slash</h2>
7
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image1.jpg"><h2>Image 2 with leading slash</h2>
8
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image2.jpg"><h2>Image 3 with leading dot slash</h2>
9
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image2.jpg"><h2>Image 4 nested</h2>
10
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/nested/image4.jpg">
11
+ </body>
12
+ </html>
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2
+ <html>
3
+ <head></head>
4
+ <body>
5
+ <h1>Hello World</h1>
6
+ <h2>Image 1 with no leading slash</h2>
7
+ <img src="images/image1.jpg"/>
8
+
9
+ <h2>Image 2 with leading slash</h2>
10
+ <img src="/images/image2.jpg"/>
11
+
12
+ <h2>Image 3 with leading dot slash</h2>
13
+ <img src="./images/image2.jpg"/>
14
+
15
+ <h2>Image 4 nested</h2>
16
+ <img src="./images/nested/image4.jpg"/>
17
+ </body>
18
+ </html>
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2
+ <html>
3
+ <head></head>
4
+ <body>
5
+ <h1>Hello World</h1>
6
+ <h2>Image 1 with no leading slash</h2>
7
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image1.jpg"><h2>Image 2 with leading slash</h2>
8
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image2.jpg"><h2>Image 3 with leading dot slash</h2>
9
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/image2.jpg"><h2>Image 4 nested</h2>
10
+ <img src="http://mattstest.s3.amazonaws.com/mattspromo/images/nested/image4.jpg">
11
+ </body>
12
+ </html>
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "S3ify" do
4
+
5
+ describe "copy_file_or_directory_to_s3" do
6
+ before(:each) do
7
+ FileUtils.rm(File.dirname(__FILE__) + '/fixtures/original/s3_index.html') if File.exists?(File.dirname(__FILE__) + '/fixtures/original/s3_index.html')
8
+ end
9
+
10
+ it "should create a new version of the file with a prefix of s3_ with the links converted to point at s3" do
11
+ S3ify.stub!(:copy_file_or_directory_to_s3)
12
+ S3ify.s3ify_folder(File.dirname(__FILE__) + '/fixtures/original', bucket = 'mattstest', s3_folder = 'mattspromo')
13
+ File.open(File.dirname(__FILE__) + '/fixtures/original/s3_index.html').read.should eql(File.open(File.dirname(__FILE__) + '/fixtures/expected/index.html').read)
14
+ end
15
+ end
16
+
17
+
18
+ describe "new_image_path" do
19
+ it "should leave paths that atart with a slash" do
20
+ S3ify.new_image_path("/images/pic1.png").should eql("/images/pic1.png")
21
+ end
22
+
23
+ it "should replace a ./ with just a slash" do
24
+ S3ify.new_image_path("./images/pic1.png").should eql("/images/pic1.png")
25
+ end
26
+
27
+ it "should add a leading slash if there isn't one" do
28
+ S3ify.new_image_path("images/pic1.png").should eql("/images/pic1.png")
29
+ end
30
+ end
31
+
32
+ describe "uploading of files to S3" do
33
+ it "each file should get uploaded to the correct folder in the correct bucket" do
34
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/index.html').at_least(:once).and_return(mock_index = File.open(File.dirname(__FILE__) + '/fixtures/original/index.html'))
35
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/index.html', mock_index, 'mattstest', {:access=>:public_read})
36
+
37
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/s3_index.html').at_least(:once).and_return(mock_s3_index = File.open(File.dirname(__FILE__) + '/fixtures/original/s3_index.html'))
38
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/s3_index.html', mock_s3_index, 'mattstest', {:access=>:public_read})
39
+
40
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/images/image1.jpg').at_least(:once).and_return(mock_image_1 = File.open(File.dirname(__FILE__) + '/fixtures/original/images/image1.jpg'))
41
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/images/image1.jpg', mock_image_1, 'mattstest', {:access=>:public_read})
42
+
43
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/images/image2.jpg').at_least(:once).and_return(mock_image_2 = File.open(File.dirname(__FILE__) + '/fixtures/original/images/image2.jpg'))
44
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/images/image2.jpg', mock_image_2, 'mattstest', {:access=>:public_read})
45
+
46
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/images/image3.jpg').at_least(:once).and_return(mock_image_3 = File.open(File.dirname(__FILE__) + '/fixtures/original/images/image3.jpg'))
47
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/images/image3.jpg', mock_image_3, 'mattstest', {:access=>:public_read})
48
+
49
+ S3ify.should_receive(:open).with(File.dirname(__FILE__) + '/fixtures/original/images/nested/image4.jpg').at_least(:once).and_return(mock_image_4 = File.open(File.dirname(__FILE__) + '/fixtures/original/images/nested/image4.jpg'))
50
+ AWS::S3::S3Object.should_receive(:store).with('mattspromo/images/nested/image4.jpg', mock_image_4, 'mattstest', {:access=>:public_read})
51
+
52
+ S3ify.s3ify_folder(File.dirname(__FILE__) + '/fixtures/original', bucket = 'mattstest', s3_folder = 'mattspromo')
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 's3ify'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattfawcett-s3ify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mattfawcett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-25 00:00:00 -07:00
13
+ default_executable: s3ify
14
+ dependencies: []
15
+
16
+ description:
17
+ email: matt@bolseragency.com
18
+ executables:
19
+ - s3ify
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - bin/s3ify
33
+ - lib/s3ify.rb
34
+ - s3ify.gemspec
35
+ - spec/fixtures/expected/index.html
36
+ - spec/fixtures/original/images/image1.jpg
37
+ - spec/fixtures/original/images/image2.jpg
38
+ - spec/fixtures/original/images/image3.jpg
39
+ - spec/fixtures/original/images/nested/image4.jpg
40
+ - spec/fixtures/original/index.html
41
+ - spec/fixtures/original/s3_index.html
42
+ - spec/s3ify_spec.rb
43
+ - spec/spec_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/mattfawcett/s3ify
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Upload HTML emaisl to S3
70
+ test_files:
71
+ - spec/s3ify_spec.rb
72
+ - spec/spec_helper.rb