roozer_client 0.1 → 0.2

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.
data/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
+ v0.2. Testing/refactoring
1
2
  v0.1. Proof of concept
2
3
 
data/Manifest CHANGED
@@ -1,7 +1,7 @@
1
1
  CHANGELOG
2
+ Manifest
2
3
  README
3
4
  Rakefile
4
5
  lib/roozer_client.rb
5
6
  test/test_all.rb
6
7
  test/test_helper.rb
7
- Manifest
data/README CHANGED
@@ -1,7 +1,12 @@
1
1
 
2
2
  require 'roozer_client'
3
3
 
4
- rc = RoozerClient.new(url: 'http://orch1:2988;http://orch1:2987', path: 'rootpath')
4
+ # Connect to a roozer server. Multiple servers can be listed seperated by
5
+ # semicolons. Client uses round robin until it finds a server that responds
6
+ # to its requests.
7
+
8
+ rc = RoozerClient.new(url: 'http://orch1:2987;http://orch2:2987', path: 'rootpath')
9
+
5
10
  rc['subpath'] = { data: "abc" }
6
11
 
7
12
  rc.list
@@ -11,4 +16,3 @@ rc['subpath']
11
16
  => { "data" => "abc" }
12
17
 
13
18
  rc.request(:delete, 'subpath/data')
14
-
data/lib/roozer_client.rb CHANGED
@@ -6,26 +6,25 @@ class RoozerClient
6
6
  def initialize(options={})
7
7
  opts = options.dup
8
8
  @urls = (opts.delete(:url) || ENV['ROOZER_URL'] || 'http://127.0.0.1:2987/').split(/;/).map(&:strip)
9
- @path = "#{opts.delete(:path) || ''}/"
9
+ @path = opts.delete(:path)
10
10
  @timeout = opts.delete(:timeout) || 10
11
11
  @opts = {timeout: @timeout, open_timeout: @timeout}.merge(opts)
12
12
  end
13
-
14
13
 
15
14
  def get(path)
16
15
  begin
17
16
  res = request(:get, path)
18
- res['path'] && res['path']['type'] == 'file' ? res['path']['value'] : nil
17
+ res['value'] if res['type'] == 'file'
19
18
  rescue RestClient::Exception => e
20
19
  raise unless e.http_code == 404
21
20
  end
22
21
  end
23
22
  alias :[] :get
24
23
 
25
- def list(path='')
24
+ def list(path=nil)
26
25
  begin
27
26
  res = request(:get, path)
28
- res['path'] && res['path']['type'] == 'dir' ? res['path']['value'] : nil
27
+ res['value'] if res['type'] == 'dir'
29
28
  rescue RestClient::Exception => e
30
29
  raise unless e.http_code == 404
31
30
  end
@@ -37,13 +36,14 @@ class RoozerClient
37
36
  alias :[]= :put
38
37
 
39
38
  def request(method, path, data=nil)
40
-
41
39
  begin
42
40
  url = @urls.first
43
41
  resource = RestClient::Resource.new(url, @opts)
44
- response = resource["#{@path}#{path}"].send(method, *[({value: data}.to_json if data), {content_type: :json, accept: :json}].compact)
42
+ fullpath = [@path,path].compact.map(&:to_s).join('/')
43
+ response = resource["/#{fullpath}"].send(method, *[({value: data}.to_json if data), {content_type: :json, accept: :json}].compact)
45
44
  JSON.parse(response) rescue {}
46
45
  rescue Errno::ECONNREFUSED, RestClient::RequestTimeout
46
+ puts "#{$!.class}, retrying..."
47
47
  @urls.rotate!
48
48
  sleep 5
49
49
  retry
@@ -52,14 +52,3 @@ class RoozerClient
52
52
 
53
53
  end
54
54
 
55
-
56
- =begin
57
-
58
- rc = RoozerClient.new(url: 'http://orch1:2988;http://orch1:2987', path: 'bigcloud')
59
- rc.list
60
- rc['x'] = { a: 'b' }
61
- rc['x']
62
- rc.request(:delete, 'x')
63
-
64
-
65
- =end
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "roozer_client"
5
- s.version = "0.1"
5
+ s.version = "0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrew Snow"]
9
- s.date = "2012-12-20"
9
+ s.date = "2013-01-03"
10
10
  s.description = "Ruby client for Roozer server"
11
11
  s.email = "andrew@modulus.org"
12
12
  s.extra_rdoc_files = ["CHANGELOG", "README", "lib/roozer_client.rb"]
13
- s.files = ["CHANGELOG", "README", "Rakefile", "lib/roozer_client.rb", "test/test_all.rb", "test/test_helper.rb", "Manifest", "roozer_client.gemspec"]
13
+ s.files = ["CHANGELOG", "Manifest", "README", "Rakefile", "lib/roozer_client.rb", "test/test_all.rb", "test/test_helper.rb", "roozer_client.gemspec"]
14
14
  s.homepage = "https://github.com/andys/roozer-client"
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Roozer_client", "--main", "README"]
16
16
  s.require_paths = ["lib"]
data/test/test_all.rb CHANGED
@@ -1,19 +1,60 @@
1
- require "#{File.dirname(__FILE__)}/../lib/restinator"
1
+ require "#{File.dirname(__FILE__)}/../lib/roozer_client"
2
2
  require 'test/unit'
3
3
  require "#{File.dirname(__FILE__)}/test_helper"
