-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalfredex.rb
65 lines (55 loc) · 2.02 KB
/
alfredex.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'yaml'
require File.join(File.dirname(__FILE__), 'alfred_feedback.rb')
class Alfredex
FILE_DATA = YAML.load_file(File.join(File.dirname(__FILE__), 'pokemon.yml'))
def self.search(string)
matches = nil
if string =~ /[\/,\,]/ # contains separators, look up multiple
mons = string.split(/[\/,\,]/).map{|n| n.strip.downcase }
dashed_mons = mons.select{|m| m =~ /\-/}
matches = FILE_DATA.select do |name, data|
if mons.include? name.downcase
true
elsif dashed_mons.length > 0 # contains dash and doesn't match, split
dashed_mons.any?{|m| m.split('-')[0] == name.downcase }
end
end
# maximum 10 lookups, don't flood the user with tabs
matches = matches.to_a[0..9]
feedback = Feedback.new
feedback.add_item({
:title => "Find Multiple Pokemon",
:subtitle => matches.map{|m| m[0]}.join(', '),
:arg => matches.map{|m| m[1]['url_name'] }.join(","),
:uid => matches.map{|m| m[1]['number'] }.join(','),
:icon => {:type => "filetype", :name => "icon.png"}
})
puts feedback.to_xml
else # single pokemon
if string =~ /^[0-9]+$/ # numeric string, check pokemon number
matches = FILE_DATA.select do |name, data|
data['number'].to_i == string.to_i
end
else # check for name
matches = FILE_DATA.select do |name, data|
name.downcase.include? string.downcase
end
matches = matches.sort_by do |k,v|
k.downcase[0..string.length-1] == string.downcase ? 0 : 1
end
end
feedback = Feedback.new
matches.each do |name, data|
feedback.add_item({
:title => "##{data['number'].to_i} #{name}",
:subtitle => data['description'],
:arg => data['url_name'],
:uid => data['number'],
:icon => {:type => "filetype", :name => "pokemon_sprites/#{data['number']}.png"}
})
end
puts feedback.to_xml
end
end
end
Alfredex.search ARGV.join.strip