umeng_push 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec75725da1a80dd8525417c31f0a1f7cb626d70b
4
+ data.tar.gz: 195ffb32fd8984c1a5623c043d222274206dea17
5
+ SHA512:
6
+ metadata.gz: 7749517290d6563f197a7db2c964b0f067b11620d22fd3b443bdfb384e13a8e33d53151a9f58c817c0e08d246dceaefadb7ea607f021f6f10c60799a5ccac37d
7
+ data.tar.gz: af14130cdab8f5e1fb946818612e2df0f21ca1c82981af60ce9026f13e5a4b62d45ef44343be267aeb5a76ce0901396ccee4a4ce4c7c7f423f7012333cb9b8a4
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.bundle/
2
+ /Gemfile.lock
3
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in umeng_push.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 zhanglinjie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # UmengPush
2
+
3
+ 此gem是对友盟推送的api封装,支持推送 查询任务 删除任务
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'umeng_push'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install umeng_push
20
+
21
+ ## Usage
22
+ rails generate umeng_push:install
23
+ UmengPush::Client.iOS.send_message(options)
24
+ UmengPush::Client.android.send_message(options)
25
+ UmengPush::Client.iOS.unicast(device_token, options)
26
+ UmengPush::Client.iOS.listcast(device_tokens, options)
27
+ UmengPush::Client.iOS.broadcast(options)
28
+ UmengPush::Client.iOS.groupcast(filter, options)
29
+ UmengPush::Client.iOS.check_task(task_id)
30
+ UmengPush::Client.iOS.cancel_task(task_id)
31
+ 推送相关options参数请参看官方文档
32
+ 返回结果失败 则抛出错误UmengPush::ResponseError
33
+ 默认请求超时是3秒
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/umeng_push/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "umeng_push"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ module UmengPush
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "This generator creates a umeng_push configuratio file at config/initializers"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+ def copy_initializer
7
+ template '../templates/umeng_push.rb', 'config/initializers/umeng_push.rb'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ if Rails.env.production?
2
+ UmengPush.configure do |config|
3
+ config.ios_appkey = 'ios_appkey'
4
+ config.ios_app_master_secret = 'ios_app_master_secret'
5
+ config.android_appkey = 'android_appkey'
6
+ config.android_app_master_secret = 'android_app_master_secret'
7
+ config.production_mode = 'true'
8
+ end
9
+ else
10
+ UmengPush.configure do |config|
11
+ config.ios_appkey = 'ios_appkey'
12
+ config.ios_app_master_secret = 'ios_app_master_secret'
13
+ config.android_appkey = 'android_appkey'
14
+ config.android_app_master_secret = 'android_app_master_secret'
15
+ config.production_mode = 'false'
16
+ end
17
+ end
@@ -0,0 +1,130 @@
1
+ require 'digest/md5'
2
+ require 'httpi'
3
+ require 'json'
4
+
5
+ module UmengPush
6
+ class Client
7
+ attr_accessor :platform, :appkey, :app_master_secret, :production_mode
8
+ def initialize(platform, appkey, app_master_secret, production_mode=false)
9
+ @platform = platform
10
+ @appkey = appkey
11
+ @app_master_secret = app_master_secret
12
+ @production_mode = production_mode
13
+ end
14
+
15
+ def self.iOS
16
+ Client.new(
17
+ "iOS",
18
+ UmengPush.configuration.ios_appkey,
19
+ UmengPush.configuration.ios_app_master_secret,
20
+ UmengPush.configuration.production_mode
21
+ )
22
+ end
23
+
24
+ def self.android
25
+ Client.new(
26
+ "android",
27
+ UmengPush.configuration.android_appkey,
28
+ UmengPush.configuration.android_app_master_secret,
29
+ UmengPush.configuration.production_mode
30
+ )
31
+ end
32
+
33
+ # 消息发送
34
+ def send_message(params={})
35
+ post("/api/send", params.merge({
36
+ appkey: appkey,
37
+ timestamp: timestamp,
38
+ production_mode: production_mode
39
+ }))
40
+ end
41
+
42
+ # 单播
43
+ def unicast(device_token, params={})
44
+ send_message(params.merge({
45
+ type: "unicast",
46
+ device_tokens: device_token
47
+ }))
48
+ end
49
+
50
+ # 组播
51
+ def listcast(device_tokens=[], params={})
52
+ send_message(params.merge({
53
+ type: "listcast",
54
+ device_tokens: device_tokens.join(",")
55
+ }))
56
+ end
57
+
58
+ # 广播
59
+ def broadcast(params={})
60
+ send_message(params.merge({
61
+ type: "broadcast"
62
+ }))
63
+ end
64
+
65
+ # 组播
66
+ def groupcast(filter={}, params={})
67
+ send_message(params.merge({
68
+ type: "groupcast",
69
+ filter: filter
70
+ }))
71
+ end
72
+
73
+ # 查询任务
74
+ def check_task(task_id)
75
+ post("/api/status", {
76
+ appkey: appkey,
77
+ timestamp: timestamp,
78
+ task_id: task_id
79
+ })
80
+ end
81
+
82
+ # 取消任务
83
+ def cancel_task(task_id)
84
+ post("/api/cancel", {
85
+ appkey: appkey,
86
+ timestamp: timestamp,
87
+ task_id: task_id
88
+ })
89
+ end
90
+
91
+ private
92
+ def post(url, payload)
93
+ params = compact_params(payload)
94
+ sign = sign(url, params)
95
+ response = nil
96
+ begin
97
+ request = HTTPI::Request.new
98
+ request.url = "#{UmengPush::HOST}#{url}?sign=#{sign}"
99
+ request.open_timeout = 3
100
+ request.read_timeout = 3
101
+ request.body = params.to_json
102
+ response = HTTPI.post(request)
103
+ rescue => e
104
+ raise ResponseError.new("0", data: {}, message: e.message)
105
+ end
106
+ response_body_json = JSON.load(response.body)
107
+ check_response!(response_body_json)
108
+ response_body_json
109
+ end
110
+
111
+ def check_response!(response_body_json)
112
+ if response_body_json['ret'] != 'SUCCESS'
113
+ raise ResponseError.new(response_body_json['data']['error_code'], data: response_body_json['data'])
114
+ end
115
+ end
116
+
117
+ def sign(url, payload)
118
+ Digest::MD5.hexdigest('POST' + UmengPush::HOST + url + payload.to_json + app_master_secret)
119
+ end
120
+
121
+ def timestamp
122
+ Time.now.to_i.to_s
123
+ end
124
+
125
+ def compact_params(params)
126
+ custom_compact = Proc.new { |k, v| v.delete_if(&custom_compact) if v.kind_of?(Hash); v.nil? }
127
+ params.delete_if &custom_compact
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,13 @@
1
+ module UmengPush
2
+ class Configuration
3
+ attr_accessor :ios_appkey, :ios_app_master_secret, :android_appkey, :android_app_master_secret, :production_mode
4
+
5
+ def initialize
6
+ @ios_appkey = 'ios_appkey'
7
+ @ios_app_master_secret = 'ios_secret'
8
+ @android_appkey = 'android_appkey'
9
+ @android_app_master_secret = 'android_secret'
10
+ @production_mode = 'false'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,121 @@
1
+ module UmengPush
2
+ class ResponseError < StandardError
3
+ attr_accessor :error_code, :data, :message
4
+ def initialize(error_code, data: {}, message: nil)
5
+ @error_code = error_code
6
+ @data = data
7
+ @message = message || error_code_message
8
+ super(@message)
9
+ end
10
+
11
+ private
12
+ def error_code_message
13
+ {
14
+ "1000" => "请求参数没有appkey",
15
+ "1001" => "请求参数没有payload",
16
+ "1002" => "请求参数payload中没有body",
17
+ "1003" => "display_type为message时,请求参数没有custom",
18
+ "1004" => "请求参数没有display_type",
19
+ "1005" => "img url格式不对,请以https或者http开始",
20
+ "1006" => "sound url格式不对,请以https或者http开始",
21
+ "1007" => "url格式不对,请以https或者http开始",
22
+ "1008" => "display_type为notification时,body中ticker不能为空",
23
+ "1009" => "display_type为notification时,body中title不能为空",
24
+ "1010" => "display_type为notification时,body中text不能为空",
25
+ "1011" => "play_vibrate的值只能为true或者false",
26
+ "1012" => "play_lights的值只能为true或者false",
27
+ "1013" => "play_sound的值只能为true或者false",
28
+ "1014" => "task-id没有找到",
29
+ "1015" => "请求参数中没有device_tokens",
30
+ "1016" => "请求参数没有type",
31
+ "1017" => "production_mode只能为true或者false",
32
+ "1018" => "appkey错误:指定的appkey尚未开通推送服务",
33
+ "1019" => "display_type填写错误",
34
+ "1020" => "应用组中尚未添加应用",
35
+ "2000" => "该应用已被禁用",
36
+ "2001" => "过期时间必须大于当前时间",
37
+ "2002" => "定时发送时间必须大于当前时间",
38
+ "2003" => "过期时间必须大于定时发送时间",
39
+ "2004" => "IP白名单尚未添加, 请到网站后台添加您的服务器IP白名单。",
40
+ "2005" => "该消息不存在",
41
+ "2006" => "validation token错误",
42
+ "2007" => "未对请求进行签名",
43
+ "2008" => "json解析错误",
44
+ "2009" => "请填写alias或者file_id",
45
+ "2010" => "与alias对应的device_tokens为空",
46
+ "2011" => "alias个数已超过50",
47
+ "2012" => "此appkey今天的广播数已超过3次",
48
+ "2013" => "消息还在排队,请稍候再查询",
49
+ "2014" => "消息取消失败,请稍候再试",
50
+ "2015" => "device_tokens个数已超过50",
51
+ "2016" => "请填写filter",
52
+ "2017" => "添加tag失败",
53
+ "2018" => "请填写file_id",
54
+ "2019" => "与此file_id对应的文件不存在",
55
+ "2020" => "服务正在升级中,请稍候再试",
56
+ "2021" => "appkey不存在",
57
+ "2022" => "payload长度过长",
58
+ "2023" => "文件上传失败,请重试",
59
+ "2024" => "限速值必须为正整数",
60
+ "2025" => "aps字段不能为空",
61
+ "2026" => "1分钟内发送任务次数超出3次",
62
+ "2027" => "签名不正确",
63
+ "2028" => "时间戳已过期",
64
+ "2029" => "content内容不能为空",
65
+ "2030" => "launch_from/not_launch_from条件中的日期须小于发送日期",
66
+ "2031" => "filter格式不正确",
67
+ "2032" => "未上传生产证书,请到Web后台上传",
68
+ "2033" => "未上传开发证书,请到Web后台上传",
69
+ "2034" => "证书已过期",
70
+ "2035" => "定时任务证书过期",
71
+ "2036" => "时间戳格式错误",
72
+ "2038" => "文件上传失败",
73
+ "2039" => "时间格式必须是yyyy-MM-dd HH:mm:ss",
74
+ "2040" => "过期时间不能超过7天",
75
+ "3000" => "数据库错误",
76
+ "3001" => "数据库错误",
77
+ "3002" => "数据库错误",
78
+ "3003" => "数据库错误",
79
+ "3004" => "数据库错误",
80
+ "4000" => "系统错误",
81
+ "4001" => "系统忙",
82
+ "4002" => "操作失败",
83
+ "4003" => "appkey格式错误",
84
+ "4004" => "消息类型格式错误",
85
+ "4005" => "msg格式错误",
86
+ "4006" => "body格式错误",
87
+ "4007" => "deliverPolicy格式错误",
88
+ "4008" => "失效时间格式错误",
89
+ "4009" => "单个服务器队列已满",
90
+ "4010" => "设备号格式错误",
91
+ "4011" => "消息扩展字段无效",
92
+ "4012" => "没有权限访问",
93
+ "4013" => "异步发送消息失败",
94
+ "4014" => "appkey和device_tokens不对应",
95
+ "4015" => "没有找到应用信息",
96
+ "4016" => "文件编码有误",
97
+ "4017" => "文件类型有误",
98
+ "4018" => "文件远程地址有误",
99
+ "4019" => "文件描述信息有误",
100
+ "4020" => "device_token有误(注意,友盟的device_token是严格的44位字符串)",
101
+ "4021" => "HSF异步服务超时",
102
+ "4022" => "appkey已经注册",
103
+ "4023" => "服务器网络异常",
104
+ "4024" => "非法访问",
105
+ "4025" => "device-token全部失败",
106
+ "4026" => "device-token部分失败",
107
+ "4027" => "拉取文件失败",
108
+ "5000" => "device_token错误",
109
+ "5001" => "证书不存在",
110
+ "5002" => "p,d是umeng保留字段",
111
+ "5003" => "alert字段不能为空",
112
+ "5004" => "alert只能是String类型",
113
+ "5005" => "device_token格式错误",
114
+ "5006" => "创建socket错误",
115
+ "5007" => "certificate_revoked错误",
116
+ "5008" => "certificate_unkown错误",
117
+ "5009" => "handshake_failure错误",
118
+ }[error_code]
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module UmengPush
2
+ VERSION = "0.0.1"
3
+ end
data/lib/umeng_push.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "umeng_push/version"
2
+ require "umeng_push/configuration"
3
+ require 'umeng_push/response_error'
4
+ require "umeng_push/client"
5
+
6
+ module UmengPush
7
+ HOST = "http://msg.umeng.com"
8
+
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def self.configure
14
+ yield configuration
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'umeng_push/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "umeng_push"
8
+ spec.version = UmengPush::VERSION
9
+ spec.authors = ["zhanglinjie"]
10
+ spec.email = ["zhanglinjie412@gmail.com"]
11
+ spec.license = "MIT"
12
+
13
+ spec.summary = "The encapsulation of umeng push api."
14
+ spec.description = spec.summary
15
+ spec.homepage = "https://github.com/zhanglinjie/umeng_push"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "httpi"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.9"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry-byebug"
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: umeng_push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - zhanglinjie
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpi
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.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: The encapsulation of umeng push api.
84
+ email:
85
+ - zhanglinjie412@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/generators/umeng_push/install_generator.rb
99
+ - lib/generators/umeng_push/templates/umeng_push.rb
100
+ - lib/umeng_push.rb
101
+ - lib/umeng_push/client.rb
102
+ - lib/umeng_push/configuration.rb
103
+ - lib/umeng_push/response_error.rb
104
+ - lib/umeng_push/version.rb
105
+ - umeng_push.gemspec
106
+ homepage: https://github.com/zhanglinjie/umeng_push
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: The encapsulation of umeng push api.
130
+ test_files: []