4
4
 
5
5
  class TestRoozerClient < Test::Unit::TestCase
6
6
 
7
7
  def setup
8
- setup_webserver
8
+ @url = setup_webserver
9
+ @client = RoozerClient.new(url: @url, path: 'test')
9
10
  end
10
11
 
11
- def test_get
12
- resp = accept_request(200, 'test') do
13
- `( echo GET /test/123 HTTP/1.0 ; echo ) | nc 127.0.0.1 7999`
14
- end
15
- assert_equal 'GET', resp.request_method
16
- assert_equal '/test/123', resp.path
17
- end
18
-
12
+ def test_list
13
+ result = nil
14
+ response = accept_request(200, {name:"/test",type:"dir",value:["sub1","sub2"]}.to_json) do
15
+ result = @client.list
16
+ end
17
+ assert response
18
+ assert_equal 'GET', response.request_method
19
+ assert_equal '/test', response.path
20
+ assert_equal ["sub1","sub2"], result
21
+ end
22
+
23
+ def test_get
24
+ result = nil
25
+ datahash = {'a' => 'b'}
26
+ response = accept_request(200, {name:"/test/sub1",type:"file",value:datahash}.to_json) do
27
+ result = @client.get('sub1')
28
+ end
29
+ assert response
30
+ assert_equal 'GET', response.request_method
31
+ assert_equal '/test/sub1', response.path
32
+ assert_equal datahash, result
33
+ end
34
+
35
+ def test_put
36
+ response = accept_request(204, '') do
37
+ @client.put('sub2', x1: 'x2')
38
+ end
39
+ assert response
40
+ assert_equal 'PUT', response.request_method
41
+ assert_equal '/test/sub2', response.path
42
+ assert_equal '{"value":{"x1":"x2"}}', response.body
43
+ end
44
+
45
+ def test_get_404
46
+ result = nil
47
+ response = accept_request(404, {error: 'not found'}.to_json) do
48
+ result = @client.get('xxx')
49
+ end
50
+ assert response
51
+ assert_equal '/test/xxx', response.path
52
+ assert_nil result
53
+ end
54
+
55
+ # def test_failover
56
+ # @client = RoozerClient.new(url: "http://127.0.0.1:7997;#{@url}", path: 'test')
57
+ # test_list
58
+ # end
59
+
19
60
  end
data/test/test_helper.rb CHANGED
@@ -1,37 +1,40 @@
1
1
  require 'webrick'
2
2
 
3
- class NewRequestException < Exception
4
- end
5
-
6
3
  class TestServlet < WEBrick::HTTPServlet::AbstractServlet
7
4
  class << self
8
- attr_accessor :request, :response, :wakeupthread
5
+ attr_accessor :request, :response #, :wakeupthread
9
6
  end
10
7
  def service(request, response)
8
+ # Save the request
9
+ body = request.body # this is needed to read the chunks from the other side before duping
11
10
  self.class.request = request.dup
11
+
12
+ # return the canned response
12
13
  response.status = self.class.response.first
13
14
  response.body = self.class.response.last
14
- TestServlet.wakeupthread.raise NewRequestException.new
15
15
  end
16
16
  end
17
17
 
18
18
  def accept_request(*response)
19
19
  TestServlet.request = nil
20
20
  TestServlet.response = response
21
- begin
22
- TestServlet.wakeupthread = Thread.current
23
- yield
24
- $webserver_timeout.times { sleep 1 }
25
- rescue NewRequestException
26
- end
21
+ yield
27
22
  TestServlet.request
28
23
  end
29
24
 
30
- def setup_webserver(timeout=3)
25
+ def setup_webserver(timeout=5)
26
+ port = 7999
31
27
  $webserver_timeout = timeout
32
- $webserverthread ||= Thread.new do
33
- $webserver ||= WEBrick::HTTPServer.new :BindAddress => '127.0.0.1', :Port => 7999, :DocumentRoot => File.dirname(__FILE__), :Logger => WEBrick::Log.new("/dev/null"), :AccessLog => [nil, nil]
34
- $webserver.mount '/test', TestServlet
35
- $webserver.start
28
+ Thread.abort_on_exception = true
29
+
30
+ if !$webserver
31
+ newflag = true
32
+ $webserver = WEBrick::HTTPServer.new :BindAddress => '127.0.0.1', :Port => port, :DocumentRoot => File.dirname(__FILE__), :Logger => WEBrick::Log.new("/dev/null"), :AccessLog => [nil, nil]
33
+ $webserver.mount '/', TestServlet
34
+ $webserverthread ||= Thread.new do
35
+ $webserver.start
36
+ end
36
37
  end
38
+ sleep 3 if newflag
39
+ "http://127.0.0.1:#{port}/"
37
40
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: roozer_client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: '0.1'
5
+ version: '0.2'
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Snow
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-20 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  requirement: !ruby/object:Gem::Requirement
@@ -53,12 +53,12 @@ extra_rdoc_files:
53
53
  - lib/roozer_client.rb
54
54
  files:
55
55
  - CHANGELOG
56
+ - Manifest
56
57
  - README
57
58
  - Rakefile
58
59
  - lib/roozer_client.rb
59
60
  - test/test_all.rb
60
61
  - test/test_helper.rb
61
- - Manifest
62
62
  - roozer_client.gemspec
63
63
  homepage: https://github.com/andys/roozer-client
64
64
  licenses: []