Class: AliyunIot::Request::Xml

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun_iot/request/xml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method: "get", path: "/", mqs_headers: {}, query: {}) ⇒ Xml

Returns a new instance of Xml.



33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def client
  @client
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def content_length
  @content_length
end

#content_md5Object (readonly)

Returns the value of attribute content_md5.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def content_md5
  @content_md5
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def content_type
  @content_type
end

#methodObject (readonly)

Returns the value of attribute method.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def method
  @method
end

#mqs_headersObject (readonly)

Returns the value of attribute mqs_headers.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def mqs_headers
  @mqs_headers
end

#uriObject (readonly)

Returns the value of attribute uri.



18
19
20
# File 'lib/aliyun_iot/request/xml.rb', line 18

def uri
  @uri
end

Instance Method Details

#content(type, values = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# 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

#executeObject



58
59
60
61
62
63
64
65
66
67
68
69
# 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