Class: Baidu::Push::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/baidu/push/client.rb

Direct Known Subclasses

AsyncClient

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/baidu/push/client.rb', line 5

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/baidu/push/client.rb', line 5

def logger
  @logger
end

Class Method Details

.setup(options = {api_key: nil, secret_key: nil}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/baidu/push/client.rb', line 7

def self.setup(options = {api_key: nil, secret_key: nil})
  raise 'api_key can not be nil.' unless options[:api_key]
  raise 'secret_key can not be nil.' unless options[:secret_key]
  md5 = Digest::MD5.hexdigest([options[:api_key], options[:secret_key]].join)
  instance_variable_get("@client_#{md5}") ||
      instance_variable_set("@client_#{md5}", begin
                                              config = Configuration.new
                                              config.api_key = options[:api_key]
                                              config.secret_key = options[:secret_key]
                                              client = self.new
                                              client.config = config
                                              client.logger = options[:logger]
                                              client
                                            end)
end

Instance Method Details

#push_msg(message) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/baidu/push/client.rb', line 27

def push_msg(message)
  raise 'invalid baidu push message' unless message.is_a?(Baidu::Push::Message)
  attr = message.non_nil_attributes
  attr.merge!(apikey: config.api_key, method: __method__)
  rest_api.connection.post do |req|
    req.url "#{rest_api.base_path}/channel"
    req.params = attr.merge(sign: get_sign('POST', "#{rest_api.default_domain}#{rest_api.base_path}/channel", attr))
  end
end

#rest_apiObject



23
24
25
# File 'lib/baidu/push/client.rb', line 23

def rest_api
  @api ||= API.new(logger: logger)
end