Skip to content

Commit c4d667d

Browse files
committed
Add native object to impression
1 parent e7b572a commit c4d667d

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-0
lines changed

asset.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package openrtb
2+
3+
// TODO: Add description, use cases and other details
4+
type Asset struct {
5+
Id *int `json:"id"` // Unique asset ID, assigned by exchange
6+
Required *int `json:"required,omitempty"` // Set to 1 if asset is required
7+
Title *Title `json:"title,omitempty"` // Title object for title assets
8+
Img *Image `json:"img,omitempty"` // Image object for image assets
9+
Video *NativeVideo `json:"video,omitempty"` // Video object for video assets
10+
Data *NativeData `json:"data,omitempty"` // Data object for ratings, price, etc.
11+
Ext Extensions `json:"ext,omitempty"`
12+
}
13+
14+
type Title struct {
15+
Len *int `json:"len"` // Maximum length of the text in the title element
16+
Ext Extensions `json:"ext,omitempty"`
17+
}
18+
19+
type Image struct {
20+
Type *int `json:"type,omitempty"` // Type ID of the image element supported by the publisher
21+
W *int `json:"w,omitempty"` // Width
22+
H *int `json:"h,omitempty"` // Height
23+
Wmax *int `json:"wmax,omitempty"` // Width maximum
24+
Hmax *int `json:"hmax,omitempty"` // Height maximum
25+
Wmin *int `json:"wmin,omitempty"` // Width minimum
26+
Hmin *int `json:"hmin,omitempty"` // Height minimum
27+
Mimes []string `json:"mimes,omitempty"` // Whitelist of content MIME types supported
28+
Ext Extensions `json:"ext,omitempty"`
29+
}
30+
31+
type NativeData struct {
32+
Type *int `json:"type"` // Type ID of the image element supported by the publisher
33+
Len *int `json:"len,omitempty"` // Maximum length of the text in the element’s response
34+
Ext Extensions `json:"ext,omitempty"`
35+
}
36+
37+
// Applies defaults
38+
func (a *Asset) WithDefaults() *Asset {
39+
if a.Required == nil {
40+
a.Required = new(int)
41+
*a.Required = 0
42+
}
43+
return a
44+
}

asset_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package openrtb
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Asset", func() {
9+
var subject *Asset
10+
11+
BeforeEach(func() {
12+
subject = new(Asset)
13+
})
14+
15+
It("should have defaults", func() {
16+
subject.WithDefaults()
17+
Expect(*subject.Required).To(Equal(0))
18+
})
19+
})

