Class: Spire::API::Channel

Inherits:
Resource show all
Defined in:
lib/spire/api/channel.rb

Overview

Object representing a Spire channel

You can get a channel object by calling [] on a Spire object

  • spire = Spire.new

  • spire.start(“your api secret”)

  • channel = spire[“channel name”]

Instance Attribute Summary collapse

Attributes inherited from Resource

#capabilities, #properties, #url

Instance Method Summary collapse

Methods inherited from Resource

#[], #delete, #get, #key, #media_type, #method_missing, #respond_to?, #schema, #update

Methods included from Requestable

included

Constructor Details

#initialize(spire, data) ⇒ Channel

Returns a new instance of Channel.



14
15
16
17
# File 'lib/spire/api/channel.rb', line 14

def initialize(spire, data)
  super
  @resources = data["resources"]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spire::API::Resource

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



12
13
14
# File 'lib/spire/api/channel.rb', line 12

def resources
  @resources
end

Instance Method Details

#publish(content) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/spire/api/channel.rb', line 82

def publish(content)
  body = {:content => content}.to_json
  response = request(:publish, body)
  unless response.status == 201
    raise "Error publishing to #{self.class.name}: (#{response.status}) #{response.body}"
  end
  API::Message.new(@api, response.data)
end

#resource_nameObject



19
20
21
# File 'lib/spire/api/channel.rb', line 19

def resource_name
  "channel"
end

#subscribe(name = nil) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/spire/api/channel.rb', line 91

def subscribe(name = nil)
  response = request(:subscribe, name)
  unless response.status == 201
    raise "Error creating subscription for #{self.name}: (#{response.status}) #{response.body}"
  end
  API::Subscription.new(@api, response.data)
end

#subscriptionsObject



66
67
68
# File 'lib/spire/api/channel.rb', line 66

def subscriptions
  @subscriptions ||= subscriptions!
end

#subscriptions!Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spire/api/channel.rb', line 70

def subscriptions!
  response = request(:subscriptions)
  unless response.status == 200
    raise "Error getting subscriptions to #{self.class.name}: (#{response.status}) #{response.body}"
  end
  @subscriptions = {}
  response.data.each do |name, properties|
    @subscriptions[name] = API::Subscription.new(@api, properties)
  end
  @subscriptions
end