mqtt_push 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe5d8e50f135ceca31d6760b6f6d2bd55da8f69d
4
+ data.tar.gz: a0143d55594624f5ca04bcd56db4e7429de3d528
5
+ SHA512:
6
+ metadata.gz: d3c6bfa05e4605bd0ec4d0f65e8515763fa77d758035d7922e31e5a67f77be288e43f30226152cb85ef62c675dbe6a919ce187237a53126b6abfb26c669eb757
7
+ data.tar.gz: 3570fa8922c2e1f9ac00d0079f605963f6f27a6b9258ee1e0820351062a75436ea43a8fba01d6232399c14b5dcf3b33dfbecf2cb53cf61a3c0b728e96b91aa28
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/
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mr.bestjane@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport'
4
+
5
+ gem 'mqtt'
6
+
7
+ # Specify your gem's dependencies in mqtt_push.gemspec
8
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # MqttPush
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/mqtt_push`. 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 'mqtt_push'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mqtt_push
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. You can also 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`, which will 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
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mqtt_push. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mqtt_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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/mqtt_push.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "mqtt_push/version"
2
+
3
+ module MqttPush
4
+ ## 推送消息辅助方法
5
+ # 1. 可自定义can_push?和push_message_json_data method
6
+ # 可在对应class中通过定义can_push?方法指定推送规则,
7
+ # 如果不需要定义规则则不用定义can_push?
8
+ # 2. push_message_json_data 定义json数据
9
+ PUSH_NOTIFY_APP_KEY = '6727fb0922117931bb65a48ee27dc0d9'
10
+
11
+ def push_notify
12
+ check_auth = self.respond_to?(:can_push?) ? can_push? : true
13
+ return unless check_auth
14
+
15
+ send_mqtt_message = Proc.new do | user |
16
+ begin
17
+ data = push_message_json_data
18
+ Rails.logger.error "mqtt error: data:#{data}, user_id: #{user.id}, usernmae: #{user.username}" unless data && user
19
+ msg = {
20
+ timestamp: Time.now.to_i,
21
+ ticket: user.ticket,
22
+ payload: {
23
+ body: {
24
+ title: data[:title],
25
+ text: data[:text],
26
+ after_open: data[:after_open],
27
+ after_open_page: data[:after_open_page]
28
+ },
29
+ extra: data[:extra],
30
+ display_type: data[:display_type] || 'notification',
31
+ play_sound: data[:play_sound] ||'true'
32
+ }
33
+ }
34
+ # title, text, after_open, after_open_page为加密字段
35
+ sign_json = msg[:payload][:body].merge!(timestamp: msg[:timestamp])
36
+ msg.merge!(sign: sign(sign_json))
37
+ $mqtt.publish(topic(user.id), msg.to_json)
38
+ Rails.logger.info "mqtt push message: topic:#{topic(user.id)}, msg:#{msg.to_json}"
39
+ rescue StandardError => e
40
+ Rails.logger.error "mqtt push message error: #{e}"
41
+ end
42
+ end
43
+ send_mqtt_message.call(user)
44
+ end
45
+
46
+ private
47
+ def sign(msg)
48
+ data = msg.select{ |_, v| v.present? }.sort.map { |k, v| "#{k}=#{v}"}.join('&') + "&key=#{PUSH_NOTIFY_APP_KEY}"
49
+ Digest::MD5.hexdigest(data).upcase
50
+ end
51
+
52
+ def topic(u_id)
53
+ "/users/#{u_id}/message"
54
+ end
55
+
56
+ end
@@ -0,0 +1,3 @@
1
+ module MqttPush
2
+ VERSION = "0.1.0"
3
+ end
data/mqtt_push.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mqtt_push/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mqtt_push"
8
+ spec.version = MqttPush::VERSION
9
+ spec.authors = ["bestjane"]
10
+ spec.email = ["mr.bestjane@gmail.com"]
11
+
12
+ spec.summary = %q{mqtt 消息推送}
13
+ spec.description = %q{一个ruby的mqtt消息推送gem}
14
+ spec.homepage = "https://github.com/bestjane/mqtt_push"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mqtt_push
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bestjane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: 一个ruby的mqtt消息推送gem
42
+ email:
43
+ - mr.bestjane@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/mqtt_push.rb
56
+ - lib/mqtt_push/version.rb
57
+ - mqtt_push.gemspec
58
+ homepage: https://github.com/bestjane/mqtt_push
59
+ licenses: []
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.5.1
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: mqtt 消息推送
81
+ test_files: []