schlep 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -26,7 +26,7 @@ To change this, you can configure things one at a time:
26
26
 
27
27
  ```rb
28
28
  Schlep.app = "My App"
29
- Schlep.hostname = "localhost"
29
+ Schlep.host = "localhost"
30
30
  Schlep.redis_url = "redis://redis:password@localhost:6379"
31
31
  ```
32
32
 
@@ -35,7 +35,7 @@ or in a block:
35
35
  ```rb
36
36
  Schlep.configure do |config|
37
37
  config.app = "My App"
38
- config.hostname = "localhost"
38
+ config.host = "localhost"
39
39
  config.redis_url = "redis://redis:password@localhost:6379"
40
40
  end
41
41
  ```
data/lib/schlep.rb CHANGED
@@ -21,38 +21,40 @@ module Schlep
21
21
  yield self
22
22
  end
23
23
 
24
- def envelope(type, message)
24
+ def envelope(type, message, options = {})
25
25
  {
26
26
  :timestamp => timestamp,
27
- :app => app,
28
- :host => hostname,
27
+ :app => options[:app] || app,
28
+ :host => options[:host] || host,
29
29
  :type => type,
30
30
  :message => serialize_message(message)
31
- }.to_json
31
+ }
32
32
  end
33
33
 
34
- def event(type, message)
35
- events type, [message]
34
+ def event(type, message, options = {})
35
+ events type, [message], options
36
36
  end
37
37
 
38
- def events(type, messages)
39
- messages.map! { |message| envelope type, message }
38
+ def events(type, messages, options = {})
39
+ options.keys.each { |k| options[k] = sanitize(options[k]) }
40
+
41
+ messages.map! { |message| envelope(type, message, options).to_json }
40
42
 
41
43
  suppress_redis_errors do
42
44
  redis.pipelined do
43
45
  while messages.any?
44
- redis.rpush key, messages.pop
46
+ redis.rpush key, messages.shift
45
47
  end
46
48
  end
47
49
  end
48
50
  end
49
51
 
50
- def hostname
51
- @hostname ||= sanitize `hostname`
52
+ def host
53
+ @host ||= sanitize `hostname`
52
54
  end
53
55
 
54
- def hostname=(string)
55
- @hostname = sanitize string
56
+ def host=(string)
57
+ @host = sanitize string
56
58
  end
57
59
 
58
60
  def key
@@ -80,7 +82,7 @@ module Schlep
80
82
  end
81
83
 
82
84
  def reset
83
- %w[app hostname redis redis_url].each do |ivar|
85
+ %w[app host redis redis_url].each do |ivar|
84
86
  instance_variable_set "@#{ivar}", nil
85
87
  end
86
88
  end
@@ -1,3 +1,3 @@
1
1
  module Schlep
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -18,30 +18,30 @@ describe Schlep do
18
18
  context ".configure" do
19
19
  it "should be configurable with setters" do
20
20
  Schlep.app = "test_app_1"
21
- Schlep.hostname = "test_hostname_1"
21
+ Schlep.host = "test_host_1"
22
22
  Schlep.redis_url = "redis://localhost:1234"
23
23
 
24
24
  Schlep.app.should == "test_app_1"
25
- Schlep.hostname.should == "test_hostname_1"
25
+ Schlep.host.should == "test_host_1"
26
26
  Schlep.redis_url.should == "redis://localhost:1234"
27
27
  end
28
28
 
29
29
  it "should be configurable with a block" do
30
30
  Schlep.configure do |config|
31
31
  config.app = "test_app_2"
32
- config.hostname = "test_hostname_2"
32
+ config.host = "test_host_2"
33
33
  config.redis_url = "redis://localhost:4321"
34
34
  end
35
35
 
36
36
  Schlep.app.should == "test_app_2"
37
- Schlep.hostname.should == "test_hostname_2"
37
+ Schlep.host.should == "test_host_2"
38
38
  Schlep.redis_url.should == "redis://localhost:4321"
39
39
  end
40
40
  end
41
41
 
42
42
  context ".envelope" do
43
- it "should return valid json" do
44
- JSON.parse(Schlep.envelope "test_type", { :one => { :two => 3 }})
43
+ it "should return a hash" do
44
+ Schlep.envelope("test", "test").should be_instance_of Hash
45
45
  end
46
46
 
47
47
  it "should accept types as strings" do
@@ -51,21 +51,40 @@ describe Schlep do
51
51
  it "should accept types as symbols" do
52
52
  Schlep.envelope :test, "test"
53
53
  end
