simplepush 0.6.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c13cefedbd1151589a852fa4f91c1f43a2ad117d01b56752fad35959cc1c3a21
4
- data.tar.gz: fde8e2a819e41a5de6c9d82142e045567f22fcf6b9a31b25b119a2ffdba8ddf8
3
+ metadata.gz: 175e1b7f5bc919a837292dde9ce32591df4f5d181132dc5ce1fb0750db9f47a3
4
+ data.tar.gz: a3556dd754d9093896fb9b769d3b9d4dae5bfec454a8a38ca32e9c58a2b5e5bb
5
5
  SHA512:
6
- metadata.gz: 044447bdfd5f5c08b441e537dc2a5984c64a005a15d49b714714f21ce62f45d21ee4a23ab10dd96ad959139c03c3ae30d153d4629de7b92e8eb059036d351307
7
- data.tar.gz: fd381bafb1e625c9a64308f7becf9c29623ddbb896c9a906fd4aa9f50e68d9f8a0937f6d66f81b0ad84f19ecb5ad1b4e9755aff7148318f11debeb5a17191ec4
6
+ metadata.gz: f3f564ae638b69b31e4ec389463429f2c1f4a9f8730a319eee9865e3dd2827c783cfea71d070bbf361ffa6596a8d9bbad29fd1c79cf02287d6894e529d6d00f3
7
+ data.tar.gz: f5129f78f8f4001e084477a02e30553c06b6453125497f2309260c93e5f278c9e9eda1d46766c23e503b73d0cc9954a9889b7342c200a2ff435750f309e1c9ef
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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simplepush (0.6.0)
4
+ simplepush (0.6.1)
5
5
  httparty
6
6
 
7
7
  GEM
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,14 @@ 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)
139
+ - 0.7.1 Fixed incorrect content-type handling.
102
140
 
103
141
  ## Contributing
104
142
 
@@ -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.0"
4
+ VERSION = "0.7.1"
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
 
@@ -10,9 +11,12 @@ class Simplepush
10
11
  base_uri 'https://api.simplepush.io'.freeze
11
12
  # For testing:
12
13
  # base_uri 'https://httpbin.org'.freeze
13
- format :json
14
+
15
+ # Don't override format use from Content-Type
16
+ # format :json
17
+
14
18
  default_timeout 5 # 5 seconds
15
- # debug_output $stdout # Uncomment to get detailled httparty log output to stdout
19
+ # debug_output $stdout # Uncomment to get detailled httparty log output to stdout
16
20
 
17
21
  # If password and salt are provided, then message and title will be encrypted.
18
22
  def initialize(key, password = nil, salt = '1789F0B8C4A051E5')
data/simplepush.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["c.oezbek@gmail.com"]
10
10
 
11
11
  spec.summary = "Ruby SimplePush.io API client (unofficial)"
12
- spec.description = "Httpary wrapper for SimplePush.io"
12
+ spec.description = "Httparty wrapper for SimplePush.io"
13
13
  spec.homepage = "https://github.com/coezbek/simplepush"
14
14
  spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
15
 
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.0
4
+ version: 0.7.1
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: 2022-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Httpary wrapper for SimplePush.io
27
+ description: Httparty wrapper for SimplePush.io
28
28
  email:
29
29
  - c.oezbek@gmail.com
30
30
  executables: []
@@ -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.6
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: Ruby SimplePush.io API client (unofficial)