Sha256: d08f38db37b70a93241cc25dfc5777dca0407645e7c8076c6f794880d49f5d0e

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

require 'openssl'
require 'base64'
require 'hkdf'
require 'net/http'
require 'json'

require 'webpush/version'
require 'webpush/encryption'
require 'webpush/request'

module Webpush

  # It is temporary URL until supported by the GCM server.
  GCM_URL = 'https://android.googleapis.com/gcm/send'
  TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm'

  class << self
    # Deliver the payload to the required endpoint given by the JavaScript
    # PushSubscription. Including an optional message requires p256dh and
    # auth keys from the PushSubscription.
    #
    # @param endpoint [String] the required PushSubscription url
    # @param message [String] the optional payload
    # @param p256dh [String] the user's public ECDH key given by the PushSubscription
    # @param auth [String] the user's private ECDH key given by the PushSubscription
    # @param options [Hash<Symbol,String>] additional options for the notification
    # @option options [String] :api_key required for Google, omit for Firefox
    # @option options [#to_s] :ttl Time-to-live in seconds
    def payload_send(endpoint:, message: "", p256dh: "", auth: "", **options)
      endpoint = endpoint.gsub(GCM_URL, TEMP_GCM_URL)

      payload = build_payload(message, p256dh, auth)

      Webpush::Request.new(endpoint, options.merge(payload: payload)).perform
    end

    private

    def build_payload(message, p256dh, auth)
      return {} if message.nil? || message.empty?

      Webpush::Encryption.encrypt(message, p256dh, auth)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
webpush-0.2.5 lib/webpush.rb
webpush-0.2.4 lib/webpush.rb
webpush-0.2.3 lib/webpush.rb
webpush-0.2.2 lib/webpush.rb
webpush-0.2.1 lib/webpush.rb
webpush-0.2.0 lib/webpush.rb