Class: Xiaomi::Push::Message::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/xiaomi/push/services/messages/base.rb

Overview

This class is abstract.

消息体的基本数据

Direct Known Subclasses

Android, IOS

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aliasString

别名

Returns:

  • (String)

    the current value of alias



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def alias
  @alias
end

#extrasString

额外参数

Returns:

  • (String)

    the current value of extras



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def extras
  @extras
end

#registration_idString

reg id

Returns:

  • (String)

    the current value of registration_id



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def registration_id
  @registration_id
end

#topicString

标签

Returns:

  • (String)

    the current value of topic



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def topic
  @topic
end

#topic_opString

配合 topics 使用

Returns:

  • (String)

    the current value of topic_op



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def topic_op
  @topic_op
end

#topicsString

多个标签

Returns:

  • (String)

    the current value of topics



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def topics
  @topics
end

#user_accountString

user account

Returns:

  • (String)

    the current value of user_account



14
15
16
# File 'lib/xiaomi/push/services/messages/base.rb', line 14

def 
  @user_account
end

Instance Method Details

#android?Bool

检测是否是 Android 消息体

Returns:

  • (Bool)


118
119
120
# File 'lib/xiaomi/push/services/messages/base.rb', line 118

def android?
  current == 'ANDROID'
end

#currentString

当前消息体类型

Returns:

  • (String)

    IOS/ANDROID



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.

设置或获取附加数据

Parameters:

  • key (String)
  • value (String) (defaults to: nil)


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

Examples:

ios10_struct('title') # => 'aps_proper_fields.title'
ios10_struct('description') # => 'aps_proper_fields.body'
ios10_struct('badge') # => 'badge'

Parameters:

  • key (String)

Returns:

  • (Bool)


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 消息体

Returns:

  • (Bool)


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 消息体

Returns:

  • (Bool)


111
112
113
# File 'lib/xiaomi/push/services/messages/base.rb', line 111

def ios?
  current == 'IOS'
end

#to_paramsHash

转换为字典

Returns:

  • (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.

设置或获取基本数据

Parameters:

  • key (String)

    设置 :registration_id, :alias, :topic, :user_account, :topics, :topic_op, :extras

  • value (String) (defaults to: nil)


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