class BaiduPushClient::Client

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/baidu_push_client/client.rb, line 67
def initialize(options)
  super
end

Public Instance Methods

payload() click to toggle source
# File lib/baidu_push_client/client.rb, line 117
def payload
  optional_payload = {
    'channel_id'=> channel_id,
    'user_id' => user_id,
    'device_type' => device_type,
    'message_type' => message_type,
    'msg_keys' => Time.now.to_i
  }
  optional_payload.delete_if {|k,v|v.nil?}

  must_payload = {
    'deploy_status' => deploy_status,
    'method' =>  api_method,
    'apikey' => ak,
    'push_type' => push_type,
    'timestamp' => Time.now.to_i,
    'expires' => Time.now.to_i+3600
  }
  must_payload.merge(optional_payload).merge({'messages' => messages})
end
push(target_hash) click to toggle source
# File lib/baidu_push_client/client.rb, line 96
def push(target_hash)
  self.attributes = self.attributes.merge target_hash
  self.api_method = 'push_msg'
  resp = rest_client.post url_path_with_query_string do |req|
    req.headers = headers
  end
  resp
end
push_all(options={}) click to toggle source
# File lib/baidu_push_client/client.rb, line 83
def push_all(options={})
  self.attributes = self.attributes.merge options
  self.api_method = 'push_msg'
  self.push_type = PUSH_TYPE[:all]
  self.message_type = MESSAGE_TYPE[:notification]
  self.device_type = nil

  resp = rest_client.post url_path_with_query_string do |req|
    req.headers = headers
  end
  resp
end
query_bindlist(target_hash) click to toggle source
# File lib/baidu_push_client/client.rb, line 71
def query_bindlist(target_hash)
  self.api_method = 'query_bindlist'
  self.attributes = self.attributes.merge target_hash

  self.push_type = nil
  self.message_type = nil
  resp = rest_client.post url_path_with_query_string do |req|
    req.headers = headers
  end
  resp
end
sign_request(query_hash) click to toggle source
# File lib/baidu_push_client/client.rb, line 105
def sign_request(query_hash)
  # bodyArgs.sign = getSign('POST', PROTOCOL_SCHEMA + host + path, bodyArgs, sk);

  method = 'POST'

  url = 'https://' + server_host + url_path
  str = query_hash.sort { |a, b| a[0]<=>b[0] }.map { |x| "#{x[0]}=#{x[1]}" }.join
  urlencoded_str = CGI.escape(method.to_s.upcase + url + str + sk)
  Digest::MD5.hexdigest(urlencoded_str)
end

Private Instance Methods

headers() click to toggle source
# File lib/baidu_push_client/client.rb, line 140
def headers

  {
    #'Content-Length': bodyStr.length,
    #'Content-Type':'application/x-www-form-urlencoded'
  }
end
rest_client() click to toggle source
# File lib/baidu_push_client/client.rb, line 158
def rest_client
  Faraday.new(url: "https://"+server_host) do |faraday|
    #faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end
end
url_path() click to toggle source
# File lib/baidu_push_client/client.rb, line 148
def url_path
  common_path + "/channel"
end
url_path_with_query_string() click to toggle source
# File lib/baidu_push_client/client.rb, line 152
def url_path_with_query_string
  hash = payload.merge({sign: sign_request(payload)})
  query_string = (hash.map{|k,v| "#{k}=#{v}"}.join("&"))
  url_path + '?' + query_string
end