Class: Fanout

Inherits:
Object
  • Object
show all
Defined in:
lib/fanout.rb

Overview

The Fanout class is used for publishing messages to Fanout.io and is configured with a Fanout.io realm and associated key. SSL can either be enabled or disabled. As a convenience, the realm and key can also be configured by setting the ‘FANOUT_REALM’ and ‘FANOUT_KEY’ environmental variables. Note that unlike the PubControl class there is no need to call the finish method manually, as it will automatically be called when the calling program exits.

Instance Method Summary collapse

Constructor Details

#initialize(realm = nil, key = nil, ssl = true) ⇒ Fanout

Initialize with a specified realm, key, and a boolean indicating wther SSL should be enabled or disabled. Note that if the realm and key are omitted then the initialize method will use the ‘FANOUT_REALM’ and ‘FANOUT_KEY’ environmental variables.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fanout.rb', line 26

def initialize(realm=nil, key=nil, ssl=true)
  if realm.nil?
    realm = ENV['FANOUT_REALM']
  end
  if key.nil?
    key = ENV['FANOUT_KEY']
  end
  if ssl
    scheme = 'https'
  else
    scheme = 'http'
  end
  uri = '%s://api.fanout.io/realm/%s' % [scheme, realm]
  @pub = PubControl.new({'uri' => uri, 'iss' => realm,
      'key' => Base64.decode64(key)})
end

Instance Method Details

#publish(channel, data, id = nil, prev_id = nil) ⇒ Object

Synchronously publish the specified data to the specified channel for the configured Fanout.io realm. Optionally provide an ID and previous ID to send along with the message.



46
47
48
# File 'lib/fanout.rb', line 46

def publish(channel, data, id=nil, prev_id=nil)
  @pub.publish(channel, Item.new(JsonObjectFormat.new(data), id, prev_id))
end

#publish_async(channel, data, id = nil, prev_id = nil, callback = nil) ⇒ Object

Asynchronously publish the specified data to the specified channel for the configured Fanout.io realm. Optionally provide an ID and previous ID to send along with the message, as well a callback method that will be called after publishing is complete and passed the result and error message if an error was encountered.



55
56
57
58
# File 'lib/fanout.rb', line 55

def publish_async(channel, data, id=nil, prev_id=nil, callback=nil)
  @pub.publish_async(channel, Item.new(JsonObjectFormat.new(data), id,
      prev_id), callback)
end