simplepush 0.6.1 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3f6d5879cdeeb658ce4b4b3dc7d08164c9295b62a525428303140b3325de225
4
- data.tar.gz: 84cba61d1dede58df0e4b85d7d55779af272dc3998366cd3fdcbd7a1a7267e76
3
+ metadata.gz: 989b00a03ea614f2d48a5f3935950f7bab1746b8e01bc53c50f5852fe8d4e3bb
4
+ data.tar.gz: 4d3bc2f8436b014e20c3ee046fae76c1a9d37e2b7a978b85952b8e9487e53dfc
5
5
  SHA512:
6
- metadata.gz: 86c8d97a10963067de04ef6ec756d0df1855cd18aa1ece60d6255e6ebf5b97292ac10caf8e3a1fbcd5de1f62d132faa74a9a9e1a6042761905aad1f06491e699
7
- data.tar.gz: b9c2919cd5909f6b77ba83b97ef5f4bc9b1186fe51ba419aac5de7848463a2c26bbeb921acef954d0684065594b55f2cd8aad69e4297a0b80c2646655bdbe78b
6
+ metadata.gz: 6c188383fb492c3f951a11f2680799879309ad9ae49df09fb4db7dcb420afde9ce134fc6173b2900be0274187a4c6379de0b77d70705e3a9476269f10ffaed49
7
+ data.tar.gz: 8f0aaa8cb706bcb301a566ceb0a08d4f9157ec96ff49a21175f03dcaff2d53962f96f4c59e997d83741a5cf9ef3e42d515e64f8ba371ec87e3131bb3d873a0ec
data/.gitignore CHANGED
@@ -6,5 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /example/push.sh
10
9
  /config.yml
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Simplepush Gem for Ruby
2
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/simplepush`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This is a simple httparty wrapper for [SimplePush.io](https://SimplePush.io).
4
4
 
5
5
  ## Installation
6
6
 
@@ -42,22 +42,58 @@ Simplepush.new('<your key>', "<pass>", "<salt>").send("Title", "Message") # Enry
42
42
 
43
43
  ## Asynchronous send
44
44
 
45
- The following does not work... `#TODO`
45
+ See [example/async.rb](example/async.rb)
46
+
47
+ ## Usage in Rails
48
+
49
+ To use in Rails, you need (should?) use a worker gem to avoid that notification calls are blocking your users.
50
+
51
+ For instance using ActiveJob you could define:
46
52
 
47
53
  ```ruby
48
- require 'async' # gem 'async'
54
+ class SimplepushJob < ApplicationJob
55
+ queue_as :default
49
56
 
50
- s = Simplepush.new('<your key>')
57
+ def perform(title, message, event=nil)
51
58
 
52
- Sync do
53
- 100.times do |i|
54
- Async do
55
- s.send("Title", i.to_s) # Unenrypted
59
+ simplepush = Rails.cache.fetch("simplepush", expires_in: 1.hour) do
60
+ cred = Rails.application.credentials.simplepush
61
+ Simplepush.new(cred[:key], cred[:pass], cred[:salt])
56
62
  end
63
+
64
+ simplepush.send(title, message, event)
57
65
  end
58
66
  end
59
67
  ```
60
68
 