impression.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Impression struct {
1313
Id *string `json:"id"` // A unique identifier for this impression
1414
Banner *Banner `json:"banner,omitempty"`
1515
Video *Video `json:"video,omitempty"`
16+
Native *Native `json:"native,omitempty"`
1617
Displaymanager *string `json:"displaymanager,omitempty"` // Name of ad mediation partner, SDK technology, etc
1718
Displaymanagerver *string `json:"displaymanagerver,omitempty"` // Version of the above
1819
Instl *int `json:"instl,omitempty"` // Interstitial, Default: 0 ("1": Interstitial, "0": Something else)

native.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package openrtb
2+
3+
// TODO: Add description, use cases and other details
4+
type Native struct {
5+
Ver *string `json:"ver,omitempty"` // Version of the Native Markup version in use
6+
Layout *int `json:"layout,omitempty"` // Layout ID of the native ad unit
7+
Adunit *int `json:"adunit,omitempty"` // Ad unit ID of the native ad unit
8+
Plcmtcnt *int `json:"plcmtcnt,omitempty"` // Number of identical placements in this Layout
9+
Seq *int `json:"seq,omitempty"` // This is not the sequence number of the content in the stream
10+
Assets []Asset `json:"assets"` // Array of Asset Objects
11+
Ext Extensions `json:"ext,omitempty"`
12+
}
13+
14+
// Applies defaults
15+
func (n *Native) WithDefaults() *Native {
16+
if n.Ver == nil {
17+
n.Ver = new(string)
18+
*n.Ver = "1"
19+
}
20+
if n.Plcmtcnt == nil {
21+
n.Plcmtcnt = new(int)
22+
*n.Plcmtcnt = 1
23+
}
24+
if n.Seq == nil {
25+
n.Seq = new(int)
26+
*n.Seq = 0
27+
}
28+
return n
29+
}

native_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package openrtb
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("Native", func() {
9+
var subject *Native
10+
11+
BeforeEach(func() {
12+
subject = new(Native)
13+
})
14+
15+
It("should have defaults", func() {
16+
subject.WithDefaults()
17+
Expect(*subject.Ver).To(Equal("1"))
18+
Expect(*subject.Plcmtcnt).To(Equal(1))
19+
Expect(*subject.Seq).To(Equal(0))
20+
})
21+
})

native_video.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package openrtb
2+
3+
// The "video" object must be included directly in the impression object if the impression offered
4+
// for auction is an in-stream video ad opportunity.
5+
type NativeVideo struct {
6+
Mimes []string `json:"mimes"` // Content MIME types supported.
7+
Minduration *int `json:"minduration"` // Minimum video ad duration in seconds
8+
Maxduration *int `json:"maxduration"` // Maximum video ad duration in seconds
9+
Protocol *int `json:"protocol,omitempty"` // Video bid response protocols
10+
Protocols []int `json:"protocols"` // Video bid response protocols
11+
W *int `json:"w,omitempty"` // Width of the player in pixels
12+
H *int `json:"h,omitempty"` // Height of the player in pixels
13+
Startdelay *int `json:"startdelay,omitempty"` // Indicates the start delay in seconds
14+
Linearity *int `json:"linearity,omitempty"` // Indicates whether the ad impression is linear or non-linear
15+
Sequence *int `json:"sequence,omitempty"` // Default: 1
16+
Battr []int `json:"battr,omitempty"` // Blocked creative attributes
17+
Maxextended *int `json:"maxextended,omitempty"` // Maximum extended video ad duration
18+
Minbitrate *int `json:"minbitrate,omitempty"` // Minimum bit rate in Kbps
19+
Maxbitrate *int `json:"maxbitrate,omitempty"` // Maximum bit rate in Kbps
20+
Boxingallowed *int `json:"boxingallowed,omitempty"` // If exchange publisher has rules preventing letter boxing
21+
Playbackmethod []int `json:"playbackmethod,omitempty"` // List of allowed playback methods
22+
Delivery []int `json:"delivery,omitempty"` // List of supported delivery methods
23+
Pos *int `json:"pos,omitempty"` // Ad Position
24+
Companionad []Banner `json:"companionad,omitempty"`
25+
Api []int `json:"api,omitempty"` // List of supported API frameworks
26+
Companiontype []int `json:"companiontype,omitempty"`
27+
Ext Extensions `json:"ext,omitempty"`
28+
}
29+
30+
// Returns the sequence number, with default fallback
31+
func (v *NativeVideo) Seq() int {
32+
if v.Sequence != nil {
33+
return *v.Sequence
34+
}
35+
return 1
36+
}
37+
38+
// Returns the boxing permission status, with default fallback
39+
func (v *NativeVideo) IsBoxingAllowed() bool {
40+
if v.Boxingallowed != nil {
41+
return *v.Boxingallowed == 1
42+
}
43+
return true
44+
}
45+
46+
// Returns the position, with default fallback
47+
func (v *NativeVideo) Position() int {
48+
if v.Pos != nil {
49+
return *v.Pos
50+
}
51+
return AD_POS_UNKNOWN
52+
}
53+
54+
// Validates the object
55+
func (v *NativeVideo) Valid() (bool, error) {
56+
if len(v.Mimes) == 0 {
57+
return false, ErrInvalidVideoMimes
58+
} else if v.Linearity == nil {
59+
return false, ErrInvalidVideoLinearity
60+
} else if v.Minduration == nil {
61+
return false, ErrInvalidVideoMinduration
62+
} else if v.Maxduration == nil {
63+
return false, ErrInvalidVideoMaxduration
64+
} else if v.Protocol == nil {
65+
return false, ErrInvalidVideoProtocol
66+
}
67+
return true, nil
68+
}
69+
70+
// Applies defaults
71+
func (v *NativeVideo) WithDefaults() *NativeVideo {
72+
if v.Sequence == nil {
73+
v.Sequence = new(int)
74+
*v.Sequence = 1
75+
}
76+
if v.Boxingallowed == nil {
77+
v.Boxingallowed = new(int)
78+
*v.Boxingallowed = 1
79+
}
80+
if v.Pos == nil {
81+
v.Pos = new(int)
82+
*v.Pos = AD_POS_UNKNOWN
83+
}
84+
return v
85+
}
86+
87+
// Set the Linearity
88+
func (v *NativeVideo) SetLinearity(lin int) *NativeVideo {
89+
if v.Linearity == nil {
90+
v.Linearity = new(int)
91+
}
92+
*v.Linearity = lin
93+
return v
94+
}
95+
96+
// Set the Minduration
97+
func (v *NativeVideo) SetMinduration(mdur int) *NativeVideo {
98+
if v.Minduration == nil {
99+
v.Minduration = new(int)
100+
}
101+
*v.Minduration = mdur
102+
return v
103+
}
104+
105+
// Set the Maxduration
106+
func (v *NativeVideo) SetMaxduration(mdur int) *NativeVideo {
107+
if v.Maxduration == nil {
108+
v.Maxduration = new(int)
109+
}
110+
*v.Maxduration = mdur
111+
return v
112+
}
113+
114+
// Set the Protocol
115+
func (v *NativeVideo) SetProtocol(p int) *NativeVideo {
116+
if v.Protocol == nil {
117+
v.Protocol = new(int)
118+
}
119+
*v.Protocol = p
120+
return v
121+
}

native_video_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package openrtb
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
)
7+
8+
var _ = Describe("NativeVideo", func() {
9+
var subject *NativeVideo
10+
11+
BeforeEach(func() { subject = new(NativeVideo) })
12+
13+
Describe("Seq()", func() {
14+
It("should return '1' as default", func() {
15+
Expect(subject.Seq()).To(Equal(1))
16+
})
17+
})
18+
19+
Describe("IsBoxingAllowed()", func() {
20+
It("should return true as default", func() {
21+
Expect(subject.IsBoxingAllowed()).To(BeTrue())
22+
})
23+
24+
It("should return false when set as false", func() {
25+
subject.Boxingallowed = new(int)
26+
*subject.Boxingallowed = 0
27+
Expect(subject.IsBoxingAllowed()).To(BeFalse())
28+
})
29+
})
30+
31+
Describe("Position()", func() {
32+
It("should return 0/AD_POST_UNKNOWN as default", func() {
33+
Expect(subject.Position()).To(Equal(0))
34+
})
35+
})
36+
37+
Describe("Valid()", func() {
38+
It("should return error messages when attributes missing", func() {
39+
ok, err := subject.Valid()
40+
Expect(err.Error()).To(Equal("openrtb parse: video has no mimes"))
41+
42+
subject.Mimes = []string{"RAND_KEY"} // With Mimes
43+
ok, err = subject.Valid()
44+
Expect(err.Error()).To(Equal("openrtb parse: video linearity missing"))
45+
46+
subject.SetLinearity(2) // With Linearity
47+
ok, err = subject.Valid()
48+
Expect(err.Error()).To(Equal("openrtb parse: video minduration missing"))
49+
50+
subject.SetMinduration(1) // With Minduration
51+
ok, err = subject.Valid()
52+
Expect(err.Error()).To(Equal("openrtb parse: video maxduration missing"))
53+
54+
subject.SetMaxduration(5) // With Maxduration
55+
ok, err = subject.Valid()
56+
Expect(err.Error()).To(Equal("openrtb parse: video protocol missing"))
57+
58+
subject.SetProtocol(1) // With Protocol
59+
ok, err = subject.Valid()
60+
Expect(err).NotTo(HaveOccurred())
61+
Expect(ok).To(BeTrue())
62+
})
63+
})
64+
65+
Describe("WithDefaults()", func() {
66+
67+
It("should return object with default values", func() {
68+
subject.WithDefaults()
69+
Expect(*subject.Sequence).To(Equal(1))
70+
Expect(*subject.Boxingallowed).To(Equal(1))
71+
Expect(*subject.Pos).To(Equal(AD_POS_UNKNOWN))
72+
})
73+
})
74+
75+
})

0 commit comments

Comments
 (0)