blather 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,4 +15,5 @@ notes
15
15
  .yardoc
16
16
  *.rbc
17
17
  vendor
18
- .rvmrc
18
+ .rvmrc
19
+ .rbx
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ v0.5.10
2
+ Feature(juandebravo): Allow configuring the wire log level
3
+ Bugfix(benlangfeld): Checking connection status before the stream is established
4
+
1
5
  v0.5.9
2
6
  Bugfix(benlangfeld): Failed connections now raise a Blather::Stream::ConnectionFailed exception
3
7
  Bugfix(crohr): Blather now supports EventMachine 1.0
data/lib/blather.rb CHANGED
@@ -77,22 +77,31 @@
77
77
  blather/stream/features/tls
78
78
  ].each { |r| require r }
79
79
 
80
- # The core Blather namespace
81
80
  module Blather
82
- # @private
83
81
  @@logger = nil
84
82
 
85
- # Get or create an instance of Logger
86
- def self.logger
87
- unless @@logger
88
- self.logger = Logger.new($stdout)
89
- self.logger.level = Logger::INFO
83
+ class << self
84
+
85
+ # Default logger level. Any internal call to log() will forward the log message to
86
+ # the default log level
87
+ attr_accessor :default_log_level
88
+
89
+ def logger
90
+ @@logger ||= Logger.new($stdout).tap {|logger| logger.level = Logger::INFO }
91
+ end
92
+
93
+ def logger=(logger)
94
+ @@logger = logger
95
+ end
96
+
97
+ def default_log_level
98
+ @default_log_level ||= :debug # by default is debug (as it used to be)
99
+ end
100
+
101
+ def log(message)
102
+ logger.send self.default_log_level, message
90
103
  end
91
- @@logger
92
- end
93
104
 
94
- # Set the Logger
95
- def self.logger=(logger)
96
- @@logger = logger
97
105
  end
106
+
98
107
  end
@@ -64,7 +64,7 @@ module Blather
64
64
 
65
65
  # Check whether the client is currently connected.
66
66
  def connected?
67
- setup? && !@stream.stopped?
67
+ setup? && !@stream.nil? && !@stream.stopped?
68
68
  end
69
69
 
70
70
  # Get the current status. Taken from the `state` attribute of Status
@@ -123,7 +123,7 @@ module Blather
123
123
  # @param [#to_xml, #to_s] stanza the stanza to send over the wire
124
124
  def send(stanza)
125
125
  data = stanza.respond_to?(:to_xml) ? stanza.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML) : stanza.to_s
126
- Blather.logger.debug "SENDING: (#{caller[1]}) #{data}"
126
+ Blather.log "SENDING: (#{caller[1]}) #{data}"
127
127
  send_data data
128
128
  end
129
129
 
@@ -152,8 +152,8 @@ module Blather
152
152
  # Called by EM with data from the wire
153
153
  # @private
154
154
  def receive_data(data)
155
- Blather.logger.debug "\n#{'-'*30}\n"
156
- Blather.logger.debug "STREAM IN: #{data}"
155
+ Blather.log "\n#{'-'*30}\n"
156
+ Blather.log "STREAM IN: #{data}"
157
157
  @parser << data
158
158
 
159
159
  rescue ParseError => e
@@ -170,7 +170,7 @@ module Blather
170
170
  # EM is supposed to close the connection when this returns false,
171
171
  # but it only does that for inbound connections, not when we
172
172
  # make a connection to another server.
173
- Blather.logger.debug("Checking SSL cert: #{pem}")
173
+ Blather.log "Checking SSL cert: #{pem}"
174
174
  return true if !@@store
175
175
  @@store.trusted?(pem).tap do |trusted|
176
176
  close_connection unless trusted
@@ -198,7 +198,7 @@ module Blather
198
198
  # Called by the parser with parsed nodes
199
199
  # @private
200
200
  def receive(node)
201
- Blather.logger.debug "RECEIVING (#{node.element_name}) #{node}"
201
+ Blather.log "RECEIVING (#{node.element_name}) #{node}"
202
202
  @node = node
203
203
 
204
204
  if @node.namespace && @node.namespace.prefix == 'stream'
@@ -227,7 +227,7 @@ module Blather
227
227
  # Ensure the JID gets attached to the client
228
228
  # @private
229
229
  def jid=(new_jid)
230
- Blather.logger.debug "NEW JID: #{new_jid}"
230
+ Blather.log "NEW JID: #{new_jid}"
231
231
  @jid = JID.new new_jid
232
232
  end
233
233
 
@@ -49,7 +49,7 @@ class Stream
49
49
  return
50
50
  end
51
51
 
52
- Blather.logger.debug "RESOURCE NODE #{@node}"
52
+ Blather.log "RESOURCE NODE #{@node}"
53
53
  # ensure this is a response to our original request
54
54
  if @id == @node['id']
55
55
  @stream.jid = JID.new @node.find_first('bind_ns:bind/bind_ns:jid', :bind_ns => BIND_NS).content
