resque-bus 0.2.7 → 0.2.8

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.
data/lib/resque-bus.rb CHANGED
@@ -117,11 +117,26 @@ module ResqueBus
117
117
  @original_redis
118
118
  end
119
119
 
120
+ def with_global_attributes(attributes)
121
+ original_timezone = false
122
+ original_locale = false
123
+
124
+ I18n.locale = attributes["bus_locale"] if defined?(I18n) && I18n.respond_to?(:locale=)
125
+ Time.zone = attributes["bus_timezone"] if defined?(Time) && Time.respond_to?(:zone=)
126
+
127
+ yield
128
+ ensure
129
+ I18n.locale = original_locale unless original_locale == false
130
+ Time.zone = original_timezone unless original_timezone == false
131
+ end
132
+
120
133
  def publish_metadata(event_type, attributes={})
121
134
  # TODO: "bus_app_key" => application.app_key ?
122
135
  bus_attr = {"bus_published_at" => Time.now.to_i, "created_at" => Time.now.to_i, "bus_event_type" => event_type}
123
- bus_attr["bus_id"] ||= "#{Time.now.to_i}-#{generate_uuid}"
136
+ bus_attr["bus_id"] = "#{Time.now.to_i}-#{generate_uuid}"
124
137
  bus_attr["bus_app_hostname"] = hostname
138
+ bus_attr["bus_locale"] = I18n.locale if defined?(I18n) && I18n.respond_to?(:locale)
139
+ bus_attr["bus_timezone"] = Time.zone.name if defined?(Time) && Time.respond_to?(:zone)
125
140
  bus_attr.merge(attributes || {})
126
141
  end
127
142
 
@@ -38,10 +38,13 @@ module ResqueBus
38
38
  def transform(method_name)
39
39
  @transform = method_name
40
40
  end
41
+
41
42
  def perform(attributes)
42
- sub_key = attributes["bus_rider_sub_key"]
43
- meth_key = sub_key.split(".").last
44
- resque_bus_execute(meth_key, attributes)
43
+ ResqueBus.with_global_attributes(attributes) do
44
+ sub_key = attributes["bus_rider_sub_key"]
45
+ meth_key = sub_key.split(".").last
46
+ resque_bus_execute(meth_key, attributes)
47
+ end
45
48
  end
46
49
 
47
50
  def resque_bus_execute(key, attributes)
@@ -33,7 +33,9 @@ module ResqueBus
33
33
 
34
34
  def execute!(attributes)
35
35
  attributes = attributes.with_indifferent_access if attributes.respond_to?(:with_indifferent_access)
36
- executor.call(attributes)
36
+ ResqueBus.with_global_attributes(attributes) do
37
+ executor.call(attributes)
38
+ end
37
39
  end
38
40
 
39
41
  def matches?(attributes)
@@ -1,5 +1,5 @@
1
1
  module Resque
2
2
  module Bus
3
- VERSION = "0.2.7"
3
+ VERSION = "0.2.8"
4
4
  end
5
5
  end
data/spec/publish_spec.rb CHANGED
@@ -44,5 +44,32 @@ describe "Publishing an event" do
44
44
  hash["class"].should == "ResqueBus::Driver"
45
45
  hash["args"].should == [ {"bus_event_type" => event_name, "two"=>"here", "one"=>1}.merge(bus_attrs).merge("bus_id" => 'app-given') ]
46
46
  end
47
+
48
+
49
+
50
+ it "should set the timezone and locale if available" do
51
+ defined?(I18n).should be_nil
52
+ Time.respond_to?(:zone).should be_false
53
+
54
+ stub_const("I18n", Class.new)
55
+ I18n.should_receive(:locale).and_return("jp")
56
+
57
+ Time.should_receive(:zone).and_return(double('zone', :name => "EST"))
58
+
59
+ hash = {:one => 1, "two" => "here", "bus_id" => "app-given" }
60
+ event_name = "event_name"
61
+
62
+ val = ResqueBus.redis.lpop("queue:resquebus_incoming")
63
+ val.should == nil
64
+
65
+ ResqueBus.publish(event_name, hash)
66
+
67
+ val = ResqueBus.redis.lpop("queue:resquebus_incoming")
68
+ hash = JSON.parse(val)
69
+ hash["class"].should == "ResqueBus::Driver"
70
+ att = hash["args"].first
71
+ att["bus_locale"].should == "jp"
72
+ att["bus_timezone"].should == "EST"
73
+ end
47
74
 
48
75
  end
data/spec/rider_spec.rb CHANGED
@@ -14,11 +14,21 @@ module ResqueBus
14
14
  end
15
15
  end
16
16
  Runner1.value.should == 0
17
- Rider.perform("bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
17
+ Rider.perform("bus_locale" => "en", "bus_timezone" => "PST", "bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
18
18
  Rider.perform("bus_rider_app_key" => "other", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
19
19
  Runner1.value.should == 1
20
20
  end
21
21
 
22
+ it "should set the timezone and locale if present" do
23
+ defined?(I18n).should be_nil
24
+ Time.respond_to?(:zone).should be_false
25
+
26
+ stub_const("I18n", Class.new)
27
+ I18n.should_receive(:locale=).with("en")
28
+ Time.should_receive(:zone=).with("PST")
29
+
30
+ Rider.perform("bus_locale" => "en", "bus_timezone" => "PST", "bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
31
+ end
22
32
 
23
33
  context "Integration Test" do
24
34
  before(:each) do
@@ -76,8 +86,6 @@ module ResqueBus
76
86
  Resque.info[:failed].should == 2
77
87
  Resque.info[:pending].should == 0
78
88
  end
79
- end
80
-
81
-
89
+ end
82
90
  end
83
91
  end
@@ -229,5 +229,24 @@ module ResqueBus
229
229
 
230
230
  ResqueBus.redis.lpop("queue:myqueue").should be_nil
231
231
  end
232
+
233
+ describe ".perform" do
234
+ let(:attributes) { {"bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_locale" => "en", "bus_timezone" => "PST"} }
235
+ it "should call the method based on key" do
236
+ SubscriberTest1.any_instance.should_receive(:event_sub)
237
+ SubscriberTest1.perform(attributes)
238
+ end
239
+ it "should set the timezone and locale if present" do
240
+ defined?(I18n).should be_nil
241
+ Time.respond_to?(:zone).should be_false
242
+
243
+ stub_const("I18n", Class.new)
244
+ I18n.should_receive(:locale=).with("en")
245
+ Time.should_receive(:zone=).with("PST")
246
+
247
+ SubscriberTest1.any_instance.should_receive(:event_sub)
248
+ SubscriberTest1.perform(attributes)
249
+ end
250
+ end
232
251
  end
233
252
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
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: 2013-08-22 00:00:00.000000000 Z
12
+ date: 2013-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: resque