Skip to content

Instantly share code, notes, and snippets.

@harlow
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlow/e45277d9ad1522c461e9 to your computer and use it in GitHub Desktop.
Save harlow/e45277d9ad1522c461e9 to your computer and use it in GitHub Desktop.
func (h *RESTHandler) finishReq(op *Operation, req *http.Request, w http.ResponseWriter) {
result, complete := op.StatusOrResult()
obj := result.Object
if complete {
status := http.StatusOK
if result.Created {
status = http.StatusCreated
}
switch stat := obj.(type) {
case *api.Status:
if stat.Code != 0 {
status = stat.Code
}
}
writeJSON(status, h.codec, obj, w)
} else {
writeJSON(http.StatusAccepted, h.codec, obj, w)
}
}
func finishStatus(r Result, complete bool) int {
if !complete {
return http.StatusAccepted
}
if stat, ok := r.Object.(*api.Status); ok && stat.Code != 0 {
return stat.Code
}
if r.Created {
return http.StatusCreated
}
return http.StatusOK
}
func (h *RESTHandler) finishReq(op *Operation, w http.ResponseWriter, req *http.Request) {
result, complete := op.StatusOrResult()
status := finishStatus(result, complete)
writeJSON(status, h.codec, result.Object, w)
}
func BrowserHeightBucket(s *session.Event) string {
browserSize := sizeFromSession(s)
if h := browserSize.GetHeight(); h > 0 {
browserHeight := int(h)
if browserHeight <= 480 {
return "small"
} else if browserHeight <= 640 {
return "medium"
} else {
return "large"
}
} else {
return "null"
}
}
func BrowserHeightBucket(s *session.Event) string {
size := sizeFromSession(s)
h := size.GetHeight()
switch {
case h <= 0:
return "null"
case h <= 480:
return "small"
case h <= 640:
return "medium"
default:
return "large"
}
}
@harlow
Copy link
Author

harlow commented Dec 14, 2014

func ExampleWrite() {
    var buf bytes.Buffer
    var pi float64 = math.Pi
    err := binary.Write(&buf, binary.LittleEndian, pi)
    if err != nil {
        fmt.Println("binary.Write failed:", err)
    }
    fmt.Printf("% x", buf.Bytes())
    // Output: 18 2d 44 54 fb 21 09 40
}

@harlow
Copy link
Author

harlow commented Dec 14, 2014

// Package math provides basic constants and mathematical functions.
package math

// A Request represents a request to run a command.
type Request struct { ..

// Encode writes the JSON encoding of req to w.
func Encode(w io.Writer, req *Request) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment