umeng_message 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f91ccbb07959a9ff8d313aac42e383a080ad46954d3803a4936145b2826a2065
4
+ data.tar.gz: ab4a8c4f34340fde60b4e2e478297fba6977f436a212175f164dbf08a8960c8f
5
+ SHA512:
6
+ metadata.gz: bc02148dce91410314becb360f42aaa7d018236ac12683f2895343fc3b9dd566ffe302a2525f101ec02cb0b6e6d0c6dbb31e92cbc5c007a8f665d9df34dbc228
7
+ data.tar.gz: 8534feded8f11c5daa4d8853564df872c6a36604aff2a6af234fb471618270078e9c9ee0ecf425310e340fd21848edfbf9b5081d3e01938d72ce61988569a32b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in umeng_message.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ copyright (c) <year> <copyright holders>
2
+
3
+
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
+
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # UmengMessage
2
+
3
+ 对[友盟](https://www.umeng.com/)消息推送的接口封装,支持接口:
4
+
5
+ - 消息发送
6
+ - 任务类消息状态查询
7
+ - 任务类消息取消
8
+ - 文件上传
9
+
10
+ ## 安装
11
+
12
+ Gemfile添加
13
+
14
+ gem 'umeng_message'
15
+
16
+ 然后执行
17
+
18
+ $ bundle
19
+
20
+ ## 使用
21
+
22
+ ### 生成配置文件
23
+
24
+ rails generate umeng_message:install
25
+
26
+ ### 推送、上传、查询、取消
27
+
28
+ UmengMessage::Subject.new('ios', options).push
29
+ UmengMessage::Subject.new('ios', options).upload
30
+ UmengMessage::Subject.new('ios', options).check
31
+ UmengMessage::Subject.new('ios', options).cancel
32
+
33
+ ### `options` 参数
34
+
35
+ - check 与 cancel 只需要 `task_id` 参数, upload 只需要 `content` 参数
36
+ - 示例: `{'task_id': 'xxxxx'}`
37
+ - 详细 `options` 参数请查看[官方文档](https://developer.umeng.com/docs/66632/detail/68343)
38
+
39
+ ### 返回结果
40
+
41
+ - 返回结果以 `error_code` 为 `nil` 表示发送成功, `error_code` 为 `999` 时为网络等错误与友盟无关
42
+ - 拓展或自定义功能请自行拓展
43
+ - 默认请求超时是3秒
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/hzlu/umeng_message/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "umeng_message"
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
@@ -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 UmengMessage
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "This generator creates a umeng_message configuratio file at config/initializers"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+ def copy_initializer
7
+ template '../templates/umeng_message.rb', 'config/initializers/umeng_message.rb'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ if Rails.env.production?
2
+ UmengMessage.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
+ UmengMessage.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,34 @@
1
+ # coding: utf-8
2
+ require "umeng_message/version"
3
+ require "umeng_message/configuration"
4
+ require 'umeng_message/params'
5
+ require 'umeng_message/sign'
6
+ require "umeng_message/subject"
7
+
8
+ module UmengMessage
9
+
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.reset
15
+ @Configuration = Configuration.new
16
+ end
17
+
18
+ def self.configure
19
+ yield configuration
20
+ end
21
+
22
+ def self.appkey platform
23
+ platform.downcase == 'ios' ? configuration.ios_appkey : configuration.android_appkey
24
+ end
25
+
26
+ def self.app_master_secret platform
27
+ platform.downcase == 'ios' ? configuration.ios_app_master_secret : configuration.android_app_master_secret
28
+ end
29
+
30
+ def self.production_mode
31
+ configuration.production_mode
32
+ end
33
+
34
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+ module UmengMessage
3
+ class Configuration
4
+ attr_accessor :ios_appkey, :ios_app_master_secret, :android_appkey, :android_app_master_secret, :production_mode
5
+
6
+ def initialize
7
+ @ios_appkey = 'ios_appkey'
8
+ @ios_app_master_secret = 'ios_secret'
9
+ @android_appkey = 'android_appkey'
10
+ @android_app_master_secret = 'android_secret'
11
+ @production_mode = 'false'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,119 @@
1
+ # coding: utf-8
2
+ module UmengMessage
3
+ module Params
4
+ extend self
5
+
6
+ def push_params(platform, options)
7
+ params = {
8
+ 'appkey' => UmengMessage.appkey(platform),
9
+ 'timestamp' => Time.now.to_i.to_s,
10
+ 'type' => options['type'],
11
+ 'device_tokens' => options['device_tokens'],
12
+ 'alias_type' => options['alias_type'],
13
+ 'alias' => options['alias'],
14
+ 'file_id' => options['file_id'],
15
+ 'policy' => {
16
+ 'start_time' => options['start_time'],
17
+ 'expire_time' => options['expire_time'],
18
+ 'max_send_num' => options['max_send_num'],
19
+ },
20
+ 'production_mode' => UmengMessage.production_mode,
21
+ 'description' => options['description'],
22
+ 'thirdparty_id' => options['thirdparty_id']
23
+ }
24
+
25
+ # 平台参数
26
+ ios_payload = {
27
+ 'payload' => {
28
+ 'aps' => {
29
+ 'alert' => options['alert'],
30
+ 'badge' => options['badge'],
31
+ 'sound' => options['sound'],
32
+ 'content-available' => options['content-available'],
33
+ 'category' => options['category']
34
+ },
35
+ 'title' => options['title'],
36
+ 'extra' => options['extra'] || {},
37
+ }
38
+ }
39
+
40
+ if(options['screen'].present?)
41
+ ios_payload['payload'].merge!({'screen' => options['screen']})
42
+ end
43
+ if options['params'].present?
44
+ ios_payload['payload'].merge!({'params' => options['params']})
45
+ end
46
+
47
+ android_payload = {
48
+ 'payload' => {
49
+ 'display_type' => options['display_type'],
50
+ 'body' => {
51
+ 'ticker' => options['ticker'],
52
+ 'title' => options['title'],
53
+ 'text' => options['text'],
54
+ 'icon' => options['icon'],
55
+ 'largeIcon' => options['largeIcon'],
56
+ 'img' => options['img'],
57
+ 'sound' => options['sound'],
58
+ 'builder_id' => (options['builder_id'] || 0),
59
+ 'play_vibrate' => (options['play_vibrate'] || 'true'),
60
+ 'play_lights' => (options['play_lights'] || 'true'),
61
+ 'play_sound' => (options['play_sound'] || 'true'),
62
+ 'after_open' => (options['after_open'] || 'go_app'),
63
+ 'url' => options['url'],
64
+ 'activity' => options['activity'],
65
+ 'custom' => options['custom']
66
+ },
67
+ 'extra' => options['extra'] || {}
68
+ }
69
+
70
+ }
71
+
72
+ # TODO 组播过滤
73
+ # if type == "groupcast"
74
+ # tag_list = targets.split(",")
75
+ # tag_list.map! { |tag| {tag: tag} }
76
+ # params = {
77
+ # filter: {
78
+ # where: {
79
+ # "and" => [{ "or" => tag_list }]
80
+ # }
81
+ # }
82
+ # }.merge(params)
83
+ # end
84
+
85
+ platform.downcase == 'ios' ? params.merge!(ios_payload) : params.merge!(android_payload)
86
+ params = compact_params(params)
87
+ end
88
+
89
+ def check_params(platform, task_id)
90
+ {
91
+ 'appkey' => UmengMessage.appkey(platform),
92
+ 'timestamp' => Time.now.to_i.to_s,
93
+ 'task_id' => task_id
94
+ }
95
+ end
96
+ def cancel_params(platform, task_id)
97
+ {
98
+ 'appkey' => UmengMessage.appkey(platform),
99
+ 'timestamp' => Time.now.to_i.to_s,
100
+ 'task_id' => task_id
101
+ }
102
+ end
103
+
104
+ def upload_params(platform, content)
105
+ {
106
+ 'appkey' => UmengMessage.appkey(platform),
107
+ 'timestamp' => Time.now.to_i.to_s,
108
+ 'content' => content
109
+ }
110
+ end
111
+
112
+ private
113
+ #取出nil的key 不然友盟解析json出错
114
+ def compact_params(params)
115
+ custom_compact = Proc.new { |k, v| v.delete_if(&custom_compact) if v.kind_of?(Hash); v.blank? }
116
+ params.delete_if &custom_compact
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ require 'digest/md5'
3
+
4
+ module UmengMessage
5
+ module Sign
6
+ def self.generate(platform, url, payload)
7
+ Digest::MD5.hexdigest('POST' + url + payload.to_json + UmengMessage.app_master_secret(platform))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,59 @@
1
+ # coding: utf-8
2
+ require 'httpi'
3
+
4
+ module UmengMessage
5
+ class Subject
6
+ attr_reader :payload, :ret, :task_id, :error_code, :file_id, :platform, :content
7
+ CAST_TYPE = %w|unicast listcast filecast broadcast groupcast customizedcast|
8
+ PUSH_URL = 'http://msg.umeng.com/api/send'
9
+ CHECK_URL = 'http://msg.umeng.com/api/status'
10
+ CANCEL_URL = 'http://msg.umeng.com/api/cancel'
11
+ UPLOAD_URL = 'http://msg.umeng.com/upload'
12
+
13
+ def initialize(platform, options)
14
+ @platform = platform
15
+ @options = options
16
+ end
17
+
18
+ def push
19
+ post_youmeng(PUSH_URL, Params.push_params(@platform, @options))
20
+ end
21
+
22
+ def check
23
+ post_youmeng(CHECK_URL, Params.check_params(@platform, @options['task_id']))
24
+ end
25
+
26
+ def cancel
27
+ post_youmeng(CANCEL_URL, Params.cancel_params(@platform, @options['task_id']))
28
+ end
29
+
30
+ def upload
31
+ post_youmeng(UPLOAD_URL, Params.upload_params(@platform, @options['content']))
32
+ end
33
+
34
+ private
35
+ def post_youmeng(url, payload)
36
+ sign = Sign.generate @platform, url, payload
37
+ begin
38
+ request = HTTPI::Request.new
39
+ request.url = "#{url}?sign=#{sign}"
40
+ request.open_timeout = 3
41
+ request.read_timeout = 3
42
+ request.body = payload.to_json
43
+ response = HTTPI.post(request)
44
+ parse_res(ActiveSupport::JSON.decode(response.body))
45
+ rescue => e
46
+ error_result e.message
47
+ end
48
+ end
49
+
50
+ def parse_res result
51
+ {result: result['ret'], error_code: result['data']['error_code'], data: result['data']}
52
+ end
53
+
54
+ def error_result(error_message)
55
+ {result: error_message, error_code: '999', data: {}}
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module UmengMessage
2
+ VERSION = "1.0.0"
3
+ end
@@ -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 'umeng_message/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'umeng_message'
8
+ spec.version = UmengMessage::VERSION
9
+ spec.authors = ['tl']
10
+ spec.email = ['tianlu1677@gmail.com']
11
+ spec.licenses = ['MIT']
12
+ spec.summary = 'The encapsulation of umeng message!'
13
+ spec.description = '友盟推送Ruby封装'
14
+ spec.homepage = 'https://github.com/hzlu/umeng_message'
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'httpi', '~> 0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.9'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 0'
25
+ spec.add_development_dependency 'pry-byebug', '~> 0'
26
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: umeng_message
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - tl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-19 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: 友盟推送Ruby封装
84
+ email:
85
+ - tianlu1677@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/generators/umeng_message/install_generator.rb
100
+ - lib/generators/umeng_message/templates/umeng_message.rb
101
+ - lib/umeng_message.rb
102
+ - lib/umeng_message/configuration.rb
103
+ - lib/umeng_message/params.rb
104
+ - lib/umeng_message/sign.rb
105
+ - lib/umeng_message/subject.rb
106
+ - lib/umeng_message/version.rb
107
+ - umeng_message.gemspec
108
+ homepage: https://github.com/hzlu/umeng_message
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: The encapsulation of umeng message!
131
+ test_files: []