Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add urgency option #57

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ Webpush.payload_send(
message: JSON.generate(message),
p256dh: "BO/aG9nYXNkZmFkc2ZmZHNmYWRzZmFl...",
auth: "aW1hcmthcmFpa3V6ZQ==",
ttl: 600 #optional, ttl in seconds, defaults to 2419200 (4 weeks),
ttl: 600, # optional, ttl in seconds, defaults to 2419200 (4 weeks)
urgency: 'normal' # optional, it can be very-low, low, normal, high, defaults to normal
)
```

Expand Down
1 change: 1 addition & 0 deletions lib/webpush.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class << self
# @option vapid [String] :private_key the VAPID private key
# @param options [Hash<Symbol,String>] additional options for the notification
# @option options [#to_s] :ttl Time-to-live in seconds
# @option options [#to_s] :urgency Urgency can be very-low, low, normal, high
def payload_send(message: "", endpoint:, p256dh: "", auth: "", vapid: {}, **options)
subscription = {
endpoint: endpoint,
Expand Down
8 changes: 7 additions & 1 deletion lib/webpush/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def headers
headers = {}
headers["Content-Type"] = "application/octet-stream"
headers["Ttl"] = ttl
headers["Urgency"] = urgency

if @payload.has_key?(:server_public_key)
headers["Content-Encoding"] = "aesgcm"
Expand Down Expand Up @@ -89,6 +90,10 @@ def ttl
@options.fetch(:ttl).to_s
end

def urgency
@options.fetch(:urgency).to_s
end

def dh_param
trim_encode64(@payload.fetch(:server_public_key))
end
Expand Down Expand Up @@ -131,7 +136,8 @@ def vapid_private_key

def default_options
{
ttl: 60*60*24*7*4 # 4 weeks
ttl: 60*60*24*7*4, # 4 weeks
urgency: 'normal'
}
end

Expand Down
9 changes: 9 additions & 0 deletions spec/webpush/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

it { expect(request.headers['Content-Type']).to eq('application/octet-stream') }
it { expect(request.headers['Ttl']).to eq('2419200') }
it { expect(request.headers['Urgency']).to eq('normal') }

describe 'from :message' do
it 'inserts encryption headers for valid payload' do
Expand All @@ -31,6 +32,14 @@
expect(request.headers['Ttl']).to eq('300')
end
end

describe 'from :urgency' do
it 'can override Urgency with :urgency option' do
request = build_request(urgency: 'high', vapid: vapid_options)

expect(request.headers['Urgency']).to eq('high')
end
end
end

describe '#build_vapid_headers' do
Expand Down
2 changes: 2 additions & 0 deletions spec/webpush_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
'Crypto-Key'=>'dh=BAgtUks5d90kFmxGevk9tH7GEmvz9DB0qcEMUsOBgKwMf-TMjsKIIG6LQvGcFAf6jcmAod15VVwmYwGIIxE4VWE',
'Encryption'=>'salt=WJeVM-RY-F9351SVxTFx_g',
'Ttl'=>'2419200',
'Urgency'=>'normal',
'User-Agent'=>'Ruby'
}
end
Expand Down Expand Up @@ -208,6 +209,7 @@
'Crypto-Key'=>'dh=BAgtUks5d90kFmxGevk9tH7GEmvz9DB0qcEMUsOBgKwMf-TMjsKIIG6LQvGcFAf6jcmAod15VVwmYwGIIxE4VWE',
'Encryption'=>'salt=WJeVM-RY-F9351SVxTFx_g',
'Ttl'=>'2419200',
'Urgency'=>'normal',
'User-Agent'=>'Ruby'
}
end
Expand Down