class AliyunIot::Request::Xml

Attributes

body[R]
client[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: {}, query: {}) click to toggle source
# File lib/aliyun_iot/request/xml.rb, line 33
def initialize(method: "get", path: "/", mqs_headers: {}, query: {})
  conf = {
      host: end_point,
      path: path
  }
  conf.merge!(query: query.to_query) unless query.empty?
  @uri = URI::HTTP.build(conf)
  @method = method
  @client = HttpClient.new(@uri.to_s)
  @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_iot/request/xml.rb, line 45
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_iot/request/xml.rb, line 58
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? }
  client.send *[method, body, headers].compact
end

Private Instance Methods

authorization(date) click to toggle source
# File lib/aliyun_iot/request/xml.rb, line 77
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_iot/request/xml.rb, line 73
def configuration
  AliyunIot.configuration
end