static_sync 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,18 @@
1
+ .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in static_sync.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Shanon McQuay
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,80 @@
1
+ # StaticSync
2
+
3
+ This gem provides a stand alone mechanism for uploading static websites to cloud hosting providers such as
4
+ [Amazon AWS](http://en.wikipedia.org/wiki/Amazon_S3#Hosting_entire_websites).
5
+
6
+ ### Features
7
+
8
+ * Standalone.
9
+ * Configurable caching.
10
+ * Automatic gzip compression.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ gem install static_sync
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ In your project directory create a `.static` file:
21
+
22
+ ```
23
+ > cat .static
24
+ local:
25
+ directory: build # The directory to upload
26
+
27
+ remote:
28
+ provider: AWS
29
+ username: my-aws-key
30
+ password: my-aws-secret
31
+ directory: my-aws-bucket
32
+ ```
33
+
34
+ And run the following command any time you want to upload.
35
+
36
+ ```bash
37
+ static_sync
38
+ ```
39
+
40
+ ### Environment Variables
41
+
42
+ You can reference environment variables in your `.static` file like this:
43
+
44
+ ```
45
+ remote:
46
+ provider: AWS
47
+ username: <%= ENV['s3_key'] %>
48
+ password: <%= ENV['s3_secret'] %>
49
+ directory: <%= ENV['s3_bucket'] %>
50
+ ```
51
+
52
+ ### Cache Control
53
+
54
+ By default uploaded files are not cached.
55
+
56
+ You can cache content for a given number of seconds by updating your `.static` file:
57
+
58
+ ```
59
+ cache:
60
+ javascript: 31536000
61
+ css: 31536000
62
+ ```
63
+
64
+ ### Compression
65
+
66
+ By default uploaded files are not compressed.
67
+
68
+ You can gzip all non binary content by updating your `.static` file:
69
+
70
+ ```
71
+ gzip: true
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/static_sync ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "static_sync"
5
+
6
+ StaticSync.upload
7
+
@@ -0,0 +1,36 @@
1
+ module StaticSync
2
+ class Config < Hash
3
+
4
+ # TODO Validate.
5
+
6
+ def cache
7
+ self['cache'] || {}
8
+ end
9
+
10
+ def source
11
+ self['local']['directory']
12
+ end
13
+
14
+ def storage
15
+ Fog::Storage.new({
16
+ :provider => self['remote']['provider'],
17
+ :aws_access_key_id => self['remote']['username'],
18
+ :aws_secret_access_key => self['remote']['password']
19
+ })
20
+ end
21
+
22
+ def storage_directory
23
+ self['remote']['directory']
24
+ end
25
+
26
+ def gzip
27
+ self['gzip']
28
+ end
29
+
30
+ def load(path = '.static')
31
+ self.replace(YAML.load_file(ERB.new(path).result))
32
+ self
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module StaticSync
2
+ class Meta
3
+
4
+ def initialize(config)
5
+ @config = config
6
+ end
7
+
8
+ #TODO This class / method has too many responsibilities.
9
+ def for(file)
10
+ meta = {
11
+ :key => file,
12
+ :body => File.new(file),
13
+ :public => true
14
+ }
15
+ MIME::Types::of(file).each do |mime|
16
+ if @config.gzip
17
+ unless mime.binary?
18
+ meta.merge!(
19
+ :body => gzip(file),
20
+ :content_encoding => 'gzip'
21
+ )
22
+ end
23
+ end
24
+ meta.merge!(
25
+ :content_type => MIME::Type.simplified(mime)
26
+ )
27
+ if @config.cache.has_key?(mime.sub_type)
28
+ expiry = @config.cache[mime.sub_type].to_i
29
+ meta.merge!(
30
+ :cache_control => "public, max-age=#{expiry}"
31
+ )
32
+ end
33
+ end
34
+ meta
35
+ end
36
+
37
+ def gzip(file)
38
+ zipped = Tempfile.new("static_sync")
39
+ Zlib::GzipWriter.open(zipped) do |archive|
40
+ archive.write File.read(file)
41
+ end
42
+ zipped
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "meta"
2
+
3
+ module StaticSync
4
+ class Storage
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ @meta = Meta.new(config)
9
+ end
10
+
11
+ def sync
12
+ Dir.chdir(@config.source) do
13
+ Dir.glob("**/*.*") do |file|
14
+ log.info("Uploading #{file}")
15
+ @config.storage.directories.get(@config.storage_directory).files.create(
16
+ @meta.for(file)
17
+ )
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def log
25
+ @log ||= begin
26
+ logger = Logger.new(STDOUT)
27
+ logger.formatter = proc do |severity, datetime, progname, msg|
28
+ "#{datetime}: #{severity} -- #{msg}\n"
29
+ end
30
+ logger
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module StaticSync
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,16 @@
1
+ require "rubygems"
2
+ require "fog"
3
+ require "erb"
4
+ require "logger"
5
+ require "tempfile"
6
+ require "zlib"
7
+
8
+ require_relative "static_sync/version"
9
+ require_relative "static_sync/config"
10
+ require_relative "static_sync/storage"
11
+
12
+ module StaticSync
13
+ def self.upload
14
+ Storage.new(Config.new.load).sync
15
+ end
16
+ end
File without changes
File without changes
data/spec/meta_spec.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "cgi"
2
+ require "mime/types"
3
+ require "tempfile"
4
+ require "zlib"
5
+ require "./lib/static_sync/config"
6
+ require "./lib/static_sync/meta"
7
+
8
+ describe StaticSync::Meta do
9
+
10
+ let(:config) { StaticSync::Config.new }
11
+
12
+ subject do
13
+ StaticSync::Meta.new(config)
14
+ end
15
+
16
+ describe "all files" do
17
+
18
+ it "should be public" do
19
+ Dir.chdir("spec/fixtures/site") do
20
+ subject.for("index.html").should include(
21
+ :key => "index.html",
22
+ :content_type => "text/html",
23
+ :public => true
24
+ )
25
+ subject.for("assets/stylesheets/screen.css").should include(
26
+ :key => "assets/stylesheets/screen.css",
27
+ :content_type => "text/css",
28
+ :public => true
29
+ )
30
+ subject.for("assets/javascripts/jquery.min.js").should include(
31
+ :key => "assets/javascripts/jquery.min.js",
32
+ :content_type => "application/javascript",
33
+ :public => true
34
+ )
35
+ end
36
+ end
37
+
38
+ it "should cache files when requested" do
39
+ config['cache'] = {
40
+ 'css' => '86400',
41
+ 'javascript' => '86400'
42
+ }
43
+ Dir.chdir("spec/fixtures/site") do
44
+ subject.for("index.html").should_not include(
45
+ :cache_control
46
+ )
47
+ subject.for("assets/stylesheets/screen.css").should include(
48
+ :cache_control
49
+ )
50
+ subject.for("assets/javascripts/jquery.min.js").should include(
51
+ :cache_control
52
+ )
53
+ end
54
+ end
55
+
56
+ it "should gzip files when requested" do
57
+ Dir.chdir("spec/fixtures/site") do
58
+ subject.for("index.html").should_not include(
59
+ :content_encoding => 'gzip'
60
+ )
61
+ config['gzip'] = true
62
+ subject.for("index.html").should include(
63
+ :content_encoding => 'gzip'
64
+ )
65
+ subject.for("assets/stylesheets/screen.css").should include(
66
+ :content_encoding => 'gzip'
67
+ )
68
+ subject.for("assets/javascripts/jquery.min.js").should include(
69
+ :content_encoding => 'gzip'
70
+ )
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,49 @@
1
+ require "./lib/static_sync"
2
+
3
+ describe StaticSync::Storage do
4
+
5
+ let(:config) do
6
+ StaticSync::Config.new.merge({
7
+ 'local' => {
8
+ 'directory' => 'spec/fixtures/site'
9
+ },
10
+ 'remote' => {
11
+ 'provider' => 'AWS',
12
+ 'username' => 'lol',
13
+ 'password' => 'cat',
14
+ 'directory' => 'bucket'
15
+ }
16
+ })
17
+ end
18
+
19
+ subject do
20
+ StaticSync::Storage.new(config)
21
+ end
22
+
23
+ before do
24
+ Fog.mock!
25
+ config.storage.directories.create(
26
+ :key => config.storage_directory,
27
+ :public => true
28
+ )
29
+ end
30
+
31
+ after do
32
+ Fog::Mock.reset
33
+ end
34
+
35
+ describe "html files" do
36
+
37
+ it "are uploaded to the remote directory" do
38
+ subject.sync
39
+
40
+ config.storage.directories.get(config.storage_directory).files.map(&:key).should == [
41
+ "assets/javascripts/jquery.min.js",
42
+ "assets/stylesheets/screen.css",
43
+ "index.html"
44
+ ]
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'static_sync/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "static_sync"
8
+ gem.version = StaticSync::VERSION
9
+ gem.authors = ["Shanon McQuay"]
10
+ gem.email = ["shanonmcquay@gmail.com"]
11
+ gem.summary = %q{Uploads your static website to cloud storage.}
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.bindir = "bin"
15
+ gem.executables << "static_sync"
16
+ gem.test_files = Dir.glob("spec/**/*.rb")
17
+ gem.require_paths = ["lib"]
18
+ gem.has_rdoc = false
19
+
20
+ gem.add_dependency("fog", [">= 1.5.0"])
21
+
22
+ gem.add_development_dependency('rspec')
23
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shanon McQuay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fog
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description:
47
+ email:
48
+ - shanonmcquay@gmail.com
49
+ executables:
50
+ - static_sync
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/static_sync
60
+ - lib/static_sync.rb
61
+ - lib/static_sync/config.rb
62
+ - lib/static_sync/meta.rb
63
+ - lib/static_sync/storage.rb
64
+ - lib/static_sync/version.rb
65
+ - spec/fixtures/site/assets/javascripts/jquery.min.js
66
+ - spec/fixtures/site/assets/stylesheets/screen.css
67
+ - spec/fixtures/site/index.html
68
+ - spec/meta_spec.rb
69
+ - spec/storage_spec.rb
70
+ - static_sync.gemspec
71
+ homepage:
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Uploads your static website to cloud storage.
95
+ test_files:
96
+ - spec/meta_spec.rb
97
+ - spec/storage_spec.rb
98
+ has_rdoc: false