Class: Xiaomi::Push::Client

Inherits:
Object
  • Object
show all
Includes:
Const
Defined in:
lib/xiaomi/push/client.rb

Overview

小米推送内置客户端

实际情况并不会直接被使用,而是使用 iOS 或 Android 的实例化

Examples:

实例化 iOS 推送客户端(环境使用 :sandbox)

client = Xiaomi::Push::IOS('Fill your app secret', :sandbox)

实例化 Android 推送客户端

client = Xiaomi::Push::Android('Fill your app secret')

See Also:

Direct Known Subclasses

Android, IOS

Constant Summary

Constants included from Const

Xiaomi::Push::Const::DEVICES, Xiaomi::Push::Const::PRODUCTION_URL, Xiaomi::Push::Const::SANDBOX_URL

Instance Attribute Summary collapse

Attributes included from Const

#base_url

Instance Method Summary collapse

Methods included from Const

#build_uri, #production, #sandbox, #use_production!, #use_sandbox!

Constructor Details

#initialize(secret, env = :production) ⇒ Client

实例化一个新的客户端

Parameters:

  • secret (String)

    小米应用的 App Secret

  • env (Symbol) (defaults to: :production)

    推送环境,可用的环境有 :production/:sandbox

Raises:

  • (RequestError)

    必须使用 Xiaomi::Push::Android 或 Xiaomi::Push::IOS 实例化

  • (RequestError)

    Android 使用 sandbox 推送环境引发异常

See Also:



30
31
32
33
34
35
36
37
# File 'lib/xiaomi/push/client.rb', line 30

def initialize(secret, env = :production)
  @device = self.class.name.split('::')[-1].upcase
  @client = HTTP.headers(authorization: "key=#{secret}")

  determine_platform!(env)

  env == :production ? use_production! : use_sandbox!
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



19
20
21
# File 'lib/xiaomi/push/client.rb', line 19

def client
  @client
end

#deviceObject (readonly)

Returns the value of attribute device.



19
20
21
# File 'lib/xiaomi/push/client.rb', line 19

def device
  @device
end

Instance Method Details

#feedbackObject

Feedback



60
61
62
# File 'lib/xiaomi/push/client.rb', line 60

def feedback
  @feedback ||= Services::Feedback.new(self)
end

#get(url, params = nil) ⇒ Hash

以 GET 方式的网络请求

Parameters:

  • url (String)
  • params (Hash) (defaults to: nil)

Returns:

  • (Hash)

    小米返回数据结构



69
70
71
72
# File 'lib/xiaomi/push/client.rb', line 69

def get(url, params = nil)
  r = @client.get(url, params: params)
  data = JSON.parse(r)
end

#messageObject

单条消息



40
41
42
# File 'lib/xiaomi/push/client.rb', line 40

def message
  @message ||= Services::Message.new(self)
end

#messagesObject

多条消息



45
46
47
# File 'lib/xiaomi/push/client.rb', line 45

def messages
  @messages ||= Services::Messages.new(self)
end

#post(url, params = nil) ⇒ Hash

以 POST 方式的网络请求

Parameters:

  • url (String)
  • params (Hash) (defaults to: nil)

Returns:

  • (Hash)

    小米返回数据结构



79
80
81
82
# File 'lib/xiaomi/push/client.rb', line 79

def post(url, params = nil)
  r = @client.post(url, form: params)
  data = JSON.parse(r)
end

#topicObject

标签



50
51
52
# File 'lib/xiaomi/push/client.rb', line 50

def topic
  @topic ||= Services::Topic.new(self)
end

#userObject

用户查询



55
56
57
# File 'lib/xiaomi/push/client.rb', line 55

def user
  @user ||= Services::User.new(self)
end