Skip to content

Commit b41c7a8

Browse files
committed
Add my command
1 parent c36f570 commit b41c7a8

File tree

8 files changed

+154
-34
lines changed

8 files changed

+154
-34
lines changed

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ qiita my <query:optional>
4949
Run folloing commands to link this workflow to Alfred app manually.
5050

5151
```
52-
$ bundle install
53-
$ bundle exec rake link
52+
$ go get github.com/uetchy/alfred-qiita-workflow
53+
$ cd $GOPATH/github.com/uetchy/alfred-qiita-workflow
54+
$ make build
55+
$ make link
5456
```
5557

5658
If you put Alfred settings to another location, you should run following lines
5759

5860
```
59-
$ rake link ALFRED_WORKFLOW_PATH=/path/to/Alfred.alfredpreferences/workflows
61+
$ ALFRED_WORKFLOW_PATH=/path/to/Alfred.alfredpreferences/workflows make link
6062
```
6163

6264
You can find Alfred preferences path by `mdfind` command like this:
@@ -66,12 +68,4 @@ $ mdfind Alfred.alfredpreferences
6668
```
6769

6870
### Testing workflow
69-
```
70-
$ bundle install
71-
$ bundle exec rspec
72-
```
73-
74-
75-
## Thanks
76-
77-
- [Alfred 2 Ruby Template](https://github.com/zhaocai/alfred2-ruby-template)
71+
`soon`

main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
const (
1212
bundleId = "co.randompaper.alfred-qiita-workflow"
13+
version = "2.0.0"
1314
)
1415

1516
func newQiitaClient() (*qiita.Client, error) {
@@ -32,14 +33,23 @@ func newQiitaClient() (*qiita.Client, error) {
3233
func main() {
3334
app := cli.NewApp()
3435
app.Name = "alfred-qiita"
36+
app.Version = version
3537
app.Commands = []cli.Command{
38+
{
39+
Name: "setup",
40+
Action: cmdSetup,
41+
},
3642
{
3743
Name: "search",
3844
Action: cmdSearch,
3945
},
4046
{
41-
Name: "setup",
42-
Action: cmdSetup,
47+
Name: "stocks",
48+
Action: cmdStocks,
49+
},
50+
{
51+
Name: "my",
52+
Action: cmdMy,
4353
},
4454
}
4555
app.Run(os.Args)

my.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"github.com/pascalw/go-alfred"
6+
)
7+
8+
func cmdMy(c *cli.Context) {
9+
query := c.Args()
10+
client, err := newQiitaClient()
11+
if err != nil {
12+
return
13+
}
14+
items, _, _ := client.Users.ListItems(nil)
15+
16+
alfred.InitTerms(query)
17+
response := alfred.NewResponse()
18+
for _, item := range items {
19+
if !alfred.MatchesTerms(query, *item.Title+*item.Body) {
20+
continue
21+
}
22+
response.AddItem(&alfred.AlfredResponseItem{
23+
Valid: true,
24+
Uid: *item.Id,
25+
Title: *item.Title,
26+
Arg: *item.URL,
27+
Subtitle: item.CreatedAt.Format("2006/01/02 15:04:05"),
28+
})
29+
}
30+
31+
response.Print()
32+
}

qiita/qiita.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package qiita
33
import (
44
"bytes"
55
"encoding/json"
6-
"io"
76
"github.com/google/go-querystring/query"
7+
"io"
88
"net/http"
99
"net/url"
1010
"reflect"
@@ -35,6 +35,7 @@ type Client struct {
3535
UserAgent string
3636

3737
Items *ItemsService
38+
Users *UsersService
3839
}
3940

4041
func addOptions(s string, opt interface{}) (string, error) {
@@ -65,6 +66,7 @@ func NewClient(httpClient *http.Client) *Client {
6566

6667
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
6768
c.Items = &ItemsService{client: c}
69+
c.Users = &UsersService{client: c}
6870
return c
6971
}
7072

File renamed without changes.

qiita/user.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

qiita/users.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package qiita
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
type UsersService struct {
9+
client *Client
10+
}
11+
12+
type User struct {
13+
Name *string `json:"name"`
14+
Description *string `json:"description"`
15+
FacebookId *string `json:"facebook_id"`
16+
FolloweesCount *int `json:"followees_count"`
17+
FollowersCount *int `json:"followers_count"`
18+
GithubLoginName *string `json:"github_login_name"`
19+
Id *string `json:"id"`
20+
ItemsCount *int `json:"items_count"`
21+
LinkedInId *string `json:"linkedin_id"`
22+
Location *string `json:"location"`
23+
Organization *string `json:"organization"`
24+
PermanentId *int `json:"permanent_id"`
25+
ProfileImageURL *string `json:"profile_image_url"`
26+
TwitterScreenName *string `json:"twitter_screen_name"`
27+
WebsiteURL *string `json:"website_url"`
28+
}
29+
30+
type StocksOptions struct {
31+
ListOptions
32+
}
33+
34+
func (s *UsersService) ListItems(opt *ListOptions) ([]Item, *http.Response, error) {
35+
u, err := addOptions("authenticated_user/items", opt)
36+
if err != nil {
37+
return nil, nil, err
38+
}
39+
req, err := s.client.NewRequest("GET", u, nil)
40+
if err != nil {
41+
return nil, nil, err
42+
}
43+
items := new([]Item)
44+
resp, err := s.client.Do(req, items)
45+
if err != nil {
46+
return nil, nil, err
47+
}
48+
49+
return *items, resp, err
50+
}
51+
52+
func (s *UsersService) ListStocks(userId string, opt *StocksOptions) ([]Item, *http.Response, error) {
53+
u := fmt.Sprintf("users/%s/stocks", userId)
54+
u, err := addOptions(u, opt)
55+
if err != nil {
56+
return nil, nil, err
57+
}
58+
req, err := s.client.NewRequest("GET", u, nil)
59+
if err != nil {
60+
return nil, nil, err
61+
}
62+
items := new([]Item)
63+
resp, err := s.client.Do(req, items)
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
return *items, resp, err
69+
}

stocks.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
"github.com/pascalw/go-alfred"
6+
)
7+
8+
func cmdStocks(c *cli.Context) {
9+
query := c.Args()
10+
client, err := newQiitaClient()
11+
if err != nil {
12+
return
13+
}
14+
items, _, _ := client.Users.ListStocks("uetchy", nil)
15+
16+
alfred.InitTerms(query)
17+
response := alfred.NewResponse()
18+
for _, item := range items {
19+
if !alfred.MatchesTerms(query, *item.Title+*item.Body) {
20+
continue
21+
}
22+
response.AddItem(&alfred.AlfredResponseItem{
23+
Valid: true,
24+
Uid: *item.Id,
25+
Title: *item.Title,
26+
Arg: *item.URL,
27+
Subtitle: *item.User.Id + " " + item.CreatedAt.Format("2006/01/02 15:04:05"),
28+
})
29+
}
30+
31+
response.Print()
32+
}

0 commit comments

Comments
 (0)