restrack 1.6.4 → 1.6.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -37,3 +37,9 @@ desc 'Run sample_app_4 tests.'
37
37
  Rake::TestTask.new('test4') { |t|
38
38
  t.pattern = 'test/sample_app_4/**/test_*.rb'
39
39
  }
40
+
41
+
42
+ desc 'Run sample_app_5 tests.'
43
+ Rake::TestTask.new('test5') { |t|
44
+ t.pattern = 'test/sample_app_5/**/test_*.rb'
45
+ }
@@ -0,0 +1,14 @@
1
+ module RESTRack
2
+ class Hooks
3
+ # Use this to execute a code block prior to all requests handled by your service.
4
+ # For example, to do database connection per request you could establish db connections and transactions here.
5
+ def pre_processor(request)
6
+
7
+ end
8
+ # Use this to execute code after all requests handled by your service.
9
+ # For example, to do database connection per request you could commit transactions and/or teardown db connections here.
10
+ def post_processor(request)
11
+
12
+ end
13
+ end # class Hooks
14
+ end # module RESTRack
@@ -6,6 +6,8 @@ class <%= @service_name.camelize %>::WebService < RESTRack::WebService; end
6
6
  RESTRack::CONFIG = RESTRack::load_config(File.join(File.dirname(__FILE__), 'config/constants.yaml'))
7
7
  RESTRack::CONFIG[:ROOT] = File.dirname(__FILE__)
8
8
 
9
+ require File.join(RESTRack::CONFIG[:ROOT], 'hooks') if File.exists?(File.join(RESTRack::CONFIG[:ROOT], 'hooks'))
10
+
9
11
  # Dynamically load all controllers
10
12
  Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
11
13
  next if File.extname(file) != '.rb'
@@ -5,17 +5,18 @@ require 'active_support/inflector'
5
5
 
6
6
  module RESTRack
7
7
  class Generator
8
-
8
+
9
9
  TEMPLATE = {
10
10
  :service => 'loader.rb.erb',
11
11
  :rackup => 'config.ru.erb',
12
12
  :constants => 'constants.yaml.erb',
13
13
  :controller => 'controller.rb.erb',
14
- :descendant_controller => 'descendant_controller.rb.erb'
14
+ :descendant_controller => 'descendant_controller.rb.erb',
15
+ :hooks => 'hooks.rb.erb'
15
16
  }
16
-
17
+
17
18
  class << self
18
-
19
+
19
20
  # Generate controller file
20
21
  def generate_controller(name)
21
22
  template = get_template_for( :controller )
@@ -24,7 +25,7 @@ module RESTRack
24
25
  # Generate view folder for controller
25
26
  FileUtils.makedirs("#{base_dir}/views/#{name}")
26
27
  end
27
-
28
+
28
29
  # Generate controller file the descends from specified parent, to enable
29
30
  # grouping of controller types and/or overarching functionality.
30
31
  def generate_descendant_controller(name, parent)
@@ -34,7 +35,7 @@ module RESTRack
34
35
  # Generate view folder for controller
35
36
  FileUtils.makedirs("#{base_dir}/views/#{name}")
36
37
  end
37
-
38
+
38
39
  # Generate a new RESTRack service
39
40
  def generate_service(name)
40
41
  FileUtils.makedirs("#{name}/config")
@@ -42,45 +43,49 @@ module RESTRack
42
43
  FileUtils.makedirs("#{name}/models")
43
44
  FileUtils.makedirs("#{name}/test")
44
45
  FileUtils.makedirs("#{name}/views")
45
-
46
+
46
47
  template = get_template_for( :service )
47
48
  resultant_string = template.result( get_binding_for_service( name ) )
48
49
  File.open("#{name}/loader.rb", 'w') {|f| f.puts resultant_string }
49
-
50
+
50
51
  template = get_template_for( :rackup )
51
52
  resultant_string = template.result( get_binding_for_service( name ) )
52
53
  File.open("#{name}/config.ru", 'w') {|f| f.puts resultant_string }
53
-
54
+
54
55
  template = get_template_for( :constants )
55
56
  resultant_string = template.result( get_binding_for_service( name ) )
56
57
  File.open("#{name}/config/constants.yaml", 'w') {|f| f.puts resultant_string }
58
+
59
+ template = get_template_for( :hooks )
60
+ resultant_string = template.result( get_binding_for_service( name ) )
61
+ File.open("#{name}/hooks.rb", 'w') {|f| f.puts resultant_string }
57
62
  end
58
-
63
+
59
64
  private
60
-
65
+
61
66
  def get_template_for(type)
62
67
  template_file = File.new(File.join(File.dirname(__FILE__),"generator/#{TEMPLATE[type]}"))
63
68
  template = ERB.new( template_file.read, nil, "%" )
64
69
  end
