Class: Aliyun::Mq::Sdk::Producer

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/aliyun/mq/sdk/producer.rb

Constant Summary collapse

DEFAULT_BASE_URI =
'http://publictest-rest.ons.aliyun.com/message/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, producer_id, opts = {}) ⇒ Producer

Returns a new instance of Producer.



9
10
11
12
13
14
15
16
# File 'lib/aliyun/mq/sdk/producer.rb', line 9

def initialize(access_key, secret_key, producer_id, opts={})
  @access_key = access_key
  @secret_key = secret_key
  @producer_id = producer_id

  @region_url = opts[:region_url] || DEFAULT_BASE_URI
  @default_topic = opts[:default_topic]
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def access_key
  @access_key
end

#default_topicObject

Returns the value of attribute default_topic.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def default_topic
  @default_topic
end

#producer_idObject

Returns the value of attribute producer_id.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def producer_id
  @producer_id
end

#region_urlObject

Returns the value of attribute region_url.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def region_url
  @region_url
end

#secret_keyObject

Returns the value of attribute secret_key.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def secret_key
  @secret_key
end

#topicObject

Returns the value of attribute topic.



7
8
9
# File 'lib/aliyun/mq/sdk/producer.rb', line 7

def topic
  @topic
end

Instance Method Details

#headers(msg, time) ⇒ Object



18
19
20
21
# File 'lib/aliyun/mq/sdk/producer.rb', line 18

def headers(msg, time)
  sign = Auth.post_sign(secret_key, topic, producer_id, msg, time)
  {"Signature" => sign, "AccessKey" => access_key, "ProducerID" => producer_id, "Content-Type" => 'text/html;charset=UTF-8'}
end

#send(msg, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aliyun/mq/sdk/producer.rb', line 23

def send(msg, opts={})
  @time = opts[:time] || (Time.now.to_f * 1000).to_i
  @topic = opts[:topic] || default_topic
  tag = opts[:tag]
  key = opts[:key]
  is_order = opts[:is_order]
  sharding_key = opts[:sharding_key]

  hds = headers(msg, @time)

  query = {"topic" => topic, "time" => @time}

  query["tag"] = tag if tag
  query["key"] = key if key

  if is_order && !sharding_key.nil?
    hds = hds.merge("isOrder" => is_order.to_s, "shardingKey" => sharding_key)
  end
  res = self.class.post(region_url, headers: hds, query: query, body: msg)
  if res.parsed_response
    rslt = Utils.symbolize_keys(JSON.parse(res.parsed_response).merge(success: true))
  else
    rslt = {success: false, msg: res.response}
  end
  rslt
end