class AliyunMns::Request

Attributes

body[R]
content_length[R]
content_md5[R]
content_type[R]
method[R]
mqs_headers[R]
uri[R]

Public Class Methods

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

Public Instance Methods

content(type, values={}) click to toggle source
# File lib/aliyun_mns/request.rb, line 44
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
execute() click to toggle source
# File lib/aliyun_mns/request.rb, line 57
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(mqs_headers).reject { |k, v| v.nil? }
  begin
    RestClient.send *[method, uri.to_s, body, headers].compact
  rescue RestClient::Exception => ex
    logger = Logger.new(STDOUT)
    logger.error ex.message
    logger.error ex.backtrace.join("\n")
    raise ex
  end
end

Private Instance Methods

authorization(date) click to toggle source
# File lib/aliyun_mns/request.rb, line 82
def authorization(date)
  canonical_resource = [uri.path, uri.query].compact.join("?")
  canonical_mq_headers = mqs_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 = OpenSSL::HMAC.digest("sha1", access_key_secret, signature)
  "MNS #{access_key_id}:#{Base64.encode64(sha1).chop}"
end
configuration() click to toggle source
# File lib/aliyun_mns/request.rb, line 78
def configuration
  AliyunMns.configuration
end