Skip to content

Commit

Permalink
feat(rtm): implement current team/user/channel api
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Sep 13, 2016
1 parent b67cbfe commit 8853da0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
19 changes: 19 additions & 0 deletions rtm_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package bearychat

import "fmt"

type RTMChannelService struct {
rtm *RTMClient
}

func newRTMChannelService(rtm *RTMClient) error {
rtm.Channel = &RTMChannelService{rtm}
return nil
}

func (s *RTMChannelService) Info(channelId string) (*Channel, error) {
channel := new(Channel)
resource := fmt.Sprintf("v1/channel.info?channel_id=%s", channelId)
_, err := s.rtm.Get(resource, channel)
return channel, err
}
11 changes: 10 additions & 1 deletion rtm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ type RTMClient struct {
// rtm api base, defaults to `https://rtm.bearychat.com`
APIBase string

// services
CurrentTeam *RTMCurrentTeamService
User *RTMUserService
Channel *RTMChannelService

httpClient *http.Client
}

type rtmOptSetter func(*RTMClient) error

// enabled services
var services = []rtmOptSetter{}
var services = []rtmOptSetter{
newRTMCurrentTeamService,
newRTMUserService,
newRTMChannelService,
}

// NewRTMClient creates a rtm client.
//
Expand Down
7 changes: 7 additions & 0 deletions rtm_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func TestNewRTMClient(t *testing.T) {
if c.APIBase != DEFAULT_RTM_API_BASE {
t.Errorf("should use default rtm api base: %s", c.APIBase)
}

if c.CurrentTeam == nil {
t.Errorf("should create current team service")
}
if c.User == nil {
t.Errorf("should create user service")
}
}

func TestNewRTMClient_error(t *testing.T) {
Expand Down
32 changes: 32 additions & 0 deletions rtm_current_team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bearychat

type RTMCurrentTeamService struct {
rtm *RTMClient
}

func newRTMCurrentTeamService(rtm *RTMClient) error {
rtm.CurrentTeam = &RTMCurrentTeamService{rtm}

return nil
}

// Retrieves current team's information.
func (s *RTMCurrentTeamService) Info() (*Team, error) {
team := new(Team)
_, err := s.rtm.Get("v1/current_team.info", team)
return team, err
}

// Retrieves current team's members.
func (s *RTMCurrentTeamService) Members() ([]*User, error) {
members := []*User{}
_, err := s.rtm.Get("v1/current_team.members?all=true", &members)
return members, err
}

// Retrieves current team's channels.
func (s *RTMCurrentTeamService) Channels() ([]*Channel, error) {
channels := []*Channel{}
_, err := s.rtm.Get("v1/current_team.channels", &channels)
return channels, err
}
19 changes: 19 additions & 0 deletions rtm_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package bearychat

import "fmt"

type RTMUserService struct {
rtm *RTMClient
}

func newRTMUserService(rtm *RTMClient) error {
rtm.User = &RTMUserService{rtm}
return nil
}

func (s *RTMUserService) Info(userId string) (*User, error) {
user := new(User)
resource := fmt.Sprintf("v1/user.info?user_id=%s", userId)
_, err := s.rtm.Get(resource, user)
return user, err
}

0 comments on commit 8853da0

Please sign in to comment.