Class: Xiaomi::Push::Message::Base Abstract
- Inherits:
-
Object
- Object
- Xiaomi::Push::Message::Base
- Defined in:
- lib/xiaomi/push/services/messages/base.rb
Overview
This class is abstract.
消息体的基本数据
Instance Attribute Summary collapse
-
#alias ⇒ String
别名.
-
#extras ⇒ String
额外参数.
-
#registration_id ⇒ String
reg id.
-
#topic ⇒ String
标签.
-
#topic_op ⇒ String
配合 topics 使用.
-
#topics ⇒ String
多个标签.
-
#user_account ⇒ String
user account.
Instance Method Summary collapse
-
#android? ⇒ Bool
检测是否是 Android 消息体.
-
#current ⇒ String
当前消息体类型.
-
#extra(key, value = nil) ⇒ void
设置或获取附加数据.
-
#ios10_struct(key) ⇒ Bool
转换 iOS 10 消息的参数.
-
#ios10_struct? ⇒ Bool
检查是否为 iOS 10 消息体.
-
#ios? ⇒ Bool
检测是否是 iOS 消息体.
-
#to_params ⇒ Hash
转换为字典.
-
#type(key, value = nil) ⇒ void
设置或获取基本数据.
Instance Attribute Details
#alias ⇒ String
别名
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def alias @alias end |
#extras ⇒ String
额外参数
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def extras @extras end |
#registration_id ⇒ String
reg id
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def registration_id @registration_id end |
#topic ⇒ String
标签
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def topic @topic end |
#topic_op ⇒ String
配合 topics 使用
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def topic_op @topic_op end |
#topics ⇒ String
多个标签
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def topics @topics end |
#user_account ⇒ String
user account
14 15 16 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 14 def user_account @user_account end |
Instance Method Details
#android? ⇒ Bool
检测是否是 Android 消息体
118 119 120 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 118 def android? current == 'ANDROID' end |
#current ⇒ String
当前消息体类型
125 126 127 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 125 def current @current ||= self.class.name.split('::')[-1].upcase end |
#extra(key, value = nil) ⇒ void
This method returns an undefined value.
设置或获取附加数据
21 22 23 24 25 26 27 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 21 def extra(key, value = nil) unless value @extras[key] else @extras[key] = value end end |
#ios10_struct(key) ⇒ Bool
转换 iOS 10 消息的参数
仅转换 title, subtitle, body 和 description
101 102 103 104 105 106 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 101 def ios10_struct(key) key = 'body' if key == 'description' return extra_key(key) unless %w(title subtitle body mutable-content).include?(key) "aps_proper_fields.#{key}" end |
#ios10_struct? ⇒ Bool
检查是否为 iOS 10 消息体
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 77 def ios10_struct? return @ios10_struct unless @ios10_struct.nil? @ios10_struct = false keys = instance_variables.map {|e| instance_key(e) } %w(title subtitle).each do |key| return @ios10_struct = true if keys.include?(key) end @ios10_struct end |
#ios? ⇒ Bool
检测是否是 iOS 消息体
111 112 113 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 111 def ios? current == 'IOS' end |
#to_params ⇒ Hash
转换为字典
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 46 def to_params hash_data = {} instance_variables.each do |ivar| key = instance_key(ivar) key = if ios? && ios10_struct? ios10_struct(key) else extra_key(key) end value = instance_variable_get(ivar) next unless value if key == 'extras' value.each do |k, v| key = "extra.#{k}" hash_data[key] = v end else hash_data[key] = value end end hash_data end |
#type(key, value = nil) ⇒ void
This method returns an undefined value.
设置或获取基本数据
34 35 36 37 38 39 40 41 42 |
# File 'lib/xiaomi/push/services/messages/base.rb', line 34 def type(key, value = nil) key = "@#{key}" if value instance_variable_set(key, value) else instance_variable_get(key) end end |