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
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'
- TEMP_GCM_URL =
'https://gcm-http.googleapis.com/gcm'
- VERSION =
"0.3.8"
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
59 60 61 62 63 64 65 66 67 |
# File 'lib/webpush.rb', line 59 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
55 56 57 |
# File 'lib/webpush.rb', line 55 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
51 52 53 |
# File 'lib/webpush.rb', line 51 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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/webpush.rb', line 31 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 |