rails-assets-for-upyun 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc2fcb5f0319a821c2f676419b42a566b61c1e93
4
- data.tar.gz: 1065800146b3eac3930e79000ba1fb823dce430a
3
+ metadata.gz: ae55bd6d901a1f386c08e8973facec323c72ca1a
4
+ data.tar.gz: 5e9e96e06eb4db0d411f095676a4b1c86298c4cc
5
5
  SHA512:
6
- metadata.gz: 7d9ff30662ada5ada2e23eb6747640911ca07c94392c5c81fcfb8840b3301224a2f02b4c9f1b0a546c58735d4b34ce518454fb9282b359c4310c8f082aad0bea
7
- data.tar.gz: cbbda02ec2907337061ea41072bcf6c88e8cb65764142c1f7b1bd47a38a8bcb94b2b8eb20c8e129d25a686ebcb70ebeeaf775f7a0e05828b30c652a1490fe030
6
+ metadata.gz: 106f8028528be7597060259c6d139781c5ecf0b292efe2f29e952cfa9cd75aebb5aef8a87c35f6cef0f6470d89303936cce357272bb3e392323c3b46070fbf26
7
+ data.tar.gz: 2cbbc918f0bd15f62912a295430de3dfe3fff5a71c8c65b2e5fdf62be43647bfdfb508e15ca41c96cf79706a153c98c739b23314fba204f7be70b846167d81be
data/.gitignore CHANGED
@@ -32,3 +32,4 @@ build/
32
32
 
33
33
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
34
  .rvmrc
35
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://ruby.taobao.org"
2
+ gem "rake"
3
+ gem "rest-client"
4
+
5
+ group :development do
6
+ gem "pry"
7
+ end
data/README.md CHANGED
@@ -1,2 +1,39 @@
1
1
  rails-assets-for-upyun
2
2
  ======================
3
+ 把预编译好的静态资源发布到又拍云上(用`rake`)
4
+
5
+ ## 用法
6
+
7
+ 1. 在 `Gemfile` 里引用我: `gem "rails-assets-for-upyun"`
8
+ 2. 首先设置好在生产环境中调用 UpYun 上的资源
9
+
10
+ ```
11
+ # .. config/environments/production.rb
12
+ ..
13
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
14
+ config.action_controller.asset_host = "awesome-bucket.b0.upaiyun.com"
15
+ ..
16
+ ```
17
+
18
+ 3. 在 `Rakefile` 里添加一个任务(起一个最酷的名字!)
19
+
20
+ ```
21
+ namespace :assets do
22
+ task :publish_my_holy_shinning_precompiled_miraculous_assets_to_the_almighty_upyun do
23
+ RailsAssetsForUpyun.publish 'awesome-bucket', 'notjustausername', 'thencomesthepassword'
24
+ end
25
+ end
26
+ ```
27
+
28
+ 4. 然后你就可以在每次发布前(`rake assets:precompile`之后)运行一遍这个 rake 任务把他们同步到又拍云上去了。
29
+
30
+ ## 卖点
31
+
32
+ * 调用 UpYun 提供的 API 进行增量发布,通过 HTTP 通讯,比 FTP 更加稳定、快速
33
+ * 使用 UpYun API 提供的签名授权,不明文传送密码,更加安全!
34
+
35
+ ## 详解
36
+
37
+ ```
38
+ RailsAssetsForUpyun.publish(bucket, username, password, bucket_path="/", localpath='public', upyun_ap="http://v0.api.upyun.com")
39
+ ```
@@ -0,0 +1,31 @@
1
+ class RailsAssetsForUpyun
2
+ def self.publish(bucket, username, password, bucket_path="/", localpath='public', upyun_ap="http://v0.api.upyun.com")
3
+ Dir[File.join localpath, "**", "*"].select{|f| File.file? f }.each do |file|
4
+ url = "/#{bucket}#{bucket_path}#{file[localpath.to_s.size + 1 .. -1]}"
5
+ date = Time.now.httpdate
6
+ size = RestClient.head("#{upyun_ap}#{url}", {\
7
+ Authorization: "UpYun #{username}:#{signature 'HEAD', url, date, 0, password}",
8
+ Date: date}) do |response, request, result, &block|
9
+ case response.code
10
+ when 200
11
+ response.headers[:x_upyun_file_size]
12
+ when 404
13
+ nil
14
+ end
15
+ end
16
+ unless size == (file_size = File.size file)
17
+ file_content = File.read(file)
18
+ p "uploading #{file}.."
19
+ RestClient.put("#{upyun_ap}#{url}", file_content,{\
20
+ Authorization: "UpYun #{username}:#{signature 'PUT', url, date, file_size, password}",
21
+ Date: date,
22
+ mkdir: 'true',
23
+ Content_MD5: Digest::MD5.hexdigest(file_content),
24
+ })
25
+ end
26
+ end
27
+ end
28
+ def self.signature(method, uri, date, content_length, password)
29
+ Digest::MD5.hexdigest "#{method}&#{uri}&#{date}&#{content_length}&#{Digest::MD5.hexdigest password}"
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "rails-assets-for-upyun"
6
+ s.version = "0.0.2"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Michael Yin"]
9
+ s.email = ["layerssss@gmail.com"]
10
+ s.homepage = "https://github.com/layerssss/rails-assets-for-upyun"
11
+ s.summary = %q{Rake task to push precompiled assets to Upyun}
12
+ s.description = %q{Rake task to push precompiled assets to Upyun}
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ s.license = 'MIT'
16
+
17
+ s.add_dependency "rest-client", [">= 1.6.7"]
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-assets-for-upyun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Yin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-16 00:00:00.000000000 Z
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -32,8 +32,11 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - .gitignore
35
+ - Gemfile
35
36
  - LICENSE
36
37
  - README.md
38
+ - lib/rails-assets-for-upyun.rb
39
+ - rails-assets-for-upyun.gemspec
37
40
  homepage: https://github.com/layerssss/rails-assets-for-upyun
38
41
  licenses:
39
42
  - MIT