Class: AliyunIot::Topic
- Inherits:
-
Object
- Object
- AliyunIot::Topic
- Defined in:
- lib/aliyun_iot/topic.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subscription_name ⇒ Object
readonly
Returns the value of attribute subscription_name.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(opts = {}) ⇒ Object
创建topic.
-
#delete ⇒ Object
删除topic.
-
#get_topic_attributes ⇒ Object
获取topic属性.
-
#initialize(name, subscription_name = nil) ⇒ Topic
constructor
A new instance of Topic.
-
#publish_message(opts = {}) ⇒ Object
发布消息.
-
#subscribe(opts = {}) ⇒ Object
订阅topic.
-
#unsubscribe ⇒ Object
退订topic.
Constructor Details
#initialize(name, subscription_name = nil) ⇒ Topic
Returns a new instance of Topic.
23 24 25 26 |
# File 'lib/aliyun_iot/topic.rb', line 23 def initialize(name, subscription_name = nil) @name = name @subscription_name = subscription_name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/aliyun_iot/topic.rb', line 7 def name @name end |
#subscription_name ⇒ Object (readonly)
Returns the value of attribute subscription_name.
7 8 9 |
# File 'lib/aliyun_iot/topic.rb', line 7 def subscription_name @subscription_name end |
Class Method Details
.[](name, subscription_name = nil) ⇒ Object
11 12 13 |
# File 'lib/aliyun_iot/topic.rb', line 11 def [](name, subscription_name = nil) Topic.new(name, subscription_name) end |
.topics(opts = {}) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/aliyun_iot/topic.rb', line 15 def topics(opts = {}) = {query: "x-mns-prefix", offset: "x-mns-marker", size: "x-mns-ret-number"} mqs_headers = opts.slice(*.keys).reduce({}) { |mqs_headers, item| k, v = *item; mqs_headers.merge!([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 |
Instance Method Details
#create(opts = {}) ⇒ Object
创建topic
29 30 31 32 33 34 35 36 |
# File 'lib/aliyun_iot/topic.rb', line 29 def create(opts={}) Request::Xml.put(topic_path) do |request| = { MaximumMessageSize: 65536 }.merge(opts) request.content :Topic, end end |
#delete ⇒ Object
删除topic
39 40 41 |
# File 'lib/aliyun_iot/topic.rb', line 39 def delete Request::Xml.delete(topic_path) end |
#get_topic_attributes ⇒ Object
获取topic属性
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 = {}) ⇒ Object
发布消息
75 76 77 78 79 80 81 82 83 |
# File 'lib/aliyun_iot/topic.rb', line 75 def (opts = {}) if opts[:MessageBody].nil? || opts[:MessageBody].blank? raise ParamsError, "publish message parameters invalid" else Request::Xml.post() do |request| request.content(:Message, opts) end end end |
#subscribe(opts = {}) ⇒ Object
订阅topic
59 60 61 62 63 64 65 66 67 |
# 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 |