restrack 0.0.1

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.
Files changed (60) hide show
  1. data/Gemfile +4 -0
  2. data/README.rdoc +71 -0
  3. data/Rakefile +32 -0
  4. data/bin/restrack +23 -0
  5. data/config/constants.yaml +8 -0
  6. data/lib/restrack/generator/constants.yaml.erb +24 -0
  7. data/lib/restrack/generator/controller.rb.erb +35 -0
  8. data/lib/restrack/generator/loader.rb.erb +21 -0
  9. data/lib/restrack/generator.rb +93 -0
  10. data/lib/restrack/http_status.rb +10 -0
  11. data/lib/restrack/resource_controller.rb +192 -0
  12. data/lib/restrack/resource_request.rb +135 -0
  13. data/lib/restrack/support.rb +56 -0
  14. data/lib/restrack/version.rb +3 -0
  15. data/lib/restrack/web_service.rb +66 -0
  16. data/lib/restrack.rb +24 -0
  17. data/restrack.gemspec +28 -0
  18. data/test/sample_app_1/config/constants.yaml +25 -0
  19. data/test/sample_app_1/controllers/bat_controller.rb +9 -0
  20. data/test/sample_app_1/controllers/baz_controller.rb +9 -0
  21. data/test/sample_app_1/controllers/baza_controller.rb +11 -0
  22. data/test/sample_app_1/controllers/bazu_controller.rb +16 -0
  23. data/test/sample_app_1/controllers/foo_bar_controller.rb +62 -0
  24. data/test/sample_app_1/loader.rb +31 -0
  25. data/test/sample_app_1/test/test_controller_actions.rb +122 -0
  26. data/test/sample_app_1/test/test_controller_modifiers.rb +153 -0
  27. data/test/sample_app_1/test/test_formats.rb +119 -0
  28. data/test/sample_app_1/test/test_resource_request.rb +160 -0
  29. data/test/sample_app_1/test/test_web_service.rb +27 -0
  30. data/test/sample_app_1/views/foo_bar/show.xml.builder +4 -0
  31. data/test/sample_app_2/config/constants.yaml +24 -0
  32. data/test/sample_app_2/controllers/bat_controller.rb +9 -0
  33. data/test/sample_app_2/controllers/baz_controller.rb +9 -0
  34. data/test/sample_app_2/controllers/baza_controller.rb +11 -0
  35. data/test/sample_app_2/controllers/bazu_controller.rb +8 -0
  36. data/test/sample_app_2/controllers/foo_bar_controller.rb +59 -0
  37. data/test/sample_app_2/loader.rb +31 -0
  38. data/test/sample_app_2/test/test_controller_modifiers.rb +121 -0
  39. data/test/sample_app_2/test/test_resource_request.rb +71 -0
  40. data/test/sample_app_2/views/foo_bar/show.xml.builder +4 -0
  41. data/test/sample_app_3/config/constants.yaml +24 -0
  42. data/test/sample_app_3/controllers/bat_controller.rb +9 -0
  43. data/test/sample_app_3/controllers/baz_controller.rb +9 -0
  44. data/test/sample_app_3/controllers/baza_controller.rb +11 -0
  45. data/test/sample_app_3/controllers/bazu_controller.rb +8 -0
  46. data/test/sample_app_3/controllers/foo_bar_controller.rb +59 -0
  47. data/test/sample_app_3/loader.rb +31 -0
  48. data/test/sample_app_3/test/test_resource_request.rb +42 -0
  49. data/test/sample_app_3/views/foo_bar/show.xml.builder +4 -0
  50. data/test/sample_app_4/config/constants.yaml +24 -0
  51. data/test/sample_app_4/controllers/bar_controller.rb +11 -0
  52. data/test/sample_app_4/controllers/baz_controller.rb +15 -0
  53. data/test/sample_app_4/controllers/foo_controller.rb +21 -0
  54. data/test/sample_app_4/loader.rb +31 -0
  55. data/test/sample_app_4/test/test_controller_modifiers.rb +28 -0
  56. data/test/sample_app_4/test/test_formats.rb +49 -0
  57. data/test/sample_app_4/views/alphatest.png +0 -0
  58. data/test/test_support.rb +20 -0
  59. data/test/test_web_service.rb +31 -0
  60. metadata +238 -0
