umeng_msg 0.1.0

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: 919287fad5c9975364d033e065d22d79b58d6b4a
4
+ data.tar.gz: 0f8a8cf8eaa52d3dfbbefb1ebf9030e0939196e5
5
+ SHA512:
6
+ metadata.gz: 5f5f3b4c50583a37a9f392d9d6061a7c823facc96257e9946ad641e7e320a70d21f7d59e95dd3611d230e83236a6e3f696b56864c2f344e62f224b5f77459992
7
+ data.tar.gz: 8bead2b2bf288c219eb5c72bfe21258a9435d905d53403b3fdf9ee2f0b0902e5945773fcb0fc31372891fc1ca8e15e9115f7f67389816eac784857bafe3c4883
data/.gitignore ADDED
@@ -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
data/.travis.yml ADDED
@@ -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_msg.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # UmengMsg
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/umeng_msg`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'umeng_msg'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install umeng_msg
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/umeng_msg/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 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_msg"
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 UmengMsg
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "This generator creates a umeng_msg configuratio file at config/initializers"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+ def copy_initializer
7
+ template '../templates/umeng_msg.rb', 'config/initializers/umeng_msg.rb'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ if Rails.env.production?
2
+ UmengMsg.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
+ UmengMsg.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,14 @@
1
+ # coding: utf-8
2
+ module UmengMsg
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,20 @@
1
+ module UmengMsg
2
+ module Logger
3
+ def logger_file
4
+ Rails.root.join("log", "umeng_push.log")
5
+ end
6
+
7
+ def error_file
8
+ Rails.root.join("log", "error.log")
9
+ end
10
+
11
+ def umeng_logger
12
+ @logger ||= Yell.new do |l|
13
+ l.level = 'gte.info'
14
+
15
+ l.adapter :datefile, logger_file, level: 'lte.error', keep: 5
16
+ l.adapter :datefile, error_file, level: 'gte.error', keep:5
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,102 @@
1
+ # coding: utf-8
2
+ module UmengMsg
3
+ module Params
4
+ extend self
5
+
6
+ def push_params(platform, **options)
7
+ params = {
8
+ 'appkey' => UmengMsg.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
+ 'file_id' => options['file_id'],
14
+ 'policy' => {
15
+ 'start_time' => options['start_time'],
16
+ 'expire_time' => options['expire_time'],
17
+ 'max_send_num' => options['max_send_num'],
18
+ },
19
+ 'production_mode' => UmengMsg.production_mode,
20
+ 'description' => options['description'],
21
+ 'thirdparty_id' => options['thirdparty_id']
22
+ }
23
+
24
+ # 平台参数
25
+ ios_payload = {
26
+ 'payload' => {
27
+ 'aps' => {
28
+ 'alert' => options['alert'],
29
+ 'badge' => options['badge'],
30
+ 'sound' => options['sound'],
31
+ 'content-available' => options['content-available'],
32
+ 'category' => options['category']
33
+ },
34
+ }
35
+ }
36
+ android_payload = {
37
+ 'payload' => {
38
+ 'display_type' => options['display_type'],
39
+ 'body' => {
40
+ 'ticker' => options['ticker'],
41
+ 'title' => options['title'],
42
+ 'text' => options['text'],
43
+ 'icon' => options['icon'],
44
+ 'largeIcon' => options['largeIcon'],
45
+ 'img' => options['img'],
46
+ 'sound' => options['sound'],
47
+ 'builder_id' => (options['builder_id'] || 0),
48
+ 'play_vibrate' => (options['play_vibrate'] || 'true'),
49
+ 'play_lights' => (options['play_lights'] || 'true'),
50
+ 'play_sound' => (options['play_sound'] || 'true'),
51
+ 'after_open' => (options['after_open'] || 'go_app'),
52
+ 'url' => options['url'],
53
+ 'activity' => options['activity'],
54
+ 'custom' => options['custom']
55
+ },
56
+ 'extra' => {
57
+ }
58
+ }
59
+
60
+ }
61
+
62
+ # TODO 组播过滤
63
+ # if type == "groupcast"
64
+ # tag_list = targets.split(",")
65
+ # tag_list.map! { |tag| {tag: tag} }
66
+ # params = {
67
+ # filter: {
68
+ # where: {
69
+ # "and" => [{ "or" => tag_list }]
70
+ # }
71
+ # }
72
+ # }.merge(params)
73
+ # end
74
+
75
+ platform.downcase == 'ios' ? params.merge(ios_payload) : params.merge(android_payload)
76
+ end
77
+
78
+ def check_params(platform, task_id)
79
+ {
80
+ 'appkey' => UmengMsg.appkey(platform),
81
+ 'timestamp' => Time.now.to_i.to_s,
82
+ 'task_id' => task_id
83
+ }
84
+ end
85
+ def cancel_params(platform, task_id)
86
+ {
87
+ 'appkey' => UmengMsg.appkey(platform),
88
+ 'timestamp' => Time.now.to_i.to_s,
89
+ 'task_id' => task_id
90
+ }
91
+ end
92
+
93
+ def upload_params(platform, content)
94
+ {
95
+ 'appkey' => UmengMsg.appkey(platform),
96
+ 'timestamp' => Time.now.to_i.to_s,
97
+ 'content' => content
98
+ }
99
+ end
100
+ end
101
+ end
102
+
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ require 'digest/md5'
3
+
4
+ module UmengMsg
5
+ module Sign
6
+ def self.generate(platform, url, payload)
7
+ Digest::MD5.hexdigest('POST' + url + payload.to_json + UmengMsg.app_master_secret(platform))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,91 @@
1
+ # coding: utf-8
2
+ require 'rest-client'
3
+
4
+ module UmengMsg
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
+ @payload = Params.push_params(platform, **options)
16
+ @content = options['content']
17
+ @file_id, @task_id = nil
18
+ @ret = { push: nil, check: nil, cancel: nil, upload: nil }
19
+ @error_code = { push: nil, check: nil, cancel: nil, upload: nil }
20
+ end
21
+
22
+ def push
23
+ sign = Sign.generate @platform, PUSH_URL, @payload
24
+ begin
25
+ parse_res RestClient.post("#{PUSH_URL}?sign=#{sign}", @payload.to_json, content_type: :json, accept: :json)
26
+ rescue => e
27
+ parse_res e.response
28
+ end
29
+ end
30
+
31
+ def check
32
+ @check_payload = Params.check_params(@platform, @task_id)
33
+ sign = Sign.generate @platform, CHECK_URL, @check_payload
34
+ begin
35
+ parse_res RestClient.post("#{CHECK_URL}?sign=#{sign}", @check_payload.to_json, content_type: :json, accept: :json)
36
+ rescue => e
37
+ parse_res e.response
38
+ end
39
+ end
40
+
41
+ def cancel
42
+ @cancel_payload = Params.cancel_params(@platform, @task_id)
43
+ sign = Sign.generate @platform, CANCEL_URL, @cancel_payload
44
+ begin
45
+ parse_res RestClient.post("#{CANCEL_URL}?sign=#{sign}", @cancel_payload.to_json, content_type: :json, accept: :json)
46
+ rescue => e
47
+ parse_res e.response
48
+ end
49
+ end
50
+
51
+ def upload
52
+ @upload_payload = Params.upload_params(@platform, @content)
53
+ sign = Sign.generate @platform, CANCEL_URL, @upload_payload
54
+ begin
55
+ parse_res RestClient.post("#{UPLOAD_URL}?sign=#{sign}", @upload_payload.to_json, content_type: :json, accept: :json)
56
+ rescue => e
57
+ parse_res e.response
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def parse_res response
64
+ res_hsh = JSON.parse(response)
65
+ @ret[:push] = res_hsh['ret']
66
+ @error_code[:push] = res_hsh['data']['error_code']
67
+ @task_id = res_hsh['data']['task_id']
68
+ end
69
+
70
+ def check_parse
71
+ res_hsh = JSON.parse(response)
72
+ @ret[:check] = res_hsh['ret']
73
+ @error_code[:check] = res_hsh['data']['error_code']
74
+ @check_data = res_hsh['data']
75
+ end
76
+
77
+ def cancel_parse
78
+ res_hsh = JSON.parse(response)
79
+ @ret[:cancel] = res_hsh['ret']
80
+ @error_code[:cancel] = res_hsh['data']['error_code']
81
+ end
82
+
83
+ def upload_parse response
84
+ res_hsh = JSON.parse(response)
85
+ @ret[:upload] = res_hsh['ret']
86
+ @error_code[:upload] = res_hsh['data']['error_code']
87
+ @file_id = res_hsh['data']['file_id']
88
+ end
89
+ end
90
+ end
91
+
@@ -0,0 +1,3 @@
1
+ module UmengMsg
2
+ VERSION = "0.1.0"
3
+ end
data/lib/umeng_msg.rb ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ require "umeng_msg/version"
3
+ require "umeng_msg/configuration"
4
+ require "umeng_msg/subject"
5
+ require 'umeng_msg/logger'
6
+ require 'umeng_msg/params'
7
+ require 'umeng_msg/sign'
8
+
9
+ module UmengMsg
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.reset
16
+ @Configuration = Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield configuration
21
+ end
22
+
23
+ def self.appkey platform
24
+ platform.downcase == 'ios' ? configuration.ios_appkey : configuration.android_appkey
25
+ end
26
+
27
+ def self.app_master_secret platform
28
+ platform.downcase == 'ios' ? configuration.ios_app_master_secret : configuration.android_app_master_secret
29
+ end
30
+
31
+ def self.production_mode
32
+ configuration.production_mode
33
+ end
34
+
35
+ end
data/umeng_msg.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'umeng_msg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "umeng_msg"
8
+ spec.version = UmengMsg::VERSION
9
+ spec.authors = ["hzlu"]
10
+ spec.email = ["hzlu2010@163.com"]
11
+
12
+ spec.summary = "The encapsulation of umeng message."
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/hzlu/umeng_msg"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_runtime_dependency "rest-client"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.9"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec"
34
+ spec.add_development_dependency "pry-byebug"
35
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: umeng_msg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hzlu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
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 message.
84
+ email:
85
+ - hzlu2010@163.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/generators/umeng_msg/install_generator.rb
99
+ - lib/generators/umeng_msg/templates/umeng_msg.rb
100
+ - lib/umeng_msg.rb
101
+ - lib/umeng_msg/configuration.rb
102
+ - lib/umeng_msg/logger.rb
103
+ - lib/umeng_msg/params.rb
104
+ - lib/umeng_msg/sign.rb
105
+ - lib/umeng_msg/subject.rb
106
+ - lib/umeng_msg/version.rb
107
+ - umeng_msg.gemspec
108
+ homepage: https://github.com/hzlu/umeng_msg
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: The encapsulation of umeng message.
131
+ test_files: []
132
+ has_rdoc: