julien51-babylon 0.1.2 → 0.1.3

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.
@@ -21,7 +21,9 @@ module Babylon
21
21
  begin
22
22
  self.send(@action_name)
23
23
  rescue
24
- Babylon.logger.error("#{$!}:\n#{$!.backtrace.join("\n")}")
24
+ Babylon.logger.error {
25
+ "#{$!}:\n#{$!.backtrace.join("\n")}"
26
+ }
25
27
  end
26
28
  self.render
27
29
  end
@@ -86,9 +88,13 @@ module Babylon
86
88
  ##
87
89
  # Creates the view and "evaluates" it to build the XML for the stanza
88
90
  def render_for_file(file)
89
- Babylon.logger.info("RENDERING : #{file}")
91
+ Babylon.logger.info {
92
+ "RENDERING : #{file}"
93
+ }
90
94
  @view = Babylon::Base::View.new(file, assigns)
91
- Babylon.logger.info(" ")
95
+ Babylon.logger.info {
96
+ " "
97
+ }
92
98
  end
93
99
 
94
100
  end
@@ -25,7 +25,7 @@ module Babylon
25
25
  raise ViewFileNotFound unless Babylon.views[@view_template]
26
26
  builder = Nokogiri::XML::Builder.new
27
27
  builder.stream do |xml|
28
- eval(Babylon.views[@view_template])
28
+ eval(Babylon.views[@view_template], binding, @view_template, 1)
29
29
  end
30
30
  builder.doc.root.children # we output the document built
31
31
  end
@@ -27,7 +27,9 @@ module Babylon
27
27
  # If ruby version is too old and SRV is unknown, this will raise a NameError
28
28
  # which is caught below
29
29
  host_from_jid = params["jid"].split("/").first.split("@").last
30
- Babylon.logger.debug("RESOLVING: _xmpp-client._tcp.#{host_from_jid} (SRV)")
30
+ Babylon.logger.debug {
31
+ "RESOLVING: _xmpp-client._tcp.#{host_from_jid} (SRV)"
32
+ }
31
33
  srv = dns.getresources("_xmpp-client._tcp.#{host_from_jid}", Resolv::DNS::Resource::IN::SRV)
32
34
  }
33
35
  # Sort SRV records: lowest priority first, highest weight first
@@ -45,7 +47,9 @@ module Babylon
45
47
  end
46
48
  }
47
49
  rescue NameError
48
- Babylon.logger.debug "Resolv::DNS does not support SRV records. Please upgrade to ruby-1.8.3 or later! \n#{$!} : #{$!.backtrace.join("\n")}"
50
+ Babylon.logger.debug {
51
+ "Resolv::DNS does not support SRV records. Please upgrade to ruby-1.8.3 or later! \n#{$!} : #{$!.backtrace.join("\n")}"
52
+ }
49
53
  end
50
54
  end
51
55
 
@@ -181,7 +185,9 @@ module Babylon
181
185
  begin
182
186
  @handler.on_connected(self) if @handler and @handler.respond_to?("on_connected")
183
187
  rescue
184
- Babylon.logger.error("on_connected failed : #{$!}\n#{$!.backtrace.join("\n")}")
188
+ Babylon.logger.error {
189
+ "on_connected failed : #{$!}\n#{$!.backtrace.join("\n")}"
190
+ }
185
191
  end
186
192
  @state = :connected
187
193
  end
@@ -52,7 +52,9 @@ module Babylon
52
52
  begin
53
53
  @handler.on_connected(self) if @handler and @handler.respond_to?("on_connected")
54
54
  rescue
55
- Babylon.logger.error("on_connected failed : #{$!}\n#{$!.backtrace.join("\n")}")
55
+ Babylon.logger.error {
56
+ "on_connected failed : #{$!}\n#{$!.backtrace.join("\n")}"
57
+ }
56
58
  end
57
59
  @state = :connected
58
60
  elsif stanza.name == "stream:error"
@@ -31,7 +31,9 @@ module Babylon
31
31
  # Executes the route for the given xml_stanza, by instantiating the controller_name, calling action_name and sending
32
32
  # the result to the connection
33
33
  def execute_route(controller_name, action_name, xml_stanza = nil)
34
- Babylon.logger.info("ROUTING TO #{controller_name}::#{action_name}")
34
+ Babylon.logger.info {
35
+ "ROUTING TO #{controller_name}::#{action_name}"
36
+ }
35
37
  stanza = nil
36
38
  stanza = Kernel.const_get(action_name.capitalize).new(xml_stanza) if xml_stanza
37
39
  controller = controller_name.new(stanza)
@@ -13,7 +13,14 @@ module Babylon
13
13
 
14
14
  # Add an outputter to the logger
15
15
  log_file = Log4r::RollingFileOutputter.new("#{Babylon.environment}", :filename => "log/#{Babylon.environment}.log", :trunc => false)
