restrack-splitter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specifying gem's dependencies in restrack.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris St. John
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = restrack-splitter
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to restrack-splitter
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) 2011 Chris St. John. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ task :default => [:test]
8
+
9
+ desc 'Run tests.'
10
+ Rake::TestTask.new('test') { |t|
11
+ t.pattern = 'test/test_*.rb'
12
+ }
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'restrack-client'
3
+
4
+ module RESTRack
5
+ class Splitter
6
+
7
+ def initialize(uri_list, format=:JSON)
8
+ @clients = uri_list.collect do |uri|
9
+ RESTRack::Client.new(uri, format)
10
+ end
11
+ @path = ''
12
+ end
13
+
14
+ def method_missing(resource_name, *args)
15
+ execute {|client| client.__send__(resource_name.to_sym, *args) }
16
+ self
17
+ end
18
+
19
+ def get
20
+ execute {|client| client.get }
21
+ end
22
+
23
+ def delete
24
+ execute {|client| client.delete }
25
+ end
26
+
27
+ def post(data=nil)
28
+ execute {|client| client.post(data) }
29
+ end
30
+
31
+ def put(data=nil)
32
+ execute {|client| client.put(data) }
33
+ end
34
+
35
+ private
36
+
37
+ def execute(&action)
38
+ threads = []
39
+ results = []
40
+ for client in @clients
41
+ threads << Thread.new(client) {|client| results << action.call(client) }
42
+ end
43
+ threads.each { |thread| thread.join }
44
+ results
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module RESTRackSplitter
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "restrack-splitter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "restrack-splitter"
7
+ s.version = RESTRackSplitter::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Chris St. John']
10
+ s.email = ['chris@stjohnstudios.com']
11
+ s.homepage = 'http://github.com/stjohncj/RESTRack-Splitter'
12
+ s.summary = %q{A library for interacting with RESTful web services. Use this to communicate with RESTRack based services.}
13
+ s.description = %q{A library for interacting with RESTful web services. Use this to communicate with RESTRack based services.}
14
+ s.rubyforge_project = "restrack-splitter"
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency 'shoulda'
21
+ s.add_runtime_dependency 'restrack-client'
22
+
23
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'restrack-splitter'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,3 @@
1
+ # Rails.root/config.ru
2
+ require 'loader'
3
+ run TestApp::WebService.new
@@ -0,0 +1,24 @@
1
+ #GENERATOR-CONST# -DO NOT REMOVE OR CHANGE THIS LINE- Application-Namespace => test_app
2
+ #
3
+ # = constants.yaml
4
+ # This is where RESTRack applications define the constants relevant to their particular
5
+ # application that are used by the RESTRack base classes.
6
+
7
+ # Application log path definition
8
+ :LOG: '/var/log/test_app/test_app.log'
9
+ # Request log path definition
10
+ :REQUEST_LOG: '/var/log/test_app/test_app.request.log'
11
+
12
+ # Logger object levels
13
+ :LOG_LEVEL: :DEBUG
14
+ :REQUEST_LOG_LEVEL: :DEBUG
15
+
16
+ # Supported formats are :JSON, :XML, :YAML, :BIN, :TEXT
17
+ :DEFAULT_FORMAT: :JSON
18
+ # The resource which will handle root level requests where the name is not specified. Best for users of this not to implement method_missing in their default controller, unless they are checking for bad URI.
19
+ :DEFAULT_RESOURCE: nil
20
+
21
+ # These are the resources which can be accessed from the root of your web service. If left empty, all resources are available at the root.
22
+ :ROOT_RESOURCE_ACCEPT: []
23
+ # These are the resources which cannot be accessed from the root of your web service. Use either this or ROOT_RESOURCE_ACCEPT as a blacklist or whitelist to establish routing (relationships defined in resource controllers define further routing).
24
+ :ROOT_RESOURCE_DENY: []
@@ -0,0 +1,39 @@
1
+ class TestApp::ParentController < RESTRack::ResourceController
2
+
3
+ has_relationship_to :responses do |id|
4
+ id
5
+ end
6
+
7
+ def index
8
+
9
+ end
10
+
11
+ def create
12
+
13
+ end
14
+
15
+ def replace
16
+
17
+ end
18
+
19
+ def destroy
20
+
21
+ end
22
+
23
+ def show(id)
24
+
25
+ end
26
+
27
+ def update(id)
28
+
29
+ end
30
+
31
+ def delete(id)
32
+
33
+ end
34
+
35
+ def add(id)
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,66 @@
1
+ class TestApp::ResponsesController < RESTRack::ResourceController
2
+
3
+ def method_missing(method_name, *args)
4
+ {
5
+ :method_called => method_name.to_s,
6
+ :args => args.join(':')
7
+ }
8
+ end
9
+
10
+ def index
11
+ {
12
+ :action => :index
13
+ }
14
+ end
15
+
16
+ def create
17
+ {
18
+ :action => :create,
19
+ :data => @input
20
+ }
21
+ end
22
+
23
+ def replace
24
+ {
25
+ :action => :replace,
26
+ :data => @input
27
+ }
28
+ end
29
+
30
+ def drop
31
+ {
32
+ :action => :destroy
33
+ }
34
+ end
35
+
36
+ def show(id)
37
+ {
38
+ :action => :show,
39
+ :id => id
40
+ }
41
+ end
42
+
43
+ def update(id)
44
+ {
45
+ :action => :update,
46
+ :id => id,
47
+ :data => @input
48
+ }
49
+ end
50
+
51
+ def destroy(id)
52
+ {
53
+ :action => :delete,
54
+ :id => id
55
+ }
56
+ end
57
+
58
+ def add(id)
59
+ {
60
+ :action => :add,
61
+ :id => id,
62
+ :data => @input
63
+ }
64
+ end
65
+
66
+ end
@@ -0,0 +1,21 @@
1
+ require 'restrack'
2
+
3
+ module TestApp; end
4
+ class TestApp::WebService < RESTRack::WebService; end
5
+
6
+ RESTRack::CONFIG = RESTRack::load_config(File.join(File.dirname(__FILE__), 'config/constants.yaml'))
7
+ RESTRack::CONFIG[:ROOT] = File.dirname(__FILE__)
8
+
9
+ # Dynamically load all controllers
10
+ Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
11
+ next if File.extname(file) != '.rb'
12
+ require file
13
+ end
14
+
15
+ if File.directory?( File.join(File.dirname(__FILE__), 'models') )
16
+ # Dynamically load all models
17
+ Find.find( File.join(File.dirname(__FILE__), 'models') ) do |file|
18
+ next if File.extname(file) != '.rb'
19
+ require file
20
+ end
21
+ end
@@ -0,0 +1,193 @@
1
+ require File.join( File.dirname(__FILE__ ), 'helper' )
2
+
3
+ class TestRestrackSplitter < Test::Unit::TestCase
4
+
5
+ context 'instantiation' do
6
+ should 'accept a String URI' do
7
+ assert_nothing_raised { client_1 = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293']) }
8
+ end
9
+ should 'accept a URI object' do
10
+ uri = URI.parse('http://localhost:9292')
11
+ assert_nothing_raised { client_2 = RESTRack::Client.new(uri) }
12
+ end
13
+ end
14
+
15
+ should 'get a resource' do
16
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
17
+ get_response = nil
18
+ assert_nothing_raised do
19
+ get_response = client.responses(1).get
20
+ end
21
+ expected_response = [{ 'action' => 'show', 'id' => '1' }, { 'action' => 'show', 'id' => '1' }]
22
+ assert_equal expected_response, get_response
23
+ end
24
+ should 'get a collection' do
25
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
26
+ get_response = nil
27
+ assert_nothing_raised do
28
+ get_response = client.responses.get
29
+ end
30
+ expected_response = [{ 'action' => 'index' }, { 'action' => 'index' }]
31
+ assert_equal expected_response, get_response
32
+ end
33
+
34
+ should 'delete a resource' do
35
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
36
+ delete_response = nil
37
+ assert_nothing_raised do
38
+ delete_response = client.responses(1).delete
39
+ end
40
+ expected_response = [{ 'action' => 'delete', 'id' => '1' }, { 'action' => 'delete', 'id' => '1' }]
41
+ assert_equal expected_response, delete_response
42
+ end
43
+ should 'delete a collection' do
44
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
45
+ delete_response = nil
46
+ assert_nothing_raised do
47
+ delete_response = client.responses.delete
48
+ end
49
+ expected_response = [{ 'action' => 'destroy' }, { 'action' => 'destroy' }]
50
+ assert_equal expected_response, delete_response
51
+ end
52
+
53
+ should 'post a resource' do
54
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
55
+ post_response = nil
56
+ data = {
57
+ 'test' => 'data'
58
+ }
59
+ assert_nothing_raised do
60
+ post_response = client.responses(1).post(data)
61
+ end
62
+ expected_response = [{ 'action' => 'add', 'id' => '1', 'data' => data }, { 'action' => 'add', 'id' => '1', 'data' => data }]
63
+ assert_equal expected_response, post_response
64
+ end
65
+ should 'post a collection' do
66
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
67
+ post_response = nil
68
+ data = {
69
+ 'test' => 'data'
70
+ }
71
+ assert_nothing_raised do
72
+ post_response = client.responses.post(data)
73
+ end
74
+ expected_response = [{ 'action' => 'create', 'data' => data }, { 'action' => 'create', 'data' => data }]
75
+ assert_equal expected_response, post_response
76
+ end
77
+
78
+ should 'put a resource' do
79
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
80
+ put_response = nil
81
+ data = {
82
+ 'test' => 'data'
83
+ }
84
+ assert_nothing_raised do
85
+ put_response = client.responses(1).put(data)
86
+ end
87
+ expected_response = [{ 'action' => 'update', 'id' => '1', 'data' => data }, { 'action' => 'update', 'id' => '1', 'data' => data }]
88
+ assert_equal expected_response, put_response
89
+ end
90
+ should 'put a collection' do
91
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
92
+ put_response = nil
93
+ data = {
94
+ 'test' => 'data'
95
+ }
96
+ assert_nothing_raised do
97
+ put_response = client.responses.put(data)
98
+ end
99
+ expected_response = [{ 'action' => 'replace', 'data' => data }, { 'action' => 'replace', 'data' => data }]
100
+ assert_equal expected_response, put_response
101
+ end
102
+
103
+ should 'send and parse response json data' do
104
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'], :JSON)
105
+ post_response = nil
106
+ data = {
107
+ 'test' => 'data'
108
+ }
109
+ assert_nothing_raised do
110
+ post_response = client.responses(1).post(data)
111
+ end
112
+ expected_response = [{ 'action' => 'add', 'id' => '1', 'data' => data }, { 'action' => 'add', 'id' => '1', 'data' => data }]
113
+ assert_equal expected_response, post_response
114
+ end
115
+ should 'send and parse response xml data' do
116
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'], :XML)
117
+ post_response = nil
118
+ data = {
119
+ 'test' => 'data'
120
+ }
121
+ assert_nothing_raised do
122
+ post_response = client.responses(1).post(data)
123
+ end
124
+ expected_response = [{ 'action' => 'add', 'id' => '1', 'data' => data }, { 'action' => 'add', 'id' => '1', 'data' => data }]
125
+ assert_equal expected_response, post_response
126
+ end
127
+ #should 'send and parse response yaml data' do
128
+ # client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'], :YAML)
129
+ # post_response = nil
130
+ # data = {
131
+ # 'test' => 'data'
132
+ # }
133
+ # assert_nothing_raised do
134
+ # post_response = client.responses(1).post(data)
135
+ # end
136
+ # expected_response = [{ 'action' => 'add', 'id' => '1', 'data' => data }, { 'action' => 'add', 'id' => '1', 'data' => data }]
137
+ # assert_equal expected_response, post_response
138
+ #end
139
+ should 'allow building of request path prior to request' do
140
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
141
+ object = client.responses(1)
142
+ assert object.class.to_s, 'RESTRack::Splitter'
143
+ assert object.const_get(:path), '/responses/1'
144
+ end
145
+ should 'pivot' do
146
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
147
+ object = client.responses(1)
148
+ get_response = nil
149
+ assert_nothing_raised do
150
+ get_response = object.get
151
+ end
152
+ expected_response = [{ 'action' => 'show', 'id' => '1' }, { 'action' => 'show', 'id' => '1' }]
153
+ assert_equal expected_response, get_response
154
+ end
155
+ should 'walk a relation path to a child resource' do
156
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
157
+ post_response = nil
158
+ data = {
159
+ 'test' => 'data'
160
+ }
161
+ assert_nothing_raised do
162
+ post_response = client.parent(1).responses.post(data)
163
+ end
164
+ expected_response = [{ 'action' => 'add', 'id' => '1', 'data' => data }, { 'action' => 'add', 'id' => '1', 'data' => data }]
165
+ assert_equal expected_response, post_response
166
+ end
167
+
168
+
169
+ should 'handle multiple sequential requests' do
170
+ client = RESTRack::Splitter.new(['http://localhost:9292', 'http://localhost:9293'])
171
+ get_response = nil
172
+ assert_nothing_raised do
173
+ get_response = client.responses(1).get
174
+ end
175
+ expected_response = [{ 'action' => 'show', 'id' => '1' }, {'action' => 'show', 'id' => '1' }]
176
+ assert_equal expected_response, get_response
177
+
178
+ get_response = nil
179
+ assert_nothing_raised do
180
+ get_response = client.responses(1).get
181
+ end
182
+ expected_response = [{ 'action' => 'show', 'id' => '1' }, {'action' => 'show', 'id' => '1' }]
183
+ assert_equal expected_response, get_response
184
+
185
+ get_response = nil
186
+ assert_nothing_raised do
187
+ get_response = client.responses(1).get
188
+ end
189
+ expected_response = [{ 'action' => 'show', 'id' => '1' }, {'action' => 'show', 'id' => '1' }]
190
+ assert_equal expected_response, get_response
191
+ end
192
+
193
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restrack-splitter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Chris St. John
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-18 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: shoulda
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: restrack-client
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: A library for interacting with RESTful web services. Use this to communicate with RESTRack based services.
39
+ email:
40
+ - chris@stjohnstudios.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .document
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/restrack-splitter.rb
54
+ - lib/restrack-splitter/version.rb
55
+ - restrack-splitter.gemspec
56
+ - test/helper.rb
57
+ - test/test_app/config.ru
58
+ - test/test_app/config/constants.yaml
59
+ - test/test_app/controllers/parent_controller.rb
60
+ - test/test_app/controllers/responses_controller.rb
61
+ - test/test_app/loader.rb
62
+ - test/test_restrack-splitter.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/stjohncj/RESTRack-Splitter
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: restrack-splitter
87
+ rubygems_version: 1.5.2
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A library for interacting with RESTful web services. Use this to communicate with RESTRack based services.
91
+ test_files:
92
+ - test/helper.rb
93
+ - test/test_app/config.ru
94
+ - test/test_app/config/constants.yaml
95
+ - test/test_app/controllers/parent_controller.rb
96
+ - test/test_app/controllers/responses_controller.rb
97
+ - test/test_app/loader.rb
98
+ - test/test_restrack-splitter.rb