Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Mar 9, 2017
2 parents 524f629 + fdd38fa commit f01a60f
Show file tree
Hide file tree
Showing 82 changed files with 2,665 additions and 1,517 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,rb,md,yml,yaml,feature}]
indent_style = space
indent_size = 2
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go
install: make test
sudo: false
go:
- 1.2
- 1.3
- 1.4

- 1.6.4
- 1.7.4
install:
- go get -u -t ./...
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
default: test
default: vet test

deps:
go get -t ./...
test:
go test -a ./...

test: deps
go test ./...
bench:
go test ./... -bench=. -run=NONE

vet:
go vet ./...
52 changes: 29 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Go OpenRTB 2.2
# Go OpenRTB v2.x

[![Build Status](https://travis-ci.org/bsm/openrtb.svg?branch=master)](https://travis-ci.org/bsm/openrtb)

Expand All @@ -8,39 +8,42 @@ OpenRTB implementation for Go

To install, use `go get`:

go get github.com/bsm/openrtb
```shell
go get github.com/bsm/openrtb
```

To update, use `go get -u`:

go get -u github.com/bsm/openrtb
## Usage

Import the package:

package main

import (
"github.com/bsm/openrtb"
)
```go
package main

## Example
import (
"log"
"github.com/bsm/openrtb"
)

// Handle a HTTP request
http.HandleFunc("/bid", func(w http.ResponseWriter, r *http.Request) {
defer r.Body().Close()
func main() {
file, err := os.Open("stored.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()

req, err := openrtb.ParseRequest(r.Body(), true)
if err != nil {
log.Println("ERROR %s", err.Error())
} else {
log.Println("INFO Received bid request %s", *req.Id)
}
var req *openrtb.BidRequest
err = json.NewDecoder(file).Decode(&req)
if err != nil {
log.Fatal(err)
}

w.WriteHeader(204) // respond with 'no bid'
})
log.Printf("%+v\n", req)
}
```

## Licence

Copyright (c) 2014 Black Square Media Ltd. All rights reserved.
Copyright (c) 2015 Black Square Media Ltd. All rights reserved.
(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining
Expand All @@ -61,3 +64,6 @@ Import the package:
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Some test examples were taken from:
https://code.google.com/p/openrtb/wiki/OpenRTB_Examples
51 changes: 0 additions & 51 deletions app.go

This file was deleted.

25 changes: 0 additions & 25 deletions app_test.go

This file was deleted.

68 changes: 68 additions & 0 deletions audio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package openrtb

import (
"encoding/json"
"errors"
)

// Validation errors
var (
ErrInvalidAudioNoMimes = errors.New("openrtb: audio has no mimes")
)

// The "audio" object must be included directly in the impression object
type Audio struct {
Mimes []string `json:"mimes"` // Content MIME types supported.
MinDuration int `json:"minduration,omitempty"` // Minimum video ad duration in seconds
MaxDuration int `json:"maxduration,omitempty"` // Maximum video ad duration in seconds
Protocols []int `json:"protocols,omitempty"` // Video bid response protocols
StartDelay int `json:"startdelay,omitempty"` // Indicates the start delay in seconds
Sequence int `json:"sequence,omitempty"` // Default: 1
BAttr []int `json:"battr,omitempty"` // Blocked creative attributes
MaxExtended int `json:"maxextended,omitempty"` // Maximum extended video ad duration
MinBitrate int `json:"minbitrate,omitempty"` // Minimum bit rate in Kbps
MaxBitrate int `json:"maxbitrate,omitempty"` // Maximum bit rate in Kbps
Delivery []int `json:"delivery,omitempty"` // List of supported delivery methods
CompanionAd []Banner `json:"companionad,omitempty"`
API []int `json:"api,omitempty"`
CompanionType []int `json:"companiontype,omitempty"`
MaxSequence int `json:"maxseq,omitempty"` // The maximumnumber of ads that canbe played in an ad pod.
Feed int `json:"feed,omitempty"` // Type of audio feed.
Stitched int `json:"stitched,omitempty"` // Indicates if the ad is stitched with audio content or delivered independently
NVol int `json:"nvol,omitempty"` // Volume normalization mode.
Ext Extension `json:"ext,omitempty"`
}

type jsonAudio Audio

// Validates the object
func (a *Audio) Validate() error {
if len(a.Mimes) == 0 {
return ErrInvalidAudioNoMimes
}
return nil
}

// MarshalJSON custom marshalling with normalization
func (a *Audio) MarshalJSON() ([]byte, error) {
a.normalize()
return json.Marshal((*jsonAudio)(a))
}

// UnmarshalJSON custom unmarshalling with normalization
func (a *Audio) UnmarshalJSON(data []byte) error {
var h jsonAudio
if err := json.Unmarshal(data, &h); err != nil {
return err
}

*a = (Audio)(h)
a.normalize()
return nil
}

func (a *Audio) normalize() {
if a.Sequence == 0 {
a.Sequence = 1
}
}
58 changes: 58 additions & 0 deletions audio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package openrtb

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Audio", func() {
var subject *Audio

BeforeEach(func() {
err := fixture("audio", &subject)
Expect(err).NotTo(HaveOccurred())
})

It("should parse correctly", func() {
Expect(subject).To(Equal(&Audio{
Mimes: []string{
"audio/mp4",
},
MinDuration: 5,
MaxDuration: 30,
Protocols: []int{AudioProtocolDAAST1, AudioProtocolDAAST1Wrapper},
Sequence: 1,
BAttr: []int{13, 14},
MaxExtended: 30,
MinBitrate: 300,
MaxBitrate: 1500,
Delivery: []int{2},
CompanionAd: []Banner{
{W: 300, H: 250, ID: "1234567893-1", Pos: 1, BAttr: []int{13, 14}, ExpDir: []int{ExpDirRight, ExpDirDown}},
{W: 728, H: 90, ID: "1234567893-2", Pos: 1, BAttr: []int{13, 14}},
},
API: []int{1, 2},
CompanionType: []int{1, 2},
}))
})

It("should validate", func() {
Expect((&Audio{
MinDuration: 5,
MaxDuration: 30,
Protocols: []int{AudioProtocolDAAST1, AudioProtocolDAAST1Wrapper},
Sequence: 1,
BAttr: []int{13, 14},
MaxExtended: 30,
MinBitrate: 300,
MaxBitrate: 1500,
Delivery: []int{2},
CompanionAd: []Banner{
{W: 300, H: 250, ID: "1234567893-1", Pos: 1, BAttr: []int{13, 14}, ExpDir: []int{ExpDirRight, ExpDirDown}},
{W: 728, H: 90, ID: "1234567893-2", Pos: 1, BAttr: []int{13, 14}},
},
CompanionType: []int{1, 2},
}).Validate()).To(Equal(ErrInvalidAudioNoMimes))
})

})
60 changes: 16 additions & 44 deletions banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,20 @@ package openrtb
// VAST response to dictate placement of the companion creatives when multiple companion ad
// opportunities of the same size are available on a page.
type Banner struct {
W *int `json:"w,omitempty"` // Width
H *int `json:"h,omitempty"` // Height
Wmax *int `json:"wmax,omitempty"` // Width maximum
Hmax *int `json:"hmax,omitempty"` // Height maximum
Wmin *int `json:"wmin,omitempty"` // Width minimum
Hmin *int `json:"hmin,omitempty"` // Height minimum
Id *string `json:"id,omitempty"` // A unique identifier
Pos *int `json:"pos,omitempty"` // Ad Position
Btype []int `json:"btype,omitempty"` // Blocked creative types
Battr []int `json:"battr,omitempty"` // Blocked creative attributes
Mimes []string `json:"mimes,omitempty"` // Whitelist of content MIME types supported
Topframe *int `json:"topframe,omitempty"` // Default: 0 ("1": Delivered in top frame, "0": Elsewhere)
Expdir []int `json:"expdir,omitempty"` // Specify properties for an expandable ad
Api []int `json:"api,omitempty"` // List of supported API frameworks
Ext Extensions `json:"ext,omitempty"`
}

// Returns topframe status, with default fallback
func (b *Banner) IsTopFrame() bool {
if b.Topframe != nil {
return *b.Topframe == 1
}
return false
}

// Returns the position, with default fallback
func (b *Banner) Position() int {
if b.Pos != nil {
return *b.Pos
}
return AD_POS_UNKNOWN
}

// Applies defaults
func (b *Banner) WithDefaults() *Banner {
if b.Topframe == nil {
b.Topframe = new(int)
*b.Topframe = 0
}
if b.Pos == nil {
b.Pos = new(int)
*b.Pos = AD_POS_UNKNOWN
}
return b
W int `json:"w,omitempty"` // Width
H int `json:"h,omitempty"` // Height
Format []Format `json:"format,omitempty"` //Array of format objects representing the banner sizes permitted.
WMax int `json:"wmax,omitempty"` // Width maximum DEPRECATED
HMax int `json:"hmax,omitempty"` // Height maximum DEPRECATED
WMin int `json:"wmin,omitempty"` // Width minimum DEPRECATED
HMin int `json:"hmin,omitempty"` // Height minimum DEPRECATED
ID string `json:"id,omitempty"` // A unique identifier
BType []int `json:"btype,omitempty"` // Blocked creative types
BAttr []int `json:"battr,omitempty"` // Blocked creative attributes
Pos int `json:"pos,omitempty"` // Ad Position
Mimes []string `json:"mimes,omitempty"` // Whitelist of content MIME types supported
TopFrame int `json:"topframe,omitempty"` // Default: 0 ("1": Delivered in top frame, "0": Elsewhere)
ExpDir []int `json:"expdir,omitempty"` // Specify properties for an expandable ad
Api []int `json:"api,omitempty"` // List of supported API frameworks
Ext Extension `json:"ext,omitempty"`
}
Loading

0 comments on commit f01a60f

Please sign in to comment.