Skip to content

Commit

Permalink
Code clean up and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
raed-shomali committed Jun 10, 2017
1 parent 963ede0 commit 9d124ce
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 210 deletions.
23 changes: 14 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package slacker

import "github.com/shomali11/slacker/expression"
import (
"github.com/shomali11/commander"
"github.com/shomali11/properties"
)

// NewCommand creates a new command structure
func NewCommand(usage string, description string, handler func(request *Request, response *Response)) *Command {
return &Command{usage: usage, description: description, handler: handler}
// NewBotCommand creates a new bot command object
func NewBotCommand(usage string, description string, handler func(request *Request, response *Response)) *BotCommand {
command := commander.NewCommand(usage)
return &BotCommand{usage: usage, description: description, handler: handler, command: command}
}

// Command structure contains the command, description and handler
type Command struct {
// BotCommand structure contains the bot's command, description and handler
type BotCommand struct {
usage string
description string
handler func(request *Request, response *Response)
command *commander.Command
}

// Match determines whether the bot should respond based on the text received
func (c *Command) Match(text string) (bool, map[string]string) {
return expression.Match(c.usage, text)
func (c *BotCommand) Match(text string) (*properties.Properties, bool) {
return c.command.Match(text)
}

// Execute executes the handler logic
func (c *Command) Execute(request *Request, response *Response) {
func (c *BotCommand) Execute(request *Request, response *Response) {
c.handler(request, response)
}
75 changes: 0 additions & 75 deletions expression/expression.go

This file was deleted.

46 changes: 0 additions & 46 deletions expression/expression_test.go

This file was deleted.

52 changes: 0 additions & 52 deletions parser/parser_test.go

This file was deleted.

16 changes: 8 additions & 8 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package slacker

import (
"github.com/nlopes/slack"
"github.com/shomali11/slacker/parser"
"github.com/shomali11/properties"
)

const (
empty = ""
)

// NewRequest creates a new Request structure
func NewRequest(event *slack.MessageEvent, parameters map[string]string) *Request {
return &Request{Event: event, parameters: parameters}
func NewRequest(event *slack.MessageEvent, properties *properties.Properties) *Request {
return &Request{Event: event, properties: properties}
}

// Request contains the Event received and parameters
type Request struct {
Event *slack.MessageEvent
parameters map[string]string
properties *properties.Properties
}

// Param attempts to look up a string value by key. If not found, return the an empty string
Expand All @@ -27,20 +27,20 @@ func (r *Request) Param(key string) string {

// StringParam attempts to look up a string value by key. If not found, return the default string value
func (r *Request) StringParam(key string, defaultValue string) string {
return parser.StringParam(key, r.parameters, defaultValue)
return r.properties.StringParam(key, defaultValue)
}

// BooleanParam attempts to look up a boolean value by key. If not found, return the default boolean value
func (r *Request) BooleanParam(key string, defaultValue bool) bool {
return parser.BooleanParam(key, r.parameters, defaultValue)
return r.properties.BooleanParam(key, defaultValue)
}

// IntegerParam attempts to look up a integer value by key. If not found, return the default integer value
func (r *Request) IntegerParam(key string, defaultValue int) int {
return parser.IntegerParam(key, r.parameters, defaultValue)
return r.properties.IntegerParam(key, defaultValue)
}

// FloatParam attempts to look up a float value by key. If not found, return the default float value
func (r *Request) FloatParam(key string, defaultValue float64) float64 {
return parser.FloatParam(key, r.parameters, defaultValue)
return r.properties.FloatParam(key, defaultValue)
}
22 changes: 11 additions & 11 deletions slacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"github.com/nlopes/slack"
"github.com/shomali11/slacker/expression"
"github.com/shomali11/commander"
"strings"
)

Expand All @@ -19,7 +19,7 @@ const (
codeMessageFormat = "`%s`"
boldMessageFormat = "*%s*"
italicMessageFormat = "_%s_"
noCommandsAvailable = "No commands were setup."
noCommandsAvailable = "No botCommands were setup."
)

// NewClient creates a new client using the Slack API
Expand All @@ -30,11 +30,11 @@ func NewClient(token string) *Slacker {
return &Slacker{Client: client, rtm: rtm}
}

// Slacker contains the Slack API, commands, and handlers
// Slacker contains the Slack API, botCommands, and handlers
type Slacker struct {
Client *slack.Client
rtm *slack.RTM
commands []*Command
botCommands []*BotCommand
initHandler func()
errorHandler func(err string)
}
Expand All @@ -51,7 +51,7 @@ func (s *Slacker) Err(errorHandler func(err string)) {

// Command define a new command and append it to the list of existing commands
func (s *Slacker) Command(usage string, description string, handler func(request *Request, response *Response)) {
s.commands = append(s.commands, NewCommand(usage, description, handler))
s.botCommands = append(s.botCommands, NewBotCommand(usage, description, handler))
}

// Listen receives events from Slack and each is handled as needed
Expand Down Expand Up @@ -112,16 +112,16 @@ func (s *Slacker) isHelpRequest(event *slack.MessageEvent) bool {
}

func (s *Slacker) handleHelp(channel string) {
if len(s.commands) == 0 {
if len(s.botCommands) == 0 {
s.sendMessage(fmt.Sprintf(italicMessageFormat, noCommandsAvailable), channel)
return
}

helpMessage := empty
for _, command := range s.commands {
for _, command := range s.botCommands {
tokens := strings.Split(command.usage, space)
for _, token := range tokens {
if expression.IsParameter(token) {
if commander.IsParameter(token) {
helpMessage += fmt.Sprintf(codeMessageFormat, token[1:len(token)-1]) + space
} else {
helpMessage += fmt.Sprintf(boldMessageFormat, token) + space
Expand All @@ -139,13 +139,13 @@ func (s *Slacker) handleMessage(event *slack.MessageEvent) {
return
}

for _, cmd := range s.commands {
isMatch, parameters := cmd.Match(event.Text)
for _, cmd := range s.botCommands {
properties, isMatch := cmd.Match(event.Text)
if !isMatch {
continue
}

cmd.Execute(NewRequest(event, parameters), NewResponse(event.Channel, s.rtm))
cmd.Execute(NewRequest(event, properties), NewResponse(event.Channel, s.rtm))
return
}
}
21 changes: 21 additions & 0 deletions vendor/github.com/shomali11/commander/LICENSE

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

16 changes: 16 additions & 0 deletions vendor/github.com/shomali11/commander/README.md

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

Loading

0 comments on commit 9d124ce

Please sign in to comment.