xiaomi-push 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2c3c36c6c5e5b4ba0a9e2c2a4613dc8d7a10cd6
4
- data.tar.gz: c81f1a3bf49e0d03207a2716e1df1674fad2d45c
3
+ metadata.gz: af6f56215b75a2b1aade41cfb44472ffee343ef2
4
+ data.tar.gz: 68cd39c19f3465b6af9fb7ae1529c5baad19f6fd
5
5
  SHA512:
6
- metadata.gz: 215b4af4c6c8e3bdaacbd5e9a6fa967e0fe25d8d8ef5cbb69607b8c90b51e8665236246483c0a8001f52052ef8f0e7c4dbd7c8a25a1fbe18ba4f829cd877c915
7
- data.tar.gz: 772e82860bcea9b62cd43a458ad37f247d6e30254755803266a5aab5c319e260a6580fa1fa070c329b8d28cd74178865dda471f5e5ca90c402f39442efa79756
6
+ metadata.gz: 4ef808ae5f8cccf0f01ddb429180865aaafe0d8bddd1d0fd4989051574b58abaeaae2fa11e3666f60166b9988dd0161b21fdde88a8000b842967b1456d9e5166
7
+ data.tar.gz: 0172848d76bd101d3a607b82c6414ff4930884e9a98d1714a49f263b7f877cfeee27771c1bcf78351d3de91bdb93a11e6692943312c7b7b79ad6f14c45f9fec2
data/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
 
4
4
  官方文档: http://dev.xiaomi.com/doc/?p=533#d5e725
5
5
 
6
+ TL;DR
7
+ =====
8
+
6
9
  安装
7
10
  ----
8
11
 
