Skip to content

Commit

Permalink
Add convenience methods for inspecting vapid key
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Oct 11, 2016
1 parent b89112a commit d4d5a6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/webpush/vapid_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def group
curve.group
end

def to_h
{ public_key: public_key, private_key: private_key }
end
alias to_hash to_h

def inspect
"#<#{self.class}:#{object_id.to_s(16)} #{to_h.map { |k, v| ":#{k}=#{v}" }.join(" ")}>"
end

private

def to_big_num(key)
Expand Down
16 changes: 16 additions & 0 deletions spec/webpush/vapid_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
expect(Base64.urlsafe_decode64(key.private_key).bytesize).to eq(32)
end

it "pretty prints encoded keys" do
key = Webpush::VapidKey.new
printed = key.inspect

expect(printed).to match(/public_key=#{key.public_key}/)
expect(printed).to match(/private_key=#{key.private_key}/)
end

it "returns hash of public and private keys" do
key = Webpush::VapidKey.new
hash = key.to_h

expect(hash[:public_key]).to eq(key.public_key)
expect(hash[:private_key]).to eq(key.private_key)
end

describe "self.from_keys" do
it "returns an encoded public key" do
key = Webpush::VapidKey.from_keys(vapid_public_key, vapid_private_key)
Expand Down

0 comments on commit d4d5a6b

Please sign in to comment.