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

Added simplified accessors Pwned.pwned? and Pwned.pwned_count #4

Merged
merged 1 commit into from
Mar 12, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Refactors exception handling with built in Ruby method ([PR #1](https://github.com/philnash/pwned/pull/1) thanks [@kpumuk](https://github.com/kpumuk))
* Passwords must be strings, the initializer will raise a `TypeError` unless `password.is_a? String`. ([dbf7697](https://github.com/philnash/pwned/commit/dbf7697e878d87ac74aed1e715cee19b73473369))
* Added Ruby on Rails validator ([PR #3](https://github.com/philnash/pwned/pull/3))
* Added simplified accessors `Pwned.pwned?`` and `Pwned.pwned_count` ([PR #4](https://github.com/philnash/pwned/pull/4))

* Minor updates
* SHA1 is only calculated once
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ rescue Pwned::Error => e
end
```

Most of the times you only care if the password has been pwned before or not. You can use simplified accessors to check whether the password has been pwned, or how many times it was pwned:

```ruby
Pwned.pwned?("password")
#=> true
Pwned.pwned_count("password")
#=> 3303003
```

#### Advanced

You can set options and headers to be used with `open-uri` when making the request to the API. HTTP headers must be string keys and the [other options are available in the `OpenURI::OpenRead` module](https://ruby-doc.org/stdlib-2.5.0/libdoc/open-uri/rdoc/OpenURI/OpenRead.html#method-i-open).
Expand Down
9 changes: 9 additions & 0 deletions lib/pwned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
end

module Pwned
# Returns true when the password has been pwned.
def self.pwned?(password, request_options={})
Pwned::Password.new(password, request_options).pwned?
end

# Returns number of times the password has been pwned.
def self.pwned_count(password, request_options={})
Pwned::Password.new(password, request_options).pwned_count
end
end
10 changes: 10 additions & 0 deletions spec/pwned/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
expect(password.pwned?).to be true
expect(@stub).to have_been_requested
end

it "works with simplified accessors" do
expect(Pwned.pwned?(password.password)).to be true
expect(Pwned.pwned_count(password.password)).to eq(3303003)
end
end

describe "when not pwned", pwned_range: "37D5B" do
Expand All @@ -51,6 +56,11 @@
expect(password.pwned_count).to eq(0)
expect(@stub).to have_been_requested
end

it "works with simplified accessors" do
expect(Pwned.pwned?(password.password)).to be false
expect(Pwned.pwned_count(password.password)).to eq(0)
end
end

describe "when the API times out" do
Expand Down