Module: AliyunMns

Defined in:
lib/aliyun_mns.rb

Defined Under Namespace

Classes: Error

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.access_keyObject

Returns the value of attribute access_key.



18
19
20
# File 'lib/aliyun_mns.rb', line 18

def access_key
  @access_key
end

.api_versionObject

Returns the value of attribute api_version.



18
19
20
# File 'lib/aliyun_mns.rb', line 18

def api_version
  @api_version
end

.endpointObject

Returns the value of attribute endpoint.



18
19
20
# File 'lib/aliyun_mns.rb', line 18

def endpoint
  @endpoint
end

.protocolObject

Returns the value of attribute protocol.



18
19
20
# File 'lib/aliyun_mns.rb', line 18

def protocol
  @protocol
end

.secret_keyObject

Returns the value of attribute secret_key.



18
19
20
# File 'lib/aliyun_mns.rb', line 18

def secret_key
  @secret_key
end

Class Method Details

.authorization(op = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/aliyun_mns.rb', line 24

def authorization(op = {})
  body = [op[:method], op[:content_md5], op[:content_type],
  op[:date], "x-mns-version:#{api_version}", op[:resource]].join("\n")

  "MNS #{access_key}:#{base64_hmac(body)}"
end

.base64_hmac(data) ⇒ Object



20
21
22
# File 'lib/aliyun_mns.rb', line 20

def base64_hmac(data)
  Base64.encode64("#{OpenSSL::HMAC.digest("sha1", secret_key, data)}").strip
end

.content_xml(content) ⇒ Object



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

def content_xml(content)
  content = <<-XML
<?xml version="1.0"?>
<Message xmlns="http://mqs.aliyuncs.com/doc/v1/">
  <MessageBody>#{content}</MessageBody>
</Message>
  XML
end

.delete(queue, receipt_handle) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/aliyun_mns.rb', line 84

def delete(queue, receipt_handle)
  httpdate = Time.now.httpdate
  uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages?ReceiptHandle=#{receipt_handle}")

  authorization_code = authorization(method: "DELETE",
                                     date: httpdate,
                                     resource: "#{uri.path}?#{uri.query}")

  request = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}", {
    "Authorization" => authorization_code, 
    "Date" => httpdate,
    "Host" => uri.host,
    "x-mns-version" => api_version
  })

  Net::HTTP.new(uri.host, uri.port).request(request)
end

.receive(queue) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aliyun_mns.rb', line 65

def receive(queue)
  httpdate = Time.now.httpdate
  uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages")
  authorization_code = authorization(method: "GET",
                                     date: httpdate,
                                     resource: uri.path)

  request = Net::HTTP::Get.new(uri.path, {
    "Authorization" => authorization_code, 
    "Date" => httpdate,
    "Host" => uri.host,
    "x-mns-version" => api_version
  })

  response = Net::HTTP.new(uri.host, uri.port).request(request)

  JSON.parse(Crack::XML.parse(response.body).to_json)
end

.send_msg(queue, content) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aliyun_mns.rb', line 40

def send_msg(queue, content) 
  httpdate = Time.now.httpdate
  uri = URI.parse("#{protocol}://#{endpoint}/queues/#{queue}/messages")
  content = content_xml(content)
  content_md5 = Base64::encode64(Digest::MD5.hexdigest(content)).chop
  authorization_code = authorization(method: "POST",
                                     content_md5: content_md5,
                                     content_type: "text/xml;charset=utf-8",
                                     date: httpdate,
                                     resource: uri.path)

  request = Net::HTTP::Post.new(uri.path, {
    "Authorization" => authorization_code, 
    "Date" => httpdate,
    "Host" => uri.host,
    "x-mns-version" => api_version,
    "Content-Length" => content.size.to_s,
    "Content-MD5" => content_md5,
    "Content-type" => "text/xml;charset=utf-8"
  })

  request.body = content
  Net::HTTP.new(uri.host, uri.port).request(request)
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (AliyunMns)

    the object that the method was called on



13
14
15
# File 'lib/aliyun_mns.rb', line 13

def self.setup
  yield self
end