Skip to content

Commit

Permalink
Add phonenumbers library for has_phone test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed May 30, 2017
1 parent c41ba17 commit a8096b1
Show file tree
Hide file tree
Showing 35 changed files with 25,009 additions and 23 deletions.
38 changes: 21 additions & 17 deletions excellent/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/phonenumbers"
"github.com/shopspring/decimal"
)

Expand Down Expand Up @@ -595,36 +596,39 @@ func HasEmail(env utils.Environment, args ...interface{}) interface{} {
return XFalseResult
}

// TODO: plug in a real phone number parsing library
var phoneRE = regexp.MustCompile(`^\+?([0-9]{7,12})$`)

// HasPhone tests whether a phone number is contained in the text
// HasPhone tests whether a phone number (in the passed in country) is contained in the text
//
// @(has_phone("my number is 2067799294")) -> true
// @(has_phone("my number is 206 779 9294").match) -> "+12067799294"
// @(has_phone("my number is none of your business")) -> false
// @(has_phone("my number is 2067799294", "US")) -> true
// @(has_phone("my number is 206 779 9294", "US").match) -> "+12067799294"
// @(has_phone("my number is none of your business", "US")) -> false
//
// @test has_phone
func HasPhone(env utils.Environment, args ...interface{}) interface{} {
if len(args) != 1 {
return fmt.Errorf("HAS_PHONE takes exactly one argument, got %d", len(args))
if len(args) != 2 {
return fmt.Errorf("HAS_PHONE takes exactly two arguments, the string to search and the country code, got %d", len(args))
}

// convert our arg to a string
// grab the text we will search
text, err := utils.ToString(env, args[0])
if err != nil {
return err
}

// split by whitespace
for _, word := range strings.Fields(text) {
phone := phoneRE.FindString(word)
if phone != "" {
return XTestResult{true, phone}
}
// and the country we are searching
country, err := utils.ToString(env, args[1])
if err != nil {
return err
}

return XFalseResult
// try to find a phone number
phone, err := phonenumbers.Parse(text, country)
if err != nil {
return XFalseResult
}

// format as E164 number
formatted := phonenumbers.Format(phone, phonenumbers.E164)
return XTestResult{true, formatted}
}

//------------------------------------------------------------------------------------------
Expand Down
16 changes: 10 additions & 6 deletions excellent/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ var testTests = []struct {
{"has_number_between", []interface{}{"nothing here", "10", "15"}, false, nil, false},
{"has_number_between", []interface{}{"one", "two"}, false, nil, true},

// {"has_number_between", []interface{}{"but foo", noStr{}, "10"}, false, nil, true},
// {"has_number_between", []interface{}{noStr{}, "but foo", "10"}, false, nil, true},
// {"has_number_between", []interface{}{"a string", "10", "not number"}, false, nil, true},
{"has_number_between", []interface{}{"but foo", noStr{}, "10"}, false, nil, true},
{"has_number_between", []interface{}{noStr{}, "but foo", "10"}, false, nil, true},
{"has_number_between", []interface{}{"a string", "10", "not number"}, false, nil, true},

{"has_date", []interface{}{"last date was 1.10.2017"}, true, time.Date(2017, 10, 1, 0, 0, 0, 0, time.UTC), false},
{"has_date", []interface{}{"last date was 1.10.99"}, true, time.Date(1999, 10, 1, 0, 0, 0, 0, time.UTC), false},
Expand Down Expand Up @@ -154,10 +154,14 @@ var testTests = []struct {
{"has_email", []interface{}{noStr{}}, false, nil, true},
{"has_email", []interface{}{"too", "many", "args"}, false, nil, true},

{"has_phone", []interface{}{"my number is 0788123123"}, true, "0788123123", false},
{"has_phone", []interface{}{"my number is +250788123123"}, true, "+250788123123", false},
{"has_phone", []interface{}{"my number is 12345"}, false, nil, false},
{"has_phone", []interface{}{"my number is 0788123123", "RW"}, true, "+250788123123", false},
{"has_phone", []interface{}{"my number is +250788123123", "RW"}, true, "+250788123123", false},
{"has_phone", []interface{}{"my number is +12065551212", "RW"}, true, "+12065551212", false},
{"has_phone", []interface{}{"my number is 12065551212", "US"}, true, "+12065551212", false},
{"has_phone", []interface{}{"my number is 206 555 1212", "US"}, true, "+12065551212", false},
{"has_phone", []interface{}{"my number is none of your business", "US"}, false, nil, false},
{"has_phone", []interface{}{noStr{}}, false, nil, true},
{"has_phone", []interface{}{"number", noStr{}}, false, nil, true},
{"has_phone", []interface{}{"too", "many", "args"}, false, nil, true},
}

Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/golang/protobuf/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions vendor/github.com/golang/protobuf/Make.protobuf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions vendor/github.com/golang/protobuf/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a8096b1

Please sign in to comment.