Class: SoftLayer::Messaging::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/softlayer/messaging/client.rb', line 7

def initialize(, options={})
  @account = 
  options[:datacenter] ||= 'dal05'
  options[:network] ||= 'public'
  if not options[:endpoint]
    options[:endpoint] = "https://#{SoftLayer.endpoints[options[:datacenter].to_s][options[:network].to_s]}"
  end
  @base_resource = RestClient::Resource.new(options[:endpoint])
  @endpoint = "#{options[:endpoint]}/v1/#{}"
  options[:endpoint] = @endpoint
  
  @resource = AuthedResource.new(options[:endpoint], {:headers => {:accept => :json}})
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



6
7
8
# File 'lib/softlayer/messaging/client.rb', line 6

def auth
  @auth
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/softlayer/messaging/client.rb', line 6

def resource
  @resource
end

Instance Method Details

#authenticate(username, api_key, options = {}) ⇒ Object



21
22
23
24
25
# File 'lib/softlayer/messaging/client.rb', line 21

def authenticate(username, api_key, options={})
  auth_endpoint = "#{@endpoint}/auth"
  @resource.auth = Auth.new(auth_endpoint, username, api_key, options)
  @resource.auth.authorize
end

#create_queue(name, properties = {}) ⇒ Object



101
102
103
104
105
# File 'lib/softlayer/messaging/client.rb', line 101

def create_queue(name, properties={})
  data = properties.to_json
  result = JSON.parse(resource["queues/#{name}"].put(data, :content_type => :json), :symbolize_names => true)
  return Queue.new(@resource, result)
end

#create_topic(name, properties = {}) ⇒ Object



107
108
109
110
111
# File 'lib/softlayer/messaging/client.rb', line 107

def create_topic(name, properties={})
  data = properties.to_json
  result = JSON.parse(resource["topics/#{name}"].put(data, :content_type => :json), :symbolize_names => true)
  return Topic.new(@resource, result)
end

#delete_queue(name, force = false) ⇒ Object



89
90
91
92
93
# File 'lib/softlayer/messaging/client.rb', line 89

def delete_queue(name, force = false)
  force = force ? 1 : 0
  JSON.parse(resource["queues/#{name}?force=#{force}"].delete(), :symbolize_names => true)
  true
end

#delete_topic(name, force = false) ⇒ Object



95
96
97
98
99
# File 'lib/softlayer/messaging/client.rb', line 95

def delete_topic(name, force = false)
  force = force ? 1 : 0
  JSON.parse(resource["topics/#{name}?force=#{force}"].delete(), :symbolize_names => true)
  true
end

#get_queue(name) ⇒ Object



77
78
79
80
81
# File 'lib/softlayer/messaging/client.rb', line 77

def get_queue(name)
  return Queue.new(@resource, :name => name).tap{ |q|
    q.load_data
  }
end

#get_topic(name) ⇒ Object



83
84
85
86
87
# File 'lib/softlayer/messaging/client.rb', line 83

def get_topic(name)
  return Topic.new(@resource, :name => name).tap{ |q|
    q.load_data
  }
end

#list_queues(options = {}) ⇒ Object Also known as: queues



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/softlayer/messaging/client.rb', line 40

def list_queues(options={})
  tags = options[:tags]
  params = {}
  params[:tags] = tags.join(',').to_s if tags
  queues = []
  result = JSON.parse(resource['queues'].get({:params => params}),  :symbolize_names => true)[:items]
  result.each do |q|
    queues << Queue.new(@resource, q)
  end
  queues
end

#list_topics(options = {}) ⇒ Object Also known as: topics



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/softlayer/messaging/client.rb', line 53

def list_topics(options={})
  tags = options[:tags]
  params = {}
  params[:tags] = tags.join(',').to_s if tags
  topics = []
  result = JSON.parse(resource['topics'].get({:params => params}), :symbolize_names => true)[:items]
  result.each do |q|
    topics << Topic.new(@resource, q)
  end
  topics
end

#pingObject



31
32
33
34
35
36
37
38
# File 'lib/softlayer/messaging/client.rb', line 31

def ping
  begin
    @base_resource['v1/ping'].get
    true
  rescue
    false
  end
end

#queue(name, options = {}) ⇒ Object



67
68
69
70
# File 'lib/softlayer/messaging/client.rb', line 67

def queue(name, options={})
  options[:name] = name
  return Queue.new(@resource, options)
end

#stats(type = 'day') ⇒ Object

Types: day, week, month



114
115
116
# File 'lib/softlayer/messaging/client.rb', line 114

def stats(type='day')
  JSON.parse(resource["stats/#{type}"].get, :symbolize_names => true)
end

#to_sObject



118
119
120
# File 'lib/softlayer/messaging/client.rb', line 118

def to_s
  "QueueClient<#{@resource.url}>"
end

#topic(name, options = {}) ⇒ Object



72
73
74
75
# File 'lib/softlayer/messaging/client.rb', line 72

def topic(name, options={})
  options[:name] = name
  return Topic.new(@resource, options)
end

#urlObject



27
28
29
# File 'lib/softlayer/messaging/client.rb', line 27

def url
  @resource.url
end