16
- log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")
16
+ case Babylon.environment
17
+ when "production"
18
+ log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")
19
+ when "development"
20
+ log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")
21
+ else
22
+ log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")
23
+ end
17
24
  Babylon.logger.add(log_file)
18
25
 
19
26
  # Requiring all models, stanza, controllers
@@ -76,10 +83,14 @@ module Babylon
76
83
  def self.add_connection_observer(observer)
77
84
  @@observers ||= Array.new
78
85
  if observer.ancestors.include? Babylon::Base::Controller
79
- Babylon.logger.debug("Added #{observer} to the list of Connection Observers")
86
+ Babylon.logger.debug {
87
+ "Added #{observer} to the list of Connection Observers"
88
+ }
80
89
  @@observers.push(observer) unless @@observers.include? observer
81
90
  else
82
- Babylon.logger.error("Observer can only be Babylon::Base::Controller")
91
+ Babylon.logger.error {
92
+ "Observer can only be Babylon::Base::Controller"
93
+ }
83
94
  false
84
95
  end
85
96
  end
@@ -111,10 +122,14 @@ module Babylon
111
122
  begin
112
123
  Babylon.router.route(stanza)
113
124
  rescue Babylon::NotConnected
114
- Babylon.logger.fatal("#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}")
125
+ Babylon.logger.fatal {
126
+ "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
127
+ }
115
128
  EventMachine::stop_event_loop
116
129
  rescue
117
- Babylon.logger.error("#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}")
130
+ Babylon.logger.error {
131
+ "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
132
+ }
118
133
  end
119
134
  end
120
135
 
@@ -45,11 +45,15 @@ module Babylon
45
45
  # It passes itself (as handler) and the configuration
46
46
  # This can very well be overwritten by subclasses.
47
47
  def self.connect(params, handler)
48
- Babylon.logger.debug("CONNECTING TO #{params["host"]}:#{params["port"]} with #{handler.inspect} as connection handler") # Very low level Logging
48
+ Babylon.logger.debug {
49
+ "CONNECTING TO #{params["host"]}:#{params["port"]} with #{handler.inspect} as connection handler" # Very low level Logging
50
+ }
49
51
  begin
50
52
  EventMachine.connect(params["host"], params["port"], self, params.merge({"handler" => handler}))
51
53
  rescue RuntimeError
52
- Babylon.logger.error("CONNECTION ERROR : #{$!.class} => #{$!}") # Very low level Logging
54
+ Babylon.logger.error {
55
+ "CONNECTION ERROR : #{$!.class} => #{$!}" # Very low level Logging
56
+ }
53
57
  raise NotConnected
54
58
  end
55
59
  end
@@ -58,18 +62,24 @@ module Babylon
58
62
  # Called when the connection is completed.
59
63
  def connection_completed
60
64
  @connected = true
61
- Babylon.logger.debug("CONNECTED") # Very low level Logging
65
+ Babylon.logger.debug {
66
+ "CONNECTED"
67
+ } # Very low level Logging
62
68
  end
63
69
 
64
70
  ##
65
71
  # Called when the connection is terminated and stops the event loop
66
72
  def unbind()
67
73
  @connected = false
68
- Babylon.logger.debug("DISCONNECTED") # Very low level Logging
74
+ Babylon.logger.debug {
75
+ "DISCONNECTED"
76
+ } # Very low level Logging
69
77
  begin
70
78
  @handler.on_disconnected() if @handler and @handler.respond_to?("on_disconnected")
71
79
  rescue
72
- Babylon.logger.error("on_disconnected failed : #{$!}\n#{$!.backtrace.join("\n")}")
80
+ Babylon.logger.error {
81
+ "on_disconnected failed : #{$!}\n#{$!.backtrace.join("\n")}"
82
+ }
73
83
  end
74
84
  end
75
85
 
@@ -94,12 +104,16 @@ module Babylon
94
104
  ##
95
105
  # Called when a full stanza has been received and returns it to the central router to be sent to the corresponding controller.
96
106
  def receive_stanza(stanza)
97
- Babylon.logger.debug("PARSED : #{stanza.to_xml}")
107
+ Babylon.logger.debug {
108
+ "PARSED : #{stanza.to_xml}"
109
+ }
98
110
  # If not handled by subclass (for authentication)
99
111
  case stanza.name
100
112
  when "stream:error"
101
113
  if !stanza.children.empty? and stanza.children.first.name == "xml-not-well-formed"
102
- Babylon.logger.error("DISCONNECTED DUE TO MALFORMED STANZA")
114
+ Babylon.logger.error {
115
+ "DISCONNECTED DUE TO MALFORMED STANZA"
116
+ }
103
117
  raise XmlNotWellFormed
104
118
  end
105
119
  # In any case, we need to close the connection.
@@ -108,7 +122,9 @@ module Babylon
108
122
  begin
109
123
  @handler.on_stanza(stanza) if @handler and @handler.respond_to?("on_stanza")
110
124
  rescue
