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

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_keyWebpush::VapidKey

Generate a VapidKey instance to obtain base64 encoded public and private keys suitable for VAPID protocol JSON web token signing

Returns:



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.

Parameters:

  • endpoint (String)

    the required PushSubscription url

  • message (String) (defaults to: "")

    the optional payload

  • p256dh (String) (defaults to: "")

    the user’s public ECDH key given by the PushSubscription

  • auth (String) (defaults to: "")

    the user’s private ECDH key given by the PushSubscription

  • vapid (Hash<Symbol,String>) (defaults to: {})

    options for VAPID

  • options (Hash<Symbol,String>)

    additional options for the notification

Options Hash (vapid:):

  • :subject (String)

    contact URI for the app server as a “mailto:” or an “https:”

  • :public_key (String)

    the VAPID public key

  • :private_key (String)

    the VAPID private key

Options Hash (**options):

  • :ttl (#to_s)

    Time-to-live in seconds

  • :urgency (#to_s)

    Urgency can be very-low, low, normal, high



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: {}, **options)
  subscription = {
    endpoint: endpoint,
    keys: {
      p256dh: p256dh,
      auth: auth
    }
  }
  Webpush::Request.new(
    message: message,
    subscription: subscription,
    vapid: vapid,
    **options
  ).perform
end