Skip to content

Commit 97a00ff

Browse files
committed
Optimize
1 parent ea39ef9 commit 97a00ff

File tree

5 files changed

+31
-42
lines changed

5 files changed

+31
-42
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ If you put Alfred settings to another location, you should run following lines
6363
$ rake link ALFRED_WORKFLOW_PATH=/path/to/Alfred.alfredpreferences/workflows
6464
```
6565

66+
You can find Alfred preferences path by `mdfind` command like this:
67+
68+
```
69+
$ mdfind Alfred.alfredpreferences
70+
```
71+
6672
### Testing workflow
6773
```
6874
$ bundle install

commands/my.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
begin
99
config = Qiita::Config.new
1010
rescue Qiita::FileMissingError
11+
# Create empty config file if it doesn't exist
1112
config.save
1213
end
1314

15+
# 'my' command needs Qiita API Token
1416
unless config.token
1517
Qiita::Alfred.message "RUN: 'qiita setup' command first"
1618
exit
@@ -19,19 +21,6 @@
1921
data = Qiita::API.get(:items, :token => config.token)
2022
data.select!{|a| a['title'].scan(QUERY).size > 0 } if QUERY
2123

22-
results = []
23-
data.each do |q|
24-
subtitle = q['stock_count'].to_s + " Stocks, " + q['comment_count'].to_s + " Comments, " + Time.parse(q['created_at']).strftime("%Y/%m/%d %H:%M:%S") + " Created"
25-
26-
item = {
27-
:uid => nil,
28-
:arg => q['url'],
29-
:title => q['title'],
30-
:subtitle => subtitle,
31-
:icon => 'icon.png'
32-
}
33-
34-
results << item
35-
end
24+
results = Qiita::Alfred.messages(data)
3625

3726
puts Qiita::Alfred.to_alfred(results)

commands/search.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@
88
data = Qiita::API.search(QUERY)
99
data.sort! {|a, b| b['stock_count'] <=> a['stock_count'] }
1010

11-
results = []
12-
data.each do |q|
13-
arg = q['url']
14-
subtitle = q['stock_count'].to_s + " Stocks, " + q['comment_count'].to_s + " Comments, " + Time.parse(q['created_at']).strftime("%Y/%m/%d %H:%M:%S") + " Created"
15-
16-
item = {
17-
:uid => nil,
18-
:arg => arg,
19-
:title => q['title'],
20-
:subtitle => subtitle,
21-
:icon => 'icon.png'
22-
}
23-
results << item
24-
end
11+
results = Qiita::Alfred.messages(data)
2512

2613
puts Qiita::Alfred.to_alfred(results)

commands/stocks.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
config.save
1212
end
1313

14+
# 'stocks' command needs Qiita API Token
1415
unless config.token
1516
Qiita::Alfred.message "RUN: 'qiita setup' command first"
1617
exit
@@ -19,19 +20,6 @@
1920
data = !QUERY.empty? ? Qiita::API.search(QUERY, :token => config.token, :stocked => 1) : Qiita::API.get(:stocks, :token => config.token)
2021
data.sort! {|a, b| b['stock_count'] <=> a['stock_count'] }
2122

22-
results = []
23-
data.each do |q|
24-
subtitle = q['stock_count'].to_s + " Stocks, " + q['comment_count'].to_s + " Comments, " + Time.parse(q['created_at']).strftime("%Y/%m/%d %H:%M:%S") + " Created"
25-
26-
item = {
27-
:uid => nil,
28-
:arg => q['url'],
29-
:title => q['title'],
30-
:subtitle => subtitle,
31-
:icon => 'icon.png'
32-
}
33-
34-
results << item
35-
end
23+
results = Qiita::Alfred.messages(data)
3624

3725
puts Qiita::Alfred.to_alfred(results)

lib/qiita/alfred.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ def self.to_alfred(array)
2626
doc.to_s
2727
end
2828

29+
def self.messages(texts_array)
30+
results = []
31+
texts_array.each do |q|
32+
subtitle = q['stock_count'].to_s + " Stocks, " + q['comment_count'].to_s + " Comments, " + Time.parse(q['created_at']).strftime("%Y/%m/%d %H:%M:%S") + " Created"
33+
34+
item = {
35+
:uid => nil,
36+
:arg => q['url'],
37+
:title => q['title'],
38+
:subtitle => subtitle,
39+
:icon => 'icon.png'
40+
}
41+
42+
results << item
43+
end
44+
45+
return results
46+
end
47+
2948
def self.message(str)
3049
puts Qiita::Alfred.to_alfred([{
3150
:uid => 0,

0 commit comments

Comments
 (0)