softlayer_messaging 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/softlayer/messaging/auth.rb +83 -0
- data/lib/softlayer/messaging/client.rb +122 -0
- data/lib/softlayer/messaging/message.rb +29 -0
- data/lib/softlayer/messaging/queue.rb +95 -0
- data/lib/softlayer/messaging/subscription.rb +30 -0
- data/lib/softlayer/messaging/topic.rb +83 -0
- data/lib/softlayer/messaging.rb +28 -0
- data/resources/config.json +7 -0
- metadata +69 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
require "rest_client"
|
3
|
+
|
4
|
+
module SoftLayer::Messaging
|
5
|
+
class Auth
|
6
|
+
attr_accessor :auth_token
|
7
|
+
def initialize(auth_endpoint, username, api_key, options={})
|
8
|
+
@username = username
|
9
|
+
@api_key = api_key
|
10
|
+
@auth_token = options[:auth_token]
|
11
|
+
@endpoint = auth_endpoint
|
12
|
+
end
|
13
|
+
|
14
|
+
def auth_token
|
15
|
+
@auth_token
|
16
|
+
end
|
17
|
+
|
18
|
+
def authorize
|
19
|
+
headers = {
|
20
|
+
:accept => :json,
|
21
|
+
'X-Auth-User' => @username,
|
22
|
+
'X-Auth-Key' => @api_key,
|
23
|
+
}
|
24
|
+
|
25
|
+
resource = RestClient::Resource.new(@endpoint, :headers => headers)
|
26
|
+
result = resource.post nil
|
27
|
+
@auth_token = result.headers[:x_auth_token]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class AuthedResource < RestClient::Resource
|
32
|
+
def auth
|
33
|
+
@options[:auth]
|
34
|
+
end
|
35
|
+
|
36
|
+
def auth=(value)
|
37
|
+
@options[:auth] = value
|
38
|
+
end
|
39
|
+
|
40
|
+
def authed_action(&block)
|
41
|
+
return block.call if auth.nil?
|
42
|
+
auth.authorize if auth.auth_token.nil?
|
43
|
+
|
44
|
+
headers['x-auth-token'] = auth.auth_token
|
45
|
+
|
46
|
+
begin
|
47
|
+
return block.call
|
48
|
+
rescue RestClient::Unauthorized
|
49
|
+
auth.authorize
|
50
|
+
headers['x-auth-token'] = auth.auth_token
|
51
|
+
return block.call
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def get(*args)
|
56
|
+
authed_action{ super(*args) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def head(*args)
|
60
|
+
authed_action{ super(*args) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def post(*args)
|
64
|
+
authed_action{ super(*args) }
|
65
|
+
end
|
66
|
+
|
67
|
+
def put(*args)
|
68
|
+
authed_action{ super(*args) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def patch(*args)
|
72
|
+
authed_action{ super(*args) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def delete(*args)
|
76
|
+
authed_action{ super(*args) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_s
|
80
|
+
"AuthedResource<#{@url}>"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module SoftLayer::Messaging
|
5
|
+
class Client
|
6
|
+
attr_accessor :auth, :resource
|
7
|
+
def initialize(account, options={})
|
8
|
+
@account = account
|
9
|
+
options[:datacenter] ||= :dal05
|
10
|
+
options[:network] ||= :public
|
11
|
+
if not options[:endpoint]
|
12
|
+
options[:endpoint] = "https://#{SoftLayer.endpoints[options[:datacenter]][options[:network]]}"
|
13
|
+
end
|
14
|
+
@base_resource = RestClient::Resource.new(options[:endpoint])
|
15
|
+
@endpoint = "#{options[:endpoint]}/v1/#{account}"
|
16
|
+
options[:endpoint] = @endpoint
|
17
|
+
|
18
|
+
@resource = AuthedResource.new(options[:endpoint], {:headers => {:accept => :json}})
|
19
|
+
end
|
20
|
+
|
21
|
+
def authenticate(username, api_key, options={})
|
22
|
+
auth_endpoint = "#{@endpoint}/auth"
|
23
|
+
@resource.auth = Auth.new(auth_endpoint, username, api_key, options)
|
24
|
+
@resource.auth.authorize
|
25
|
+
end
|
26
|
+
|
27
|
+
def url
|
28
|
+
@resource.url
|
29
|
+
end
|
30
|
+
|
31
|
+
def ping
|
32
|
+
begin
|
33
|
+
@base_resource['v1/ping'].get
|
34
|
+
true
|
35
|
+
rescue
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def list_queues(options={})
|
41
|
+
tags = options[:tags]
|
42
|
+
params = {}
|
43
|
+
params[:tags] = tags.join(',').to_s if tags
|
44
|
+
queues = []
|
45
|
+
result = JSON.parse(resource['queues'].get({:params => params}), :symbolize_names => true)[:items]
|
46
|
+
result.each do |q|
|
47
|
+
queues << Queue.new(@resource, q)
|
48
|
+
end
|
49
|
+
queues
|
50
|
+
end
|
51
|
+
alias :queues :list_queues
|
52
|
+
|
53
|
+
def list_topics(options={})
|
54
|
+
tags = options[:tags]
|
55
|
+
params = {}
|
56
|
+
params[:tags] = tags.join(',').to_s if tags
|
57
|
+
topics = []
|
58
|
+
result = JSON.parse(resource['topics'].get({:params => params}), :symbolize_names => true)[:items]
|
59
|
+
result.each do |q|
|
60
|
+
topics << Topic.new(@resource, q)
|
61
|
+
end
|
62
|
+
topics
|
63
|
+
end
|
64
|
+
alias :topics :list_topics
|
65
|
+
|
66
|
+
|
67
|
+
def queue(name, options={})
|
68
|
+
options[:name] = name
|
69
|
+
return Queue.new(@resource, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
def topic(name, options={})
|
73
|
+
options[:name] = name
|
74
|
+
return Topic.new(@resource, options)
|
75
|
+
end
|
76
|
+
|
77
|
+
def get_queue(name)
|
78
|
+
return Queue.new(@resource, :name => name).tap{ |q|
|
79
|
+
q.load_data
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_topic(name)
|
84
|
+
return Topic.new(@resource, :name => name).tap{ |q|
|
85
|
+
q.load_data
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
def delete_queue(name, force = false)
|
90
|
+
force = force ? 1 : 0
|
91
|
+
JSON.parse(resource["queues/#{name}?force=#{force}"].delete(), :symbolize_names => true)
|
92
|
+
true
|
93
|
+
end
|
94
|
+
|
95
|
+
def delete_topic(name, force = false)
|
96
|
+
force = force ? 1 : 0
|
97
|
+
JSON.parse(resource["topics/#{name}?force=#{force}"].delete(), :symbolize_names => true)
|
98
|
+
true
|
99
|
+
end
|
100
|
+
|
101
|
+
def create_queue(name, properties={})
|
102
|
+
data = properties.to_json
|
103
|
+
result = JSON.parse(resource["queues/#{name}"].put(data, :content_type => :json), :symbolize_names => true)
|
104
|
+
return Queue.new(@resource, result)
|
105
|
+
end
|
106
|
+
|
107
|
+
def create_topic(name, properties={})
|
108
|
+
data = properties.to_json
|
109
|
+
result = JSON.parse(resource["topics/#{name}"].put(data, :content_type => :json), :symbolize_names => true)
|
110
|
+
return Topic.new(@resource, result)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Types: day, week, month
|
114
|
+
def stats(type='day')
|
115
|
+
JSON.parse(resource["stats/#{type}"].get, :symbolize_names => true)
|
116
|
+
end
|
117
|
+
|
118
|
+
def to_s
|
119
|
+
"QueueClient<#{@resource.url}>"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
module SoftLayer::Messaging
|
3
|
+
class Message
|
4
|
+
attr_accessor :id,:resource,:data
|
5
|
+
|
6
|
+
def initialize(parent_resource, data)
|
7
|
+
@id = data[:id]
|
8
|
+
@data = data
|
9
|
+
@resource = parent_resource["messages/#{@id}"]
|
10
|
+
end
|
11
|
+
|
12
|
+
def body
|
13
|
+
data[:body]
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete
|
17
|
+
JSON.parse(@resource.delete, :symbolize_names => true)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
if data[:body].length > 10
|
22
|
+
"Message<#{@id}, \"#{data[:body][0..9]}...\">"
|
23
|
+
else
|
24
|
+
"Message<#{@id}, \"#{data[:body]}\">"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
module SoftLayer::Messaging
|
3
|
+
class Queue
|
4
|
+
attr_accessor :name, :resource
|
5
|
+
|
6
|
+
def initialize(parent_resource, options)
|
7
|
+
@name = options.delete(:name)
|
8
|
+
options.delete(:message)
|
9
|
+
if options and options.any?
|
10
|
+
@data = options
|
11
|
+
end
|
12
|
+
@resource = parent_resource["queues/#{@name}"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def visibility_interval
|
16
|
+
@data[:visibility_interval]
|
17
|
+
end
|
18
|
+
|
19
|
+
def message_count
|
20
|
+
@data[:message_count]
|
21
|
+
end
|
22
|
+
|
23
|
+
def visible_message_count
|
24
|
+
@data[:visible_message_count]
|
25
|
+
end
|
26
|
+
|
27
|
+
def expiration
|
28
|
+
@data[:expiration]
|
29
|
+
end
|
30
|
+
|
31
|
+
def tags
|
32
|
+
@data[:tags]
|
33
|
+
end
|
34
|
+
|
35
|
+
def message(id, data={})
|
36
|
+
data[:id] = id
|
37
|
+
data.delete(:message)
|
38
|
+
Message.new(@resource, data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def data
|
42
|
+
@data ||= load_data
|
43
|
+
end
|
44
|
+
alias :detail :data
|
45
|
+
|
46
|
+
def load_data
|
47
|
+
response = {}
|
48
|
+
data = JSON.parse(resource.get)
|
49
|
+
data.each do |k,v|
|
50
|
+
response[k.to_sym] = v
|
51
|
+
end
|
52
|
+
return @data = response
|
53
|
+
end
|
54
|
+
|
55
|
+
def modify(properties={})
|
56
|
+
data = properties.to_json
|
57
|
+
result = JSON.parse(resource.put(data, :content_type => :json), :symbolize_names => true)
|
58
|
+
result.delete(:message)
|
59
|
+
@data = result
|
60
|
+
return self
|
61
|
+
end
|
62
|
+
|
63
|
+
def delete(force = false)
|
64
|
+
force = force ? 1 : 0
|
65
|
+
result = resource.delete({:params => {:force => force}})
|
66
|
+
JSON.parse(result, :symbolize_names => true)
|
67
|
+
true
|
68
|
+
end
|
69
|
+
|
70
|
+
def pop(options = {})
|
71
|
+
options[:limit] ||= 1
|
72
|
+
response = resource["messages?batch=#{options[:limit]}"].get()
|
73
|
+
messages = []
|
74
|
+
JSON.parse(response, :symbolize_names => true)[:items].each do |m|
|
75
|
+
messages << message(m[:id], m)
|
76
|
+
end
|
77
|
+
return messages
|
78
|
+
end
|
79
|
+
|
80
|
+
def push(msg, options={})
|
81
|
+
options[:body] = msg
|
82
|
+
body = options.to_json
|
83
|
+
result = JSON.parse(resource["messages"].post(body, :content_type => :json), :symbolize_names => true)
|
84
|
+
message(result[:id], result)
|
85
|
+
end
|
86
|
+
|
87
|
+
def delete_message(mid)
|
88
|
+
JSON.parse(resource["messages/#{mid}"].delete, :symbolize_names => true)
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_s
|
92
|
+
"Queue<#{@name}>"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
module SoftLayer::Messaging
|
3
|
+
class Subscription
|
4
|
+
attr_accessor :id, :resource, :data
|
5
|
+
|
6
|
+
def initialize(parent_resource, data)
|
7
|
+
@id = data[:id]
|
8
|
+
@data = data
|
9
|
+
@resource = parent_resource["subscriptions/#{@id.to_s}"]
|
10
|
+
end
|
11
|
+
|
12
|
+
def endpoint
|
13
|
+
data[:endpoint]
|
14
|
+
end
|
15
|
+
|
16
|
+
def endpoint_type
|
17
|
+
data[:endpoint_type]
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete
|
21
|
+
result = resource.delete
|
22
|
+
JSON.parse(result, :symbolize_names => true)
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"Subscription<#{@id}, #{data[:endpoint_type]}>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
module SoftLayer::Messaging
|
3
|
+
class Topic
|
4
|
+
attr_accessor :name, :resource
|
5
|
+
|
6
|
+
def initialize(parent_resource, options)
|
7
|
+
@name = options.delete(:name)
|
8
|
+
options.delete(:message)
|
9
|
+
if options and options.any?
|
10
|
+
@data = options
|
11
|
+
end
|
12
|
+
@resource = parent_resource["topics/#{@name}"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def tags
|
16
|
+
@data[:tags]
|
17
|
+
end
|
18
|
+
|
19
|
+
def subscription(id, data={})
|
20
|
+
data[:id] = id
|
21
|
+
Subscription.new(@resource, data)
|
22
|
+
end
|
23
|
+
|
24
|
+
def data
|
25
|
+
@data ||= load_data
|
26
|
+
end
|
27
|
+
alias :detail :data
|
28
|
+
|
29
|
+
def load_data
|
30
|
+
response = {}
|
31
|
+
data = JSON.parse(@resource.get, :symbolize_names => true)
|
32
|
+
data.each do |k,v|
|
33
|
+
response[k.to_sym] = v
|
34
|
+
end
|
35
|
+
return @data = response
|
36
|
+
end
|
37
|
+
|
38
|
+
def modify(properties={})
|
39
|
+
data = properties.to_json
|
40
|
+
result = JSON.parse(resource.put(data, :content_type => :json), :symbolize_names => true)
|
41
|
+
result.delete(:message)
|
42
|
+
@data = result
|
43
|
+
return self
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(force = false)
|
47
|
+
force = force ? 1 : 0
|
48
|
+
result = @resource.delete({:params => {:force => force}})
|
49
|
+
JSON.parse(result, :symbolize_names => true)
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def publish(msg, tags = [])
|
54
|
+
message = {:body => msg, :tags => tags}.to_json
|
55
|
+
JSON.parse(@resource["/messages"].post(message, :content_type => :json), :symbolize_names => true)
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def subscriptions
|
60
|
+
results = JSON.parse(@resource["/subscriptions"].get, :symbolize_names => true)
|
61
|
+
subs = []
|
62
|
+
results[:items].each { |result|
|
63
|
+
subs << Subscription.new(@resource, result)
|
64
|
+
}
|
65
|
+
subs
|
66
|
+
end
|
67
|
+
|
68
|
+
def create_subscription(type, properties={})
|
69
|
+
sub = {:endpoint_type => type, :endpoint => properties}
|
70
|
+
results = JSON.parse(@resource["/subscriptions"].post(sub.to_json, :content_type => :json), :symbolize_names => true)
|
71
|
+
Subscription.new @resource, results
|
72
|
+
end
|
73
|
+
|
74
|
+
def delete_subscription(subscription_name)
|
75
|
+
JSON.parse(@resource["/subscriptions/#{subscription_name}"].delete(), :symbolize_names => true)
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
def to_s
|
80
|
+
"Topic<#{@name}>"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# See COPYING for license information.
|
2
|
+
require "json"
|
3
|
+
module SoftLayer
|
4
|
+
class << self
|
5
|
+
def endpoints
|
6
|
+
@endpoints ||= load_endpoint
|
7
|
+
end
|
8
|
+
|
9
|
+
def load_endpoint
|
10
|
+
JSON.parse(
|
11
|
+
File.open(File.join(File.dirname(File.expand_path(__FILE__)), '../../resources/config.json'), "r").read,
|
12
|
+
:symbolize_names => true)[:endpoints]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
module SL
|
19
|
+
include SoftLayer
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'softlayer/messaging/auth'
|
23
|
+
require 'softlayer/messaging/client'
|
24
|
+
require 'softlayer/messaging/queue'
|
25
|
+
require 'softlayer/messaging/message'
|
26
|
+
require 'softlayer/messaging/topic'
|
27
|
+
require 'softlayer/messaging/subscription'
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: softlayer_messaging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kevin McDonald
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Softlayer Messaging Ruby Client
|
31
|
+
email:
|
32
|
+
- kmcdonald@softlayer.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/softlayer/messaging/auth.rb
|
38
|
+
- lib/softlayer/messaging/client.rb
|
39
|
+
- lib/softlayer/messaging/message.rb
|
40
|
+
- lib/softlayer/messaging/queue.rb
|
41
|
+
- lib/softlayer/messaging/subscription.rb
|
42
|
+
- lib/softlayer/messaging/topic.rb
|
43
|
+
- lib/softlayer/messaging.rb
|
44
|
+
- resources/config.json
|
45
|
+
homepage: https://github.com/softlayer/softlayer-message-queue-ruby
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.3.6
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Softlayer Messaging Ruby Client
|
69
|
+
test_files: []
|