skates 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/skates/router.rb CHANGED
@@ -8,6 +8,10 @@ module Skates
8
8
 
9
9
  attr_reader :routes, :connection
10
10
 
11
+ def before_route(&block)
12
+ @before_route = block
13
+ end
14
+
11
15
  def initialize
12
16
  @routes = []
13
17
  end
@@ -21,10 +25,28 @@ module Skates
21
25
  ##
22
26
  # Look for the first matching route and calls the corresponding action for the corresponding controller.
23
27
  # Sends the response on the XMPP stream/
28
+ # If the before_route callback is defined, it is called.
29
+ # If the callback returns true, then, the route is NOT executed.
30
+ # This callback is very useful when an application wants to redirect any stanza it receives before handling it to the routing mechanism.
24
31
  def route(xml_stanza)
25
- route = routes.select{ |r| r.accepts?(xml_stanza) }.first
26
- return false unless route
27
- execute_route(route.controller, route.action, xml_stanza)
32
+ abort = if !@before_route.nil?
33
+ begin
34
+ @before_route.call(xml_stanza)
35
+ rescue
36
+ Skates.logger.info {
37
+ "Failed to execute before_route callback. Resuming Routing"
38
+ }
39
+ false
40
+ end
41
+ end
42
+ if !abort
43
+ route = routes.select{ |r| r.accepts?(xml_stanza) }.first
44
+ return false unless route
45
+ execute_route(route.controller, route.action, xml_stanza)
46
+ else
47
+ # The callback triggered abortion of teh routing mechanism.
48
+ return false
49
+ end
28
50
  end
29
51
 
30
52
  ##
data/lib/skates/runner.rb CHANGED
@@ -10,8 +10,7 @@ module Skates
10
10
  # Prepares the Application to run.
11
11
  def self.prepare(env)
12
12
  # Load the configuration
13
- config_file = File.open('config/config.yaml')
14
- Skates.config = YAML.load(config_file)[Skates.environment]
13
+ Skates.config = YAML.load_file(Skates.config_file)[Skates.environment]
15
14
 
16
15
  Skates.reopen_logs
17
16
 
data/lib/skates.rb CHANGED
@@ -32,7 +32,9 @@ require 'skates/base/stanza'
32
32
  # This will generate some folders and files for your application. Please see README.rdoc for further instructions
33
33
 
34
34
  module Skates
35
-
35
+
36
+ @@config_file = nil
37
+
36
38
  def self.environment=(_env)
37
39
  @@env = _env
38
40
  end
@@ -105,6 +107,18 @@ module Skates
105
107
  @@logger = logger
106
108
  end
107
109
 
110
+ ##
111
+ # Set the configuration file for this component.
112
+ def self.config_file=(file)
113
+ @@config_file = file
114
+ end
115
+
116
+ ##
117
+ # Return the configuration file for this component.
118
+ def self.config_file
119
+ @@config_file
120
+ end
121
+
108
122
  ##
109
123
  # Set the configuration for this component.
110
124
  def self.config=(conf)
@@ -59,31 +59,94 @@ describe Skates::StanzaRouter do
59
59
  end
60
60
  end
61
61
 
62
- it "should check each routes to see if they match the stanza and take the first of the matching" do
63
- @router.routes.each do |r|
64
- r.should_receive(:accepts?).with(@xml)
65
- end
66
- @router.route(@xml)
67
- end
68
-
69
- describe "if one route is found" do
62
+ context "when the before_route callback is defined" do
70
63
  before(:each) do
71
- @accepting_route = mock(Skates::Route, :accepts? => true, :action => "action", :controller => "controller", :xpath => "xpath")
72
- @router.routes << @accepting_route
64
+ @proc = Proc.new { |stanza|
65
+ }
66
+ @router.before_route(&@proc)
73
67
  end
74
-
75
- it "should call execute_route" do
76
- @router.should_receive(:execute_route).with(@accepting_route.controller, @accepting_route.action, @xml)
68
+
69
+ it "should call the callback" do
70
+ @proc.should_receive(:call).with(@xml)
77
71
  @router.route(@xml)
78
72
  end
73
+
74
+ context "when the callback returns true" do
75
+ before(:each) do
76
+ @proc = Proc.new { |stanza|
77
+ true
78
+ }
79
+ @router.before_route(&@proc)
80
+ end
81
+
82
+ it "should not even check if a route accepts this stanza" do
83
+ @router.routes.each do |r|
84
+ r.should_not_receive(:accepts?).with(@xml)
85
+ end
86
+ @router.route(@xml)
87
+ end
88
+ end
89
+
90
+ context "when the callback returns false" do
91
+ before(:each) do
92
+ @proc = Proc.new { |stanza|
93
+ false
94
+ }
95
+ @router.before_route(&@proc)
96
+ end
97
+
98
+ it "should check if a route accepts this stanza" do
99
+ @router.routes.each do |r|
100
+ r.should_receive(:accepts?).with(@xml)
101
+ end
102
+ @router.route(@xml)
103
+ end
104
+ end
105
+
106
+ context "when the callback raises an error" do
107
+ before(:each) do
108
+ @proc = Proc.new { |stanza|
109
+ raise
110
+ }
111
+ @router.before_route(&@proc)
112
+ end
113
+
114
+ it "should check if a route accepts this stanza" do
115
+ @router.routes.each do |r|
116
+ r.should_receive(:accepts?).with(@xml)
117
+ end
118
+ @router.route(@xml)
119
+ end
120
+ end
121
+
79
122
  end