65
-
70
+
66
71
  def get_binding_for_controller(name)
67
72
  @name = name
68
73
  @service_name = get_service_name
69
74
  binding
70
75
  end
71
-
76
+
72
77
  def get_binding_for_descendant_controller(name, parent)
73
78
  @name = name
74
79
  @parent = parent
75
80
  @service_name = get_service_name
76
81
  binding
77
82
  end
78
-
83
+
79
84
  def get_binding_for_service(name)
80
85
  @service_name = name
81
86
  binding
82
87
  end
83
-
88
+
84
89
  def get_service_name
85
90
  line = ''
86
91
  begin
@@ -96,7 +101,7 @@ module RESTRack
96
101
  end
97
102
  return service_name
98
103
  end
99
-
104
+
100
105
  def base_dir
101
106
  base_dir = nil
102
107
  this_path = File.join( Dir.pwd, 'config/constants.yaml')
@@ -111,7 +116,7 @@ module RESTRack
111
116
  raise 'The config/constants.yaml file could not found when determining base_dir!' unless base_dir
112
117
  return base_dir
113
118
  end
114
-
119
+
115
120
  end # class << self
116
121
 
117
122
  end # class
@@ -1,3 +1,3 @@
1
1
  module RESTRack
2
- VERSION = "1.6.4"
2
+ VERSION = "1.6.5"
3
3
  end
@@ -4,12 +4,19 @@ module RESTRack
4
4
  # Establish the namespace pointer.
5
5
  def initialize
6
6
  RESTRack::CONFIG[:SERVICE_NAME] = self.class.to_s.split('::')[0].to_sym
7
+ @request_hook = RESTRack::Hooks.new if RESTRack.const_defined?(:Hooks)
7
8
  end
8
9
 
9
10
  # Handle requests in the Rack way.
10
11
  def call( env )
11
12
  resource_request = RESTRack::ResourceRequest.new( :request => Rack::Request.new(env) )
13
+ unless @request_hook.nil? or (RESTRack::CONFIG.has_key?(:PRE_PROCESSOR_DISABLED) and RESTRack::CONFIG[:PRE_PROCESSOR_DISABLED])
14
+ @request_hook.pre_processor(resource_request)
15
+ end
12
16
  response = RESTRack::Response.new(resource_request)
17
+ unless @request_hook.nil? or (RESTRack::CONFIG.has_key?(:POST_PROCESSOR_DISABLED) and RESTRack::CONFIG[:POST_PROCESSOR_DISABLED])
18
+ @request_hook.post_processor(resource_request)
19
+ end
13
20
  return response.output
14
21
  end # method call
15
22
 
@@ -27,11 +27,13 @@
27
27
  # The stack trace will not be added to 500 response body by default, set to true to enable.
28
28
  :SHOW_STACK: true
29
29
 
30
+ # :TRANSCODE: and :FORCE_ENCODING: are optional config settings
30
31
  # String#encode will be called when this value is set
31
32
  #:TRANSCODE: ISO-8859-1 #or UTF-8 etc
32
33
  # String#force_encoding will be called when this value is set
33
34
  #:FORCE_ENCODING: ISO-8859-1
34
35
 
36
+ # :CORS: is an optional config setting
35
37
  # CORS Header configuration
36
38
  # Supported:
37
39
  # - Access-Control-Allow-Origin: http://localhost
@@ -48,3 +50,7 @@
48
50
  #:CORS:
49
51
  # Access-Control-Allow-Origin: http://restrack.me
50
52
  # Access-Control-Allow-Methods: POST, GET
