kooaba 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/examples/query.rb +12 -0
- data/examples/upload.rb +21 -0
- data/kooaba.gemspec +24 -0
- data/lib/kooaba.rb +1 -0
- data/lib/kooaba/base.rb +28 -0
- data/lib/kooaba/item.rb +19 -0
- data/lib/kooaba/multipart_message.rb +75 -0
- data/lib/kooaba/query.rb +15 -0
- data/lib/kooaba/query_request.rb +41 -0
- data/lib/kooaba/upload_request.rb +49 -0
- data/lib/kooaba/version.rb +3 -0
- metadata +80 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# kooaba
|
2
|
+
|
3
|
+
This gem is currently in Beta phase. We advise you not to use this in a production system!
|
4
|
+
|
5
|
+
This is a lightweight gem (no other dependencies) for accessing the [kooaba APIs](http://kooaba.github.com).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
1. Make sure you have an account on the kooaba platform at [https://platform.kooaba.com/](https://platform.kooaba.com). In the API Access section you will find your API keys for uploading items and making queries.
|
10
|
+
|
11
|
+
|
12
|
+
2. Install the kooaba gem:
|
13
|
+
|
14
|
+
|
15
|
+
gem install kooaba
|
16
|
+
|
17
|
+
|
18
|
+
## Uploading an item
|
19
|
+
|
20
|
+
In order to upload items into your account, you need an UPLOAD\_KEY and a BUCKET\_ID. Both of these you can find in your account at https://platform.kooaba.com .
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require 'kooaba'
|
24
|
+
|
25
|
+
# set the upload key
|
26
|
+
Kooaba.upload_key = <UPLOAD_KEY>
|
27
|
+
|
28
|
+
# initialize the item
|
29
|
+
item = Kooaba::Item.new(
|
30
|
+
:title => "A lake",
|
31
|
+
:metadata => nil,
|
32
|
+
:image_files => <PATH_TO_IMAGE_ON_LOCAL_SYSTEM>,
|
33
|
+
:referenceId => "lake"
|
34
|
+
)
|
35
|
+
|
36
|
+
# select the bucket you want to put the item into
|
37
|
+
bucket_id = <BUCKET_ID>
|
38
|
+
|
39
|
+
# upload the item
|
40
|
+
response = Kooaba.upload(item, bucket_id)
|
41
|
+
|
42
|
+
puts "Response code: #{response.code}"
|
43
|
+
puts "Response body: #{response.body}"
|
44
|
+
|
45
|
+
|
46
|
+
The reponse will look like:
|
47
|
+
|
48
|
+
Response code: 201
|
49
|
+
Response body: {"uuid":"d956a280-d678-4260-9115-d0382175ae90","enabled":true,"images":[{"sha1":"3d3d8d00cdaf3de4f346f84e83e207780a7e2afe"}]}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
## Making a query
|
54
|
+
|
55
|
+
To make a query you need a QUERY_KEY which you can find under the API Access section in your account at https://platform.kooaba.com .
|
56
|
+
|
57
|
+
require 'rubygems'
|
58
|
+
require 'kooaba'
|
59
|
+
|
60
|
+
# set the query key
|
61
|
+
Kooaba.query_key = <QUERY_KEY>
|
62
|
+
|
63
|
+
# send the query to the kooaba servers
|
64
|
+
query = Kooaba::Query.new(:image_path => <PATH_TO_QUERY_IMAGE_ON_LOCAL_SYSTEM>)
|
65
|
+
response = Kooaba.query(query)
|
66
|
+
|
67
|
+
# inspect the result
|
68
|
+
puts "Response code: #{response.code}"
|
69
|
+
puts "Response body: #{response.body}"
|
70
|
+
|
71
|
+
|
72
|
+
The response will look like this:
|
73
|
+
|
74
|
+
Response code: 200
|
75
|
+
Response body: {"query_id":"698c666aa23348a0b5ef54d9bc2d7ca3","results":[{"item_uuid":"4b641fc5-2c50-44b5-b529-c9e4092ec47f","bucket_uuid":"108695a2-7825-4a98-8bda-b980782c5e33","service_id":"object_retrieval","score":3.301587,"recognitions":[{"score":3.301587,"id":"image.sha1:dd96b28d55db1492c8dd9709ba480efd287fa122","reference_projection":[{"x":6.596069,"y":2.501495},{"x":-4.0,"y":1153.0},{"x":1851.0,"y":1160.0},{"x":1834.0,"y":3.0}],"bounding_box":[{"x":427.0,"y":28.0},{"x":427.0,"y":1090.0},{"x":1644.0,"y":1090.0},{"x":1644.0,"y":28.0}]}],"title":"A lake"},{"item_uuid":"b9914fad-5064-437d-b84b-71c9d69fd248","bucket_uuid":"108695a2-7825-4a98-8bda-b980782c5e33","service_id":"object_retrieval","score":3.301587,"recognitions":[{"score":3.301587,"id":"image.sha1:dd96b28d55db1492c8dd9709ba480efd287fa122","reference_projection":[{"x":6.596069,"y":2.501495},{"x":-4.0,"y":1153.0},{"x":1851.0,"y":1160.0},{"x":1834.0,"y":3.0}],"bounding_box":[{"x":427.0,"y":28.0},{"x":427.0,"y":1090.0},{"x":1644.0,"y":1090.0},{"x":1644.0,"y":28.0}]}],"title":"A lake"}]}
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/examples/query.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "kooaba.rb")
|
2
|
+
|
3
|
+
# set the query key
|
4
|
+
Kooaba.query_key = <QUERY_KEY>
|
5
|
+
|
6
|
+
# send the query to the kooaba servers
|
7
|
+
query = Kooaba::Query.new(:image_path => <PATH_TO_IMAGE>)
|
8
|
+
response = Kooaba.query(query)
|
9
|
+
|
10
|
+
# inspect the result
|
11
|
+
puts "Response code: #{response.code}"
|
12
|
+
puts "Response body: #{response.body}"
|
data/examples/upload.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "kooaba.rb")
|
2
|
+
|
3
|
+
# set the upload key
|
4
|
+
Kooaba.upload_key = <UPLOAD_KEY>
|
5
|
+
|
6
|
+
# initialize the item
|
7
|
+
item = Kooaba::Item.new(
|
8
|
+
:title => "A lake",
|
9
|
+
:metadata => nil,
|
10
|
+
:image_files => <PATH_TO_IMAGE>,
|
11
|
+
:referenceId => "lake"
|
12
|
+
)
|
13
|
+
|
14
|
+
# select the bucket you want to put the item into
|
15
|
+
bucket_id = <BUCKET_ID>
|
16
|
+
|
17
|
+
# upload the item
|
18
|
+
response = Kooaba.upload(item, bucket_id)
|
19
|
+
|
20
|
+
puts "Response code: #{response.code}"
|
21
|
+
puts "Response body: #{response.body}"
|
data/kooaba.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "kooaba/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "kooaba"
|
7
|
+
s.version = Kooaba::VERSION
|
8
|
+
s.authors = ["Cristi Prodan"]
|
9
|
+
s.email = ["prodan@kooaba.com"]
|
10
|
+
s.homepage = "https://github.com/kooaba/kooaba-ruby"
|
11
|
+
s.summary = %q{A gem for accessing the kooaba APIs}
|
12
|
+
s.description = %q{A lightweight gem for accessing the kooaba APIs. For more information on the APIs check out http://kooaba.github.com}
|
13
|
+
|
14
|
+
s.rubyforge_project = "kooaba"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
data/lib/kooaba.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir[File.join(File.dirname(__FILE__), '/kooaba/*.rb')].each {|file| require file }
|
data/lib/kooaba/base.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Kooaba
|
2
|
+
QUERY_URL = "https://query-api.kooaba.com/v4/"
|
3
|
+
UPLOAD_URL = "https://upload-api.kooaba.com/api/v4/"
|
4
|
+
|
5
|
+
def self.upload_key=(upload_key)
|
6
|
+
@@upload_key = upload_key
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.query_key=(query_key)
|
10
|
+
@@query_key = query_key
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.upload_key
|
14
|
+
@@upload_key
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.query_key
|
18
|
+
@@query_key
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.upload(item, bucket_id)
|
22
|
+
UploadRequest.new(item, bucket_id).start
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.query(image_path)
|
26
|
+
QueryRequest.new(image_path).start
|
27
|
+
end
|
28
|
+
end
|
data/lib/kooaba/item.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kooaba
|
2
|
+
|
3
|
+
class Item
|
4
|
+
attr_accessor :title
|
5
|
+
attr_accessor :metadata
|
6
|
+
attr_accessor :enabled
|
7
|
+
attr_accessor :reference_id
|
8
|
+
attr_accessor :image_files
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@title = options[:title]
|
12
|
+
@metadata = options[:metadata]
|
13
|
+
@enabled = options[:enabled] || true
|
14
|
+
@reference_id = options[:reference_id]
|
15
|
+
@image_files = options[:image_files] || []
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#
|
2
|
+
# Class implementing the MIME multipart message format (currently only containing the "Form Data" subtype)
|
3
|
+
#
|
4
|
+
# Created 2008-10-30 by Joachim Fornallaz <fornallaz@kooaba.com>
|
5
|
+
#
|
6
|
+
|
7
|
+
module Kooaba
|
8
|
+
module TypedFile
|
9
|
+
attr_accessor :content_type
|
10
|
+
end
|
11
|
+
|
12
|
+
class MultipartMessage
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@file_parts = []
|
16
|
+
@text_parts = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_file_part(name, file, type)
|
20
|
+
case file
|
21
|
+
when String
|
22
|
+
io = open(file)
|
23
|
+
else
|
24
|
+
io = file
|
25
|
+
end
|
26
|
+
|
27
|
+
unless io.respond_to?(:content_type)
|
28
|
+
io.extend(TypedFile)
|
29
|
+
content_type = (type == "image/jpeg") ? "image/jpg" : type
|
30
|
+
io.content_type = content_type
|
31
|
+
end
|
32
|
+
|
33
|
+
@file_parts << [name, io]
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_text_part(name, text)
|
37
|
+
@text_parts << [name, text]
|
38
|
+
end
|
39
|
+
|
40
|
+
def content_type
|
41
|
+
"multipart/form-data; boundary=#{boundary_token}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def body
|
45
|
+
boundary_marker = "--#{boundary_token}\r\n"
|
46
|
+
body = @text_parts.map { |param|
|
47
|
+
(name, value) = param
|
48
|
+
boundary_marker + text_to_multipart(name, value)
|
49
|
+
}.join('') + @file_parts.map { |param|
|
50
|
+
(name, value) = param
|
51
|
+
boundary_marker + data_to_multipart(name, value)
|
52
|
+
}.join('') + "--#{boundary_token}--\r\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def boundary_token
|
58
|
+
@boundary_token ||= [Array.new(8) {rand(256)}].join
|
59
|
+
end
|
60
|
+
|
61
|
+
def data_to_multipart(key, data)
|
62
|
+
filename = data.respond_to?(:original_filename) ? data.original_filename : File.basename(data.path)
|
63
|
+
mime_type = data.content_type
|
64
|
+
part = "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{filename}\"\r\n"
|
65
|
+
part += "Content-Transfer-Encoding: binary\r\n"
|
66
|
+
part += "Content-Type: #{mime_type}\r\n\r\n#{data.read}\r\n"
|
67
|
+
data.rewind
|
68
|
+
part
|
69
|
+
end
|
70
|
+
|
71
|
+
def text_to_multipart(key,value)
|
72
|
+
"Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{value}\r\n"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/lib/kooaba/query.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Kooaba
|
2
|
+
|
3
|
+
class Query
|
4
|
+
attr_accessor :image_path
|
5
|
+
attr_accessor :max_results
|
6
|
+
attr_accessor :user_data
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@image_path = options[:image_path]
|
10
|
+
@max_results = options[:max_results]
|
11
|
+
@user_data = options[:user_data]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
module Kooaba
|
8
|
+
|
9
|
+
class QueryRequest
|
10
|
+
|
11
|
+
def initialize(query)
|
12
|
+
@message = MultipartMessage.new
|
13
|
+
content_type = `file --mime-type -b #{query.image_path}`.chop
|
14
|
+
@message.add_file_part('image', query.image_path, content_type)
|
15
|
+
@message.add_text_part('max_results', query.max_results) if query.max_results
|
16
|
+
@message.add_text_part('user_data', query.user_data) if query.user_data
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Returns the http response from the Kooaba servers.
|
21
|
+
#
|
22
|
+
def start
|
23
|
+
url = URI.parse(Kooaba::QUERY_URL + "query")
|
24
|
+
|
25
|
+
http = Net::HTTP.new(url.host, url.port)
|
26
|
+
http.use_ssl = true
|
27
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
28
|
+
|
29
|
+
req = Net::HTTP::Post.new(url.path)
|
30
|
+
|
31
|
+
req.body = @message.body
|
32
|
+
req['date'] = Time.new.httpdate
|
33
|
+
req['content-type'] = @message.content_type
|
34
|
+
req['authorization'] = "Token #{Kooaba.query_key}"
|
35
|
+
|
36
|
+
http.start { |h| h.request(req) }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
module Kooaba
|
8
|
+
|
9
|
+
class UploadRequest
|
10
|
+
|
11
|
+
attr_accessor :message
|
12
|
+
attr_accessor :bucket_id
|
13
|
+
|
14
|
+
def initialize(item, bucket_id)
|
15
|
+
@bucket_id = bucket_id
|
16
|
+
@message = MultipartMessage.new
|
17
|
+
item.image_files.each do |image_path|
|
18
|
+
content_type = `file --mime-type -b #{image_path}`
|
19
|
+
@message.add_file_part('images', image_path, content_type)
|
20
|
+
end
|
21
|
+
@message.add_text_part('referenceId', item.reference_id) if item.reference_id
|
22
|
+
@message.add_text_part('title', item.title) if item.title
|
23
|
+
@message.add_text_part('enabled', item.enabled)
|
24
|
+
@message.add_text_part('metadata', item.metadata) if item.metadata
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Returns the http response from the Kooaba servers.
|
29
|
+
#
|
30
|
+
def start
|
31
|
+
url = URI.parse(Kooaba::UPLOAD_URL + "buckets/" + bucket_id + "/items")
|
32
|
+
|
33
|
+
http = Net::HTTP.new(url.host, url.port)
|
34
|
+
http.use_ssl = true
|
35
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
36
|
+
|
37
|
+
req = Net::HTTP::Post.new(url.path)
|
38
|
+
|
39
|
+
req.body = @message.body
|
40
|
+
req['date'] = Time.new.httpdate
|
41
|
+
req['content-type'] = @message.content_type
|
42
|
+
req['authorization'] = "Token #{Kooaba.upload_key}"
|
43
|
+
|
44
|
+
http.start { |h| h.request(req) }
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kooaba
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Cristi Prodan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-12-18 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A lightweight gem for accessing the kooaba APIs. For more information on the APIs check out http://kooaba.github.com
|
22
|
+
email:
|
23
|
+
- prodan@kooaba.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- examples/query.rb
|
36
|
+
- examples/upload.rb
|
37
|
+
- kooaba.gemspec
|
38
|
+
- lib/kooaba.rb
|
39
|
+
- lib/kooaba/base.rb
|
40
|
+
- lib/kooaba/item.rb
|
41
|
+
- lib/kooaba/multipart_message.rb
|
42
|
+
- lib/kooaba/query.rb
|
43
|
+
- lib/kooaba/query_request.rb
|
44
|
+
- lib/kooaba/upload_request.rb
|
45
|
+
- lib/kooaba/version.rb
|
46
|
+
homepage: https://github.com/kooaba/kooaba-ruby
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: kooaba
|
75
|
+
rubygems_version: 1.8.15
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: A gem for accessing the kooaba APIs
|
79
|
+
test_files: []
|
80
|
+
|