Class: Spire::API::Application
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
included
Constructor Details
#initialize(api, data) ⇒ Application
Returns a new instance of Application.
12
13
14
15
|
# File 'lib/spire/api/application.rb', line 12
def initialize(api, 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
#resources ⇒ Object
Returns the value of attribute resources.
6
7
8
|
# File 'lib/spire/api/application.rb', line 6
def resources
@resources
end
|
Instance Method Details
#authenticate(login, password) ⇒ Object
Authenticates with the application using basic auth
204
205
206
207
208
209
210
|
# File 'lib/spire/api/application.rb', line 204
def authenticate(login, password)
response = request(:authenticate, {:login => login, :password => password})
unless response.status == 200
raise "Error authenticating for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@api, response.data)
end
|
#authenticate_with_post(login, password) ⇒ Object
Alternative application authentication, without using basic auth
213
214
215
216
217
218
219
|
# File 'lib/spire/api/application.rb', line 213
def authenticate_with_post(login, password)
response = request(:authenticate_with_post, {:login => login, :password => password})
unless response.status == 201
raise "Error authenticating for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@api, response.data)
end
|
#channels ⇒ Object
318
319
320
|
# File 'lib/spire/api/application.rb', line 318
def channels
@channels ||= channels!
end
|
#channels! ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/spire/api/application.rb', line 305
def channels!
response = request(:channels)
unless response.status == 200
raise "Error retrieving Channels: (#{response.status}) #{response.body}"
end
channels_data = response.data
@channels = {}
channels_data.each do |name, properties|
@channels[name] = API::Channel.new(@api, properties)
end
@channels
end
|
#create_channel(name, options = {}) ⇒ Object
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/spire/api/application.rb', line 280
def create_channel(name, options={})
message_limit = options[:message_limit]
message_ttl = options[:message_ttl]
response = request(:create_channel, name, message_limit, message_ttl)
unless response.status == 201
raise "Error creating Channel: (#{response.status}) #{response.body}"
end
properties = response.data
channels[name] = API::Channel.new(@api, properties)
end
|
#create_member(member_data) ⇒ Object
233
234
235
236
237
238
239
|
# File 'lib/spire/api/application.rb', line 233
def create_member(member_data)
response = request(:create_member, member_data)
unless response.status == 201
raise "Error creating member for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@api, response.data)
end
|
#create_subscription(subscription_name, channel_names) ⇒ Object
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/spire/api/application.rb', line 291
def create_subscription(subscription_name, channel_names)
channel_urls = channel_names.flatten.map { |name| self.channels[name].url rescue nil }.compact
response = request(:create_subscription, subscription_name, channel_urls)
unless response.status == 201
raise "Error creating Subscription: (#{response.status}) #{response.body}"
end
data = response.data
subscription = API::Subscription.new(@api, data)
if subscription_name
subscriptions[data["name"]] = subscription
end
subscription
end
|
#get_channel(name) ⇒ Object
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/spire/api/application.rb', line 338
def get_channel(name)
response = request(:channel_by_name, name)
unless response.status == 200
raise "Error finding channel with name #{name}: (#{response.status}) #{response.body}"
end
properties = response.data[name]
channel = API::Channel.new(@api, properties)
@channels[name] = channel if @channels.is_a?(Hash)
channel
end
|
#get_member(member_login) ⇒ Object
269
270
271
272
273
274
275
276
277
278
|
# File 'lib/spire/api/application.rb', line 269
def get_member(member_login)
response = request(:member_by_login, member_login)
unless response.status == 200
raise "Error finding member with login #{member_login}: (#{response.status}) #{response.body}"
end
properties = response.data[member_login]
member = API::Member.new(@api, properties)
@members[member_login] = member if @members.is_a?(Hash)
member
end
|
#get_subscription(name) ⇒ Object
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/spire/api/application.rb', line 349
def get_subscription(name)
response = request(:subscription_by_name, name)
unless response.status == 200
raise "Error finding subscription with name #{name}: (#{response.status}) #{response.body}"
end
properties = response.data[name]
sub = API::Subscription.new(@api, properties)
@subscriptions[name] = sub if @subscriptions.is_a?(Hash)
sub
end
|
#members(options = {}) ⇒ Object
250
251
252
253
|
# File 'lib/spire/api/application.rb', line 250
def members(options = {})
@members ||= {}
@members[options.hash] || members!(options)
end
|
#members!(options = {}) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/spire/api/application.rb', line 255
def members!(options = {})
response = request(:members, options)
unless response.status == 200
raise "Error getting members for application #{self.name}: (#{response.status}) #{response.body}"
end
@members ||= {}
hsh_key = options.hash
@members[hsh_key] ||= {}
response.data.each do |login, properties|
@members[hsh_key][login] = API::Member.new(@api, properties)
end
@members[hsh_key]
end
|
#request_member_password_reset(email) ⇒ Object
Resets a members password based on email
242
243
244
245
246
247
248
|
# File 'lib/spire/api/application.rb', line 242
def request_member_password_reset(email)
response = request(:reset_member_password, email)
unless response.status == 202
raise "Error reseting password for email #{email} in app #{self.name}: (#{response.status}) #{response.body}"
end
true
end
|
#reset_password(reset_key, new_password = nil) ⇒ Object
If you do not give a new password, you will get back an authenticated member but have to change the password at a later time (using the returned capability)
223
224
225
226
227
228
229
230
231
|
# File 'lib/spire/api/application.rb', line 223
def reset_password(reset_key, new_password = nil)
hsh = {:reset_key => reset_key}
hsh[:password] = new_password if new_password
response = request(:authenticate_with_post, hsh)
unless response.status == 201
raise "Error reseting password for application #{self.name}: (#{response.status}) #{response.body}"
end
API::Member.new(@api, response.data)
end
|
#resource_name ⇒ Object
8
9
10
|
# File 'lib/spire/api/application.rb', line 8
def resource_name
"application"
end
|
#subscriptions ⇒ Object
322
323
324
|
# File 'lib/spire/api/application.rb', line 322
def subscriptions
@subscriptions ||= subscriptions!
end
|
#subscriptions! ⇒ Object
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/spire/api/application.rb', line 326
def subscriptions!
response = request(:subscriptions)
unless response.status == 200
raise "Error retrieving Subscriptions: (#{response.status}) #{response.body}"
end
@subscriptions = {}
response.data.each do |name, properties|
@subscriptions[name] = API::Subscription.new(@api, properties)
end
@subscriptions
end
|