DavinciRubyClientSDK 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23aff6934752e33eec4f946f8c962d9134ee42a7
4
+ data.tar.gz: 8a269d7b9ee275ad42bce94517906e72d58f896a
5
+ SHA512:
6
+ metadata.gz: a8819fd10d1a058624f6429b6ac8c9a149d4fcde94af041763b8ab2c4716ec2834f69f4fe2f5b26684b68387a3bbd587ce3eebe1031ea4b2491fa07443dff77e
7
+ data.tar.gz: 5061cf38e29318dd7203654e82ea55bbc9855df548950d25b7cc20029e9e04135d09973eb2425bfa4758a732182cff0875520e94dab141dca40d0195ff95f46e
data/.gitignore ADDED
@@ -0,0 +1,29 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
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
18
+
19
+ .idea
20
+ .DS_Store
21
+ Thumbs.db
22
+ .cache
23
+ .project
24
+ .settings
25
+ .tmproj
26
+ *.esproj
27
+ nbproject
28
+
29
+ bin
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'DavinciRubyClientSDK/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'DavinciRubyClientSDK'
8
+ spec.version = DavinciRubyClientSDK::VERSION
9
+ spec.authors = ['RaymondChou']
10
+ spec.email = ['zhouyt@outlook.com']
11
+ spec.description = %q{Davinci Client SDK Gem for Ruby}
12
+ spec.summary = %q{Davinci Client SDK Gem for Ruby}
13
+ spec.homepage = 'https://github.com/iiseeuu/DavinciRubyClientSDK'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'bundler', '~> 1.3'
22
+ spec.add_dependency 'rake'
23
+ spec.add_dependency 'faraday'
24
+ end
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://ruby.taobao.org/'
2
+
3
+ gem 'faraday'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 RaymondChou
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,31 @@
1
+ # DavinciRubyClientSDK
2
+
3
+ [![Build Status](https://travis-ci.org/iiseeuu/DavinciRubyClientSDK.png?branch=master)](https://travis-ci.org/iiseeuu/DavinciRubyClientSDK)
4
+
5
+ Davinci Client SDK Gem for Ruby
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'DavinciRubyClientSDK'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install DavinciRubyClientSDK
20
+
21
+ ## Usage
22
+
23
+ TODO
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,69 @@
1
+ module DavinciRubyClientSDK
2
+ class Blueprint
3
+
4
+ attr_accessor :tag, :layers, :tmp_layer, :tmp_inspiration
5
+
6
+ def initialize(tag)
7
+ @blueprint = Hash.new
8
+ @layers = Array.new
9
+ self.tag = tag
10
+ end
11
+
12
+ def add_layer(index)
13
+ self.tmp_layer = Hash.new
14
+ self.tmp_inspiration = Hash.new
15
+ self.tmp_layer[:layer] = index
16
+
17
+ yield self if block_given?
18
+
19
+ self.tmp_layer[:inspiration] = self.tmp_inspiration
20
+
21
+ @layers << self.tmp_layer
22
+
23
+ self.tmp_layer = nil
24
+ self.tmp_inspiration = nil
25
+ end
26
+
27
+ def canvas
28
+ @canvas ||= Hash.new
29
+ yield self if block_given?
30
+ @canvas
31
+ end
32
+
33
+ def add_canvas_option(key, value)
34
+ @canvas[key.to_sym] = value
35
+ end
36
+
37
+ def mount
38
+ @mount ||= Hash.new
39
+ yield self if block_given?
40
+ @mount
41
+ end
42
+
43
+ def add_mount_option(key, value)
44
+ @mount[key.to_sym] = value
45
+ end
46
+
47
+ def technique=(value)
48
+ self.tmp_layer[:technique] = value.to_s
49
+ end
50
+
51
+ def inspiration=(value = {})
52
+ self.tmp_inspiration = value
53
+ end
54
+
55
+ def add_inspiration(key, value)
56
+ self.tmp_inspiration[key.to_sym] = value
57
+ end
58
+
59
+ def to_blueprint
60
+ {
61
+ :tag => @tag,
62
+ :layers => @layers,
63
+ :canvas => @canvas,
64
+ :mount => @mount
65
+ }
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ module DavinciRubyClientSDK
2
+ class Common
3
+
4
+ class << self
5
+ def check_app_key
6
+ if DavinciRubyClientSDK::Setting.app_key.nil? or DavinciRubyClientSDK::Setting.app_secret.nil? or DavinciRubyClientSDK::Setting.server_address.nil?
7
+ raise '未设置app_key, app_secret, server_address'
8
+ end
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require 'openssl'
2
+ require 'json'
3
+
4
+ module DavinciRubyClientSDK
5
+ class Receiver
6
+ attr_accessor :mode
7
+
8
+ def initialize(mode = :notify)
9
+ @mode = mode
10
+ end
11
+
12
+ def receive(params, headers = [])
13
+ if @mode ==:notify
14
+ if self.sign_check(params, headers['Davinci-Signature'], headers['Service'], headers['User-Agent'])
15
+ error = params['error'].nil? ? false : true
16
+ return error, params
17
+ else
18
+ # 验签失败
19
+ params['error'] = 'SignCheckError or ServerNotTrust'
20
+ return true, params
21
+ end
22
+
23
+ elsif @mode == :response
24
+ begin
25
+ params = JSON.parse params
26
+ error = params['error'].nil? ? false : true
27
+ rescue Exception => err
28
+ error = true
29
+ end
30
+ return error, params
31
+ else
32
+ raise 'receiver not found!'
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def sign_check(params, header_signs, service, ua)
39
+ sorted_body = Hash[params.sort]
40
+ body_array = Array.new
41
+ sorted_body.map do |k, v|
42
+ body_array << "#{k}=#{v}"
43
+ end
44
+ app_key = DavinciRubyClientSDK::Setting.app_secret
45
+ signed_string = OpenSSL::HMAC.hexdigest('sha1', app_key, body_array.join('&'))
46
+ signed_string == header_signs && service == 'Davinci' && ua == 'Davinci'
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module DavinciRubyClientSDK
2
+ class Setting
3
+ class << self
4
+ attr_accessor :app_key, :app_secret, :server_address
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module DavinciRubyClientSDK
2
+ class Translator
3
+
4
+ class << self
5
+
6
+ def mxp_to_blueprint
7
+
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module DavinciRubyClientSDK
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,72 @@
1
+ require 'DavinciRubyClientSDK/common'
2
+ require 'DavinciRubyClientSDK/blueprint'
3
+ require 'DavinciRubyClientSDK/receiver'
4
+ require 'DavinciRubyClientSDK/setting'
5
+ require 'DavinciRubyClientSDK/translator'
6
+ require 'DavinciRubyClientSDK/version'
7
+ require 'faraday'
8
+ include Faraday
9
+
10
+ module DavinciRubyClientSDK
11
+
12
+ class API
13
+
14
+ def initialize
15
+ DavinciRubyClientSDK::Common.check_app_key
16
+ end
17
+
18
+ def draw(label, blueprints, options = {})
19
+ options[:mode] ||= 'sync'
20
+ @call_method = 'post'
21
+ @call_path = "/draw/#{options[:mode]}"
22
+ @call_body = {:label => label, :blueprints => blueprints}
23
+ @call_body[:notify_url] = options[:notify_url] unless options[:notify_url].nil?
24
+ self
25
+ end
26
+
27
+ def call
28
+ begin
29
+
30
+ url = DavinciRubyClientSDK::Setting.server_address + @call_path
31
+ connect = Faraday.new(:url => url) do |faraday|
32
+ faraday.request :url_encoded
33
+ faraday.adapter Faraday.default_adapter
34
+ faraday.basic_auth(DavinciRubyClientSDK::Setting.app_key, DavinciRubyClientSDK::Setting.app_secret)
35
+ end
36
+
37
+ response = connect.send("#{@call_method}") do |req|
38
+ if @call_method == 'post'
39
+ req.body = @call_body
40
+ elsif @call_method == 'get'
41
+ @call_params.map do |key, value|
42
+ req.params[key] = value
43
+ end
44
+ end
45
+ end.body
46
+
47
+ rescue Exception => err
48
+ @error = err
49
+
50
+ ensure
51
+ return @error, response
52
+
53
+ end
54
+ end
55
+
56
+ def get(path, params)
57
+ @call_method = 'get'
58
+ @call_path = path
59
+ @call_params = params
60
+ self
61
+ end
62
+
63
+ def post(path, body)
64
+ @call_method = 'post'
65
+ @call_path = path
66
+ @call_body = body
67
+ self
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,114 @@
1
+ # coding: utf-8
2
+ require 'test/unit'
3
+ require 'DavinciRubyClientSDK'
4
+
5
+ class TestDavinciRubyClientSDK < Test::Unit::TestCase
6
+
7
+
8
+ def test_blueprint_hash_in
9
+ assert = [{:layer => 1, :technique => 'draw_image', :inspiration => {:test=>"a"}}]
10
+
11
+ result = DavinciRubyClientSDK::Blueprint::new('aa')
12
+ result.add_layer 1 do |layer|
13
+ layer.technique = :draw_image
14
+ layer.inspiration = {:test=>"a"}
15
+ end
16
+
17
+ result = result.layers
18
+
19
+ assert_equal assert, result
20
+ end
21
+
22
+ def test_blueprint_one_step_in
23
+ assert = [{:layer => 1, :technique => 'draw_image', :inspiration => {:test=>"a"}}]
24
+
25
+ result = DavinciRubyClientSDK::Blueprint::new('aa')
26
+ result.add_layer 1 do |layer|
27
+ layer.technique = :draw_image
28
+ layer.add_inspiration :test, 'a'
29
+ end
30
+ result = result.layers
31
+
32
+ assert_equal assert, result
33
+ end
34
+
35
+ def test_canvas
36
+ assert = {:width => 100}
37
+
38
+ result = DavinciRubyClientSDK::Blueprint::new('aa')
39
+ result.canvas do |option|
40
+ option.add_canvas_option :width, 100
41
+ end
42
+
43
+ result = result.canvas
44
+
45
+ assert_equal assert, result
46
+ end
47
+
48
+ def test_mount
49
+ assert = {:ext => 'jpg'}
50
+
51
+ result = DavinciRubyClientSDK::Blueprint::new('aa')
52
+ result.mount do |option|
53
+ option.add_mount_option :ext, 'jpg'
54
+ end
55
+
56
+ result = result.mount
57
+
58
+ assert_equal assert, result
59
+ end
60
+
61
+ def test_draw_sync
62
+ DavinciRubyClientSDK::Setting.app_key = '123'
63
+ DavinciRubyClientSDK::Setting.app_secret = '123'
64
+ DavinciRubyClientSDK::Setting.server_address = 'http://127.0.0.1:9000'
65
+
66
+ blueprints = Array.new
67
+
68
+ blueprints << {:test => 'test'}
69
+
70
+ api = DavinciRubyClientSDK::API.new
71
+ error, result = api.draw('label', blueprints).call
72
+
73
+ p error
74
+ p result
75
+ end
76
+
77
+ def test_api_get
78
+ DavinciRubyClientSDK::Setting.app_key = '123'
79
+ DavinciRubyClientSDK::Setting.app_secret = '123'
80
+ DavinciRubyClientSDK::Setting.server_address = 'http://127.0.0.1:9000'
81
+
82
+ blueprints = {:test => 'test', :p => 111}
83
+
84
+ api = DavinciRubyClientSDK::API.new
85
+ error, result = api.get('/font/list', blueprints).call
86
+
87
+ p error
88
+ p result
89
+ end
90
+
91
+ def test_receiver
92
+ DavinciRubyClientSDK::Setting.app_key = 'memeing'
93
+ DavinciRubyClientSDK::Setting.app_secret = 'xE7xkJhvy7cHxuI'
94
+ DavinciRubyClientSDK::Setting.server_address = 'http://davinci.memeing.cn'
95
+ end
96
+
97
+
98
+ #receiver test
99
+ #require 'webrick'
100
+ #
101
+ #inclue WEBrick
102
+ #
103
+ #log = [[ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT + ' POST=%{body}n' + 'HEADER=%{header}n']]
104
+ #
105
+ #server = WEBrick::HTTPServer.new :Port => 9000, :AccessLog => log
106
+ #server.mount_proc '/' do |req, res|
107
+ # req.attributes['body'] = req.body
108
+ # req.attributes['header'] = req.header
109
+ # res.body = "Web server response:\n"
110
+ #end
111
+ #server.start
112
+
113
+
114
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DavinciRubyClientSDK
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - RaymondChou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-04 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: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: :runtime
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: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Davinci Client SDK Gem for Ruby
56
+ email:
57
+ - zhouyt@outlook.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - DavinciRubyClientSDK.gemspec
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/DavinciRubyClientSDK.rb
70
+ - lib/DavinciRubyClientSDK/blueprint.rb
71
+ - lib/DavinciRubyClientSDK/common.rb
72
+ - lib/DavinciRubyClientSDK/receiver.rb
73
+ - lib/DavinciRubyClientSDK/setting.rb
74
+ - lib/DavinciRubyClientSDK/translator.rb
75
+ - lib/DavinciRubyClientSDK/version.rb
76
+ - test/test_DavinciRubyClientSDK.rb
77
+ homepage: https://github.com/iiseeuu/DavinciRubyClientSDK
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.3
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Davinci Client SDK Gem for Ruby
101
+ test_files:
102
+ - test/test_DavinciRubyClientSDK.rb