Skip to content

Commit

Permalink
adding common patterns of parsing data to provile resource, updating …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
Daniel1984 authored and JacobPlaster committed Aug 24, 2020
1 parent 38b2b18 commit 936d850
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pkg/models/pulse/pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var pulseFields = map[string]int{
"PulseProfile": 18,
}

// NewSingleFromRaw returns pointer to Pulse message
func NewSingleFromRaw(raw []interface{}) (*Pulse, error) {
// FromRaw returns pointer to Pulse message
func FromRaw(raw []interface{}) (*Pulse, error) {
if len(raw) < 19 {
return nil, fmt.Errorf("data slice too short for Pulse Message: %#v", raw)
}
Expand Down Expand Up @@ -83,8 +83,8 @@ func NewSingleFromRaw(raw []interface{}) (*Pulse, error) {
return p, nil
}

// NewFromRaw returns slice of Pulse message pointers
func NewFromRaw(raws []interface{}) ([]*Pulse, error) {
// SnapshotFromRaw returns slice of Pulse message pointers
func SnapshotFromRaw(raws []interface{}) ([]*Pulse, error) {
if len(raws) < 1 {
return nil, fmt.Errorf("data slice is too short for Pulse History: %#v", raws)
}
Expand All @@ -93,7 +93,7 @@ func NewFromRaw(raws []interface{}) ([]*Pulse, error) {

for _, raw := range raws {
raw := raw.([]interface{})
p, err := NewSingleFromRaw(raw)
p, err := FromRaw(raw)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/models/pulse/pulse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestNewPulseFromRaw(t *testing.T) {
[]interface{}{"abc123"},
}

pm, err := pulse.NewFromRaw(payload)
pm, err := pulse.SnapshotFromRaw(payload)
require.NotNil(t, err)
require.Nil(t, pm)
})
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestNewPulseFromRaw(t *testing.T) {
},
}

pm, err := pulse.NewFromRaw(payload)
pm, err := pulse.SnapshotFromRaw(payload)
require.Nil(t, err)

expected := &pulse.Pulse{
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestNewPulseFromRaw(t *testing.T) {
},
}

pm, err := pulse.NewFromRaw(payload)
pm, err := pulse.SnapshotFromRaw(payload)
require.Nil(t, err)

expected := &pulse.Pulse{
Expand All @@ -127,6 +127,8 @@ func TestNewPulseFromRaw(t *testing.T) {
Picture: "picture",
Text: "text",
TwitterHandle: "twitter",
Followers: 30,
Following: 5,
},
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/models/pulseprofile/pulseprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type PulseProfile struct {
Picture string
Text string
TwitterHandle string
Followers int64
Following int64
}

var pulseProfileFields = map[string]int{
Expand All @@ -23,6 +25,8 @@ var pulseProfileFields = map[string]int{
"Picture": 5,
"Text": 6,
"TwitterHandle": 9,
"Followers": 11,
"Following": 12,
}

// NewFromRaw takes in slice of interfaces and converts them to
Expand All @@ -40,6 +44,8 @@ func NewFromRaw(raw []interface{}) (*PulseProfile, error) {
pp.Picture = convert.SValOrEmpty(raw[pulseProfileFields["Picture"]])
pp.Text = convert.SValOrEmpty(raw[pulseProfileFields["Text"]])
pp.TwitterHandle = convert.SValOrEmpty(raw[pulseProfileFields["TwitterHandle"]])
pp.Followers = convert.I64ValOrZero(raw[pulseProfileFields["Followers"]])
pp.Following = convert.I64ValOrZero(raw[pulseProfileFields["Following"]])

return pp, nil
}
2 changes: 2 additions & 0 deletions pkg/models/pulseprofile/pulseprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestNewProfileFromRaw(t *testing.T) {
Picture: "picture",
Text: "text",
TwitterHandle: "twitter",
Followers: 30,
Following: 5,
}
assert.Equal(t, expected, pp)
})
Expand Down
6 changes: 3 additions & 3 deletions v2/rest/pulse.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ps *PulseService) PublicPulseHistory(limit int, end common.Mts) ([]*pulse.
return nil, err
}

pph, err := pulse.NewFromRaw(raw)
pph, err := pulse.SnapshotFromRaw(raw)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (ps *PulseService) AddPulse(p *pulse.Pulse) (*pulse.Pulse, error) {
return nil, err
}

pm, err := pulse.NewSingleFromRaw(raw)
pm, err := pulse.FromRaw(raw)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (ps *PulseService) PulseHistory(isPublic bool) ([]*pulse.Pulse, error) {
return nil, err
}

pph, err := pulse.NewFromRaw(raw)
pph, err := pulse.SnapshotFromRaw(raw)
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions v2/rest/pulse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestPublicPulseProfile(t *testing.T) {
Picture: "picture",
Text: "text",
TwitterHandle: "twitter",
Followers: 30,
Following: 5,
}
assert.Equal(t, expected, pp)
})
Expand Down Expand Up @@ -400,6 +402,8 @@ func TestPulseHistory(t *testing.T) {
Picture: "picture",
Text: "text",
TwitterHandle: "twitter",
Followers: 30,
Following: 5,
},
}

Expand Down

0 comments on commit 936d850

Please sign in to comment.