111
- Babylon.logger.error("on_stanza failed : #{$!}\n#{$!.backtrace.join("\n")}")
125
+ Babylon.logger.error {
126
+ "on_stanza failed : #{$!}\n#{$!.backtrace.join("\n")}"
127
+ }
112
128
  end
113
129
  end
114
130
  end
@@ -130,8 +146,10 @@ module Babylon
130
146
  def send_chunk(string)
131
147
  raise NotConnected unless @connected
132
148
  return if string.blank?
133
- raise StanzaTooBig if string.length > XmppConnection.max_stanza_size
134
- Babylon.logger.debug("SENDING : " + string)
149
+ raise StanzaTooBig, "Stanza Too Big (#{string.length} vs. #{XmppConnection.max_stanza_size})\n #{string}" if string.length > XmppConnection.max_stanza_size
150
+ Babylon.logger.debug {
151
+ "SENDING : " + string
152
+ }
135
153
  send_data string
136
154
  end
137
155
 
@@ -139,10 +157,14 @@ module Babylon
139
157
  # receive_data is called when data is received. It is then passed to the parser.
140
158
  def receive_data(data)
141
159
  begin
142
- Babylon.logger.debug("RECEIVED : #{data}")
160
+ # Babylon.logger.debug {
161
+ # "RECEIVED : #{data}"
162
+ # }
143
163
  @parser.push(data)
144
164
  rescue
145
- Babylon.logger.error("#{$!}\n#{$!.backtrace.join("\n")}")
165
+ Babylon.logger.error {
166
+ "#{$!}\n#{$!.backtrace.join("\n")}"
167
+ }
146
168
  end
147
169
  end
148
170
  end
@@ -49,7 +49,7 @@ describe Babylon::Base::View do
49
49
 
50
50
  it "should call eval on the view file" do
51
51
  Babylon.views.stub!(:[]).with(@view_template).and_return(@xml_string)
52
- @view.should_receive(:eval).with(@xml_string)
52
+ @view.should_receive(:eval).with(@xml_string, an_instance_of(Binding), @view_template, 1)
53
53
  @view.evaluate
54
54
  end
55
55
 
@@ -62,14 +62,9 @@ describe Babylon::XmppConnection do
62
62
  @connection.post_init
63
63
  @connection.instance_variable_get("@parser").should == parser
64
64
  end
65
-
66
65
  end
67
66
 
68
67
  describe "connection_completed" do
69
- it "should write a log message" do
70
- Babylon.logger.should_receive(:debug).with("CONNECTED")
71
- @connection.connection_completed
72
- end
73
68
  it "should set @connected to true" do
74
69
  @connection.connection_completed
75
70
  @connection.instance_variable_get("@connected").should be_true
@@ -77,11 +72,6 @@ describe Babylon::XmppConnection do
77
72
  end
78
73
 
79
74
  describe "unbind" do
80
- it "should write a log message, and call on_disconnected" do
81
- Babylon.logger.should_receive(:debug).with("DISCONNECTED")
82
- handler_mock.should_receive(:on_disconnected)
83
- @connection.unbind
84
- end
85
75
  it "should set @connected to false" do
86
76
  @connection.connection_completed
87
77
  @connection.unbind
@@ -95,11 +85,6 @@ describe Babylon::XmppConnection do
95
85
  @doc = Nokogiri::XML::Document.new
96
86
  end
97
87
 
98
- it "should write a log message for debug" do
99
- Babylon.logger.should_receive(:debug).with(/PARSED/)
100
- @connection.receive_stanza(Nokogiri::XML::Node.new("node", @doc))
101
- end
102
-
103
88
  describe "with an stanza that starts with stream:error" do
104
89
 
105
90
  before(:each) do
@@ -118,10 +103,6 @@ describe Babylon::XmppConnection do
118
103
  @error_stanza.add_child(@xml_not_well_formed_stanza)
119
104
  end
120
105
 
121
- it "should write an error to the log and raise an error" do
122
- Babylon.logger.should_receive(:error).with(/DISCONNECTED DUE TO MALFORMED STANZA/)
123
- lambda {@connection.receive_stanza(@error_stanza)}.should raise_error(Babylon::XmlNotWellFormed)
124
- end
125
106
  end
126
107
  end
127
108
 
@@ -206,12 +187,6 @@ describe Babylon::XmppConnection do
206
187
  @connection.instance_variable_get("@parser").stub!(:push).and_return(true)
207
188
  end
208
189
 
209
- it "should show a message on the log" do
210
- data = "<hello>hello world!</hello>"
211
- Babylon.logger.should_receive(:debug).with("RECEIVED : #{data}")
212
- @connection.__send__(:receive_data, data)
213
- end
214
-
215
190
  it "should push the received data to the parser" do
216
191
  data = "<hello>hello world!</hello>"
217
192
  @connection.instance_variable_get("@parser").should_receive(:push).with(data).and_return(true)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: julien51-babylon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - julien Genestoux