Refactor to make the maintanance simpler

- Fixed issue with webhooks
This commit is contained in:
Claudemiro
2018-11-25 17:40:43 +01:00
parent 4523549f71
commit 8da763ec2f
40 changed files with 1973 additions and 1820 deletions
+37
View File
@@ -0,0 +1,37 @@
// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package connection
import (
"time"
log "github.com/golang/glog"
)
// Socket interface to write to the client
type Socket interface {
WriteJSON(interface{}) error
}
// Connection An user connection
type Connection struct {
SocketID string
Socket Socket
CreatedAt time.Time
}
// Create a new Subscriber
func New(socketID string, s Socket) *Connection {
log.Infof("Creating a new Subscriber %+v", socketID)
return &Connection{SocketID: socketID, Socket: s, CreatedAt: time.Now()}
}
// Publish the message to websocket attached to this client
func (conn *Connection) Publish(m interface{}) {
if err := conn.Socket.WriteJSON(m); err != nil {
log.Errorf("error writing json into Socket, %+v", err)
}
}
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2016 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package connection
import (
"ipe/mocks"
"testing"
)
func TestNewConnection(t *testing.T) {
expectedSocketID := "socketID"
expectedSocket := mocks.MockSocket{}
c := New(expectedSocketID, expectedSocket)
if c.SocketID != expectedSocketID {
t.Errorf("c.SocketID == %s, wants %s", c.SocketID, expectedSocketID)
}
if c.Socket != expectedSocket {
t.Errorf("c.Socket == %v, wants %v", c.Socket, expectedSocket)
}
if c.CreatedAt.IsZero() {
t.Errorf("c.createdAt.IsZero() == %t, wants %t", c.CreatedAt.IsZero(), false)
}
}