Sha256: 392dcec2fffc380ca16e6e6fe1d09abb304018c2df811583b46384e226816b47

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module AliyunMns
  class Message
    attr_reader :queue, :id, :body_md5, :body, :receipt_handle, :enqueue_at, :first_enqueue_at, :next_visible_at, :dequeue_count, :priority

    def initialize(queue, content)
      h = Hash.xml_object(content, "Message")
      @queue = queue
      @id = h["MessageId"]
      @body_md5 = h["MessageBodyMD5"]
      @body = h["MessageBody"]
      @enqueue_at = Time.at(h["EnqueueTime"].to_i/1000.0)
      @first_enqueue_at = Time.at(h["FirstDequeueTime"].to_i/1000.0)
      @dequeue_count = h["DequeueCount"].to_i
      @priority = h["Priority"].to_i
      @receipt_handle = h["ReceiptHandle"]
      @next_visible_at = Time.at(h["NextVisibleTime"].to_i/1000.0)
    end

    #删除消息
    def delete
      check_receipt_handle
      AliyunMns::Request.delete(queue.messages_path, params: {:ReceiptHandle => receipt_handle})
    end

    #修改消息可见时间
    def change_visibility(seconds)
      check_receipt_handle
      AliyunMns::Request.put(queue.messages_path, params: {:ReceiptHandle => receipt_handle, :VisibilityTimeout => seconds})
    end

    def to_s
      s = {
          "队列" => queue.name,
          "ID" => id,
          "MD5" => body_md5,
          "Receipt handle" => receipt_handle,
          "Enqueue at" => enqueue_at,
          "First enqueue at" => first_enqueue_at,
          "Next visible at" => next_visible_at,
          "Dequeue count" => dequeue_count,
          "Priority" => priority
      }.collect { |k, v| "#{k}: #{v}" }

      sep = "=============================================>"
      s.unshift sep
      s << sep
      s << body
      s.join("\n")
    end

    private
    def check_receipt_handle
      raise "No receipt handle for this operation" unless receipt_handle
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aliyun_mns-0.1.11 lib/aliyun_mns/message.rb
aliyun_mns-0.1.10 lib/aliyun_mns/message.rb
aliyun_mns-0.1.9 lib/aliyun_mns/message.rb
aliyun_mns-0.1.8 lib/aliyun_mns/message.rb