53
+
54
+ # :PRE_PROCESSOR_DISABLED: and :POST_PROCESSOR_DISABLED: are optional config settings and are false by default
55
+ #:PRE_PROCESSOR_DISABLED: true
56
+ #:POST_PROCESSOR_DISABLED: true
@@ -0,0 +1,24 @@
1
+ #GENERATOR-CONST# -DO NOT REMOVE OR CHANGE THIS LINE- Application-Namespace => sample_app_5
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_5.log'
9
+ # Request log path definition
10
+ :REQUEST_LOG: '/var/log/restrack/sample_app_5.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,3 @@
1
+ # rackup config.ru
2
+ require File.join(File.dirname(__FILE__),'loader')
3
+ run SampleApp5::WebService.new
@@ -0,0 +1,11 @@
1
+ class SampleApp5::HookController < RESTRack::ResourceController
2
+
3
+ def show(id)
4
+ if id == 'pre_processor'
5
+ { 'pre_processor_flag' => @resource_request.instance_variable_get(:@pre_processor_executed).to_s }
6
+ else
7
+ { 'neither' => true }
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,14 @@
1
+ module RESTRack
2
+ class Hooks
3
+ # Use this to execute a code block prior to all requests handled by your service.
4
+ # For example, to do database connection per request you could establish db connections and transactions here.
5
+ def pre_processor(request)
6
+ request.instance_variable_set(:@pre_processor_executed, true)
7
+ end
8
+ # Use this to execute code after all requests handled by your service.
9
+ # For example, to do database connection per request you could commit transactions and/or teardown db connections here.
10
+ def post_processor(request)
11
+ $post_processor_executed = true
12
+ end
13
+ end # class Hooks
14
+ end # module RESTRack
@@ -0,0 +1,26 @@
1
+ # for development only
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__),'../../lib'))
3
+ #####
4
+ require 'restrack'
5
+
6
+ module SampleApp5; end
7
+ class SampleApp5::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
+ require File.join(RESTRack::CONFIG[:ROOT], 'hooks') if File.exists?(File.join(RESTRack::CONFIG[:ROOT], 'hooks'))
13
+
14
+ # Dynamically load all controllers
15
+ Find.find( File.join(File.dirname(__FILE__), 'controllers') ) do |file|
16
+ next if File.extname(file) != '.rb'
17
+ require file
18
+ end
19
+
20
+ if File.directory?( File.join(File.dirname(__FILE__), 'models') )
21
+ # Dynamically load all models
22
+ Find.find( File.join(File.dirname(__FILE__), 'models') ) do |file|
23
+ next if File.extname(file) != '.rb'
24
+ require file
25
+ end
26
+ end
@@ -0,0 +1,63 @@
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 SampleApp5::TestHooks < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @ws = SampleApp5::WebService.new
11
+ end
12
+
13
+ def test_pre_processor_enabled
14
+ env = Rack::MockRequest.env_for('/hook/pre_processor', {
15
+ :method => 'GET'
16
+ })
17
+ output = ''
18
+ assert_nothing_raised do
19
+ output = @ws.call(env)
20
+ end
21
+ test_val = { 'pre_processor_flag' => true.to_s }.to_json
22
+ assert_equal test_val, output[2][0]
23
+ end
24
+
25
+ def test_pre_processor_disabled
26
+ RESTRack::CONFIG[:PRE_PROCESSOR_DISABLED] = true
27
+ env = Rack::MockRequest.env_for('/hook/pre_processor', {
28
+ :method => 'GET'
29
+ })
30
+ output = ''
31
+ assert_nothing_raised do
32
+ output = @ws.call(env)
33
+ end
34
+ test_val = { 'pre_processor_flag' => nil.to_s }.to_json
35
+ assert_equal test_val, output[2][0]
36
+ RESTRack::CONFIG[:PRE_PROCESSOR_DISABLED] = false
37
+ end
38
+
39
+ def test_post_processor_enabled
40
+ env = Rack::MockRequest.env_for('/hook/post_processor', {
41
+ :method => 'GET'
42
+ })
43
+ output = ''
44
+ assert_nothing_raised do
45
+ output = @ws.call(env)
46
+ end
47
+ assert $post_processor_executed
48
+ $post_processor_executed = nil
49
+ end
50
+
51
+ def test_post_processor_disabled
52
+ RESTRack::CONFIG[:POST_PROCESSOR_DISABLED] = true
53
+ env = Rack::MockRequest.env_for('/hook/post_processor', {
54
+ :method => 'GET'
55
+ })
56
+ output = ''
57
+ assert_nothing_raised do
58
+ output = @ws.call(env)
59
+ end
60
+ assert !$post_processor_executed
61
+ RESTRack::CONFIG[:POST_PROCESSOR_DISABLED] = false
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.6.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-30 00:00:00.000000000 Z
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -169,6 +169,7 @@ files:
169
169
  - lib/restrack/generator/constants.yaml.erb
170
170
  - lib/restrack/generator/controller.rb.erb
171
171
  - lib/restrack/generator/descendant_controller.rb.erb
172
+ - lib/restrack/generator/hooks.rb.erb
172
173
  - lib/restrack/generator/loader.rb.erb
173
174
  - lib/restrack/http_status.rb
174
175
  - lib/restrack/resource_controller.rb
@@ -226,6 +227,12 @@ files:
226
227
  - test/sample_app_4/test/test_controller_modifiers.rb
227
228
  - test/sample_app_4/test/test_formats.rb
228
229
  - test/sample_app_4/views/alphatest.png
230
+ - test/sample_app_5/config.ru
231
+ - test/sample_app_5/config/constants.yaml
232
+ - test/sample_app_5/controllers/hook_controller.rb
233
+ - test/sample_app_5/hooks.rb
234
+ - test/sample_app_5/loader.rb
235
+ - test/sample_app_5/test/test_hooks.rb
229
236
  - test/test_support.rb
230
237
  - test/test_web_service.rb
231
238
  homepage: http://github.com/stjohncj/RESTRack