@@ -0,0 +1,24 @@
1
+ #GENERATOR-CONST# -DO NOT REMOVE OR CHANGE THIS LINE- Application-Namespace => SampleApp
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/restrack/sample_app.log'
9
+ # Request log path definition
10
+ :REQUEST_LOG: '/var/log/restrack/sample_app.request.log'
11
+
12
+ # Logger object levels
13
+ :LOG_LEVEL: :DEBUG # Logger object level
14
+ :REQUEST_LOG_LEVEL: :DEBUG # Logger object level
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: 'bazu'
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: [ 'baz' ]
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,9 @@
1
+ class SampleApp::BatController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return { :WOM => 'BAT!' } if id == 777
5
+ return { :WOM => 'NOBAT!' } if id == '777'
6
+ return { :SUHWING => 'BATTER' }
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class SampleApp::BazController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return { :BAZ => 'HOLA!' } if id == 777
5
+ return { :BAZ => 'ALOHA!' } if id == '777'
6
+ return { :OTHER => 'YUP' }
7
+ end
8
+
9
+ end
@@ -0,0 +1,11 @@
1
+ class SampleApp::BazaController < RESTRack::ResourceController
2
+
3
+ keyed_with_type Fixnum
4
+
5
+ def show(id)
6
+ return { :BAZA => 'YESSIR' } if id == 1
7
+ return { :NOWAY => 'JOSE' } if id == 8
8
+ return { :NOTTODAY => 'JOSEPH' }
9
+ end
10
+
11
+ end
@@ -0,0 +1,8 @@
1
+ class SampleApp::BazuController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ return 1 if id == 1
5
+ return 0
6
+ end
7
+
8
+ end
@@ -0,0 +1,59 @@
1
+ class SampleApp::FooBarController < RESTRack::ResourceController
2
+
3
+ has_direct_relationship_to( :baz, :as => :baz ) do |id|
4
+ if id =='144'
5
+ output = '777'
6
+ else
7
+ output = '666'
8
+ end
9
+ output # You can't "return" from a Proc! It will do a "return" in the outer method. Remember a "Proc" is not a Method.
10
+ end
11
+
12
+ has_direct_relationship_to( :bat, :as => :slugger ) do |id|
13
+ if id =='144'
14
+ output = '777'
15
+ else
16
+ output = '666'
17
+ end
18
+ output # You can't "return" from a Proc! It will do a "return" in the outer method. Remember a "Proc" is not a Method.
19
+ end
20
+
21
+ has_direct_relationships_to( :baza, :as => :children ) do |id|
22
+ [1,2,3,4,5,6,7,8,9]
23
+ end
24
+
25
+ has_mapped_relationships_to( :bazu, :as => :maps ) do |id|
26
+ {
27
+ :first => 1,
28
+ :second => 2,
29
+ :third => 3
30
+ }
31
+ end
32
+
33
+ def index
34
+ [1,2,3,4,5,6,7]
35
+ end
36
+ def create
37
+ { :success => true }
38
+ end
39
+ def replace
40
+ { :success => true }
41
+ end
42
+ def drop
43
+ { :success => true }
44
+ end
45
+
46
+ def show(id)
47
+ { :foo => 'bar', :baz => 123 }
48
+ end
49
+ def update(id)
50
+ { :success => true }
51
+ end
52
+ def destroy(id)
53
+ { :success => true }
54
+ end
55
+ def add(id)
56
+ { :success => true }
57
+ end
58
+
59
+ end
@@ -0,0 +1,31 @@
1
+ # for development only
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'../../lib'))
3
+ #####
4
+ require 'restrack'
5
+
6
+ module SampleApp; end
7
+ class SampleApp::WebService < RESTRack::WebService; end
8
+
9
+ RESTRack::CONFIG = RESTRack::load_config(File.join(File.dirname(__FILE__), 'config/constants.yaml'))
10
+ RESTRack::CONFIG[:ROOT] = File.dirname(__FILE__)
11
+
12
+ # Dynamically load all controllers
13
+ Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
14
+ next if File.extname(file) != '.rb'
15
+ require file
16
+ end
17
+
18
+ if File.directory?( File.join(File.dirname(__FILE__), 'models') )
19
+ # Dynamically load all models
20
+ Find.find( File.join(File.dirname(__FILE__), 'models') ) do |file|
21
+ next if File.extname(file) != '.rb'
22
+ require file
23
+ end
24
+ end
25
+
26
+ puts "sample_app_3 RESTRack::CONFIG:\n"
27
+ config = RESTRack::CONFIG.keys.map {|c| c.to_s }.sort
28
+ config.each do |key|
29
+ puts "\t" + key + ' => ' + RESTRack::CONFIG[key.to_sym].to_s
30
+ end
31
+ puts "\n"
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+
6
+ class SampleApp::TestResourceRequest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @ws = SampleApp::WebService.new # init logs
10
+ end
11
+
12
+ ## 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.
13
+ #:ROOT_RESOURCE_ACCEPT: [ 'baz' ]
14
+ ## 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).
15
+ ##:ROOT_RESOURCE_DENY: []
16
+ def test_root_resource_denied
17
+ # ROOT_RESOURCE_DENY is not supplied, baz is an allowed resource
18
+ env = Rack::MockRequest.env_for('/baz/144', {
19
+ :method => 'GET'
20
+ })
21
+ output = ''
22
+ assert_nothing_raised do
23
+ output = @ws.call(env)
24
+ end
25
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
26
+ assert_not_equal 403, output[0]
27
+ end
28
+
29
+ def test_root_resource_accept
30
+ # this resource is not in ROOT_RESOURCE_ACCEPT, which is set to ['baz']
31
+ env = Rack::MockRequest.env_for('/foo_bar/144', {
32
+ :method => 'GET'
33
+ })
34
+ output = ''
35
+ assert_nothing_raised do
36
+ output = @ws.call(env)
37
+ end
38
+ #test_val = [403, {"Content-Type"=>"text/plain"}, "HTTPStatus::HTTP403Forbidden\nYou are forbidden to access that resource."]
39
+ assert_equal 403, output[0]
40
+ end
41
+
42
+ end
@@ -0,0 +1,4 @@
1
+ xml.data do
2
+ xml.foo data[:foo]
3
+ xml.baz data[:baz]
4
+ end
@@ -0,0 +1,24 @@
1
+ #GENERATOR-CONST# -DO NOT REMOVE OR CHANGE THIS LINE- Application-Namespace => SampleApp
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/restrack/sample_app.log'
9
+ # Request log path definition
10
+ :REQUEST_LOG: '/var/log/restrack/sample_app.request.log'
11
+
12
+ # Logger object levels
13
+ :LOG_LEVEL: :DEBUG # Logger object level
14
+ :REQUEST_LOG_LEVEL: :DEBUG # Logger object level
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: ''
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,11 @@
1
+ class SampleApp::BarController < RESTRack::ResourceController
2
+
3
+ def index
4
+ [0,1,2,3]
5
+ end
6
+
7
+ def show(id)
8
+ data = "Hello from Bar with id of #{id}."
9
+ end
10
+
11
+ end
@@ -0,0 +1,15 @@
1
+ class SampleApp::BazController < RESTRack::ResourceController
2
+
3
+ def initialize
4
+ @resource_request.mime_type = RESTRack.mime_type_for(:JSON)
5
+ end
6
+
7
+ def index
8
+ ['cat', 'dog', 'rat', 'emu']
9
+ end
10
+
11
+ def show(id)
12
+ data = { id => "Hello from Bazzz." }
13
+ end
14
+
15
+ end
@@ -0,0 +1,21 @@
1
+ class SampleApp::FooController < RESTRack::ResourceController
2
+
3
+ has_relationship_to( :bar )
4
+ has_relationship_to( :baz, :as => :bazzz )
5
+
6
+ def show_yaml(id)
7
+ @resource_request.mime_type = RESTRack.mime_type_for('yaml')
8
+ data = { :foo => id, :baz => 'bat' }
9
+ end
10
+
11
+ def show_text(id)
12
+ @resource_request.mime_type = RESTRack.mime_type_for('text')
13
+ data = "Hello #{id}!"
14
+ end
15
+
16
+ def show_image(id)
17
+ @resource_request.mime_type = RESTRack.mime_type_for('png')
18
+ data = File.read(File.join(File.dirname(__FILE__),'../views/alphatest.png'))
19
+ end
20
+
21
+ end
@@ -0,0 +1,31 @@
1
+ # for development only
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'../../lib'))
3
+ #####
4
+ require 'restrack'
5
+
6
+ module SampleApp; end
7
+ class SampleApp::WebService < RESTRack::WebService; end
8
+
9
+ RESTRack::CONFIG = RESTRack::load_config(File.join(File.dirname(__FILE__), 'config/constants.yaml'))
10
+ RESTRack::CONFIG[:ROOT] = File.dirname(__FILE__)
11
+
12
+ # Dynamically load all controllers
13
+ Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
14
+ next if File.extname(file) != '.rb'
15
+ require file
16
+ end
17
+
18
+ if File.directory?( File.join(File.dirname(__FILE__), 'models') )
19
+ # Dynamically load all models
20
+ Find.find( File.join(File.dirname(__FILE__), 'models') ) do |file|
21
+ next if File.extname(file) != '.rb'
22
+ require file
23
+ end
24
+ end
25
+
26
+ puts "sample_app_4 RESTRack::CONFIG:\n"
27
+ config = RESTRack::CONFIG.keys.map {|c| c.to_s }.sort
28
+ config.each do |key|
29
+ puts "\t" + key + ' => ' + RESTRack::CONFIG[key.to_sym].to_s
30
+ end
31
+ puts "\n"
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+ require 'pp'
6
+
7
+ class SampleApp::TestControllerModifiers < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @ws = SampleApp::WebService.new
11
+ end
12
+
13
+ def test_has_relationship_to
14
+ env = Rack::MockRequest.env_for('/foo/123/bar', {
15
+ :method => 'GET'
16
+ })
17
+ output = ''
18
+ assert_nothing_raised do
19
+ output = @ws.call(env)
20
+ end
21
+ test_val = [0,1,2,3].to_json
22
+ assert_equal test_val, output[2]
23
+
24
+
25
+ #"Hello from Bar with id of
26
+ end
27
+
28
+ end
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','loader'))
5
+ require 'pp'
6
+
7
+ class SampleApp::TestFormats < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @ws = SampleApp::WebService.new
11
+ end
12
+
13
+ # Test YAML, text, and an image format
14
+ def test_yaml
15
+ env = Rack::MockRequest.env_for('/foo/123/show_yaml', {
16
+ :method => 'GET'
17
+ })
18
+ output = ''
19
+ assert_nothing_raised do
20
+ output = @ws.call(env)
21
+ end
22
+ test_val = YAML.dump( { :foo => '123', :baz => 'bat' } )
23
+ assert_equal test_val, output[2]
24
+ end
25
+
26
+ def test_text
27
+ env = Rack::MockRequest.env_for('/foo/123/show_text', {
28
+ :method => 'GET'
29
+ })
30
+ output = ''
31
+ assert_nothing_raised do
32
+ output = @ws.call(env)
33
+ end
34
+ test_val = 'Hello 123!'
35
+ assert_equal test_val, output[2]
36
+ end
37
+
38
+ def test_image
39
+ env = Rack::MockRequest.env_for('/foo/123/show_image', {
40
+ :method => 'GET'
41
+ })
42
+ output = ''
43
+ assert_nothing_raised do
44
+ output = @ws.call(env)
45
+ end
46
+ assert_equal output[2].length, 26529
47
+ end
48
+
49
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.expand_path(File.join(File.dirname(__FILE__),'../lib/restrack'))
4
+
5
+ module RESTRack
6
+ class TestSupport < Test::Unit::TestCase
7
+
8
+ def test_constants
9
+ assert_nothing_raised do
10
+ RESTRack::CONFIG[:LOG].to_sym.to_s
11
+ RESTRack::CONFIG[:REQUEST_LOG].to_sym.to_s
12
+ RESTRack::CONFIG[:DEFAULT_FORMAT].to_sym.to_s
13
+ RESTRack::CONFIG[:DEFAULT_RESOURCE].to_sym.to_s
14
+ assert RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT].blank? || RESTRack::CONFIG[:ROOT_RESOURCE_ACCEPT].class == Array
15
+ assert RESTRack::CONFIG[:ROOT_RESOURCE_DENY].blank? || RESTRack::CONFIG[:ROOT_RESOURCE_DENY].class == Array
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require File.expand_path(File.join(File.dirname(__FILE__),'../lib/restrack'))
5
+
6
+ module RESTRack
7
+ class TestWebService < Test::Unit::TestCase
8
+
9
+ def test_call
10
+ env = Rack::MockRequest.env_for('/foo', {
11
+ :method => 'POST',
12
+ :params => %Q|[
13
+ {
14
+ "bar": "baz"
15
+ }
16
+ ]|
17
+ })
18
+ assert_nothing_raised do
19
+ ws = RESTRack::WebService.new # init logs
20
+ ws.call(env)
21
+ end
22
+ end
23
+
24
+ def test_initialize
25
+ assert_nothing_raised do
26
+ ws = RESTRack::WebService.new
27
+ end
28
+ end
29
+
30
+ end # class TestWebService
31
+ end # module RESTRack