Merge pull request #44 from 178inaba/public_timeline

Add GetTimelinePublic
pull/45/head
mattn 2017-04-26 09:36:19 +09:00 committed by GitHub
commit d0fb60d3ec
2 changed files with 46 additions and 1 deletions

View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"time"
"strconv" "strconv"
"time"
) )
// Status is struct to hold status. // Status is struct to hold status.
@ -157,6 +157,21 @@ func (c *Client) GetTimelineHome(ctx context.Context) ([]*Status, error) {
return statuses, nil return statuses, nil
} }
// GetTimelinePublic return statuses from public timeline.
func (c *Client) GetTimelinePublic(ctx context.Context, isLocal bool) ([]*Status, error) {
params := url.Values{}
if isLocal {
params.Set("local", "t")
}
var statuses []*Status
err := c.doAPI(ctx, http.MethodGet, "/api/v1/timelines/public", params, &statuses, nil)
if err != nil {
return nil, err
}
return statuses, nil
}
// GetTimelineHashtag return statuses from tagged timeline. // GetTimelineHashtag return statuses from tagged timeline.
func (c *Client) GetTimelineHashtag(ctx context.Context, tag string) ([]*Status, error) { func (c *Client) GetTimelineHashtag(ctx context.Context, tag string) ([]*Status, error) {
var statuses []*Status var statuses []*Status

View File

@ -327,6 +327,36 @@ func TestUnfavourite(t *testing.T) {
} }
} }
func TestGetTimelinePublic(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("local") == "" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
_, err := client.GetTimelinePublic(context.Background(), false)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
tl, err := client.GetTimelinePublic(context.Background(), true)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(tl) != 2 {
t.Fatalf("result should be two: %d", len(tl))
}
if tl[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", tl[0].Content)
}
if tl[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", tl[1].Content)
}
}
func TestGetTimelineHashtag(t *testing.T) { func TestGetTimelineHashtag(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/timelines/tag/zzz" { if r.URL.Path != "/api/v1/timelines/tag/zzz" {