示例#1
0
文件: post.go 项目: minodisk/qiitactl
// ShowPosts outputs your posts fetched from Qiita to stdout.
func (r ShowPostsRunner) Run(c api.Client, o GlobalOptions, w io.Writer) (err error) {
	posts, err := model.FetchPosts(c, nil)
	if err != nil {
		return
	}
	err = printPosts(w, posts, nil)
	if err != nil {
		return
	}

	teams, err := model.FetchTeams(c)
	if err != nil {
		return
	}
	for _, team := range teams {
		posts, err = model.FetchPosts(c, &team)
		if err != nil {
			return
		}
		err = printPosts(w, posts, &team)
		if err != nil {
			return
		}
	}
	return
}
示例#2
0
文件: post.go 项目: minodisk/qiitactl
// FetchPosts fetches your posts from Qiita to current working directory.
func (r FetchPostsRunner) Run(c api.Client, o GlobalOptions, w io.Writer) (err error) {
	posts, err := model.FetchPosts(c, nil)
	if err != nil {
		return
	}
	err = posts.Save()
	if err != nil {
		return
	}

	teams, err := model.FetchTeams(c)
	if err != nil {
		return
	}
	for _, team := range teams {
		var posts model.Posts
		posts, err = model.FetchPosts(c, &team)
		if err != nil {
			return
		}
		err = posts.Save()
		if err != nil {
			return
		}
	}
	return
}
示例#3
0
func TestTeams_FetchTeams(t *testing.T) {
	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/teams", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			log.Fatalf("wrong method: %s", r.Method)
		}
		w.Write([]byte(`[
			{
				"active": true,
				"id": "increments",
				"name": "Increments Inc."
			}
		]`))
	})
	server := httptest.NewServer(mux)
	defer server.Close()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		t.Fatal(err)
	}

	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)

	teams, err := model.FetchTeams(client)
	if err != nil {
		t.Fatal(err)
	}

	if len(teams) != 1 {
		t.Fatalf("wrong length of teams: expected %d, but actual %d", 1, len(teams))
	}

	if teams[0].Active != true || teams[0].ID != "increments" || teams[0].Name != "Increments Inc." {
		t.Errorf("wrong team: %+v", teams[0])
	}
}
示例#4
0
func TestTeams_FetchTeamsError(t *testing.T) {
	mux := http.NewServeMux()
	mux.HandleFunc("/api/v2/teams", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(503)
	})
	server := httptest.NewServer(mux)
	defer server.Close()

	err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
	if err != nil {
		t.Fatal(err)
	}

	client := api.NewClient(func(subDomain, path string) (url string) {
		url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
		return
	}, inf)

	_, err = model.FetchTeams(client)
	if err == nil {
		t.Fatal("should occur error")
	}
}