@@ -120,7 +120,7 @@ class Stream
120
120
  key, value = statement.split('=')
121
121
  res[key] = value.delete('"') unless key.empty?
122
122
  end
123
- Blather.logger.debug "CHALLENGE DECODE: #{res.inspect}"
123
+ Blather.log "CHALLENGE DECODE: #{res.inspect}"
124
124
 
125
125
  @nonce ||= res['nonce']
126
126
  @realm ||= res['realm']
@@ -155,8 +155,8 @@ class Stream
155
155
  @response[:response] = generate_response
156
156
  @response.each { |k,v| @response[k] = "\"#{v}\"" unless [:nc, :qop, :response, :charset].include?(k) }
157
157
 
158
- Blather.logger.debug "CHALLENGE RESPONSE: #{@response.inspect}"
159
- Blather.logger.debug "CH RESP TXT: #{@response.map { |k,v| "#{k}=#{v}" } * ','}"
158
+ Blather.log "CHALLENGE RESPONSE: #{@response.inspect}"
159
+ Blather.log "CH RESP TXT: #{@response.map { |k,v| "#{k}=#{v}" } * ','}"
160
160
 
161
161
  # order is to simplify testing
162
162
  # Ruby 1.9 eliminates the need for this with ordered hashes
@@ -19,14 +19,14 @@ class Stream
19
19
  end
20
20
 
21
21
  def receive_data(string)
22
- Blather.logger.debug "PARSING: (#{string})" if @@debug
22
+ Blather.log "PARSING: (#{string})" if @@debug
23
23
  @parser << string
24
24
  self
25
25
  end
26
26
  alias_method :<<, :receive_data
27
27
 
28
28
  def start_element_namespace(elem, attrs, prefix, uri, namespaces)
29
- Blather.logger.debug "START ELEM: (#{{:elem => elem, :attrs => attrs, :prefix => prefix, :uri => uri, :ns => namespaces}.inspect})" if @@debug
29
+ Blather.log "START ELEM: (#{{:elem => elem, :attrs => attrs, :prefix => prefix, :uri => uri, :ns => namespaces}.inspect})" if @@debug
30
30
 
31
31
  args = [elem]
32
32
  args << @current.document if @current
@@ -64,7 +64,7 @@ class Stream
64
64
  end
65
65
 
66
66
  def end_element_namespace(elem, prefix, uri)
67
- Blather.logger.debug "END ELEM: #{{:elem => elem, :prefix => prefix, :uri => uri}.inspect}" if @@debug
67
+ Blather.log "END ELEM: #{{:elem => elem, :prefix => prefix, :uri => uri}.inspect}" if @@debug
68
68
 
69
69
  if elem == 'stream'
70
70
  node = XMPPNode.new('end')
@@ -79,12 +79,12 @@ class Stream
79
79
  end
80
80
 
81
81
  def characters(chars = '')
82
- Blather.logger.debug "CHARS: #{chars}" if @@debug
82
+ Blather.log "CHARS: #{chars}" if @@debug
83
83
  @current << Nokogiri::XML::Text.new(chars, @current.document) if @current
84
84
  end
85
85
 
86
86
  def warning(msg)
87
- Blather.logger.debug "PARSE WARNING: #{msg}" if @@debug
87
+ Blather.log "PARSE WARNING: #{msg}" if @@debug
88
88
  end
89
89
 
90
90
  def error(msg)
@@ -1,4 +1,4 @@
1
1
  module Blather
2
2
  # Blather version number
3
- VERSION = '0.5.9'
3
+ VERSION = '0.5.10'
4
4
  end
@@ -4,13 +4,13 @@ require 'blather/client/client'
4
4
  describe Blather::Client do
5
5
  before do
6
6
  @client = Blather::Client.new
7
- @stream = mock()
7
+ @stream = mock
8
8
  @stream.stubs(:send)
9
9
  @jid = Blather::JID.new('n@d/r')
10
- @client.post_init @stream, @jid
11
10
  end
12
11
 
13
12
  it 'provides a Blather::JID reader' do
13
+ @client.post_init @stream, @jid
14
14
  @client.must_respond_to :jid
15
15
  @client.jid.must_equal @jid
16
16
  end
@@ -21,6 +21,7 @@ describe Blather::Client do
21
21
  end
22
22
 
23
23
  it 'provides a status reader' do
24
+ @client.post_init @stream, @jid
24
25
  @client.must_respond_to :status
25
26
  @client.status = :away
26
27
  @client.status.must_equal :away
@@ -67,13 +68,20 @@ describe Blather::Client do
67
68
  end
68
69
 
69
70
  it 'knows if it is connected' do
70
- stream = mock()
71
+ stream = mock
71
72
  stream.expects(:stopped?).returns false
72
73
  @client.setup('me.com', 'secret')
73
74
  @client.post_init stream, Blather::JID.new('me.com')
74
75
  @client.connected?.must_equal true
75
76
  end
76
77
 
