From d148dc0c24852d99a8887bb097ab95a5bf637a87 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 17 Oct 2014 08:54:06 -0700 Subject: [PATCH] add octocat and zen endpoints okay, so these aren't really that useful buy hey, why not? :) MMM. .MMM MMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMM __________ MMMMMMMMMMMMMMMMMMMMM | | MMMMMMMMMMMMMMMMMMMMMMM | Ship It! | MMMMMMMMMMMMMMMMMMMMMMMM |_ ______| MMMM::- -:::::::- -::MMMM |/ MM~:~ ~:::::~ ~:~MM .. MMMMM::. .:::+:::. .::MMMMM .. .MM::::: ._. :::::MM. MMMM;:::::;MMMM -MM MMMMMMM ^ M+ MMMMMMMMM MMMMMMM MM MM MM MM MM MM MM MM MM MM MM .~~MM~MM~MM~MM~~. ~~~~MM:~MM~~~MM~:MM~~~~ ~~~~~~==~==~~~==~==~~~~~~ ~~~~~~==~==~==~==~~~~~~ :~==~==~==~==~~ --- github/misc.go | 48 ++++++++++++++++++++++++++++++++++++++++++--- github/misc_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/github/misc.go b/github/misc.go index 1cec5a2..4a9bb99 100644 --- a/github/misc.go +++ b/github/misc.go @@ -7,6 +7,8 @@ package github import ( "bytes" + "fmt" + "net/url" ) // MarkdownOptions specifies optional parameters to the Markdown method. @@ -48,7 +50,7 @@ func (c *Client) Markdown(text string, opt *MarkdownOptions) (string, *Response, } } - req, err := c.NewRequest("POST", "/markdown", request) + req, err := c.NewRequest("POST", "markdown", request) if err != nil { return "", nil, err } @@ -66,7 +68,7 @@ func (c *Client) Markdown(text string, opt *MarkdownOptions) (string, *Response, // // GitHub API docs: https://developer.github.com/v3/emojis/ func (c *Client) ListEmojis() (map[string]string, *Response, error) { - req, err := c.NewRequest("GET", "/emojis", nil) + req, err := c.NewRequest("GET", "emojis", nil) if err != nil { return nil, nil, err } @@ -104,7 +106,7 @@ type APIMeta struct { // // GitHub API docs: https://developer.github.com/v3/meta/ func (c *Client) APIMeta() (*APIMeta, *Response, error) { - req, err := c.NewRequest("GET", "/meta", nil) + req, err := c.NewRequest("GET", "meta", nil) if err != nil { return nil, nil, err } @@ -117,3 +119,43 @@ func (c *Client) APIMeta() (*APIMeta, *Response, error) { return meta, resp, nil } + +// Octocat returns an ASCII art octocat with the specified message in a speech +// bubble. If message is empty, a random zen phrase is used. +func (c *Client) Octocat(message string) (string, *Response, error) { + u := "octocat" + if message != "" { + u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message)) + } + + req, err := c.NewRequest("GET", u, nil) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + resp, err := c.Do(req, buf) + if err != nil { + return "", resp, err + } + + return buf.String(), resp, nil +} + +// Zen returns a random line from The Zen of GitHub. +// +// see also: http://warpspire.com/posts/taste/ +func (c *Client) Zen() (string, *Response, error) { + req, err := c.NewRequest("GET", "zen", nil) + if err != nil { + return "", nil, err + } + + buf := new(bytes.Buffer) + resp, err := c.Do(req, buf) + if err != nil { + return "", resp, err + } + + return buf.String(), resp, nil +} diff --git a/github/misc_test.go b/github/misc_test.go index 9b7e5c9..33c3db6 100644 --- a/github/misc_test.go +++ b/github/misc_test.go @@ -89,3 +89,49 @@ func TestAPIMeta(t *testing.T) { t.Errorf("APIMeta returned %+v, want %+v", meta, want) } } + +func TestOctocat(t *testing.T) { + setup() + defer teardown() + + input := "input" + output := "sample text" + + mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"s": input}) + w.Header().Set("Content-Type", "application/octocat-stream") + fmt.Fprint(w, output) + }) + + got, _, err := client.Octocat(input) + if err != nil { + t.Errorf("Octocat returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Octocat returned %+v, want %+v", got, want) + } +} + +func TestZen(t *testing.T) { + setup() + defer teardown() + + output := "sample text" + + mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.Header().Set("Content-Type", "text/plain;charset=utf-8") + fmt.Fprint(w, output) + }) + + got, _, err := client.Zen() + if err != nil { + t.Errorf("Zen returned error: %v", err) + } + + if want := output; got != want { + t.Errorf("Zen returned %+v, want %+v", got, want) + } +}