123
+
124
+ context "when the before_route callback is not defined" do
125
+ it "should check each routes to see if they match the stanza and take the first of the matching" do
126
+ @router.routes.each do |r|
127
+ r.should_receive(:accepts?).with(@xml)
128
+ end
129
+ @router.route(@xml)
130
+ end
131
+
132
+ context "if one route is found" do
133
+ before(:each) do
134
+ @accepting_route = mock(Skates::Route, :accepts? => true, :action => "action", :controller => "controller", :xpath => "xpath")
135
+ @router.routes << @accepting_route
136
+ end
137
+
138
+ it "should call execute_route" do
139
+ @router.should_receive(:execute_route).with(@accepting_route.controller, @accepting_route.action, @xml)
140
+ @router.route(@xml)
141
+ end
142
+ end
80
143
 
81
- describe "if no route matches the stanza" do
82
- it "should return false" do
83
- @router.route(@xml).should be_false
144
+ context "if no route matches the stanza" do
145
+ it "should return false" do
146
+ @router.route(@xml).should be_false
147
+ end
84
148
  end
85
149
  end
86
-
87
150
  end
88
151
 
89
152
  describe "execute_route" do
@@ -8,9 +8,9 @@ describe Skates::Runner do
8
8
 
9
9
  describe ".prepare" do
10
10
  before(:each) do
11
- @stub_config_file = File.open("config/config.yaml")
12
- @stub_config_content = File.read("config/config.yaml")
13
- File.stub!(:open).with('config/config.yaml').and_return(@stub_config_file)
11
+ @config = {"production"=>{"port"=>5278, "auto-reconnect"=>true, "jid"=>"component.server.com", "host"=>"localhost", "password"=>"password"}, "development"=>{"auto-reconnect"=>true, "jid"=>"user@server.com", "application_type"=>"client", "password"=>"password"}, "test"=>{"port"=>5278, "auto-reconnect"=>true, "jid"=>"component.server.com", "host"=>"localhost", "password"=>"password"}}
12
+ Skates.config_file = "config/config.yaml"
13
+ YAML.stub(:load_file).and_return(@config)
14
14
  Skates::Runner.stub!(:require_directory).and_return(true)
15
15
  end
16
16
 
@@ -47,13 +47,13 @@ describe Skates::Runner do
47
47
  end
48
48
 
49
49
  it "should load the configuration file" do
50
- File.should_receive(:open).with('config/config.yaml').and_return(@stub_config_file)
50
+ YAML.should_receive(:load_file).with('config/config.yaml').and_return(@config)
51
51
  Skates::Runner.prepare("test")
52
52
  end
53
53
 
54
54
  it "should assign the configuration" do
55
55
  Skates::Runner.prepare("test")
56
- Skates.config.should == YAML.load(@stub_config_content)["test"]
56
+ Skates.config.should == {"port"=>5278, "jid"=>"component.server.com", "auto-reconnect"=>true, "host"=>"localhost", "password"=>"password"}
57
57
  end
58
58
 
59
59
  it "should cache the views" do
@@ -8,6 +8,7 @@ require "rubygems"
8
8
  require "skates"
9
9
  require File.dirname(__FILE__) + "/dependencies"
10
10
 
11
+ Skates.config_file = 'config/config.yaml'
11
12
 
12
13
  # Start the App
13
14
  Skates::Runner::run(SKATES_ENV || "development") do
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 1
9
+ version: 0.3.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - julien Genestoux
@@ -9,59 +14,73 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-10 00:00:00 -06:00
17
+ date: 2010-04-05 00:00:00 +02:00
13
18
  default_executable: skates
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: eventmachine
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 12
30
+ - 10
23
31
  version: 0.12.10
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: log4r
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
33
43
  version: "0"
34
- version:
44
+ type: :runtime
45
+ version_requirements: *id002
35
46
  - !ruby/object:Gem::Dependency
36
47
  name: nokogiri
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
40
50
  requirements:
41
51
  - - ">="
42
52
  - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 4
56
+ - 1
43
57
  version: 1.4.1
44
- version:
58
+ type: :runtime
59
+ version_requirements: *id003
45
60
  - !ruby/object:Gem::Dependency
46
61
  name: templater
47
- type: :runtime
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
50
64
  requirements:
51
65
  - - ">="
52
66
  - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
53
69
  version: "0"
54
- version:
70
+ type: :runtime
71
+ version_requirements: *id004
55
72
  - !ruby/object:Gem::Dependency
56
73
  name: utf8cleaner
57
- type: :runtime
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Requirement
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
60
76
  requirements:
61
77
  - - ">="
62
78
  - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
63
81
  version: "0"
64
- version:
82
+ type: :runtime
83
+ version_requirements: *id005
65
84
  description:
66
85
  email: julien.genestoux@gmail.com
67
86
  executables:
@@ -114,14 +133,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
133
  requirements:
115
134
  - - ">="
116
135
  - !ruby/object:Gem::Version
136
+ segments:
137
+ - 0
117
138
  version: "0"
118
- version:
119
139
  required_rubygems_version: !ruby/object:Gem::Requirement
120
140
  requirements:
121
141
  - - ">="
122
142
  - !ruby/object:Gem::Version
143
+ segments:
144
+ - 0
123
145
  version: "0"
124
- version:
125
146
  requirements:
126
147
  - eventmachine
127
148
  - yaml
@@ -135,7 +156,7 @@ requirements:
135
156
  - resolv
136
157
  - utf8cleaner
137
158
  rubyforge_project: skates
138
- rubygems_version: 1.3.5
159
+ rubygems_version: 1.3.6
139
160
  signing_key:
140
161
  specification_version: 3
141
162
  summary: Skates is a framework to create EventMachine based XMPP External Components in Ruby.