69
+ This takes credentials from Rails `credentials.yml.enc`, which you can edit using `rails credentials:edit`. Add the following:
70
+
71
+ ```yml
72
+ simplepush:
73
+ key: <your key>
74
+ pass: <your password>
75
+ salt: <your salt>
76
+ ```
77
+
78
+ In your code you can then dispatch messages to SimplePush using:
79
+
80
+ ```ruby
81
+ SimplepushJob.perform_later("My app", "User #{current_user.email} perform admin action...")
82
+ ```
83
+
84
+ ## Usage with Encryption Notification Gem
85
+
86
+ Since 0.7.0, this Gem provides an integration with the [Exception Notification Gem](https://github.com/smartinez87/exception_notification). To enable this, add the following to your `environment.rb`:
87
+
88
+ ```ruby
89
+ # production.rb
90
+ config.middleware.use ExceptionNotification::Rack, simplepush: {
91
+ title_prefix: "[Crash in #{Rails.application.class.module_parent.name}] "
92
+ }
93
+ ```
94
+
95
+ This depends on the credentials defined above. Exceptions which hit the production are then reported via Simplepush.
96
+
61
97
  ## Example of query
62
98
 
63
99
  The following is a sample of the query as it is produced:
@@ -93,12 +129,13 @@ The following is a sample of the query as it is produced:
93
129
  - [x] Encrypted Messages
94
130
  - [x] Processing responses
95
131
  - [x] Async calls
96
- - [ ] Example how to integrate into rails to notify of failures
132
+ - [x] Example how to integrate into rails to notify of failures
97
133
 
98
134
  ## Changelog
99
135
 
100
136
  - 0.5.0 Initial Commits
101
137
  - 0.6.0 Changing API to cache keys, better examples
138
+ - 0.7.0 Added support for [Exception Notification Gem](https://github.com/smartinez87/exception_notification)
102
139
 
103
140
  ## Contributing
104
141
 
@@ -0,0 +1,131 @@
1
+ # Only load this file if exception_notification Gem is bundled
2
+ return unless Gem.loaded_specs.has_key? 'exception_notification'
3
+
4
+ module ExceptionNotifier
5
+
6
+ class SimplepushNotifier < BaseNotifier
7
+
8
+ attr_reader :client, :default_options
9
+
10
+ def initialize(options)
11
+ cred = Rails.application.credentials.simplepush
12
+ @client = Simplepush.new(cred[:key], cred[:pass], cred[:salt])
13
+ @default_options = options
14
+ end
15
+
16
+ def call(exception, options = {})
17
+ event = SimplepushExceptionEvent.new(exception, options.reverse_merge(default_options))
18
+ @client.send(event.formatted_title, event.formatted_body)
19
+ end
20
+
21
+ #
22
+ # This class is responsible to format title and message from given exception and options.
23
+ #
24
+ # Adapted from https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier/datadog_notifier.rb
25
+ # Version: committed on 27 Dec 2019
26
+ #
27
+ # Released under MIT license: https://github.com/smartinez87/exception_notification/blob/master/MIT-LICENSE
28
+ #
29
+ class SimplepushExceptionEvent
30
+ include ExceptionNotifier::BacktraceCleaner
31
+
32
+ MAX_TITLE_LENGTH = 120
33
+ MAX_VALUE_LENGTH = 300
34
+ MAX_BACKTRACE_SIZE = 3
35
+
36
+ attr_reader :exception,
37
+ :options
38
+
39
+ def initialize(exception, options)
40
+ @exception = exception
41
+ @options = options
42
+ end
43
+
44
+ def request
45
+ @request ||= ActionDispatch::Request.new(options[:env]) if options[:env]
46
+ end
47
+
48
+ def controller
49
+ @controller ||= options[:env] && options[:env]['action_controller.instance']
50
+ end
51
+
52
+ def backtrace
53
+ @backtrace ||= exception.backtrace ? clean_backtrace(exception) : []
54
+ end
55
+
56
+ def title_prefix
57
+ options[:title_prefix] || ''
58
+ end
59
+
60
+ def formatted_title
61
+ title =
62
+ "#{title_prefix}#{controller_subtitle} (#{exception.class}) #{exception.message.inspect}"
63
+
64
+ truncate(title, MAX_TITLE_LENGTH)
65
+ end
66
+
67
+ def formatted_body
68
+ text = []
69
+
70
+ text << formatted_backtrace
71
+ text << formatted_request if request
72
+ text << formatted_session if request
73
+
74
+ text.join("\n------------------\n")
75
+ end
76
+
77
+ def formatted_key_value(key, value)
78
+ "#{key}: #{value}"
79
+ end
80
+
81
+ def formatted_request
82
+ text = []
83
+ text << '# Request'
84
+ text << formatted_key_value('URL', request.url)
85
+ text << formatted_key_value('HTTP Method', request.request_method)
86
+ text << formatted_key_value('IP Address', request.remote_ip)
87
+ text << formatted_key_value('Parameters', request.filtered_parameters.inspect)
88
+ text << formatted_key_value('Timestamp', Time.current)
89
+ text << formatted_key_value('Server', Socket.gethostname)
90
+ text << formatted_key_value('Rails root', Rails.root) if defined?(Rails) && Rails.respond_to?(:root)
91
+ text << formatted_key_value('Process', $PROCESS_ID)
92
+ text.join("\n")
93
+ end
94
+
95
+ def formatted_session
96
+ text = []
97
+ text << '# Session'
98
+ text << formatted_key_value('Data', request.session.to_hash)
99
+ text.join("\n")
100
+ end
101
+
102
+ def formatted_backtrace
103
+ size = [backtrace.size, MAX_BACKTRACE_SIZE].min
104
+
105
+ text = []
106
+ text << '# Backtrace'
107
+ text << '````'
108
+ size.times { |i| text << backtrace[i] }
109
+ text << '````'
110
+ text.join("\n")
111
+ end
112
+
113
+ def truncate(string, max)
114
+ string.length > max ? "#{string[0...max]}..." : string
115
+ end
116
+
117
+ def inspect_object(object)
118
+ case object
119
+ when Hash, Array
120
+ truncate(object.inspect, MAX_VALUE_LENGTH)
121
+ else
122
+ object.to_s
123
+ end
124
+ end
125
+
126
+ def controller_subtitle
127
+ "#{controller.controller_name} #{controller.action_name}" if controller
128
+ end
129
+ end
130
+ end
131
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Simplepush
4
- VERSION = "0.6.1"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/simplepush.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "simplepush/version"
3
+ require_relative 'simplepush/version'
4
4
  require 'httparty'
5
+ require_relative 'integrations/simplepush_notifier'
5
6
 
6
7
  class Simplepush
7
8
 
@@ -12,7 +13,7 @@ class Simplepush
12
13
  # base_uri 'https://httpbin.org'.freeze
13
14
  format :json
14
15
  default_timeout 5 # 5 seconds
15
- # debug_output $stdout # Uncomment to get detailled httparty log output to stdout
16
+ # debug_output $stdout # Uncomment to get detailled httparty log output to stdout
16
17
 
17
18
  # If password and salt are provided, then message and title will be encrypted.
18
19
  def initialize(key, password = nil, salt = '1789F0B8C4A051E5')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplepush
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Oezbek
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-31 00:00:00.000000000 Z
11
+ date: 2021-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -41,6 +41,7 @@ files:
41
41
  - example/async.rb
42
42
  - example/config.rb
43
43
  - example/example.rb
44
+ - lib/integrations/simplepush_notifier.rb
44
45
  - lib/simplepush.rb
45
46
  - lib/simplepush/version.rb
46
47
  - simplepush.gemspec
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  - !ruby/object:Gem::Version
67
68
  version: '0'
68
69
  requirements: []
69
- rubygems_version: 3.2.3
70
+ rubygems_version: 3.1.4
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: Ruby SimplePush.io API client (unofficial)