Skip to content

Commit

Permalink
Refactor isBot (avct#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade authored Mar 7, 2019
1 parent 1f566b7 commit 6ec1e55
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
6 changes: 3 additions & 3 deletions browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (u *UserAgent) evalBrowserName(ua string) bool {
// Blackberry goes first because it reads as MSIE & Safari
if strings.Contains(ua, "blackberry") || strings.Contains(ua, "playbook") || strings.Contains(ua, "bb10") || strings.Contains(ua, "rim ") {
u.Browser.Name = BrowserBlackberry
return u.isBot()
return u.maybeBot()
}

if strings.Contains(ua, "applewebkit") {
Expand Down Expand Up @@ -92,7 +92,7 @@ func (u *UserAgent) evalBrowserName(ua string) bool {
goto notwebkit

}
return u.isBot()
return u.maybeBot()
}

notwebkit:
Expand Down Expand Up @@ -159,7 +159,7 @@ notwebkit:

}

return u.isBot()
return u.maybeBot()
}

// Retrieve browser version
Expand Down
16 changes: 5 additions & 11 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var (
)

func (u *UserAgent) evalOS(ua string) bool {

s := strings.IndexRune(ua, '(')
e := strings.IndexRune(ua, ')')
if s > e {
Expand Down Expand Up @@ -102,23 +101,18 @@ func (u *UserAgent) evalOS(ua string) bool {
}
}

return u.isBot()
return u.maybeBot()
}

func (u *UserAgent) isBot() bool {

if u.OS.Platform == PlatformBot || u.OS.Name == OSBot {
u.DeviceType = DeviceComputer
return true
}

if u.Browser.Name >= BrowserBot && u.Browser.Name <= BrowserYahooBot {
// maybeBot checks if the UserAgent is a bot and sets
// all bot related fields if it is
func (u *UserAgent) maybeBot() bool {
if u.IsBot() {
u.OS.Platform = PlatformBot
u.OS.Name = OSBot
u.DeviceType = DeviceComputer
return true
}

return false
}

Expand Down
1 change: 1 addition & 0 deletions uasurfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (ua *UserAgent) Reset() {
ua.DeviceType = DeviceUnknown
}

// IsBot returns true if the UserAgent represent a bot
func (ua *UserAgent) IsBot() bool {
if ua.Browser.Name >= BrowserBot && ua.Browser.Name <= BrowserYahooBot {
return true
Expand Down

0 comments on commit 6ec1e55

Please sign in to comment.