Class: AliyunMns::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method: "get", path: "/", mqs_headers: {}, params: {}) ⇒ Request

Returns a new instance of Request.



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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def body
  @body
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def content_length
  @content_length
end

#content_md5Object (readonly)

Returns the value of attribute content_md5.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def content_md5
  @content_md5
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def content_type
  @content_type
end

#methodObject (readonly)

Returns the value of attribute method.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def method
  @method
end

#mqs_headersObject (readonly)

Returns the value of attribute mqs_headers.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def mqs_headers
  @mqs_headers
end

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/aliyun_mns/request.rb', line 16

def uri
  @uri
end

Instance Method Details

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



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

#executeObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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
    raise RequestException.new(ex)
  end
end