kanoko 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4136c44dd096d3ec43f5f699ed6479c17fa4e4d1
4
+ data.tar.gz: 948b66876485aae4a6bfd43454d45efb0afc2d4e
5
+ SHA512:
6
+ metadata.gz: 302f15f1eb1d3cabf61cadb48bc98fb951e3eea4d3e886cf348d6917e4f879bb413e1c8560bbd3f41e0c996cc50e5f19070fab6591afed1fb4e041806e9903c8
7
+ data.tar.gz: 5614f54f60f066149da8a40fd17c72716db1e0f4676d3efba6b67db877e1008db2faca37306f4d7ebd0d00c26ee94a6747f1875a493f80922e0f3e63a03796ed
@@ -0,0 +1,2 @@
1
+ pkg/
2
+ tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kanoko (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.6.1)
10
+ rake (10.4.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler
17
+ kanoko!
18
+ minitest
19
+ rake
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 spice-life
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,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ task :default => [:test]
4
+ Rake::TestTask.new {|t| t.libs << 'test'}
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "kanoko/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kanoko"
7
+ spec.version = Kanoko::VERSION
8
+ spec.authors = ["ksss"]
9
+ spec.email = ["co000ri@gmail.com"]
10
+ spec.summary = %q{kanoko is a active image generater library.}
11
+ spec.description = %q{kanoko is a active image generater library.}
12
+ spec.homepage = "https://github.com/spice-life/kanoko"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "minitest"
23
+ end
@@ -0,0 +1,37 @@
1
+ require 'uri'
2
+ require 'kanoko/configure'
3
+
4
+ module Kanoko
5
+ # example:
6
+ # Kanoko.configure.resource_host = "http://example.com"
7
+ # p Kanoko.configure #=> #<Kanoko::Configure ...>
8
+ def configure
9
+ @configure ||= Configure.new
10
+ end
11
+ module_function :configure
12
+
13
+ def configure=(value)
14
+ @configure = value
15
+ end
16
+
17
+ def url_for(func, args, src)
18
+ if configure.resource_host.nil?
19
+ fail ConfigureError, "`resource_host' must be set"
20
+ end
21
+ "#{configure.resource_host}#{make_path(func, args, src)}"
22
+ end
23
+ module_function :url_for
24
+
25
+ def make_hash(*args)
26
+ configure.hash_proc.call(*args)
27
+ end
28
+ module_function :make_hash
29
+
30
+ private
31
+
32
+ def make_path(func, args, src)
33
+ hash = make_hash(func, args, src)
34
+ "/#{hash}/#{[func, args, src].map{|i| URI.encode_www_form_component(i)}.join('/')}"
35
+ end
36
+ module_function :make_path
37
+ end
@@ -0,0 +1,60 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'kanoko/errors'
4
+
5
+ module Kanoko
6
+ class Configure
7
+ attr_accessor :digest_func, :secret_key, :hash_proc
8
+
9
+ # resource_host expect String
10
+ # digest_func expect String
11
+ # secret_key expect String
12
+ # hash_proc expect Proc
13
+ #
14
+ # example:
15
+ # Kanoko.configure.tap do |c|
16
+ # c.resource_host = "http://example.com"
17
+ # c.digest_func = "sha1"
18
+ # c.secret_key = "secret"
19
+ # end
20
+ # Kanoko.url_for(:resize, "100x100") #=> "http://example.com/.../.../..."
21
+ def initialize
22
+ @resource_host = nil
23
+ @digest_func = nil
24
+ @secret_key = nil
25
+ @hash_proc = ->(*args){
26
+ if @digest_func.nil? || @secret_key.nil?
27
+ fail ConfigureError, "`digest_func' and `secret_key' must be set"
28
+ end
29
+ Base64.urlsafe_encode64(
30
+ OpenSSL::HMAC.digest @digest_func,
31
+ @secret_key,
32
+ args.map(&:to_s).join(',')
33
+ )
34
+ }
35
+ end
36
+
37
+ def resource_host=(host)
38
+ @resource_host = normalize_url(host)
39
+ end
40
+
41
+ def resource_host
42
+ @resource_host
43
+ end
44
+
45
+ private
46
+
47
+ def normalize_url(host)
48
+ case host
49
+ when %r{\Ahttps?://}
50
+ host
51
+ when %r{\A[^/]}
52
+ "http://#{host}"
53
+ when %r{\A//}
54
+ "http:#{host}"
55
+ else
56
+ fail ConfigureError, "invalid resource_host `#{host}'"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Kanoko
2
+ class ConfigureError < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Kanoko
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require 'minitest/autorun'
2
+ require 'kanoko/configure'
3
+
4
+ class TestKanokoConfigure < Minitest::Test
5
+ def setup
6
+ @config = Kanoko::Configure.new
7
+ end
8
+
9
+ def test_resource_host
10
+ assert_raises(Kanoko::ConfigureError) { @config.resource_host = "/example.com" }
11
+
12
+ @config.resource_host = "example.com"
13
+ assert_equal "http://example.com", @config.resource_host
14
+
15
+ @config.resource_host = "http://example.com"
16
+ assert_equal "http://example.com", @config.resource_host
17
+
18
+ @config.resource_host = "https://example.com"
19
+ assert_equal "https://example.com", @config.resource_host
20
+ end
21
+
22
+ def test_hash_proc_by_default_error
23
+ @config.digest_func = nil
24
+ @config.secret_key = nil
25
+ assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
26
+
27
+ @config.digest_func = "sha1"
28
+ @config.secret_key = nil
29
+ assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
30
+
31
+ @config.digest_func = nil
32
+ @config.secret_key = "test"
33
+ assert_raises(Kanoko::ConfigureError){ @config.hash_proc.call }
34
+ end
35
+
36
+ def test_hash_proc_by_default
37
+ @config.digest_func = "sha1"
38
+ @config.secret_key = "test"
39
+ assert_equal "yrYrwA2D_XJwEyaWOr3S8GPWtd8=", @config.hash_proc.call("aaa")
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'kanoko'
3
+
4
+ class TestKanoko < Minitest::Test
5
+ def setup
6
+ Kanoko.configure.digest_func = "sha1"
7
+ Kanoko.configure.secret_key = "test"
8
+ Kanoko.configure.resource_host = "http://example.com"
9
+ end
10
+
11
+ def change_hash_proc(hash_proc)
12
+ before = Kanoko.configure.hash_proc
13
+ Kanoko.configure.hash_proc = hash_proc
14
+ yield
15
+ Kanoko.configure.hash_proc = before
16
+ end
17
+
18
+ def test_configure
19
+ assert_kind_of Kanoko::Configure, Kanoko.configure
20
+ end
21
+
22
+ def test_configure_with_block
23
+ assert_equal "sha1", Kanoko.configure.digest_func
24
+ Kanoko.configure.digest_func = "ok"
25
+ assert_equal "ok", Kanoko.configure.digest_func
26
+ end
27
+
28
+ def test_url_for_with_default_hash_proc
29
+ change_hash_proc(proc{ "aaa" }) do
30
+ assert_equal "http://example.com/aaa/test_func/test_args/test_path", Kanoko.url_for(:test_func, "test_args", "test_path")
31
+ assert_equal "http://example.com/aaa/test_func/%2F%3F-_%3D%21%40%23%3C%3E%5C/%2F%3F-_%3D%21%40%23%3C%3E%5C", Kanoko.url_for(:test_func, "/?-_=!@#<>\\", "/?-_=!@#<>\\")
32
+ end
33
+ end
34
+
35
+ def test_make_hash
36
+ change_hash_proc(proc{ "bbb" }) do
37
+ assert_equal "bbb", Kanoko.make_hash(:test_func, "test_args", "test_path")
38
+ end
39
+ end
40
+
41
+ def test_make_hash_custom_hash_proc
42
+ change_hash_proc(proc{ "ccc" }) do
43
+ assert_equal "ccc", Kanoko.make_hash(:test_func, "test_args", "test_path")
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kanoko
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: kanoko is a active image generater library.
56
+ email:
57
+ - co000ri@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - Rakefile
67
+ - kanoko.gemspec
68
+ - lib/kanoko.rb
69
+ - lib/kanoko/configure.rb
70
+ - lib/kanoko/errors.rb
71
+ - lib/kanoko/version.rb
72
+ - test/test_configure.rb
73
+ - test/test_kanoko.rb
74
+ homepage: https://github.com/spice-life/kanoko
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: kanoko is a active image generater library.
98
+ test_files:
99
+ - test/test_configure.rb
100
+ - test/test_kanoko.rb