Class: AliMns::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



16
17
18
# File 'lib/ali_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/ali_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/ali_mns/request.rb', line 16

def content_type
  @content_type
end

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#mns_headersObject (readonly)

Returns the value of attribute mns_headers.



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

def mns_headers
  @mns_headers
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

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



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



84
85
86
# 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

#executeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



56
57
58
59
60
61
# 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