Class: SoftLayer::Messaging::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/messaging/topic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_resource, options) ⇒ Topic

Returns a new instance of Topic.



6
7
8
9
10
11
12
13
# File 'lib/softlayer/messaging/topic.rb', line 6

def initialize(parent_resource, options)
  @name = options.delete(:name)
  options.delete(:message)
  if options and options.any?
    @data = options
  end
  @resource = parent_resource["topics/#{@name}"]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/softlayer/messaging/topic.rb', line 4

def name
  @name
end

#resourceObject

Returns the value of attribute resource.



4
5
6
# File 'lib/softlayer/messaging/topic.rb', line 4

def resource
  @resource
end

Instance Method Details

#create_subscription(type, properties = {}) ⇒ Object



68
69
70
71
72
# File 'lib/softlayer/messaging/topic.rb', line 68

def create_subscription(type, properties={})
  sub = {:endpoint_type => type, :endpoint => properties}
  results = JSON.parse(@resource["/subscriptions"].post(sub.to_json, :content_type => :json), :symbolize_names => true)
  Subscription.new @resource, results
end

#dataObject Also known as: detail



24
25
26
# File 'lib/softlayer/messaging/topic.rb', line 24

def data
  @data ||= load_data
end

#delete(force = false) ⇒ Object



46
47
48
49
50
51
# File 'lib/softlayer/messaging/topic.rb', line 46

def delete(force = false)
  force = force ? 1 : 0
  result = @resource.delete({:params => {:force => force}})
  JSON.parse(result, :symbolize_names => true)
  true
end

#delete_subscription(subscription_name) ⇒ Object



74
75
76
77
# File 'lib/softlayer/messaging/topic.rb', line 74

def delete_subscription(subscription_name)
  JSON.parse(@resource["/subscriptions/#{subscription_name}"].delete(), :symbolize_names => true)
  true
end

#load_dataObject



29
30
31
32
33
34
35
36
# File 'lib/softlayer/messaging/topic.rb', line 29

def load_data
  response = {}
  data = JSON.parse(@resource.get, :symbolize_names => true)
  data.each do |k,v|
    response[k.to_sym] = v
  end
  return @data = response
end

#modify(properties = {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/softlayer/messaging/topic.rb', line 38

def modify(properties={})
  data = properties.to_json
  result = JSON.parse(resource.put(data, :content_type => :json), :symbolize_names => true)
  result.delete(:message)
  @data = result
  return self
end

#publish(msg, tags = []) ⇒ Object



53
54
55
56
57
# File 'lib/softlayer/messaging/topic.rb', line 53

def publish(msg, tags = [])
  message = {:body => msg, :tags => tags}.to_json
  JSON.parse(@resource["/messages"].post(message, :content_type => :json), :symbolize_names => true)
  true
end

#subscription(id, data = {}) ⇒ Object



19
20
21
22
# File 'lib/softlayer/messaging/topic.rb', line 19

def subscription(id, data={})
  data[:id] = id
  Subscription.new(@resource, data)
end

#subscriptionsObject



59
60
61
62
63
64
65
66
# File 'lib/softlayer/messaging/topic.rb', line 59

def subscriptions
  results = JSON.parse(@resource["/subscriptions"].get, :symbolize_names => true)
  subs = []
  results[:items].each { |result|
    subs << Subscription.new(@resource, result)
  }
  subs
end

#tagsObject



15
16
17
# File 'lib/softlayer/messaging/topic.rb', line 15

def tags
  @data[:tags]
end

#to_sObject



79
80
81
# File 'lib/softlayer/messaging/topic.rb', line 79

def to_s
  "Topic<#{@name}>"
end