Skip to content

Commit

Permalink
Improve API with issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jan 4, 2017
1 parent e676e2e commit 6295b42
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 197 deletions.
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ import (
"github.com/go-http-utils/negotiator"
)

negotiator := negotiator.New(req)
negotiator := negotiator.New(req.Header)
```

### Accept
### Type

```go
// Assume that the Accept header is "text/html, application/*;q=0.9, image/jpeg;q=0.8"

negotiator.Accept([]string{"text/html", "application/json", "image/jpeg"})
negotiator.Type()
// -> "text/html"

negotiator.Accept([]string{"application/json", "image/jpeg", "text/plain"})
negotiator.Type("text/html", "application/json", "image/jpeg")
// -> "text/html"

negotiator.Type("application/json", "image/jpeg", "text/plain")
// -> "application/json"

negotiator.Accept([]string{"text/plain"})
negotiator.Type("text/plain")
// -> ""
```

Expand All @@ -44,10 +47,13 @@ negotiator.Accept([]string{"text/plain"})
```go
// Assume that the Accept-Encoding header is "gzip, compress;q=0.2, identity;q=0.5"

negotiator.Encoding([]string{"identity", "gzip"})
negotiator.Encoding()
// -> "gzip"

negotiator.Encoding("identity", "gzip")
// -> "gzip"

negotiator.Encoding([]string{"compress", "identity"})
negotiator.Encoding("compress", "identity")
// -> "identity"
```

Expand All @@ -56,10 +62,13 @@ negotiator.Encoding([]string{"compress", "identity"})
```go
// Assume that the Accept-Language header is "en;q=0.8, es, pt"

negotiator.Language([]string{"en", "es", "fr"})
negotiator.Language()
// -> "es"

negotiator.Language([]string{"es", "pt"})
negotiator.Language("en", "es", "fr")
// -> "es"

negotiator.Language("es", "pt")
// -> "es"
```

Expand All @@ -68,9 +77,12 @@ negotiator.Language([]string{"es", "pt"})
```go
// Assume that the Accept-Charset header is "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2"

negotiator.Charset([]string{"utf-8", "iso-8859-1", "iso-8859-5"})
negotiator.Charset()
// -> "utf-8"

negotiator.Charset("utf-8", "iso-8859-1", "iso-8859-5")
// -> "utf-8"

negotiator.Charset([]string{"iso-8859-5"})
negotiator.Charset("iso-8859-5")
// -> ""
```
26 changes: 13 additions & 13 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ import (
func ExampleNegotiator_Accept() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept", "text/html, application/*;q=0.9, image/jpeg;q=0.8")
negotiator := negotiator.New(req)
negotiator := negotiator.New(req.Header)

fmt.Println(negotiator.Accept([]string{"text/html", "application/json", "image/jpeg"}))
fmt.Println(negotiator.Type("text/html", "application/json", "image/jpeg"))
// -> "text/html"

fmt.Println(negotiator.Accept([]string{"application/json", "image/jpeg", "text/plain"}))
fmt.Println(negotiator.Type("application/json", "image/jpeg", "text/plain"))
// -> "application/json"

fmt.Println(negotiator.Accept([]string{"text/plain"}))
fmt.Println(negotiator.Type("text/plain"))
// -> ""
}

func ExampleNegotiator_Encoding() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "gzip, compress;q=0.2, identity;q=0.5")
negotiator := negotiator.New(req)
negotiator := negotiator.New(req.Header)

fmt.Println(negotiator.Encoding([]string{"identity", "gzip"}))
fmt.Println(negotiator.Encoding("identity", "gzip"))
// -> "gzip"

fmt.Println(negotiator.Encoding([]string{"compress", "identity"}))
fmt.Println(negotiator.Encoding("compress", "identity"))
// -> "identity"
}

func ExampleNegotiator_Language() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Language", "en;q=0.8, es, pt")
negotiator := negotiator.New(req)
negotiator := negotiator.New(req.Header)

fmt.Println(negotiator.Language([]string{"en", "es", "fr"}))
fmt.Println(negotiator.Language("en", "es", "fr"))
// -> "es"

fmt.Println([]string{"es", "pt"})
fmt.Println(negotiator.Language("es", "pt"))
// -> "es"
}

func ExampleNegotiator_Charset() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Language", "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2")
negotiator := negotiator.New(req)
negotiator := negotiator.New(req.Header)

fmt.Println(negotiator.Charset([]string{"UTF-8", "ISO-8859-1", "ISO-8859-5"}))
fmt.Println(negotiator.Charset("UTF-8", "ISO-8859-1", "ISO-8859-5"))
// -> "UTF-8"

fmt.Println(negotiator.Charset([]string{"ISO-8859-5"}))
fmt.Println(negotiator.Charset("ISO-8859-5"))
// -> ""
}
35 changes: 16 additions & 19 deletions negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,38 @@ func formatHeaderVal(val string) string {

// Negotiator repensents the HTTP negotiator.
type Negotiator struct {
req *http.Request
header http.Header
}

// New creates an instance of Negotiator.
func New(req *http.Request) Negotiator {
return Negotiator{req}
func New(header http.Header) *Negotiator {
return &Negotiator{header}
}

// Accept returns the most preferred content types from the HTTP Accept header.
func (n Negotiator) Accept(offers []string) (bestOffer string, matched bool) {
parser := newHeaderParser(n.req.Header, true)

// Type returns the most preferred content type from the HTTP Accept header.
// If nothing accepted, then empty string is returned.
func (n *Negotiator) Type(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, true)
return parser.selectOffer(offers, parser.parse(headers.Accept))
}

// Language returns the most preferred language from the HTTP Accept-Language
// header.
func (n Negotiator) Language(offers []string) (bestOffer string, matched bool) {
parser := newHeaderParser(n.req.Header, false)

// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Language(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptLanguage))
}

// Encoding returns the most preferred language from the HTTP Accept-Encoding
// header.
func (n Negotiator) Encoding(offers []string) (bestOffer string, matched bool) {
parser := newHeaderParser(n.req.Header, false)

// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Encoding(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptEncoding))
}

// Charset returns the most preferred language from the HTTP Accept-Charset
// header.
func (n Negotiator) Charset(offers []string) (bestOffer string, matched bool) {
parser := newHeaderParser(n.req.Header, false)

// header. If nothing accepted, then empty string is returned.
func (n *Negotiator) Charset(offers ...string) (bestOffer string) {
parser := newHeaderParser(n.header, false)
return parser.selectOffer(offers, parser.parse(headers.AcceptCharset))
}
Loading

0 comments on commit 6295b42

Please sign in to comment.