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

Count API #4

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ result.exist
result.sources
```

## Generate API
## Generate API
Guesses the most likely email of a person from his first name, his last name and a domain name.
```ruby
email_hunter.generate('gmail.com', 'Davide', 'Santangelo')
Expand All @@ -93,6 +93,18 @@ email_hunter.generate('gmail.com', 'Davide', 'Santangelo')
result.status
result.email
result.score

## Count API
Returns the number of email addresses found for a domain.
_*This is a free API call*_
```ruby
email_hunter.count('gmail.com')
```

## Accessing count response
```ruby
result.status
result.count
```

## License
Expand Down
5 changes: 3 additions & 2 deletions lib/email_hunter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'search'))
require File.expand_path(File.join(File.dirname(__FILE__), 'generate'))
require File.expand_path(File.join(File.dirname(__FILE__), 'verify'))
require File.expand_path(File.join(File.dirname(__FILE__), 'count'))

module EmailHunter
class Api
Expand Down Expand Up @@ -32,8 +33,8 @@ def generate(domain, first_name, last_name)
EmailHunter::Generate.new(domain, first_name, last_name, self.key).hunt
end

def verify(email)
EmailHunter::Verify.new(email, self.key).hunt
def count(domain)
EmailHunter::Count.new(domain).hunt
end
end
end
27 changes: 27 additions & 0 deletions lib/email_hunter/count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'faraday'
require 'json'

API_COUNT_URL = 'https://api.emailhunter.co/v1/email-count?'

module EmailHunter
class Count
attr_reader :status, :count

def initialize(domain)
@domain = domain
end

def hunt
response = apiresponse
Struct.new(*response.keys).new(*response.values) unless response.empty?
end

private

def apiresponse
url = URI.parse(URI.encode("#{API_COUNT_URL}domain=#{@domain}"))
response = Faraday.new(url).get
response.success? ? JSON.parse(response.body, {symbolize_names: true}) : []
end
end
end