kinu 1.0.0.alpha1

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: eefe3f6842660260d2c269f9eca9d01331792ff8
4
+ data.tar.gz: 75a8f77ab2a77b5972765b4ec9123baa3529f671
5
+ SHA512:
6
+ metadata.gz: 7c555a4afe49dff17c5af04a4f4f6fda01db3174fdc169ad71cd97fa864eef045c829d5d4e1aca082ef615008d913363e8479225e70faac9995bceb3949815cc
7
+ data.tar.gz: b4b8ee66f58300d63a0742943598c8d5d4acc9b3f789856d0ca36bb89a319b0765e369fb4a7f8b9cedf3f0c2154ceda759e041b2b2210aad6c20a2b0c18b5427
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kinu-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,50 @@
1
+ # Kinu ruby client
2
+
3
+ [Kinu](https://github.com/TakatoshiMaeda/kinu) ruby client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kinu'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kinu
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'kinu'
25
+
26
+ Kinu.configure do |c|
27
+ c.host = '127.0.0.1'
28
+ c.port = 80
29
+ c.ssl = true
30
+ end
31
+
32
+ resource = Kinu::Resource.new(:foods, 1)
33
+ resource.upload(open('/path/to/image.jpg'))
34
+ resource.uri(width: 280, height: 300) # => #<URI::HTTP http://127.0.0.1/images/foods/w=280,h=300/1.jpg>
35
+
36
+ sandbox = Kinu::Sandbox.upload(open('/path/to/image.jpg'))
37
+ sandbox.uri(width: 280, height: 300) # => #<URI::HTTP http://192.168.99.100:5001/images/__sandbox__/w=280,h=300/1abd5e51-2ba4-43e5-a355-c2abee318d3f.jpg>
38
+ sandbox.attach_to(:foods, 1) # => #<Kinu::Resource:0x007fa4538098b0 @id="1", @name="foods">
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TakatoshiMaeda/kinu-ruby.
50
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kinu"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kinu/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kinu"
8
+ spec.version = Kinu::VERSION
9
+ spec.authors = ["Takatoshi Maeda"]
10
+ spec.email = ["me@tmd.tw"]
11
+
12
+ spec.summary = %q{image server kinu ruby client}
13
+ spec.description = %q{image server kinu ruby client}
14
+ spec.homepage = "https://github.com/TakatoshiMaeda/kinu-ruby"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata['allowed_push_host'] = "https://rubygems.org/"
18
+ else
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "faraday"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "pry"
32
+ end
@@ -0,0 +1,21 @@
1
+ require 'kinu/configuration'
2
+ require 'kinu/version'
3
+ require 'kinu/resource'
4
+ require 'kinu/sandbox'
5
+
6
+ module Kinu
7
+ USER_AGENT = "KinuRubyClient/#{Kinu::VERSION}".freeze
8
+
9
+ def self.base_uri
10
+ raise "Kinu.config.host is not set." if config.host.empty?
11
+ URI::HTTP.build(scheme: config.scheme, host: config.host, port: config.port)
12
+ end
13
+
14
+ def self.configure
15
+ yield config
16
+ end
17
+
18
+ def self.config
19
+ @config ||= Configuration.new
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ module Kinu
2
+ class Configuration
3
+ attr_writer :host, :port, :ssl
4
+
5
+ def host
6
+ @host || ""
7
+ end
8
+
9
+ def port
10
+ if @port
11
+ @port
12
+ elsif ssl?
13
+ 443
14
+ else
15
+ 80
16
+ end
17
+ end
18
+
19
+ def ssl?
20
+ @ssl
21
+ end
22
+
23
+ def scheme
24
+ if ssl?
25
+ :https
26
+ else
27
+ :http
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ module Kinu
2
+ class BadRequestError < StandardError
3
+ def initialize(response)
4
+ @response = response
5
+ @status = response.status
6
+ super(response.headers['x-kinu-badrequest-reason'])
7
+ end
8
+ end
9
+
10
+ class HttpError < StandardError
11
+ attr_reader :status, :response
12
+
13
+ def initialize(response)
14
+ @response = response
15
+ @status = response.status
16
+ super("status: #{status}, response: #{response.body}")
17
+ end
18
+ end
19
+ class ServerError < HttpError; end
20
+ class ClientError < HttpError; end
21
+ end
@@ -0,0 +1,33 @@
1
+ module Kinu
2
+ class Geometry
3
+ AVAILABLE_TYPES = {
4
+ width: :w,
5
+ height: :h,
6
+ quality: :q,
7
+ crop: :c,
8
+ original: :o,
9
+ middle: :m,
10
+ }.freeze
11
+
12
+ def initialize(options)
13
+ @options = options
14
+ end
15
+
16
+ def empty?
17
+ @options.empty?
18
+ end
19
+
20
+ def valid?
21
+ !(@options[:width].nil? && @options[:height].nil?)
22
+ end
23
+
24
+ def to_s
25
+ geo = []
26
+ AVAILABLE_TYPES.each do |full_name, short_name|
27
+ next if @options[full_name].nil?
28
+ geo << "#{short_name}=#{@options[full_name]}"
29
+ end
30
+ geo.join(',')
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,85 @@
1
+ require 'faraday'
2
+ require 'kinu/errors'
3
+
4
+ module Kinu
5
+ class HttpClient
6
+ def self.post(path, params)
7
+ new(:post, path, params).run
8
+ end
9
+
10
+ def self.multipart_post(path, params)
11
+ new(:post, path, params, multipart: true).run
12
+ end
13
+
14
+ def initialize(method, path, params, multipart: false)
15
+ @method = method
16
+ @path = path
17
+ @params = params
18
+ @multipart = multipart
19
+ end
20
+
21
+ def run
22
+ response = connection.send(@method, @path, @params, "User-Agent" => Kinu::USER_AGENT)
23
+ case response.status
24
+ when 400
25
+ raise BadRequestError.new(response)
26
+ when 400...500
27
+ raise ClientError.new(response)
28
+ when 500...600
29
+ raise ServerError.new(response)
30
+ end
31
+
32
+ case response.headers['content-type']
33
+ when 'application/json'
34
+ JSON.parse(response.body)
35
+ else
36
+ response.body
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def connection
43
+ Faraday::Connection.new(Kinu.base_uri) do |builder|
44
+ builder.request :multipart if @multipart
45
+ builder.request :url_encoded
46
+ builder.adapter :net_http
47
+ end
48
+ end
49
+
50
+ class UploadFile
51
+ def initialize(file)
52
+ @file = file
53
+ end
54
+
55
+ def content_type
56
+ case File.extname(@file.path)
57
+ when '.jpg', '.jpeg'
58
+ 'image/jpeg'
59
+ when '.png'
60
+ 'image/png'
61
+ when '.gif'
62
+ 'image/gif'
63
+ else
64
+ 'application/octet-stream'
65
+ end
66
+ end
67
+
68
+ def original_filename
69
+ File.basename(@file.path)
70
+ end
71
+
72
+ def respond_to?(name)
73
+ io.respond_to?(name)
74
+ end
75
+
76
+ def method_missing(*args)
77
+ io.send(*args)
78
+ end
79
+
80
+ def io
81
+ @io ||= Faraday::UploadIO.new(@file, content_type, original_filename)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,23 @@
1
+ require 'kinu/geometry'
2
+ require 'kinu/resource_base'
3
+ require 'kinu/http_client'
4
+
5
+ module Kinu
6
+ class Resource < ResourceBase
7
+ def upload(file)
8
+ Kinu::HttpClient.multipart_post(
9
+ '/upload',
10
+ {
11
+ name: @name,
12
+ id: @id,
13
+ image: Kinu::HttpClient::UploadFile.new(file),
14
+ },
15
+ )
16
+ nil
17
+ end
18
+
19
+ def attach_from_sandbox(sandbox_id)
20
+ Sandbox.attach(@name, @id, sandbox_id)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ require 'kinu/geometry'
2
+ require 'kinu/http_client'
3
+
4
+ module Kinu
5
+ class ResourceBase
6
+ attr_reader :name, :id
7
+
8
+ def initialize(name, id)
9
+ @name, @id = name, id
10
+ end
11
+
12
+ def uri(options)
13
+ timestamp = options.delete(:timestamp)
14
+ format = (options.delete(:format) || :jpg)
15
+ geometry = Geometry.new(options)
16
+ build_uri(geometry, format, timestamp)
17
+ end
18
+
19
+ def path(options)
20
+ format = (options.delete(:format) || :jpg)
21
+ geometry = Geometry.new(options)
22
+ build_path(geometry, format)
23
+ end
24
+
25
+ private
26
+
27
+ def build_path(geometry, format)
28
+ raise ArgumentError, "required geometry hash." if geometry.empty?
29
+ raise ArgumentError, "invalid geometry, height or width is required." unless geometry.valid?
30
+ "/images/#{@name}/#{geometry}/#{@id}.#{format}"
31
+ end
32
+
33
+ def build_uri(geometry, format, timestamp)
34
+ uri = base_uri
35
+ uri.path = build_path(geometry, format)
36
+ uri.query = "#{timestamp.to_s}" if timestamp
37
+ uri
38
+ end
39
+
40
+ def base_uri
41
+ Kinu.base_uri
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ require 'json'
2
+ require 'kinu/resource_base'
3
+
4
+ module Kinu
5
+ class Sandbox < ResourceBase
6
+ def self.upload(file)
7
+ response = Kinu::HttpClient.multipart_post(
8
+ '/sandbox',
9
+ {
10
+ image: Kinu::HttpClient::UploadFile.new(file),
11
+ }
12
+ )
13
+ new(response['id'])
14
+ end
15
+
16
+ def self.attach(name, id, sandbox_id)
17
+ response = Kinu::HttpClient.post(
18
+ '/sandbox/attach',
19
+ {
20
+ name: name,
21
+ id: id,
22
+ sandbox_id: sandbox_id,
23
+ }
24
+ )
25
+ Resource.new(response['name'], response['id'])
26
+ end
27
+
28
+ def initialize(id)
29
+ super('__sandbox__', id)
30
+ end
31
+
32
+ def attach_to(name, attach_id)
33
+ self.class.attach(name, attach_id, self.id)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Kinu
2
+ VERSION = "1.0.0.alpha1"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kinu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Takatoshi Maeda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: image server kinu ruby client
70
+ email:
71
+ - me@tmd.tw
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - kinu.gemspec
84
+ - lib/kinu.rb
85
+ - lib/kinu/configuration.rb
86
+ - lib/kinu/errors.rb
87
+ - lib/kinu/geometry.rb
88
+ - lib/kinu/http_client.rb
89
+ - lib/kinu/resource.rb
90
+ - lib/kinu/resource_base.rb
91
+ - lib/kinu/sandbox.rb
92
+ - lib/kinu/version.rb
93
+ homepage: https://github.com/TakatoshiMaeda/kinu-ruby
94
+ licenses: []
95
+ metadata:
96
+ allowed_push_host: https://rubygems.org/
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.1
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: image server kinu ruby client
117
+ test_files: []