54
+
55
+ it "should allow the app to be overridden" do
56
+ Schlep.app = "test_app"
57
+ e = Schlep.envelope("test", "test", :app => "another_app")
58
+ e[:app].should == "another_app"
59
+ Schlep.app.should == "test_app"
60
+ end
61
+
62
+ it "should allow the host to be overridden" do
63
+ Schlep.host = "test_host"
64
+ e = Schlep.envelope("test", "test", :host => "another_host")
65
+ e[:host].should == "another_host"
66
+ Schlep.host.should == "test_host"
67
+ end
68
+
69
+ it "should not allow the timestamp to be overridden" do
70
+ e = Schlep.envelope("test", "test", :timestamp => 1234567890)
71
+ e[:timestamp].should_not == 1234567890
72
+ end
54
73
  end
55
74
 
56
- context ".hostname" do
75
+ context ".host" do
57
76
  it "should be a string" do
58
- Schlep.hostname.should be_instance_of String
77
+ Schlep.host.should be_instance_of String
59
78
  end
60
79
 
61
80
  it "should not include a newline from the hostname command" do
62
- Schlep.hostname.should_not match /\s/
81
+ Schlep.host.should_not match /\s/
63
82
  end
64
83
 
65
84
  it "should not remove dashes, underscores, or periods" do
66
- Schlep.hostname = "this-is_a.hostname"
85
+ Schlep.host = "this-is_a.host"
67
86
 
68
- Schlep.hostname.should == "this-is_a.hostname"
87
+ Schlep.host.should == "this-is_a.host"
69
88
  end
70
89
  end
71
90
 
@@ -112,12 +131,12 @@ describe Schlep do
112
131
  it "should reset instance variables to nil" do
113
132
  Schlep.configure do |config|
114
133
  config.app = "test_app"
115
- config.hostname = "test_hostname"
134
+ config.host = "test_host"
116
135
  end
117
136
 
118
137
  Schlep.reset
119
138
 
120
- %w[app hostname redis].each do |ivar|
139
+ %w[app host redis].each do |ivar|
121
140
  Schlep.instance_variable_get("@#{ivar}").should be_nil
122
141
  end
123
142
  end
data/spec/redis_spec.rb CHANGED
@@ -24,6 +24,16 @@ describe Schlep do
24
24
  Schlep.event :test, "test"
25
25
 
26
26
  Schlep.redis.llen(Schlep.key).should == 1
27
+ envelope = JSON.parse(Schlep.redis.lpop(Schlep.key))
28
+ envelope['type'].should == "test"
29
+ envelope['message'].should == "test"
30
+ end
31
+
32
+ it "should sanitize overridden options" do
33
+ Schlep.event "test", "test", :app => "a:b/c^d$$e", :host => "a:b/c^d$$e"
34
+ envelope = JSON.parse(Schlep.redis.lpop(Schlep.key))
35
+ envelope['app'].should == "a:b:c:d:e"
36
+ envelope['host'].should == "a:b:c:d:e"
27
37
  end
28
38
 
29
39
  it "should suppress connection errors" do
@@ -38,6 +48,19 @@ describe Schlep do
38
48
  Schlep.events :test, [1,2,3]
39
49
 
40
50
  Schlep.redis.llen(Schlep.key).should == 3
51
+
52
+ (1..3).each do |n|
53
+ envelope = JSON.parse(Schlep.redis.lpop(Schlep.key))
54
+ envelope['type'].should == "test"
55
+ envelope['message'].should == n
56
+ end
57
+ end
58
+
59
+ it "should sanitize overridden options" do
60
+ Schlep.events "test", ["test"], :app => "a:b/c^d$$e", :host => "a:b/c^d$$e"
61
+ envelope = JSON.parse(Schlep.redis.lpop(Schlep.key))
62
+ envelope['app'].should == "a:b:c:d:e"
63
+ envelope['host'].should == "a:b:c:d:e"
41
64
  end
42
65
 
43
66
  it "should suppress connection errors" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schlep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
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: 2012-01-11 00:00:00.000000000 Z
12
+ date: 2012-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &70158924470240 !ruby/object:Gem::Requirement
16
+ requirement: &70191267022160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70158924470240
24
+ version_requirements: *70191267022160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard-rspec
27
- requirement: &70158924469800 !ruby/object:Gem::Requirement
27
+ requirement: &70191267021720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70158924469800
35
+ version_requirements: *70191267021720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70158924469380 !ruby/object:Gem::Requirement
38
+ requirement: &70191267021300 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70158924469380
46
+ version_requirements: *70191267021300
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70158924468960 !ruby/object:Gem::Requirement
49
+ requirement: &70191267020820 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70158924468960
57
+ version_requirements: *70191267020820
58
58
  description: Ruby client for schlep. Schlep provides a simple interface for logging
59
59
  and broadcasting events.
60
60
  email: