Class: Webpush::Request
- Inherits:
-
Object
- Object
- Webpush::Request
- Defined in:
- lib/webpush/request.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Method Summary collapse
- #body ⇒ Object
-
#build_vapid_header ⇒ Object
rubocop:enable Metrics/MethodLength.
-
#headers ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#initialize(message: '', subscription:, vapid:, **options) ⇒ Request
constructor
A new instance of Request.
-
#perform ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#proxy_options ⇒ Object
rubocop:enable Metrics/AbcSize.
Constructor Details
#initialize(message: '', subscription:, vapid:, **options) ⇒ Request
Returns a new instance of Request.
14 15 16 17 18 19 20 |
# File 'lib/webpush/request.rb', line 14 def initialize(message: '', subscription:, vapid:, **) endpoint = subscription.fetch(:endpoint) @endpoint = endpoint.gsub(GCM_URL, TEMP_GCM_URL) @payload = build_payload(, subscription) @vapid_options = vapid @options = .merge() end |
Instance Method Details
#body ⇒ Object
80 81 82 |
# File 'lib/webpush/request.rb', line 80 def body @payload || '' end |
#build_vapid_header ⇒ Object
rubocop:enable Metrics/MethodLength
70 71 72 73 74 75 76 77 78 |
# File 'lib/webpush/request.rb', line 70 def build_vapid_header # https://tools.ietf.org/id/draft-ietf-webpush-vapid-03.html vapid_key = vapid_pem ? VapidKey.from_pem(vapid_pem) : VapidKey.from_keys(vapid_public_key, vapid_private_key) jwt = JWT.encode(jwt_payload, vapid_key.curve, 'ES256', jwt_header_fields) p256ecdsa = vapid_key.public_key_for_push_header "vapid t=#{jwt},k=#{p256ecdsa}" end |
#headers ⇒ Object
rubocop:disable Metrics/MethodLength
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/webpush/request.rb', line 49 def headers headers = {} headers['Content-Type'] = 'application/octet-stream' headers['Ttl'] = ttl headers['Urgency'] = urgency if @payload headers['Content-Encoding'] = 'aes128gcm' headers["Content-Length"] = @payload.length.to_s end if api_key? headers['Authorization'] = "key=#{api_key}" elsif vapid? headers["Authorization"] = build_vapid_header end headers end |
#perform ⇒ Object
rubocop:disable Metrics/AbcSize
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/webpush/request.rb', line 23 def perform http = Net::HTTP.new(uri.host, uri.port, *) http.use_ssl = true http.ssl_timeout = @options[:ssl_timeout] unless @options[:ssl_timeout].nil? http.open_timeout = @options[:open_timeout] unless @options[:open_timeout].nil? http.read_timeout = @options[:read_timeout] unless @options[:read_timeout].nil? req = Net::HTTP::Post.new(uri.request_uri, headers) req.body = body resp = http.request(req) verify_response(resp) resp end |
#proxy_options ⇒ Object
rubocop:enable Metrics/AbcSize
40 41 42 43 44 45 46 |
# File 'lib/webpush/request.rb', line 40 def return [] unless @options[:proxy] proxy_uri = URI.parse(@options[:proxy]) [proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password] end |