Skip to content

Commit

Permalink
Merge pull request #4 from kpumuk/dmytro/simplified-access
Browse files Browse the repository at this point in the history
Added simplified accessors Pwned.pwned? and Pwned.pwned_count
  • Loading branch information
philnash committed Mar 12, 2018
2 parents d46d439 + 32b847f commit b03ecf9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
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

0 comments on commit b03ecf9

Please sign in to comment.