kooaba 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-18mode # JRuby in 1.8 mode
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-18mode
7
+ - rbx-19mode
8
+ - 1.8.7
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'rake', :group => :test
4
+
3
5
  # Specify your gem's dependencies in kooaba.gemspec
4
6
  gemspec
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # kooaba
2
2
 
3
+ [![Build Status](https://secure.travis-ci.org/kooaba/kooaba-api-v4-rubygem.png)](http://travis-ci.org/kooaba/kooaba-api-v4-rubygem)
4
+
3
5
  This gem is currently in Beta phase. We advise you not to use this in a production system!
4
6
 
5
7
  This is a lightweight gem (no other dependencies) for accessing the [kooaba APIs](http://kooaba.github.com).
@@ -27,10 +29,10 @@ In order to upload items into your account, you need a `data-key` and a `bucket-
27
29
 
28
30
  # initialize the item
29
31
  item = Kooaba::Item.new(
30
- :title => "A lake",
31
- :metadata => nil,
32
- :image_files => <path-to-image-on-local-filesystem>,
33
- :referenceId => "lake"
32
+ :title => "A lake", # the title of the item (String)
33
+ :metadata => nil, # metadata associated with the item. It must be a valid JSON String (String)
34
+ :image_files => [<path-to-image-on-local-filesystem>], # images associated with the item (Array of Strings)
35
+ :reference_id => "lake" # the reference id of an item (String)
34
36
  )
35
37
 
36
38
  # select the bucket you want to put the item into
@@ -39,16 +41,23 @@ In order to upload items into your account, you need a `data-key` and a `bucket-
39
41
  # upload the item
40
42
  response = Kooaba.upload(item, bucket_id)
41
43
 
44
+ # The response is a Net::HTTPResponse.
42
45
  puts "Response code: #{response.code}"
46
+ puts "Response message: #{response.message}"
43
47
  puts "Response body: #{response.body}"
44
48
 
49
+ puts "Item uuid: #{item.uuid}"
50
+ puts "Item enabled: #{item.enabled}"
51
+ puts "Item images sha1's: #{item.images_sha1.join(", ")}"
45
52
 
46
53
  The reponse will look like:
47
54
 
48
55
  Response code: 201
49
56
  Response body: {"uuid":"d956a280-d678-4260-9115-d0382175ae90","enabled":true,"images":[{"sha1":"3d3d8d00cdaf3de4f346f84e83e207780a7e2afe"}]}
50
57
 
51
-
58
+ Item uuid: d956a280-d678-4260-9115-d0382175ae90
59
+ Item enabled: true
60
+ Item images sha1's: 3d3d8d00cdaf3de4f346f84e83e207780a7e2afe
52
61
 
53
62
  ## Making a query
54
63
 
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
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
@@ -5,10 +5,10 @@ Kooaba.data_key = <data-key-secret-token>
5
5
 
6
6
  # initialize the item
7
7
  item = Kooaba::Item.new(
8
- :title => "A lake",
9
- :metadata => nil,
10
- :image_files => <path-to-image-on-local-filesystem>,
11
- :referenceId => "lake"
8
+ :title => "A lake", # the title of the item (String)
9
+ :metadata => nil, # metadata associated with the item. It must be a valid JSON String (String)
10
+ :image_files => <path-to-image-on-local-filesystem>, # images associated with the item (Array of Strings)
11
+ :reference_id => "lake" # the reference id of an item (String)
12
12
  )
13
13
 
14
14
  # select the bucket you want to put the item into
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
22
+ s.add_development_dependency "json"
23
+ s.add_development_dependency "mocha"
24
+ s.add_runtime_dependency "json"
24
25
  end
@@ -19,10 +19,12 @@ module Kooaba
19
19
  end
20
20
 
21
21
  def self.upload(item, bucket_id)
22
+ raise ArgumentError, "You need to specify the Data Key before uploading an item" if Kooaba::data_key == nil
22
23
  UploadRequest.new(item, bucket_id).start
23
24
  end
24
25
 
25
26
  def self.query(image_path)
27
+ raise ArgumentError, "You need to specify the Query Key before making queries" if Kooaba.query_key == nil
26
28
  QueryRequest.new(image_path).start
27
29
  end
28
30
  end
@@ -1,6 +1,10 @@
1
1
  module Kooaba
2
2
 
3
3
  class Item
4
+
5
+ attr_accessor :uuid
6
+ attr_accessor :images_sha1
7
+
4
8
  attr_accessor :title
5
9
  attr_accessor :metadata
6
10
  attr_accessor :enabled
@@ -8,9 +12,14 @@ module Kooaba
8
12
  attr_accessor :image_files
9
13
 
10
14
  def initialize(options = {})
15
+ raise TypeError, "enabled should be true or false" unless [nil, true, false].include? options[:enabled]
16
+ if !options[:image_files].instance_of?(Array) && options[:image_files] != nil
17
+ raise TypeError, "image_files must be a list of paths (Array of Strings)"
18
+ end
19
+
11
20
  @title = options[:title]
12
21
  @metadata = options[:metadata]
13
- @enabled = options[:enabled] || true
22
+ @enabled = options[:enabled] == nil ? true : options[:enabled]
14
23
  @reference_id = options[:reference_id]
15
24
  @image_files = options[:image_files] || []
16
25
  end
@@ -19,7 +19,7 @@ module Kooaba
19
19
  def add_file_part(name, file, type)
20
20
  case file
21
21
  when String
22
- io = open(file)
22
+ io = File.open(file, "rb")
23
23
  else
24
24
  io = file
25
25
  end
@@ -6,9 +6,13 @@ module Kooaba
6
6
  attr_accessor :user_data
7
7
 
8
8
  def initialize(options = {})
9
- @image_path = options[:image_path]
10
- @max_results = options[:max_results]
11
- @user_data = options[:user_data]
9
+ if options[:image_path] == nil || options[:image_path] == ""
10
+ raise ArgumentError, "you need to provide the path of the query image"
11
+ end
12
+
13
+ @image_path = options[:image_path]
14
+ @max_results = options[:max_results] || 10
15
+ @user_data = options[:user_data]
12
16
  end
13
17
  end
14
18
 
@@ -4,6 +4,10 @@ require 'net/http'
4
4
  require 'net/https'
5
5
  require 'time'
6
6
 
7
+ require 'json'
8
+ require 'net/http'
9
+
10
+
7
11
  module Kooaba
8
12
 
9
13
  class UploadRequest
@@ -11,14 +15,17 @@ module Kooaba
11
15
  attr_accessor :message
12
16
  attr_accessor :bucket_id
13
17
 
18
+ attr_accessor :item
19
+
14
20
  def initialize(item, bucket_id)
21
+ @item = item
15
22
  @bucket_id = bucket_id
16
23
  @message = MultipartMessage.new
17
24
  item.image_files.each do |image_path|
18
25
  content_type = `file --mime-type -b #{image_path}`
19
26
  @message.add_file_part('images', image_path, content_type)
20
27
  end
21
- @message.add_text_part('referenceId', item.reference_id) if item.reference_id
28
+ @message.add_text_part('reference_id', item.reference_id) if item.reference_id
22
29
  @message.add_text_part('title', item.title) if item.title
23
30
  @message.add_text_part('enabled', item.enabled)
24
31
  @message.add_text_part('metadata', item.metadata) if item.metadata
@@ -30,9 +37,17 @@ module Kooaba
30
37
  def start
31
38
  url = URI.parse(Kooaba::UPLOAD_URL + "buckets/" + bucket_id + "/items")
32
39
 
40
+ resp = make_request(url)
41
+ parse_request(resp)
42
+
43
+ return resp
44
+ end
45
+
46
+ def make_request(url)
33
47
  http = Net::HTTP.new(url.host, url.port)
34
48
  http.use_ssl = true
35
49
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
+ http.read_timeout = 500
36
51
 
37
52
  req = Net::HTTP::Post.new(url.path)
38
53
 
@@ -41,7 +56,39 @@ module Kooaba
41
56
  req['content-type'] = @message.content_type
42
57
  req['authorization'] = "Token #{Kooaba.data_key}"
43
58
 
44
- http.start { |h| h.request(req) }
59
+ http.request(req)
60
+ end
61
+
62
+ def parse_request(http_resp)
63
+ case http_resp
64
+ when Net::HTTPSuccess
65
+ parse_2xx(http_resp)
66
+ when Net::HTTPClientError
67
+ parse_4xx(http_resp)
68
+ when Net::HTTPServerError
69
+ parse_5xx(http_resp)
70
+ else
71
+ unknown_response(http_resp)
72
+ end
73
+ end
74
+
75
+ def parse_2xx(http_resp)
76
+ json = JSON.parse(http_resp.body)
77
+ @item.uuid = json["uuid"]
78
+ @item.enabled = json["enabled"]
79
+ @item.images_sha1 = json["images"].empty? ? [] : json["images"].map {|a| a["sha1"]}
80
+ end
81
+
82
+ def parse_4xx(http_resp)
83
+ # do nothing for now
84
+ end
85
+
86
+ def parse_5xx(http_resp)
87
+ # do nothing for now
88
+ end
89
+
90
+ def unknown_response(http_resp)
91
+ raise StandardError, "Unknown response: #{http_resp.code} #{http_resp.body} "
45
92
  end
46
93
 
47
94
  end
@@ -1,3 +1,3 @@
1
1
  module Kooaba
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require 'kooaba'
3
+
4
+ class BaseTest < Test::Unit::TestCase
5
+ def test_data_key
6
+ Kooaba.data_key = "123"
7
+ assert_equal Kooaba.data_key, "123"
8
+ end
9
+
10
+ def test_query_key
11
+ Kooaba.query_key = "123"
12
+ assert_equal Kooaba.query_key, "123"
13
+ end
14
+
15
+ def test_upload_no_key
16
+ assert_raise ArgumentError do
17
+ Kooaba.data_key = nil
18
+ Kooaba.upload(Kooaba::Item.new, "test")
19
+ end
20
+ end
21
+
22
+ def test_query_no_key
23
+ assert_raise ArgumentError do
24
+ Kooaba.query_key = nil
25
+ Kooaba.query("/path/to/image")
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'kooaba'
3
+
4
+ class ItemTest < Test::Unit::TestCase
5
+ def test_init_item
6
+ item = Kooaba::Item.new(
7
+ :title => "A lake",
8
+ :metadata => nil,
9
+ :image_files => ["/path/to/image"],
10
+ :reference_id => "lake",
11
+ :enabled => true
12
+ )
13
+
14
+ assert_equal item.title, "A lake"
15
+ assert_equal item.metadata, nil
16
+ assert_equal item.image_files, ["/path/to/image"]
17
+ assert_equal item.reference_id, "lake"
18
+ assert_equal item.enabled, true
19
+ end
20
+
21
+ def test_init_item_default_attributes
22
+ item = Kooaba::Item.new
23
+
24
+ assert_equal item.title, nil
25
+ assert_equal item.metadata, nil
26
+ assert_equal item.enabled, true
27
+ assert_equal item.reference_id, nil
28
+ assert_equal item.image_files, []
29
+ end
30
+
31
+ def test_init_wrong_type_for_enabled
32
+ assert_raise TypeError do
33
+ item = Kooaba::Item.new(
34
+ :enabled => 1
35
+ )
36
+ end
37
+ end
38
+
39
+ def test_init_wrong_type_for_image_files
40
+ assert_raise TypeError do
41
+ item = Kooaba::Item.new(
42
+ :image_files => "something"
43
+ )
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,31 @@
1
+ require 'test/unit'
2
+ require 'kooaba'
3
+
4
+ class QueryTest < Test::Unit::TestCase
5
+ def test_query_init
6
+ query = Kooaba::Query.new(
7
+ :image_path => "/path/to/image.png",
8
+ :max_results => 2,
9
+ :user_data => "some data"
10
+ )
11
+
12
+ assert_equal query.image_path, "/path/to/image.png"
13
+ assert_equal query.max_results, 2
14
+ assert_equal query.user_data, "some data"
15
+ end
16
+
17
+ def test_query_no_image
18
+ assert_raise ArgumentError do
19
+ query = Kooaba::Query.new()
20
+ end
21
+ end
22
+
23
+ def test_default_parameters
24
+ query = Kooaba::Query.new(
25
+ :image_path => "/path/to/image.png"
26
+ )
27
+
28
+ assert_equal query.max_results, 10
29
+ assert_equal query.user_data, nil
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'test/unit'
2
+ require 'kooaba'
3
+ require 'mocha/setup'
4
+
5
+ class UploadRequestTest < Test::Unit::TestCase
6
+
7
+ include Kooaba
8
+
9
+ def test_parse_item
10
+ item = Item.new(:title => 1)
11
+ req = UploadRequest.new(item, "123")
12
+
13
+ http_resp = Net::HTTPSuccess.new(1.0, 200, "hi")
14
+ http_resp.stubs(:code).returns(200)
15
+ http_resp.stubs(:kind_of?).returns('Net::HTTPSuccess')
16
+ http_resp.stubs(:body).returns('{"uuid":"07382685-ba1d-47bf-be78-7be0db2ebf92","enabled":true,"images":[{"sha1":"14970c3a70d7d295250a10783c7e222625a757ea"},{"sha1":"704f8c649f41262a9232639573dc2210431ebf3b"}]}')
17
+
18
+ req.stubs(:make_request).returns(http_resp)
19
+ req.start
20
+
21
+ assert_equal item.uuid, "07382685-ba1d-47bf-be78-7be0db2ebf92"
22
+ assert_equal item.enabled, true
23
+ assert_equal item.images_sha1, ["14970c3a70d7d295250a10783c7e222625a757ea", "704f8c649f41262a9232639573dc2210431ebf3b"]
24
+ end
25
+
26
+ def test_parse_item_no_sha1s
27
+ item = Item.new(:title => 1)
28
+ req = UploadRequest.new(item, "123")
29
+
30
+ http_resp = Net::HTTPSuccess.new(1.0, 200, "hi")
31
+ http_resp.stubs(:code).returns(200)
32
+ http_resp.stubs(:kind_of?).returns('Net::HTTPSuccess')
33
+ http_resp.stubs(:body).returns('{"uuid":"07382685-ba1d-47bf-be78-7be0db2ebf92","enabled":true,"images":[]}')
34
+
35
+ req.stubs(:make_request).returns(http_resp)
36
+ req.start
37
+
38
+ assert_equal item.uuid, "07382685-ba1d-47bf-be78-7be0db2ebf92"
39
+ assert_equal item.enabled, true
40
+ assert_equal item.images_sha1, []
41
+ end
42
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kooaba
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Cristi Prodan
@@ -15,9 +15,50 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-11 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2013-01-16 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: mocha
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: json
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
21
62
  description: A lightweight gem for accessing the kooaba APIs. For more information on the APIs check out http://kooaba.github.com
22
63
  email:
23
64
  - prodan@kooaba.com
@@ -29,6 +70,7 @@ extra_rdoc_files: []
29
70
 
30
71
  files:
31
72
  - .gitignore
73
+ - .travis.yml
32
74
  - Gemfile
33
75
  - README.md
34
76
  - Rakefile
@@ -43,6 +85,10 @@ files:
43
85
  - lib/kooaba/query_request.rb
44
86
  - lib/kooaba/upload_request.rb
45
87
  - lib/kooaba/version.rb
88
+ - test/test_base.rb
89
+ - test/test_item.rb
90
+ - test/test_query.rb
91
+ - test/test_upload_request.rb
46
92
  homepage: https://github.com/kooaba/kooaba-api-v4-rubygem
47
93
  licenses: []
48
94
 
@@ -76,5 +122,8 @@ rubygems_version: 1.8.15
76
122
  signing_key:
77
123
  specification_version: 3
78
124
  summary: A gem for accessing the kooaba APIs
79
- test_files: []
80
-
125
+ test_files:
126
+ - test/test_base.rb
127
+ - test/test_item.rb
128
+ - test/test_query.rb
129
+ - test/test_upload_request.rb