Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
namreg committed Oct 16, 2018
1 parent 882d39b commit 4c09dbe
Show file tree
Hide file tree
Showing 7 changed files with 4,114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: go

go:
- 1.x

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Igor German

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## DED (Disposable Email Detector)

[![Build Status](https://www.travis-ci.org/namreg/ded.svg?branch=master)](https://www.travis-ci.org/namreg/ded)
[![Go Report Card](https://goreportcard.com/badge/github.com/namreg/ded)](https://goreportcard.com/report/github.com/namreg/ded)
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/namreg/ded/blob/master/LICENSE)
[![GoDoc](https://godoc.org/github.com/namreg/ded?status.svg)](https://godoc.org/github.com/namreg/ded)
71 changes: 71 additions & 0 deletions ded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ded

import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"sync"
)

//ErrEmailMalformed error returns when email is not valid.
var ErrEmailMalformed = errors.New("ded: email malformed")

//IsDisposableEmail checks whether the given email is disposable.
func IsDisposableEmail(email string) (bool, error) {
parts := strings.SplitN(email, "@", 2)
if len(parts) != 2 {
return false, ErrEmailMalformed
}
return IsDisposableDomain(parts[1])
}

//IsDisposableDomain checks whether the given domain is disposable.
func IsDisposableDomain(domain string) (bool, error) {
domains.once.Do(func() { domains.loadFromFile("domains.txt") })
if domains.err != nil {
return false, domains.err
}
domain = strings.TrimSpace(domain)
return domains.has(normalizeString(domain)), nil
}

func normalizeString(str string) string {
return strings.ToLower(strings.TrimSpace(str))
}

var domains = new(collection)

type collection struct {
items map[string]struct{}
err error
once sync.Once
}

func (c *collection) has(item string) bool {
_, ok := c.items[item]
return ok
}

func (c *collection) loadFromFile(filename string) {
f, err := os.Open(filename)
if err != nil {
c.err = fmt.Errorf("ded: could not open %q file: %v", filename, err)
return
}
c.items = make(map[string]struct{})

s := bufio.NewScanner(f)
for s.Scan() {
domain := normalizeString(s.Text())
if domain == "" {
continue
}
c.items[domain] = struct{}{}
}

if err := s.Err(); err != nil {
c.err = fmt.Errorf("ded: scanner error: %v", err)
}
}
56 changes: 56 additions & 0 deletions ded_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ded

import (
"testing"
)

func TestIsDisposableEmail(t *testing.T) {
tests := []struct {
name string
email string
want bool
wantErr error
}{
{"not disposable email", "[email protected]", false, nil},
{"disposable email", "[email protected]", true, nil},
{"ignore spaces", " [email protected] ", true, nil},
{"case insensitive", "[email protected]", true, nil},
{"malformed email", "eml.com", false, ErrEmailMalformed},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IsDisposableEmail(tt.email)
if err != tt.wantErr {
t.Fatalf("IsDisposableEmail() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.want {
t.Fatalf("IsDisposableEmail() = %v, want %v", got, tt.want)
}
})
}
}

func TestIsDisposableDomain(t *testing.T) {
tests := []struct {
name string
domain string
want bool
wantErr error
}{
{"not disposable domain", "google.com", false, nil},
{"disposable domain", "mail.wtf", true, nil},
{"ignore spaces", " mail.wtf ", true, nil},
{"case insensitive", "MAIL.wtf", true, nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IsDisposableDomain(tt.domain)
if err != tt.wantErr {
t.Fatalf("TestIsDisposableDomain() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.want {
t.Fatalf("TestIsDisposableDomain() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 4c09dbe

Please sign in to comment.