Module: Particle::Client::Publish

Included in:
Particle::Client
Defined in:
lib/particle/client/publish.rb

Overview

Client methods for the Particle publish API

Constant Summary collapse

PUBLISH_PATH =
"v1/devices/events"

Instance Method Summary collapse

Instance Method Details

#publish(options) ⇒ boolean

Publish an event to your devices

Parameters:

  • options (Hash)
    • :name [String]: name of the event to publish

    • :data [String,Hash]: optional data to include with the event

    • :ttl [Number]: optional Time-To-Live in seconds for the event (currently ignored by the cloud)

    • :private [boolean]: optional key to indicate event should be published only to your own devices

Returns:

  • (boolean)

    true for success



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/particle/client/publish.rb', line 20

def publish(options)
  params = {
    name: options.fetch(:name)
  }
  case options[:data]
  when Hash, Array then params[:data] = options[:data].to_json
  else params[:data] = options[:data].to_s
  end

  params[:ttl] = options[:ttl] if options[:ttl]
  params[:private] = true if options[:private]

  result = post(PUBLISH_PATH, params)
  result[:ok]
end