Skip to content

Commit

Permalink
fixed test bus implementation, improved pubnub publish error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edraut committed Oct 21, 2014
1 parent 70743a0 commit f9ce085
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
18 changes: 11 additions & 7 deletions app/assets/javascripts/foreign_office.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ var ForeignOffice = Class.extend({
this.channels_by_name = [];
},
config: function(config){
third_party_busname = this.busMap(config.bus_name)
if (typeof(eval(third_party_busname)) != 'undefined') {
bus_class = eval(config.bus_name);
} else {
bus_class = eval(config.bus_name);
third_party_library = this.library_name_for(config.bus_name)
try {
bus_class = eval(third_party_library)
}
catch(err){
bus_class = TestBus
}
this.bus = new bus_class(config);
Expand All @@ -33,9 +35,9 @@ var ForeignOffice = Class.extend({
channelNames: function(){
return $.map(this.channels,function(channel){return channel.channel_name});
},
busMap: function(bus_name) {
var map = { PubnubBus: 'PUBNUB',
PusherBus: 'Pusher'}[bus_name]
library_name_for: function(bus_name){
var library_map = {PubnubBus: 'PUBNUB', PusherBus: 'Pusher'}
return library_map[bus_name]
}
});

Expand All @@ -44,6 +46,8 @@ foreign_office = new ForeignOffice();
var ForeignOfficeChannel = Class.extend({
init: function(channel_name){
var foreign_office_channel = this;
debug_logger.log("Subscribing to: ");
debug_logger.log(channel_name);
foreign_office.bus.subscribe({
channel : channel_name,
callback : function(m){
Expand Down
4 changes: 3 additions & 1 deletion app/assets/javascripts/pubnub_bus.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
var PubnubBus = Class.extend({
init: function(config){
debug_logger.log("initializing pubnub js")
this.pubnub = PUBNUB.init({
publish_key : config.publish_key,
subscribe_key : config.subscribe_key,
ssl : config.ssl
});
},
subscribe: function(subscription){
debug_logger.log("subscribing with PubnubBus")
debug_logger.log(subscription)
this.pubnub.subscribe({
channel : subscription.channel,
backfill: true,
message : function(m){subscription.callback(m)}
});

Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/pusher_bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ var PusherBus = Class.extend({
this.channel.bind('publish', function(data){
subscription.callback(data);
})
}
},
third_party_library: 'Pusher'
})
21 changes: 10 additions & 11 deletions lib/foreign_office/busses/pubnub_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ def self.publish(message)
self.connection.publish(
channel: message[:channel],
message: message,
http_sync: true,
callback: lambda{|response|
if 0 == JSON.parse(response.instance_variable_get(:@response)).first #pubnub publishing failed
if attempts <= 3
self.publish!(message, attempts) #retry if we failed
else
Rails.logger.debug("ForeignOffice#publish! failed after #{attempts} attempts. message: #{message.inspect}")
end
end
}
)
http_sync: true
) do |envelope|
if '200' != envelope.status_code.to_s
Rails.logger.error "ForeignOffice error esponse:"
Rails.logger.error envelope.message
Rails.logger.error envelope.channel
Rails.logger.error envelope.status_code
Rails.logger.error envelope.timetoken
end
end
end

end
2 changes: 1 addition & 1 deletion lib/foreign_office/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ForeignOffice
VERSION = "0.1.1"
VERSION = "0.1.2"
end

0 comments on commit f9ce085

Please sign in to comment.