78
+ describe 'if it has been setup but not connected yet' do
79
+ it 'should consider itself disconnected' do
80
+ @client.setup('me.com', 'secret')
81
+ @client.connected?.must_equal false
82
+ end
83
+ end
84
+
77
85
  it 'writes to the connection the closes when #close is called' do
78
86
  stream = mock()
79
87
  stream.expects(:close_connection_after_writing)
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blather do
4
+
5
+ describe "while accessing to Logger object" do
6
+ it "should return a Logger instance" do
7
+ Blather.logger.must_be_instance_of Logger
8
+ end
9
+
10
+ it "should config log level to info by default" do
11
+ Blather.logger.level.must_equal 1
12
+ end
13
+ end
14
+
15
+ describe "while using the log method" do
16
+ after do
17
+ Blather.default_log_level = :debug
18
+ end
19
+
20
+ it "should forward to debug by default" do
21
+ Blather.logger.expects(:debug).with("foo bar").once
22
+ Blather.log "foo bar"
23
+ end
24
+
25
+ %w<debug info error fatal>.each do |val|
26
+ it "should forward to #{val} if configured that default level" do
27
+ Blather.logger.expects(val.to_sym).with("foo bar").once
28
+ Blather.default_log_level = val.to_sym
29
+ Blather.log "foo bar"
30
+ end
31
+ end
32
+
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blather
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.5.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-24 00:00:00.000000000 Z
12
+ date: 2011-12-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &2151820120 !ruby/object:Gem::Requirement
16
+ requirement: &2152201300 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.12.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2151820120
24
+ version_requirements: *2152201300
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &2151817400 !ruby/object:Gem::Requirement
27
+ requirement: &2152228020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.4.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2151817400
35
+ version_requirements: *2152228020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: niceogiri
38
- requirement: &2151816340 !ruby/object:Gem::Requirement
38
+ requirement: &2152227220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.1.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2151816340
46
+ version_requirements: *2152227220
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: activesupport
49
- requirement: &2151814180 !ruby/object:Gem::Requirement
49
+ requirement: &2152226520 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 3.0.7
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2151814180
57
+ version_requirements: *2152226520
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: minitest
60
- requirement: &2151826240 !ruby/object:Gem::Requirement
60
+ requirement: &2152225740 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.7.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2151826240
68
+ version_requirements: *2152225740
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
- requirement: &2151823960 !ruby/object:Gem::Requirement
71
+ requirement: &2152224800 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.9.12
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2151823960
79
+ version_requirements: *2152224800
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &2151822020 !ruby/object:Gem::Requirement
82
+ requirement: &2152224280 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *2151822020
90
+ version_requirements: *2152224280
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rcov
93
- requirement: &2151832740 !ruby/object:Gem::Requirement
93
+ requirement: &2152223400 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 0.9.9
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *2151832740
101
+ version_requirements: *2152223400
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: yard
104
- requirement: &2151843420 !ruby/object:Gem::Requirement
104
+ requirement: &2152222880 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 0.6.1
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *2151843420
112
+ version_requirements: *2152222880
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rake
115
- requirement: &2151848800 !ruby/object:Gem::Requirement
115
+ requirement: &2152222460 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *2151848800
123
+ version_requirements: *2152222460
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: guard-minitest
126
- requirement: &2151861200 !ruby/object:Gem::Requirement
126
+ requirement: &2152221960 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *2151861200
134
+ version_requirements: *2152221960
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: bluecloth
137
- requirement: &2151857080 !ruby/object:Gem::Requirement
137
+ requirement: &2152221420 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *2151857080
145
+ version_requirements: *2152221420
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: growl_notify
148
- requirement: &2151869860 !ruby/object:Gem::Requirement
148
+ requirement: &2152220860 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *2151869860
156
+ version_requirements: *2152220860
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: rb-fsevent
159
- requirement: &2151865360 !ruby/object:Gem::Requirement
159
+ requirement: &2152282580 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,7 +164,7 @@ dependencies:
164
164
  version: '0'
165
165
  type: :development
166
166
  prerelease: false
167
- version_requirements: *2151865360
167
+ version_requirements: *2152282580
168
168
  description: An XMPP DSL for Ruby written on top of EventMachine and Nokogiri
169
169
  email: sprsquish@gmail.com
170
170
  executables: []
@@ -307,6 +307,7 @@ files:
307
307
  - spec/blather/stream/parser_spec.rb
308
308
  - spec/blather/stream/ssl_spec.rb
309
309
  - spec/blather/xmpp_node_spec.rb
310
+ - spec/blather_spec.rb
310
311
  - spec/fixtures/pubsub.rb
311
312
  - spec/spec_helper.rb
312
313
  - yard/templates/default/class/html/handlers.erb
@@ -386,6 +387,6 @@ test_files:
386
387
  - spec/blather/stream/parser_spec.rb
387
388
  - spec/blather/stream/ssl_spec.rb
388
389
  - spec/blather/xmpp_node_spec.rb
390
+ - spec/blather_spec.rb
389
391
  - spec/fixtures/pubsub.rb
390
392
  - spec/spec_helper.rb
391
- has_rdoc: