onyx_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'sinatra'
4
+
5
+ group :development, :test do
6
+ gem "rspec", "~> 2.8.0"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", "~> 1.1.3"
9
+ gem "jeweler", "~> 1.8.4"
10
+ gem "simplecov"
11
+ gem "debugger"
12
+ end
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ columnize (0.3.6)
5
+ debugger (1.2.2)
6
+ columnize (>= 0.3.1)
7
+ debugger-linecache (~> 1.1.1)
8
+ debugger-ruby_core_source (~> 1.1.5)
9
+ debugger-linecache (1.1.2)
10
+ debugger-ruby_core_source (>= 1.1.1)
11
+ debugger-ruby_core_source (1.1.5)
12
+ diff-lcs (1.1.3)
13
+ git (1.2.5)
14
+ jeweler (1.8.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ rdoc
19
+ json (1.7.5)
20
+ multi_json (1.3.7)
21
+ rack (1.4.1)
22
+ rack-protection (1.2.0)
23
+ rack
24
+ rake (10.0.2)
25
+ rdoc (3.12)
26
+ json (~> 1.4)
27
+ rspec (2.8.0)
28
+ rspec-core (~> 2.8.0)
29
+ rspec-expectations (~> 2.8.0)
30
+ rspec-mocks (~> 2.8.0)
31
+ rspec-core (2.8.0)
32
+ rspec-expectations (2.8.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.8.0)
35
+ simplecov (0.7.1)
36
+ multi_json (~> 1.0)
37
+ simplecov-html (~> 0.7.1)
38
+ simplecov-html (0.7.1)
39
+ sinatra (1.3.3)
40
+ rack (~> 1.3, >= 1.3.6)
41
+ rack-protection (~> 1.2)
42
+ tilt (~> 1.3, >= 1.3.3)
43
+ tilt (1.3.3)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (~> 1.1.3)
50
+ debugger
51
+ jeweler (~> 1.8.4)
52
+ rdoc (~> 3.12)
53
+ rspec (~> 2.8.0)
54
+ simplecov
55
+ sinatra
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Pedro Henrique Linhares
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = Onyx Client
2
+
3
+ Onyx Client is a client to the Onyx image search server which is built on top of Opala.
4
+
5
+ == Contributing to Onyx client
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Pedro Henrique Linhares. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "onyx_client"
18
+ gem.homepage = "http://github.com/pedrolinhares/onyx_client"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Onyx client}
21
+ gem.description = %Q{Onyx(Image search service) client}
22
+ gem.email = "pedrolmota@gmail.com"
23
+ gem.authors = ["Pedro Henrique Linhares"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "onyx_client #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ require File.dirname(__FILE__) + '/onyx_client/fake_server'
2
+ require File.dirname(__FILE__) + '/onyx_client/configuration'
3
+ require File.dirname(__FILE__) + '/onyx_client/client'
@@ -0,0 +1,43 @@
1
+ #encoding: utf-8
2
+
3
+ require 'base64'
4
+ require 'net/http'
5
+
6
+ module Onyx
7
+ class Client
8
+ class << self
9
+ def index(key, string_file)
10
+ settings = Onyx::Configuration.settings
11
+ response = Net::HTTP.start(settings[:host], settings[:port]) do |http|
12
+ request = Net::HTTP::Put.new(settings[:path])
13
+ params = { key: key, image: string_file }
14
+ request.body = params.to_json
15
+ http.request(request)
16
+ end
17
+ JSON.parse(response.body, :symbolize_names => true)
18
+ end
19
+
20
+ def search(string_file)
21
+ settings = Onyx::Configuration.settings
22
+ response = Net::HTTP.start(settings[:host], settings[:port]) do |http|
23
+ request = Net::HTTP::Post.new(settings[:path])
24
+ params = { image: string_file }
25
+ request.body = params.to_json
26
+ http.request(request)
27
+ end
28
+ JSON.parse(response.body, :symbolize_names => true)
29
+ end
30
+
31
+ def delete(key)
32
+ settings = Onyx::Configuration.settings
33
+ response = Net::HTTP.start(settings[:host], settings[:port]) do |http|
34
+ request = Net::HTTP::Delete.new(settings[:path])
35
+ params = { key: key }
36
+ request.body = params.to_json
37
+ http.request(request)
38
+ end
39
+ JSON.parse(response.body, :symbolize_names => true)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ module Onyx
2
+ class Configuration
3
+ class << self
4
+ def host(host = nil)
5
+ @host = host unless host.nil?
6
+ @host
7
+ end
8
+
9
+ def port(port = nil)
10
+ @port = port unless port.nil?
11
+ @port
12
+ end
13
+
14
+ def path(path = nil)
15
+ @path = path unless path.nil?
16
+ @path
17
+ end
18
+
19
+ def configure
20
+ yield self if block_given?
21
+ settings
22
+ end
23
+
24
+ def settings
25
+ {host: @host, port: @port, path: @path}
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'sinatra'
2
+ require 'sinatra/base'
3
+ require 'debugger'
4
+ require 'json'
5
+
6
+ module Onyx
7
+ class FakeServer < Sinatra::Base
8
+
9
+ @@storage = {}
10
+
11
+ #indexar
12
+ put '/' do
13
+ params = JSON.parse(request.body.read)
14
+ @@storage[params["key"]] = params["image"]
15
+ { code: '200', message: 'message' }.to_json
16
+ end
17
+
18
+ #buscar
19
+ post '/' do
20
+ params = JSON.parse(request.body.read)
21
+ key = @@storage.select {|key, value| value == params["image"] }.keys.last
22
+ [{ key: key, score: "1.0" }].to_json
23
+ end
24
+
25
+ #excluir
26
+ delete '/' do
27
+ params = JSON.parse(request.body.read)
28
+ if @@storage.has_key?(params["key"])
29
+ @@storage.delete params["key"]
30
+ { code: '200', message: 'message' }.to_json
31
+ else
32
+ { code: '404', message: 'message' }.to_json
33
+ end
34
+ end
35
+
36
+ def self.start
37
+ @thread = Thread.new do
38
+ run!
39
+ end
40
+ sleep(1)
41
+ end
42
+
43
+ def self.stop
44
+ @thread.kill
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,46 @@
1
+ #encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ describe "Onyx::Client" do
6
+ before(:all) do
7
+ Onyx::FakeServer.start
8
+ end
9
+
10
+ after(:all) do
11
+ Onyx::FakeServer.stop
12
+ end
13
+
14
+ before(:each) do
15
+ Onyx::Configuration.configure do |conf|
16
+ conf.host 'localhost'
17
+ conf.port '4567'
18
+ conf.path '/'
19
+ end
20
+ end
21
+
22
+ it "index a image" do
23
+ key = 'testing'
24
+ img_base64 = Base64.encode64(File.open('spec/resources/debian.jpg').read)
25
+ result = Onyx::Client.index(key, img_base64)
26
+ result[:code].should == '200'
27
+ result.should == { code: '200', message: 'message' }
28
+ end
29
+
30
+ it 'searches for an image' do
31
+ img_base64 = Base64.encode64(File.open('spec/resources/debian.jpg').read)
32
+ key = 'testing'
33
+ Onyx::Client.index(key, img_base64)
34
+ result = Onyx::Client.search(img_base64)
35
+ result.size.should == 1
36
+ result.first[:key].should == key
37
+ result.should == [{ key: key, score: "1.0" }]
38
+ end
39
+
40
+ it 'deletes an image' do
41
+ key = 'testing'
42
+ result = Onyx::Client.delete(key)
43
+ result[:code].should == '200'
44
+ result.should == { code: '200', message: 'message' }
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Onyx::Configuration" do
4
+ Configuration = Onyx::Configuration
5
+
6
+ it 'sets server host' do
7
+ Configuration.host 'localhost'
8
+ Configuration.instance_variable_get(:@host).should == 'localhost'
9
+ end
10
+
11
+ it 'sets server port' do
12
+ Configuration.port '8080'
13
+ Configuration.instance_variable_get(:@port).should == '8080'
14
+ end
15
+
16
+ it 'sets server path' do
17
+ Configuration.path '/serverpath/'
18
+ Configuration.instance_variable_get(:@path).should == '/serverpath/'
19
+ end
20
+
21
+ it 'settings returns hash with configurations' do
22
+ Configuration.host 'localhost'
23
+ Configuration.port '80'
24
+ Configuration.path '/path/'
25
+ Configuration.settings.should == {host: 'localhost', port: '80', path: '/path/'}
26
+ end
27
+
28
+ it 'configure method receives a block to define configurations parameters' do
29
+ Configuration.configure do |conf|
30
+ conf.host 'localhost'
31
+ conf.port '80'
32
+ conf.path '/path/'
33
+ end
34
+
35
+ Configuration.settings.should == {host: 'localhost', port: '80', path: '/path/'}
36
+ end
37
+ end
Binary file
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'onyx_client'
5
+ require 'base64'
6
+ require 'debugger'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onyx_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pedro Henrique Linhares
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: &73412670 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *73412670
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &73412380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *73412380
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &73412110 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *73412110
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &73411750 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.3
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *73411750
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &73411400 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *73411400
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &73411110 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *73411110
80
+ - !ruby/object:Gem::Dependency
81
+ name: debugger
82
+ requirement: &73410820 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *73410820
91
+ description: Onyx(Image search service) client
92
+ email: pedrolmota@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - .document
100
+ - .rspec
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.rdoc
105
+ - Rakefile
106
+ - VERSION
107
+ - lib/onyx_client.rb
108
+ - lib/onyx_client/client.rb
109
+ - lib/onyx_client/configuration.rb
110
+ - lib/onyx_client/fake_server.rb
111
+ - spec/client_spec.rb
112
+ - spec/configuration_spec.rb
113
+ - spec/resources/debian.jpg
114
+ - spec/spec_helper.rb
115
+ homepage: http://github.com/pedrolinhares/onyx_client
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: 194803985
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 1.8.15
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Onyx client
143
+ test_files: []