@@ -49,15 +52,24 @@ message = {
49
52
  ### iOS
50
53
  message = Xiaomi::Push::Message::IOS.new(
51
54
  description:'iOS 主要显示描述',
52
- badge:10
55
+ badge:10,
56
+ extras: {
57
+ uri: 'app://bbs?id=8624',
58
+ source: 'mpush'
59
+ }
53
60
  )
54
61
 
55
62
  ### Android
56
63
  message = Xiaomi::Push::Message::Android.new(
57
64
  title:'标题要有吸引力',
58
65
  description:'描述可以在手机显示两行',
59
- notify_type:'DEFAULT_ALL'
66
+ notify_type:'DEFAULT_ALL',
67
+ extras: {
68
+ source: 'mpush'
69
+ }
60
70
  )
71
+ #### 支持单独追加 extra
72
+ message.extra('uri', 'app://bbs?id=8624')
61
73
 
62
74
  # 发消息
63
75
  ## 根据 regid
@@ -99,7 +111,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
99
111
 
100
112
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
113
 
102
- 如何共享
114
+ 贡献代码
103
115
  --------
104
116
 
105
117
  1. Fork it ( https://github.com/[my-github-username]/xiaomi-push/fork )
data/Rakefile CHANGED
@@ -21,9 +21,9 @@ task :send => :dotenv do
21
21
  client = Xiaomi::Push::Android.new(ENV['XIAOMI_PUSH_ANDROID_SECRET'])
22
22
 
23
23
  p "Send message to android device"
24
- # client.message.send(reg_id:'', data:message_data)
24
+ client.message.send(reg_id:'', message:message)
25
25
  r = client.message.send(alias:'866383029998732', message:message)
26
- # client.message.send(topic:'test', data:message_data)
26
+ r = client.message.send(topic:'test', message:message)
27
27
  ap r
28
28
 
29
29
  p "Send message to ios device"
@@ -36,6 +36,20 @@ task :send => :dotenv do
36
36
  ap r
37
37
  end
38
38
 
39
+ namespace :topic do
40
+ task :subscribe => :dotenv do
41
+ client = Xiaomi::Push::Android.new(ENV['XIAOMI_PUSH_ANDROID_SECRET'])
42
+ r = client.topic.subscribe(alias:'866383029998732', name:'test')
43
+ ap r
44
+ end
45
+
46
+ task :unsubscribe => :dotenv do
47
+ client = Xiaomi::Push::Android.new(ENV['XIAOMI_PUSH_ANDROID_SECRET'])
48
+ r = client.topic.unsubscribe(alias:'866383029998732', name:'test')
49
+ ap r
50
+ end
51
+ end
52
+
39
53
  task :message do
40
54
  ios_message = Xiaomi::Push::Message::IOS.new(title:'dddd')
41
55
  ios_message.extra('url', 'http://www.xxx.com')
@@ -43,3 +57,9 @@ task :message do
43
57
 
44
58
  p ios_message.build
45
59
  end
60
+
61
+ task :feedback do
62
+ client = Xiaomi::Push::Android.new(ENV['XIAOMI_PUSH_ANDROID_SECRET'])
63
+ r = client.feedback.invalid
64
+ ap r
65
+ end
data/bin/xmp CHANGED
@@ -7,7 +7,7 @@ require "xiaomi/push"
7
7
  require "awesome_print"
8
8
 
9
9
 
10
- program :version, '0.0.1'
10
+ program :version, Xiaomi::Push::VERSION
11
11
  program :description, 'xiaomi push command line tool'
12
12
 
13
13
  program :help, 'Author', 'icyleaf'
@@ -19,123 +19,4 @@ global_option('--verbose') { $verbose = true }
19
19
  default_command :help
20
20
  never_trace!
21
21
 
22
- command :message do |c|
23
- c.syntax = 'xmp message [options]'
24
- c.summary = '发送小米推送消息'
25
- c.description = '使用小米推送消息(目前仅支持 regid/alias/topic 推送方式)'
26
-
27
- # normal params
28
- c.option '--device DEVICE', ['android', 'ios'], '设备类型'
29
- c.option '--secret SECRET', '应用密钥'
30
-
31
- # type
32
- c.option '--regid REGID', 'reg id'
33
- c.option '--alias ALIAS', '别名'
34
- c.option '--topic TOPIC', '订阅名'
35
-
36
- # message
37
- c.option '-i', '--title TITLE', '消息标题(仅 Android 有效)'
38
- c.option '-d', '--description DESCRIPTION', '消息主体描述'
39
- c.option '-b', '--badge BADGE', Integer, '消息数字'
40
- c.option '-e', '--extras KEY=VALUE', Array, '自定义数据(使用 KEY=VALUE 方式,多个以逗号不带空格分隔)'
41
-
42
- # ## ios only
43
- # c.option '-y', '--category CATEGORY', '推送类别名称 (仅 iOS 有效)'
44
- # c.option '-v', '--environment ENV', [:production, :sandbox], '推送环境(仅 iOS 有效)'
45
- #
46
- # ## android only
47
- # c.option '-g', '--through THROUGHT', [0, 1], '消息传递方式(仅 iOS 有效)'
48
- # c.option '-n', '--notify NOTIFY', [-1, 1, 2, 4], 'message notify type'
49
-
50
- c.action do |args, options|
51
- ap options if $verbose
52
-
53
- @device = options.device.capitalize if options.device
54
- @secret = options.secret
55
-
56
- determine_device! unless @device
57
- determine_secret! unless @secret
58
-
59
- determine_channel!(options)
60
- determine_message!(options)
61
-
62
- sent!
63
- end
64
-
65
- private
66
-
67
- def sent!
68
- message_data = {
69
- @channel.to_sym => @channel_id,
70
- :message => @message,
71
- }
72
-
73
- if $verbose
74
- ap message_data
75
- end
76
-
77
- client = Xiaomi::Push.const_get(@device).new(@secret)
78
- r = client.message.send(message_data)
79
-
80
- ap r
81
- end
82
-
83
- def determine_android_message!(options)
84
- @message = Xiaomi::Push::Message::Android.new(
85
- title: @title,
86
- description: @description,
87
- badge: @badge,
88
- extras: @extras
89
- )
90
- end
91
-
92
- def determine_ios_message!(options)
93
- @message = Xiaomi::Push::Message::IOS.new(
94
- description: @description,
95
- badge: @badge,
96
- extras: @extras
97
- )
98
- end
99
-
100
- def determine_message!(options)
101
- @title = options.title
102
- @description = options.description
103
- @badge = options.badge
104
-
105
- @extras = if options.extras
106
- Hash[options.extras.collect{|data| data.split(/\=/)}]
107
- else
108
- nil
109
- end
110
-
111
- case @device.downcase
112
- when 'android'
113
- determine_android_message!(options)
114
- when 'ios'
115
- determine_ios_message!(options)
116
- end
117
- end
118
-
119
- def determine_device!
120
- devices = %w[Android iOS].freeze
121
- @device = choose "选择推送设备:", *devices
122
- end
123
-
124
- def determine_secret!
125
- @secret ||= ask '小米应用密钥:'
126
- end
127
-
128
- def determine_channel!(options)
129
- channles = %w[regid alias topic].freeze
130
- @channel = channles.select { |k| options.__hash__.has_key?k.to_sym }
131
-
132
- unless @channel.count > 0
133
- @channel = choose "选择推送方式:", *channles
134
-
135
- @channel_id = ask "输入 #{@channel} 的值:"
136
- else
137
- @channel = @channel[0]
138
- @channel_id = options.__hash__[@channel.to_sym]
139
- end
140
- end
141
- end
22
+ require 'xiaomi/push/commands'
@@ -8,7 +8,6 @@ require "xiaomi/push/devices/android"
8
8
 
9
9
  require "xiaomi/push/services/message"
10
10
  require "xiaomi/push/services/topic"
11
- require "xiaomi/push/services/alias"
12
11
  require "xiaomi/push/services/feedback"
13
12
 
14
13
 
@@ -9,13 +9,12 @@ module Xiaomi
9
9
  attr_reader :device, :secret, :header
10
10
  def initialize(secret)
11
11
  @device = self.class.name.split("::")[-1].upcase
12
+ @secret = secret
12
13
 
13
14
  unless DEVICES.include?@device
14
15
  raise NameError, 'Instance using Xiaomi::Push::Android or Xiaomi::Push::IOS'
15
16
  end
16
17
 
17
- @secret = secret
18
-
19
18
  @header = {
20
19
  'Authorization' => "key=#{@secret}"
21
20
  }
@@ -31,6 +30,16 @@ module Xiaomi
31
30
  @topic ||= Services::Topic.new(self)
32
31
  end
33
32
 
33
+ def feedback
34
+ @feedback ||= Services::Feedback.new(self)
35
+ end
36
+
37
+
38
+ def request(url, params)
39
+ r = RestClient.post url, params, @header
40
+ data = MultiJson.load r
41
+ end
42
+
34
43
  end
35
44
  end
36
45
  end
@@ -0,0 +1,2 @@
1
+ # require 'xiaomi/push/commands/message'
2
+ require 'xiaomi/push/commands/feedback'
@@ -0,0 +1,39 @@
1
+ command :feedback do |c|
2
+ c.syntax = 'xmp feedback [options]'
3
+ c.summary = '获取小米无效的设备列表'
4
+ c.description = ''
5
+
6
+ # normal params
7
+ c.option '--device DEVICE', ['android', 'ios'], '设备类型'
8
+ c.option '--secret SECRET', '应用密钥'
9
+
10
+ c.action do |args, options|
11
+ ap options if $verbose
12
+
13
+ @device = options.device.capitalize if options.device
14
+ @secret = options.secret
15
+
16
+ determine_device! unless @device
17
+ determine_secret! unless @secret
18
+
19
+ feedback!
20
+ end
21
+
22
+ private
23
+
24
+ def feedback!
25
+ client = Xiaomi::Push.const_get(@device).new(@secret)
26
+ r = client.feedback.invalid
27
+
28
+ ap r
29
+ end
30
+
31
+ def determine_device!
32
+ devices = %w[Android iOS].freeze
33
+ @device = choose "选择推送设备:", *devices
34
+ end
35
+
36
+ def determine_secret!
37
+ @secret ||= ask '小米应用密钥:'
38
+ end
39
+ end
@@ -0,0 +1,120 @@
1
+ command :message do |c|
2
+ c.syntax = 'xmp message [options]'
3
+ c.summary = '发送小米推送消息'
4
+ c.description = '使用小米推送消息(目前仅支持 regid/alias/topic 推送方式)'
5
+
6
+ # normal params
7
+ c.option '--device DEVICE', ['android', 'ios'], '设备类型'
8
+ c.option '--secret SECRET', '应用密钥'
9
+
10
+ # type
11
+ c.option '--regid REGID', 'reg id'
12
+ c.option '--alias ALIAS', '别名'
13
+ c.option '--topic TOPIC', '订阅名'
14
+
15
+ # message
16
+ c.option '-i', '--title TITLE', '消息标题(仅 Android 有效)'
17
+ c.option '-d', '--description DESCRIPTION', '消息主体描述'
18
+ c.option '-b', '--badge BADGE', Integer, '消息数字'
19
+ c.option '-e', '--extras KEY=VALUE', Array, '自定义数据(使用 KEY=VALUE 方式,多个以逗号不带空格分隔)'
20
+
21
+ # ## ios only
22
+ # c.option '-y', '--category CATEGORY', '推送类别名称 (仅 iOS 有效)'
23
+ # c.option '-v', '--environment ENV', [:production, :sandbox], '推送环境(仅 iOS 有效)'
24
+ #
25
+ # ## android only
26
+ # c.option '-g', '--through THROUGHT', [0, 1], '消息传递方式(仅 iOS 有效)'
27
+ # c.option '-n', '--notify NOTIFY', [-1, 1, 2, 4], 'message notify type'
28
+
29
+ c.action do |args, options|
30
+ ap options if $verbose
31
+
32
+ @device = options.device.capitalize if options.device
33
+ @secret = options.secret
34
+
35
+ determine_device! unless @device
36
+ determine_secret! unless @secret
37
+
38
+ determine_channel!(options)
39
+ determine_message!(options)
40
+
41
+ sent!
42
+ end
43
+
44
+ private
45
+
46
+ def sent!
47
+ message_data = {
48
+ @channel.to_sym => @channel_id,
49
+ :message => @message,
50
+ }
51
+
52
+ if $verbose
53
+ ap message_data
54
+ end
55
+
56
+ client = Xiaomi::Push.const_get(@device).new(@secret)
57
+ r = client.message.send(message_data)
58
+
59
+ ap r
60
+ end
61
+
62
+ def determine_android_message!(options)
63
+ @message = Xiaomi::Push::Message::Android.new(
64
+ title: @title,
65
+ description: @description,
66
+ badge: @badge,
67
+ extras: @extras
68
+ )
69
+ end
70
+
71
+ def determine_ios_message!(options)
72
+ @message = Xiaomi::Push::Message::IOS.new(
73
+ description: @description,
74
+ badge: @badge,
75
+ extras: @extras
76
+ )
77
+ end
78
+
79
+ def determine_message!(options)
80
+ @title = options.title
81
+ @description = options.description
82
+ @badge = options.badge
83
+
84
+ @extras = if options.extras
85
+ Hash[options.extras.collect{|data| data.split(/\=/)}]
86
+ else
87
+ nil
88
+ end
89
+
90
+ case @device.downcase
91
+ when 'android'
92
+ determine_android_message!(options)
93
+ when 'ios'
94
+ determine_ios_message!(options)
95
+ end
96
+ end
97
+
98
+ def determine_device!
99
+ devices = %w[Android iOS].freeze
100
+ @device = choose "选择推送设备:", *devices
101
+ end
102
+
103
+ def determine_secret!
104
+ @secret ||= ask '小米应用密钥:'
105
+ end
106
+
107
+ def determine_channel!(options)
108
+ channles = %w[regid alias topic].freeze
109
+ @channel = channles.select { |k| options.__hash__.has_key?k.to_sym }
110
+
111
+ unless @channel.count > 0
112
+ @channel = choose "选择推送方式:", *channles
113
+
114
+ @channel_id = ask "输入 #{@channel} 的值:"
115
+ else
116
+ @channel = @channel[0]
117
+ @channel_id = options.__hash__[@channel.to_sym]
118
+ end
119
+ end
120
+ end
@@ -2,8 +2,17 @@ module Xiaomi
2
2
  module Push
3
3
  module Services
4
4
  class Feedback
5
+
6
+ attr_reader :context
7
+
5
8
  def initialize(context)
9
+ @context = context
10
+ end
6
11
 
12
+ def invalid
13
+ url = 'https://feedback.xmpush.xiaomi.com/v1/feedback/fetch_invalid_regids'
14
+ r = RestClient.get url, @context.header
15
+ data = MultiJson.load r
7
16
  end
8
17
  end
9
18
  end
@@ -58,6 +58,8 @@ module Xiaomi
58
58
  end
59
59
  end
60
60
 
61
+ private
62
+
61
63
  def fetch_message_type(data)
62
64
  type, value = nil
63
65
  MESSAGE_TYPE.select do |k,v|
@@ -2,9 +2,41 @@ module Xiaomi
2
2
  module Push
3
3
  module Services
4
4
  class Topic
5
+ attr_reader :context
5
6
  def initialize(context)
7
+ @context = context
8
+ end
9
+
10
+ def subscribe(**options)
11
+ url, params = prepare_params(__method__.to_s, options)
12
+ data = @context.request url, params
13
+ end
6
14
 
15
+ def unsubscribe(**options)
16
+ url, params = prepare_params(__method__.to_s, options)
17
+ data = @context.request url, params
7
18
  end
19
+
20
+ private
21
+ def prepare_params(uri, options)
22
+ if options.has_key?(:reg_id)
23
+ type = "registration_id"
24
+ value = options[:reg_id]
25
+ url = @context.build_uri("topic/#{uri}")
26
+ elsif options.has_key?(:alias)
27
+ type = "aliases"
28
+ value = options[:alias]
29
+ url = @context.build_uri("topic/#{uri}/alias")
30
+ end
31
+
32
+ params = {
33
+ type.to_sym => value,
34
+ :topic => options[:name],
35
+ :category => options[:category],
36
+ }
37
+
38
+ [url, params]
39
+ end
8
40
  end
9
41
  end
10
42
  end
@@ -1,5 +1,5 @@
1
1
  module Xiaomi
2
2
  module Push
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xiaomi-push
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - icyleaf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -139,11 +139,13 @@ files:
139
139
  - bin/xmp
140
140
  - lib/xiaomi/push.rb
141
141
  - lib/xiaomi/push/client.rb
142
+ - lib/xiaomi/push/commands.rb
143
+ - lib/xiaomi/push/commands/feedback.rb
144
+ - lib/xiaomi/push/commands/message.rb
142
145
  - lib/xiaomi/push/const.rb
143
146
  - lib/xiaomi/push/devices/android.rb
144
147
  - lib/xiaomi/push/devices/ios.rb
145
148
  - lib/xiaomi/push/error.rb
146
- - lib/xiaomi/push/services/alias.rb
147
149
  - lib/xiaomi/push/services/feedback.rb
148
150
  - lib/xiaomi/push/services/message.rb
149
151
  - lib/xiaomi/push/services/messages/android.rb
@@ -1,11 +0,0 @@
1
- module Xiaomi
2
- module Push
3
- module Services
4
- class Alias
5
- def initialize(context)
6
-
7
- end
8
- end
9
- end
10
- end
11
- end