xmpush 0.0.2
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 +7 -0
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/lib/xmpush/android_builder.rb +23 -0
- data/lib/xmpush/feedback.rb +5 -0
- data/lib/xmpush/http_base.rb +28 -0
- data/lib/xmpush/ios_builder.rb +25 -0
- data/lib/xmpush/message.rb +23 -0
- data/lib/xmpush/stats.rb +5 -0
- data/lib/xmpush/subscription.rb +5 -0
- data/lib/xmpush/targeted_message.rb +5 -0
- data/lib/xmpush/tracer.rb +5 -0
- data/lib/xmpush/version.rb +3 -0
- data/lib/xmpush/xm_resources.rb +34 -0
- data/lib/xmpush.rb +94 -0
- data/xmpush.gemspec +26 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0eb9c801b8cccddda27ce474d7d3701f8a727d0e
|
4
|
+
data.tar.gz: 58524b2cea5b138d96bb6bf61c3c612d3cfb70f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8944f441646d4fae28152f1fb25d1363629dd98bac24f7b860163063b4e77dbde76edb9b6e5eed50506e7c61d9f682da53c8dac3d5182d4adf89b4b4858927e7
|
7
|
+
data.tar.gz: 07fec128fdf247a56eb22fca4eb979edaf750164d77cfdb1147c6760d7cfa349fdaf430953756e7c8f57371a3f1d8942fcb8734c8f13fd30ca149ca1362670b9
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
.DS_Store
|
19
|
+
lib/.DS_Store
|
20
|
+
lib/**/.DS_Store
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
## About
|
2
|
+
|
3
|
+
[XiaoMi push](dev.xiaomi.com) Ruby SDK
|
4
|
+
|
5
|
+
## Usage:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
@xm_service = Xmpush::Service.config do |s|
|
9
|
+
s.sandbox = true
|
10
|
+
s.ios_secret = "ios secret key"
|
11
|
+
s.bundle_id = "com.xxx.ios"
|
12
|
+
s.android_secret = 'android secret key'
|
13
|
+
s.package_name = "com.xxx.android"
|
14
|
+
s.connection_adapter = :net_http # default
|
15
|
+
end
|
16
|
+
|
17
|
+
# just iOS
|
18
|
+
message1 = {description: 'hello'}
|
19
|
+
@message1 = @xm_service.build(:ios, message1)
|
20
|
+
@xm_service.push(:all, @message1)
|
21
|
+
|
22
|
+
message2 = {description: 'hello alias'}
|
23
|
+
@message2 = @xm_service.build(:ios, message2)
|
24
|
+
|
25
|
+
@xm_service.push(:alias, @message2, alias: '1043478')
|
26
|
+
|
27
|
+
# iOS and Android
|
28
|
+
message3 = {description: 'push to iOS and Android client'}
|
29
|
+
@message2 = @xm_service.build(message3)
|
30
|
+
@xm_service.push(:all, @message3)
|
31
|
+
|
32
|
+
```
|
33
|
+
|
34
|
+
## TODO
|
35
|
+
|
36
|
+
- Android push
|
37
|
+
- feedback/ stats/ tracer/ targeted_message
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Xmpush
|
2
|
+
class AndroidBuilder < Message
|
3
|
+
|
4
|
+
# 设置通知类型
|
5
|
+
NOTIFY_TYPE_DEFAULT_ALL = -1
|
6
|
+
NOTIFY_TYPE_DEFAULT_SOUND = 1 # 使用默认提示音提示
|
7
|
+
NOTIFY_TYPE_DEFAULT_VIBRATE = 2 # 使用默认震动提示
|
8
|
+
NOTIFY_TYPE_DEFAULT_LIGHTS = 4 # 使用默认led灯光提示
|
9
|
+
|
10
|
+
NTF_CENTER_NTF = 0 # 通知栏消息
|
11
|
+
PASS_THROUGH_NTF = 1 # 透传消息
|
12
|
+
|
13
|
+
attr_accessor :pass_through, :notify_id
|
14
|
+
|
15
|
+
def initialize(**message)
|
16
|
+
@pass_through = message.delete(:pass_through) || 0
|
17
|
+
@notify_id = message.delete(:notify_id) || 0
|
18
|
+
super(message)
|
19
|
+
# @extra = {sound_url: sound_url, badge: badge}
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
module Xmpush
|
5
|
+
class HttpBase
|
6
|
+
attr_accessor :secret_key, :url, :host
|
7
|
+
|
8
|
+
def initialize(host="", secret_key="", connection_adapter = :net_http, headers={})
|
9
|
+
@host = host
|
10
|
+
@secret_key = secret_key
|
11
|
+
@connection_adapter = connection_adapter
|
12
|
+
@headers = headers
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(url, body={}); conn.post url, body end
|
16
|
+
def get(url, params={}); conn.get url, params end
|
17
|
+
|
18
|
+
def conn
|
19
|
+
headers = {authorization: "key=#{@secret_key}"}.merge(@headers)
|
20
|
+
Faraday.new(:url => @host, :headers => headers) do |faraday|
|
21
|
+
faraday.request :url_encoded
|
22
|
+
faraday.response :logger
|
23
|
+
faraday.adapter @connection_adapter
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Xmpush
|
2
|
+
class IosBuilder < Message
|
3
|
+
attr_accessor :sound_url, :badge
|
4
|
+
|
5
|
+
def initialize(**message)
|
6
|
+
sound_url = message.delete(:sound_url) || ""
|
7
|
+
badge = message.delete(:badge) || 1
|
8
|
+
super(message)
|
9
|
+
@extra = {sound_url: sound_url, badge: badge}
|
10
|
+
end
|
11
|
+
|
12
|
+
def build
|
13
|
+
if @extra
|
14
|
+
extra_message = extra(@extra)
|
15
|
+
end
|
16
|
+
message = {
|
17
|
+
restricted_package_name: @restricted_package_name,
|
18
|
+
description: @description
|
19
|
+
}
|
20
|
+
message.merge!(extra_message)
|
21
|
+
return message
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Xmpush
|
2
|
+
class Message
|
3
|
+
EXTRA_PREFIX = 'extra.'
|
4
|
+
attr_accessor :payload, :title, :description, :time_to_live, :time_to_send, :extra, :notify_type, :restricted_package_name
|
5
|
+
|
6
|
+
def initialize(payload: "", restricted_package_name: "", title: "", description: "", time_to_live: "", time_to_send: "", extra: "", notify_type: "")
|
7
|
+
@payload = payload
|
8
|
+
@restricted_package_name = restricted_package_name
|
9
|
+
@title = title
|
10
|
+
@description = description
|
11
|
+
@time_to_live = time_to_live
|
12
|
+
@time_to_send = time_to_send
|
13
|
+
@extra = extra
|
14
|
+
@notify_type = notify_type
|
15
|
+
end
|
16
|
+
|
17
|
+
def extra(options={})
|
18
|
+
options.keys.inject({}) do |h, key|
|
19
|
+
h["#{EXTRA_PREFIX}#{key}"] = options.delete(key); h
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/xmpush/stats.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Xmpush
|
2
|
+
class XmResource
|
3
|
+
|
4
|
+
# Xmpush::XmResource::OFFICIAL_DOMAIN
|
5
|
+
|
6
|
+
OFFICIAL_DOMAIN = "https://api.xmpush.xiaomi.com"
|
7
|
+
SANDBOX_DOMAIN = "https://sandbox.xmpush.xiaomi.com"
|
8
|
+
REG_URL = "/v2/message/regid"
|
9
|
+
ALIAS_URL = "/v2/message/alias"
|
10
|
+
TOPIC_URL = "/v2/message/topic"
|
11
|
+
MTOPIC_URL = "/v2/message/multi_topic"
|
12
|
+
ALL_URL = "/v2/message/all"
|
13
|
+
|
14
|
+
SUBSCRIBE_URL= "/v2/topic/subscribe"
|
15
|
+
UNSUBSCRIBE_URL = "/v2/topic/unsubscribe"
|
16
|
+
|
17
|
+
MMREG_URL = "/v2/multi_messages/regids"
|
18
|
+
MMALIAS_URL = "/v2/multi_messages/aliases"
|
19
|
+
|
20
|
+
STATS_URL = '/v1/stats/message/counters'
|
21
|
+
TRACE_URL = '/v1/trace/message/status'
|
22
|
+
TRACES_URL = '/v1/trace/messages/status'
|
23
|
+
VALIDATE_REGIDS = '/v1/validation/regids'
|
24
|
+
|
25
|
+
|
26
|
+
FETCH_INVALID_REGIDS_URL = "https://feedback.xmpush.xiaomi.com/v1/feedback/fetch_invalid_regids"
|
27
|
+
|
28
|
+
UNION = 'UNION'
|
29
|
+
INTERSECTION = 'INTERSECTION'
|
30
|
+
EXCEPT = 'EXCEPT'
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/xmpush.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'xmpush/xm_resources'
|
2
|
+
require 'xmpush/http_base'
|
3
|
+
require 'xmpush/message'
|
4
|
+
require 'xmpush/ios_builder'
|
5
|
+
require 'xmpush/android_builder'
|
6
|
+
require 'xmpush/targeted_message'
|
7
|
+
require 'xmpush/result'
|
8
|
+
require 'xmpush/stats'
|
9
|
+
require 'xmpush/subscription'
|
10
|
+
require 'xmpush/tracer'
|
11
|
+
require 'xmpush/feedback'
|
12
|
+
|
13
|
+
|
14
|
+
module Xmpush
|
15
|
+
class Service
|
16
|
+
|
17
|
+
attr_accessor :ios_secret, :android_secret, :bundle_id, :package_name, :sandbox
|
18
|
+
|
19
|
+
def initialize(ios_secret: "", android_secret: "", bundle_id: "", package_name: "", connection_adapter: :net_http, sandbox: false)
|
20
|
+
@sandbox = sandbox
|
21
|
+
@ios_secret = ios_secret
|
22
|
+
@bundle_id = bundle_id
|
23
|
+
@android_secret = android_secret
|
24
|
+
@package_name = package_name
|
25
|
+
@connection_adapter = connection_adapter
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.config
|
29
|
+
yield service = new; service
|
30
|
+
end
|
31
|
+
|
32
|
+
def build(build_type, message={})
|
33
|
+
case build_type
|
34
|
+
when :ios
|
35
|
+
build_message = { ios: ios_builder(message)}
|
36
|
+
when :android
|
37
|
+
build_message = { android: android_builder(message) }
|
38
|
+
else
|
39
|
+
build_message = { ios: ios_builder(message), android: android_builder(message)}
|
40
|
+
end
|
41
|
+
return build_message
|
42
|
+
end
|
43
|
+
|
44
|
+
def push(push_type, message, options={})
|
45
|
+
case push_type
|
46
|
+
when :alias
|
47
|
+
return "must input alias" unless options[:alias]
|
48
|
+
message.values.each{|v| v.merge!(alias: options[:alias])}
|
49
|
+
resource_post('alias', message)
|
50
|
+
when :regid
|
51
|
+
return "must input regid" unless options[:regid]
|
52
|
+
message.values.each{|v| v.merge!(regid: options[:regid])}
|
53
|
+
resource_post('regid', message)
|
54
|
+
when :topic
|
55
|
+
return "must input topic" unless options[:topic]
|
56
|
+
message.values.each{|v| v.merge!(topic: options[:topic])}
|
57
|
+
resource_post('topic', message)
|
58
|
+
else #:all
|
59
|
+
resource_post('all', message)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def ios_builder(message={})
|
66
|
+
message[:restricted_package_name] = @bundle_id
|
67
|
+
IosBuilder.new(message).build
|
68
|
+
end
|
69
|
+
|
70
|
+
def android_builder(message={})
|
71
|
+
message[:restricted_package_name] = @bundle_id
|
72
|
+
AndroidBuilder.new(message).build
|
73
|
+
end
|
74
|
+
|
75
|
+
def ios_http
|
76
|
+
host = @sandbox ? Xmpush::XmResource::SANDBOX_DOMAIN : Xmpush::XmResource::OFFICIAL_DOMAIN
|
77
|
+
Xmpush::HttpBase.new(host, @ios_secret, @connection_adapter)
|
78
|
+
end
|
79
|
+
|
80
|
+
def android_http
|
81
|
+
return "android don't suppot sandbox" if @sandbox
|
82
|
+
host = @sandbox ? Xmpush::XmResource::SANDBOX_DOMAIN : Xmpush::XmResource::OFFICIAL_DOMAIN
|
83
|
+
Xmpush::HttpBase.new(host, @android_secret, @connection_adapter)
|
84
|
+
end
|
85
|
+
|
86
|
+
def resource_post(name, message)
|
87
|
+
ios_result = ios_http.post(Xmpush::XmResource.const_get("#{name.upcase}_URL"), message[:ios]) if message[:ios]
|
88
|
+
puts "====== ios_result: #{ios_result.inspect}"
|
89
|
+
android_result = android_http.post(Xmpush::XmResource.const_get("#{name.upcase}_URL"), message[:android]) if message[:android]
|
90
|
+
puts "====== android_result: #{android_result.inspect}"
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
data/xmpush.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'xmpush/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.platform = Gem::Platform::RUBY
|
8
|
+
spec.name = "xmpush"
|
9
|
+
spec.version = Xmpush::VERSION
|
10
|
+
spec.authors = ["blackanger"]
|
11
|
+
spec.email = ["blackanger.z@gmail.com"]
|
12
|
+
spec.summary = %q{XiaoMi push Ruby Server SDK.}
|
13
|
+
spec.description = %q{XiaoMi push Ruby Server SDK}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "faraday"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xmpush
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- blackanger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: XiaoMi push Ruby Server SDK
|
56
|
+
email:
|
57
|
+
- blackanger.z@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/xmpush.rb
|
68
|
+
- lib/xmpush/android_builder.rb
|
69
|
+
- lib/xmpush/feedback.rb
|
70
|
+
- lib/xmpush/http_base.rb
|
71
|
+
- lib/xmpush/ios_builder.rb
|
72
|
+
- lib/xmpush/message.rb
|
73
|
+
- lib/xmpush/stats.rb
|
74
|
+
- lib/xmpush/subscription.rb
|
75
|
+
- lib/xmpush/targeted_message.rb
|
76
|
+
- lib/xmpush/tracer.rb
|
77
|
+
- lib/xmpush/version.rb
|
78
|
+
- lib/xmpush/xm_resources.rb
|
79
|
+
- xmpush.gemspec
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: XiaoMi push Ruby Server SDK.
|
104
|
+
test_files: []
|