class AliyunIot::Topic

Attributes

name[R]
subscription_name[R]

Public Class Methods

[](name, subscription_name = nil) click to toggle source
# File lib/aliyun_iot/topic.rb, line 11
def [](name, subscription_name = nil)
  Topic.new(name, subscription_name)
end
new(name, subscription_name = nil) click to toggle source
# File lib/aliyun_iot/topic.rb, line 23
def initialize(name, subscription_name = nil)
  @name = name
  @subscription_name = subscription_name
end
topics(opts = {}) click to toggle source
# File lib/aliyun_iot/topic.rb, line 15
def topics(opts = {})
  mqs_options = {query: "x-mns-prefix", offset: "x-mns-marker", size: "x-mns-ret-number"}
  mqs_headers = opts.slice(*mqs_options.keys).reduce({}) { |mqs_headers, item| k, v = *item; mqs_headers.merge!(mqs_options[k] => v) }
  response = Request::Xml.get("/topics", mqs_headers: mqs_headers)
  Hash.xml_array(response, "Topics", "Topic").collect { |item| Topic.new(URI(item["TopicURL"]).path.sub!(/^\/topics\//, "")) }
end

Public Instance Methods

create(opts={}) click to toggle source

创建topic

# File lib/aliyun_iot/topic.rb, line 29
def create(opts={})
  Request::Xml.put(topic_path) do |request|
    msg_options = {
        MaximumMessageSize: 65536
    }.merge(opts)
    request.content :Topic, msg_options
  end
end
delete() click to toggle source

删除topic

# File lib/aliyun_iot/topic.rb, line 39
def delete
  Request::Xml.delete(topic_path)
end
get_topic_attributes() click to toggle source

获取topic属性

# File lib/aliyun_iot/topic.rb, line 44
def get_topic_attributes
  topic_hash = Hash.from_xml(Request::Xml.get(topic_path))
  {
      topic_name: topic_hash["Topic"]["TopicName"],
      create_time: topic_hash["Topic"]["CreateTime"],
      last_modify_time: topic_hash["Topic"]["LastModifyTime"],
      maximum_message_size: topic_hash["Topic"]["MaximumMessageSize"],
      message_retention_period: topic_hash["Topic"]["MessageRetentionPeriod"],
      message_ount: topic_hash["Topic"]["MessageCount"],
      logging_enabled: topic_hash["Topic"]["LoggingEnabled"]
  }

end
publish_message(opts = {}) click to toggle source

发布消息

# File lib/aliyun_iot/topic.rb, line 75
def publish_message(opts = {})
  if opts[:MessageBody].nil? || opts[:MessageBody].blank?
    raise ParamsError, "publish message parameters invalid"
  else
    Request::Xml.post(message_path) do |request|
      request.content(:Message, opts)
    end
  end
end
subscribe(opts = {}) click to toggle source

订阅topic

# File lib/aliyun_iot/topic.rb, line 59
def subscribe(opts = {})
  if opts[:Endpoint].nil? || opts[:Endpoint].blank?
    raise ParamsError, "subscribe parameters invalid"
  else
    Request::Xml.put(subscribe_path) do |request|
      request.content(:Subscription, opts)
    end
  end
end
unsubscribe() click to toggle source

退订topic

# File lib/aliyun_iot/topic.rb, line 70
def unsubscribe
  Request::Xml.delete(subscribe_path)
end

Private Instance Methods

message_path() click to toggle source
# File lib/aliyun_iot/topic.rb, line 95
def message_path
  "/topics/#{name}/messages"
end
subscribe_path() click to toggle source
# File lib/aliyun_iot/topic.rb, line 91
def subscribe_path
  "/topics/#{name}/subscriptions/#{subscription_name}"
end
topic_path() click to toggle source
# File lib/aliyun_iot/topic.rb, line 87
def topic_path
  "/topics/#{name}"
end