Class: Cloudfuji::Event

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

Overview

Cloudfuji::Event lists all the events from the Cloudfuji server. All events are hashes with the following keys:

  • category

  • name

  • data

Data will hold the arbitrary data for the type of event signalled

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Event

Returns a new instance of Event.



67
68
69
70
71
# File 'lib/cloudfuji/event.rb', line 67

def initialize(options={})
  @category = options["category"]
  @name     = options["name"]
  @data     = options["data"]
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



9
10
11
# File 'lib/cloudfuji/event.rb', line 9

def category
  @category
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/cloudfuji/event.rb', line 9

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/cloudfuji/event.rb', line 9

def name
  @name
end

Class Method Details

.event_url(event_ido_id) ⇒ Object



16
17
18
# File 'lib/cloudfuji/event.rb', line 16

def event_url(event_ido_id)
  "#{Cloudfuji::Platform.host}/apps/#{Cloudfuji::Platform.name}/events/#{event_ido_id}.json"
end

.events_urlObject



12
13
14
# File 'lib/cloudfuji/event.rb', line 12

def events_url
  "#{Cloudfuji::Platform.host}/apps/#{Cloudfuji::Platform.name}/events.json"
end

.find(event_ido_id) ⇒ Object

Find an event by its ido_id Be careful not to abuse this - an app can be throttled if requesting too many events too quickly, which will cause errors and a bad user experience for the end user



24
25
26
# File 'lib/cloudfuji/event.rb', line 24

def find(event_ido_id)
  Cloudfuji::Command.get_command(event_url(event_ido_id))
end

.publish(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cloudfuji/event.rb', line 28

def publish(options={})
  # Enforce standard format on client side so that any errors
  # can be more quickly caught for the developer
  return StandardError("Cloudfuji::Event format incorrect, please make sure you're using the correct structure for sending events") unless !options[:name].nil? && !options[:category].nil? && !options[:data].nil?

  publish_locally(options) and return if ENV['CLOUDFUJI_LOCAL_APPS'].present?

  payload = {
    :category => options[:category],
    :name     => options[:name],
    :data     => options[:data]
  }

  Cloudfuji::Command.post_command(events_url, payload)
end

.publish_locally(options) ⇒ Object

During development, allow developers to configure a list of local ports & keys to post Cloudfuji events e.g. CLOUDFUJI_LOCAL_APPS=3000:abcdef,3001:abcdef



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudfuji/event.rb', line 47

def publish_locally(options)
  payload = {
    :category    => options[:category],
    :event       => options[:name],      # Cloudfuji client uses :event key
    :data        => options[:data],
    'auth_token' => ''                   # Cloudfuji client uses 'key' instead of 'auth_token'
  }

  ENV['CLOUDFUJI_LOCAL_APPS'].split(',').each do |app|
    port, key = app.split(":")
    unless port && key
      raise "ENV['CLOUDFUJI_LOCAL_APPS'] not formatted correctly, expecting: CLOUDFUJI_LOCAL_APPS=3000:abcdef,3001:abcdef"
    end
    payload[:key] = key
    url = "localhost:#{port.strip}/cloudfuji/data"
    Cloudfuji::Command.post_command(url, payload)
  end
end