Module: Webpush
- Defined in:
- lib/webpush.rb,
lib/webpush/errors.rb,
lib/webpush/railtie.rb,
lib/webpush/request.rb,
lib/webpush/version.rb,
lib/webpush/vapid_key.rb,
lib/webpush/encryption.rb
Overview
Push API implementation
Defined Under Namespace
Modules: Encryption Classes: ConfigurationError, Error, ExpiredSubscription, InvalidSubscription, PayloadTooLarge, PushServiceError, Railtie, Request, ResponseError, TooManyRequests, Unauthorized, VapidKey
Constant Summary collapse
- GCM_URL =
It is temporary URL until supported by the GCM server.
'https://android.googleapis.com/gcm/send'.freeze
- TEMP_GCM_URL =
'https://fcm.googleapis.com/fcm'.freeze
- VERSION =
'1.0.0'.freeze
Class Method Summary collapse
- .decode64(str) ⇒ Object
- .encode64(bytes) ⇒ Object
-
.generate_key ⇒ Webpush::VapidKey
Generate a VapidKey instance to obtain base64 encoded public and private keys suitable for VAPID protocol JSON web token signing.
-
.payload_send(message: '', endpoint:, p256dh: '', auth: '', vapid: {}, **options) ⇒ Object
Deliver the payload to the required endpoint given by the JavaScript PushSubscription.
Class Method Details
.decode64(str) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/webpush.rb', line 60 def decode64(str) # For Ruby < 2.3, Base64.urlsafe_decode64 strict decodes and will raise errors if encoded value is not properly padded # Implementation: http://ruby-doc.org/stdlib-2.3.0/libdoc/base64/rdoc/Base64.html#method-i-urlsafe_decode64 str = str.ljust((str.length + 3) & ~3, '=') if !str.end_with?('=') && str.length % 4 != 0 Base64.urlsafe_decode64(str) end |
.encode64(bytes) ⇒ Object
56 57 58 |
# File 'lib/webpush.rb', line 56 def encode64(bytes) Base64.urlsafe_encode64(bytes) end |
.generate_key ⇒ Webpush::VapidKey
Generate a VapidKey instance to obtain base64 encoded public and private keys suitable for VAPID protocol JSON web token signing
52 53 54 |
# File 'lib/webpush.rb', line 52 def generate_key VapidKey.new end |
.payload_send(message: '', endpoint:, p256dh: '', auth: '', vapid: {}, **options) ⇒ Object
Deliver the payload to the required endpoint given by the JavaScript PushSubscription. Including an optional message requires p256dh and auth keys from the PushSubscription.
rubocop:disable Metrics/ParameterLists
38 39 40 41 42 43 44 45 |
# File 'lib/webpush.rb', line 38 def payload_send(message: '', endpoint:, p256dh: '', auth: '', vapid: {}, **) Webpush::Request.new( message: , subscription: subscription(endpoint, p256dh, auth), vapid: vapid, ** ).perform end |