Module: RAWS::SQS::Adapter::Adapter20090201

Included in:
RAWS::SQS::Adapter
Defined in:
lib/raws/sqs/adapter.rb

Constant Summary collapse

URI =
'https://queue.amazonaws.com/'
PARAMS =
{'Version' => '2009-02-01'}

Instance Method Summary collapse

Instance Method Details

#add_permission(queue_url, label, permissions) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/raws/sqs/adapter.rb', line 190

def add_permission(queue_url, label, permissions)
  params = {
    'Action' => 'AddPermission',
    'Label'  => label
  }
  params.merge!(pack_permission(permissions))

  connect('GET', queue_url, PARAMS.merge(params))
end

#change_message_visibility(queue_url, receipt_handle, visibility_timeout) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/raws/sqs/adapter.rb', line 171

def change_message_visibility(queue_url, receipt_handle, visibility_timeout)
  params = {
    'Action'            => 'ChangeMessageVisibility',
    'ReceiptHandle'     => receipt_handle,
    'VisibilityTimeout' => visibility_timeout
  }

  connect('GET', queue_url, PARAMS.merge(params))
end

#connect(method, base_uri, params, parser = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/raws/sqs/adapter.rb', line 84

def connect(method, base_uri, params, parser={})
  doc = nil

  RAWS::SQS.http.connect(
    "#{base_uri}?#{sign(method, base_uri, params)}"
  ) do |request|
    request.method = method
    response = request.send
    doc = response.parse(parser)
    response
  end

  doc
end

#create_queue(queue_name, default_visibility_timeout = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/raws/sqs/adapter.rb', line 99

def create_queue(queue_name, default_visibility_timeout=nil)
  params = {
    'Action'    => 'CreateQueue',
    'QueueName' => queue_name
  }

  if default_visibility_timeout
    params['DefaultVisibilityTimeout'] = default_visibility_timeout
  end

  connect('GET', URI, PARAMS.merge(params))
end

#delete_message(queue_url, receipt_handle) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/raws/sqs/adapter.rb', line 181

def delete_message(queue_url, receipt_handle)
  params = {
    'Action'        => 'DeleteMessage',
    'ReceiptHandle' => receipt_handle
  }

  connect('GET', queue_url, PARAMS.merge(params))
end

#delete_queue(queue_url) ⇒ Object



112
113
114
115
116
# File 'lib/raws/sqs/adapter.rb', line 112

def delete_queue(queue_url)
  params = {'Action' => 'DeleteQueue'}

  connect('GET', queue_url, PARAMS.merge(params))
end

#get_queue_attributes(queue_url, *attrs) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/raws/sqs/adapter.rb', line 125

def get_queue_attributes(queue_url, *attrs)
  params = {'Action' => 'GetQueueAttributes'}
  if attrs.empty?
    params.merge!(pack_attrs(['All']))
  else
    params.merge!(pack_attrs(attrs))
  end

  connect(
    'GET',
    queue_url,
    PARAMS.merge(params),
    :multiple => %w'Attribute',
    :unpack   => %w'Attribute'
  )
end

#list_queues(prefix = nil) ⇒ Object



118
119
120
121
122
123
# File 'lib/raws/sqs/adapter.rb', line 118

def list_queues(prefix=nil)
  params = {'Action' => 'ListQueues'}
  params['QueueNamePrefix'] = prefix if prefix

  connect('GET', URI, PARAMS.merge(params), 'QueueUrl')
end

#pack_attrs(attrs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/raws/sqs/adapter.rb', line 6

def pack_attrs(attrs)
  params = {}

  if(attrs.size == 1)
    params["AttributeName"] = attrs.first
  else
    i = 1
    attrs.each do |val|
      params["AttributeName.#{i}"] = val
      i += 1
    end
  end

  params
end

#pack_nv_attrs(attrs, replaces = nil, prefix = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/raws/sqs/adapter.rb', line 22

def pack_nv_attrs(attrs, replaces=nil, prefix=nil)
  params = {}

  i = 1
  attrs.each do |key, val|
    if !replaces.nil? && replaces.include?(key)
      params["#{prefix}Attribute.#{i}.Replace"] = 'true'
    end

    if val.is_a? Array
      val.each do |v|
        params["#{prefix}Attribute.#{i}.Name"]  = key
        params["#{prefix}Attribute.#{i}.Value"] = v
        i += 1
      end
    else
      params["#{prefix}Attribute.#{i}.Name"]  = key
      params["#{prefix}Attribute.#{i}.Value"] = val
      i += 1
    end
  end

  params
end

#pack_permission(params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/raws/sqs/adapter.rb', line 47

def pack_permission(params)
  ret = {}

  i = 1
  params.each do |id, permissions|
    permissions.each do |permission|
      ret["AWSAccountId.#{i}"] = id
      ret["ActionName.#{i}"]   = permission
      i += 1
    end
  end

  ret
end

#receive_message(queue_url, params = {}, *attrs) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/raws/sqs/adapter.rb', line 158

def receive_message(queue_url, params={}, *attrs)
  params.merge! 'Action' => 'ReceiveMessage'
  params.merge! pack_attrs(attrs)

  connect(
    'GET',
    queue_url,
    PARAMS.merge(params),
    :multiple => %w'Message Attribute',
    :unpack   => %w'Attribute'
  )
end

#remove_permission(queue_url, label) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/raws/sqs/adapter.rb', line 200

def remove_permission(queue_url, label)
  params = {
    'Action' => 'RemovePermission',
    'Label'  => label
  }

  connect('GET', queue_url, PARAMS.merge(params))
end

#send_message(queue_url, msg) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/raws/sqs/adapter.rb', line 149

def send_message(queue_url, msg)
  params = {
    'Action'      => 'SendMessage',
    'MessageBody' => msg
  }

  connect('GET', queue_url, PARAMS.merge(params))
end

#set_queue_attributes(queue_url, attrs) ⇒ Object



142
143
144
145
146
147
# File 'lib/raws/sqs/adapter.rb', line 142

def set_queue_attributes(queue_url, attrs)
  params = {'Action' => 'SetQueueAttributes'}
  params.merge!(pack_nv_attrs(attrs))

  connect('GET', queue_url, PARAMS.merge(params))
end

#sign(method, base_uri, params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/raws/sqs/adapter.rb', line 62

def sign(method, base_uri, params)
  path = {
    'AWSAccessKeyId'   => RAWS.aws_access_key_id,
    'SignatureMethod'  => 'HmacSHA256',
    'SignatureVersion' => '2',
    'Timestamp'        => Time.now.utc.iso8601
  }.merge(params).map do |key, val|
    "#{RAWS.escape(key)}=#{RAWS.escape(val)}"
  end.sort.join('&')

  uri = ::URI.parse(base_uri)
  "#{path}&Signature=" << RAWS.escape(
    [
      ::OpenSSL::HMAC.digest(
        ::OpenSSL::Digest::SHA256.new,
        RAWS.aws_secret_access_key,
        "#{method.upcase}\n#{uri.host.downcase}\n#{uri.path}\n#{path}"
      )
    ].pack('m').strip
  )
end