Module: Webpush
- Defined in:
- lib/webpush.rb,
lib/webpush/errors.rb,
lib/webpush/request.rb,
lib/webpush/version.rb,
lib/webpush/vapid_key.rb,
lib/webpush/encryption.rb
Defined Under Namespace
Modules: Encryption Classes: ConfigurationError, Error, ExpiredSubscription, InvalidSubscription, PayloadTooLarge, Request, ResponseError, TooManyRequests, VapidKey
Constant Summary collapse
- GCM_URL =
It is temporary URL until supported by the GCM server.
'https://android.googleapis.com/gcm/send'- TEMP_GCM_URL =
'https://gcm-http.googleapis.com/gcm'- VERSION =
"0.3.3"
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
57 58 59 60 61 62 63 64 65 |
# File 'lib/webpush.rb', line 57 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 if !str.end_with?("=") && str.length % 4 != 0 str = str.ljust((str.length + 3) & ~3, "=") end Base64.urlsafe_decode64(str) end |
.encode64(bytes) ⇒ Object
53 54 55 |
# File 'lib/webpush.rb', line 53 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
49 50 51 |
# File 'lib/webpush.rb', line 49 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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/webpush.rb', line 29 def payload_send(message: "", endpoint:, p256dh: "", auth: "", vapid: {}, **) subscription = { endpoint: endpoint, keys: { p256dh: p256dh, auth: auth } } Webpush::Request.new( message: , subscription: subscription, vapid: vapid, ** ).perform end |