class AliMns::Request

Attributes

body[R]
content_length[R]
content_md5[R]
content_type[R]
date[R]
method[R]
mns_headers[R]
uri[R]

Public Class Methods

new(method: "get", path: "/", mns_headers: {}) click to toggle source
# File lib/ali_mns/request.rb, line 32
def initialize method: "get", path: "/", mns_headers: {}, params: {}, body: nil
  conf = {
    host: host,
    path: path
  }
  conf.merge!(query: params.to_query) unless params.empty?
  @uri = URI::HTTP.build(conf)
  @method = method
  @mns_headers = mns_headers.merge("x-mns-version" => "2015-06-06")
end

Public Instance Methods

content(type, values={}) click to toggle source
# File lib/ali_mns/request.rb, line 43
def content type, values={}
  ns = "http://mns.aliyuncs.com/doc/v1/"
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.send(type.to_sym, xmlns: ns) do |b|
      values.each{|k,v| b.send k.to_sym, v}
    end
  end
  @body = builder.to_xml
  @content_md5 = Base64::encode64(Digest::MD5.hexdigest(body)).chop
  @content_length = body.size
  @content_type = "text/xml;charset=utf-8"
end
delete_with_body(url, body, headers, &block) click to toggle source
# File lib/ali_mns/request.rb, line 84
def delete_with_body(url, body, headers, &block)
  response = RestClient::Request.execute(:method => :delete, :url => url, :payload => body, :headers => headers, &block)
end
execute() click to toggle source
# File lib/ali_mns/request.rb, line 63
def execute
  date = DateTime.now.httpdate
  headers =  {
    "Authorization" => authorization(date),
    "Content-Length" => content_length || 0,
    "Content-Type" => content_type,
    "Content-MD5" => content_md5,
    "Date" => date,
    "Host" => uri.host
  }.merge(mns_headers).reject{|k,v| v.nil?}
  begin
    if method == :delete && body.present?
      delete_with_body(uri.to_s, body, headers)
    else
      RestClient.send *[method, uri.to_s, body, headers].compact
    end
  rescue Exception => ex
    puts ex.message
  end
end
xml_content(xml) click to toggle source
# File lib/ali_mns/request.rb, line 56
def xml_content(xml)
  @body = xml.to_s
  @content_md5 = Base64::encode64(Digest::MD5.hexdigest(body)).chop
  @content_length = body.size
  @content_type = "text/xml;charset=utf-8"
end

Private Instance Methods

authorization(date) click to toggle source
# File lib/ali_mns/request.rb, line 93
def authorization date
  canonical_resource = [uri.path, uri.query].compact.join("?")
  canonical_mq_headers = mns_headers.sort.collect{|k,v| "#{k.downcase}:#{v}"}.join("\n")
  method = self.method.to_s.upcase
  signature = [method, content_md5 || "" , content_type || "" , date, canonical_mq_headers, canonical_resource].join("\n")
  sha1 = Digest::HMAC.digest(signature, key, Digest::SHA1)
  "MNS #{access_id}:#{Base64.encode64(sha1).chop}"
end
configuration() click to toggle source
# File lib/ali_mns/request.rb, line 89
def configuration
  AliMns.configuration
end