rack-qiniu_mock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.swp
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
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 rack-qiniu_mock.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-qiniu_mock (0.0.1)
5
+ mini_magick
6
+ rack
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ mini_magick (3.4)
12
+ subexec (~> 0.2.1)
13
+ rack (1.4.1)
14
+ subexec (0.2.2)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ rack-qiniu_mock!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 LI Daobing
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.
@@ -0,0 +1,55 @@
1
+ # Rack::QiniuMock
2
+
3
+ 七牛云存储的开发辅助工具,方便地支持在本地进行图片缩放。
4
+
5
+ 七牛云存储的反向代理CDN+图片缩放的服务很好用,但有一个问题,在开发环境下,无法享受到该服务。这个 gem 提供了在开发环境下模拟七牛云存储基本图片缩放的功能。
6
+
7
+ ## 使用方法
8
+
9
+ 完整的范例参考: https://github.com/lidaobing/paperclip-qiniu-example/tree/local_storage
10
+
11
+ * 将如下一行加入到你的 Rails 应用的 `Gemfile
12
+
13
+ ```ruby
14
+ gem 'rack-qiniu_mock', :group => :development
15
+ ```
16
+
17
+ * 在 `config/environments/development.rb` 中间加入如下一行
18
+
19
+ ```ruby
20
+ config.middleware.insert_after ActionDispatch::Static, ::Rack::QiniuMock
21
+ ```
22
+
23
+ * 创建 `config/qiniu_mock.yml`, 内容如下
24
+
25
+ ```yaml
26
+ separator: '-'
27
+ suffixs:
28
+ large: ['resize', '660x400']
29
+ medium: ['resize', '220x150']
30
+ small: ['resize_with_crop', '50x50']
31
+ ```
32
+
33
+ * 下载 `https://raw.github.com/lidaobing/paperclip-qiniu-example/local_storage/config/initializers/qiniu_image.rb` 到 `config/initializers/qiniu_image.rb`
34
+
35
+ * 修改 image_tag 为如下的形式
36
+
37
+ ```ruby
38
+ <%= image_tag qiniu_image(image.file, "medium")%>
39
+ ```
40
+
41
+ 其中 `image.file` 为 `Paperclip::Attachment`, `medium` 为你想使用的变换格式。
42
+
43
+
44
+ ## TODO
45
+
46
+ * 合并 qiniu_image 到该 gem
47
+ * 支持其他格式类型
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rack/qiniu_mock"
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ module QiniuMock
3
+ autoload :Context, 'rack/qiniu_mock/context'
4
+
5
+ def self.new(app, options={}, &b)
6
+ Context.new(app, options, &b)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ require "mini_magick"
2
+ require 'yaml'
3
+
4
+ module Rack::QiniuMock
5
+ class Context
6
+ def initialize(app, opts={})
7
+ @app = app
8
+ @root = File.expand_path("public", Rails.root)
9
+ @config = YAML.load_file(File.expand_path("config/qiniu_mock.yml", Rails.root))
10
+ @separator = @config['separator'] || '-'
11
+ @suffixs = @config['suffixs'] || {}
12
+ yield self if block_given?
13
+ end
14
+
15
+ def call(env)
16
+ original_file, mtime = get_original_file(::Rack::Utils.unescape(env['PATH_INFO']))
17
+ if original_file
18
+ data = original_file.to_blob
19
+ [200, {
20
+ "Content-Type" => original_file.mime_type,
21
+ "Content-Length" => data.size.to_s,
22
+ "Cache-Control" => "public, max-age=31536000",
23
+ "Last-Modified" => mtime.httpdate
24
+ }, [data]]
25
+ else
26
+ @app.call(env)
27
+ end
28
+ end
29
+
30
+ private
31
+ def resize(image, size)
32
+ image.resize size
33
+ image
34
+ end
35
+
36
+ def resize_with_crop(image, size)
37
+ image.combine_options do |c|
38
+ c.resize "#{size}^"
39
+ c.gravity "center"
40
+ c.crop "#{size}+0+0"
41
+ end
42
+ image
43
+ end
44
+
45
+ def get_original_file(path)
46
+ path_format = path.split(@separator)[-1]
47
+
48
+ config = @suffixs[path_format]
49
+ return nil if config.nil?
50
+
51
+ original_path = @root + path.chomp(@separator + path_format)
52
+ return nil unless File.exist?(original_path)
53
+
54
+ res = MiniMagick::Image.open(original_path)
55
+ [self.__send__(config[0], res, *config[1..-1]), File.mtime(original_path)]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module QiniuMock
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/qiniu_mock/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rack-qiniu_mock"
8
+ gem.version = Rack::QiniuMock::VERSION
9
+ gem.authors = ["LI Daobing"]
10
+ gem.email = ["lidaobing@gmail.com"]
11
+ gem.description = %q{qiniutek.com image resize service mocker}
12
+ gem.summary = %q{mock qiniutek.com image resize service so you can use image resize service under development}
13
+ gem.homepage = "https://github.com/rdd-giga/rack-qiniu_mock"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency "rack"
20
+ gem.add_dependency "mini_magick"
21
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-qiniu_mock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - LI Daobing
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mini_magick
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: qiniutek.com image resize service mocker
47
+ email:
48
+ - lidaobing@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/rack-qiniu_mock.rb
60
+ - lib/rack/qiniu_mock.rb
61
+ - lib/rack/qiniu_mock/context.rb
62
+ - lib/rack/qiniu_mock/version.rb
63
+ - rack-qiniu_mock.gemspec
64
+ homepage: https://github.com/rdd-giga/rack-qiniu_mock
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: mock qiniutek.com image resize service so you can use image resize service
88
+ under development
89
+ test_files: []
90
+ has_rdoc: