Skip to content
Browse files

Remove ruby template

  • Loading branch information...
1 parent 819c148 commit 9bb88ca76380ca25980304ccce703499576a6220 @ruedap committed
View
1 workflow/Gemfile
@@ -1,7 +1,6 @@
source 'https://rubygems.org'
gem 'plist', '3.1.0'
-gem 'alfred-workflow', '1.11.3'
gem 'htmlentities', '4.3.1'
group :test do
View
3 workflow/Gemfile.lock
@@ -1,8 +1,6 @@
GEM
remote: https://rubygems.org/
specs:
- alfred-workflow (1.11.3)
- plist (>= 3.1.0)
coveralls (0.7.0)
multi_json (~> 1.3)
rest-client
@@ -30,7 +28,6 @@ PLATFORMS
ruby
DEPENDENCIES
- alfred-workflow (= 1.11.3)
coveralls (= 0.7.0)
htmlentities (= 4.3.1)
minitest (= 5.0.8)
View
3 workflow/encode.rb
@@ -5,8 +5,7 @@
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
require 'bundle/bundler/setup'
-require 'alfred'
require 'lib/font_awesome'
-character_code = ARGV[0].chomp.split("|||")[1]
+character_code = ARGV[0].chomp.split('|||')[1]
print FontAwesome.to_character_reference(character_code)
View
26 workflow/lib/font_awesome.rb
@@ -1,12 +1,15 @@
# encoding: utf-8
+require 'yaml'
require 'htmlentities'
+# FontAwesome class
class FontAwesome
attr_reader :icons
ICONS = YAML.load_file(File.expand_path('./icons.yml'))['icons']
+ # FontAwesome::Icon class
class Icon
attr_reader :id, :unicode
@@ -33,10 +36,10 @@ def self.to_character_reference(character_code)
HTMLEntities.new.decode("&#x#{character_code};")
end
- def initialize(query = '')
+ def initialize(queries = [])
icon_filenames = glob_icon_filenames
@icons = icon_filenames.map { |name| Icon.new(name) }
- select!(query.split)
+ select!(queries)
end
def select!(queries, icons = @icons)
@@ -58,13 +61,20 @@ def item_hash(icon)
}
end
- def add_items(feedback, icons = @icons)
- icons.each { |icon| feedback.add_item(item_hash(icon)) }
- feedback
+ def item_xml(options = {})
+ <<-XML
+<item arg="#{options[:arg]}" uid="#{options[:uid]}">
+ <title>#{options[:title]}</title>
+ <subtitle>#{options[:subtitle]}</subtitle>
+ <icon>#{options[:icon][:name]}</icon>
+</item>
+ XML
end
- def to_alfred(alfred)
- add_items(alfred.feedback).to_alfred
+ def to_alfred
+ item_xml = @icons.map { |icon| item_xml(item_hash(icon)) }.join
+ puts xml = "<?xml version='1.0'?>\n<items>\n#{item_xml}</items>"
+ xml
end
private
@@ -72,7 +82,7 @@ def to_alfred(alfred)
def glob_icon_filenames
Dir.glob(File.expand_path('./icons/fa-*.png')).map do |path|
md = /\/fa-(.+)\.png/.match(path)
- (md && md[1]) ? md[1] : nil
+ md && md[1] ? md[1] : nil
end.compact.uniq.sort
end
end
View
27 workflow/main.rb
@@ -4,21 +4,24 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
+require 'logger'
require 'bundle/bundler/setup'
-require 'alfred'
require 'lib/font_awesome'
-def puts_log(str)
- log_file = File.expand_path('~/Library/Logs/Alfred-Workflow.log')
- File.open(log_file, 'a+') do |log|
- log.puts "[PUTS LOG] #{str}\n"
- log.flush
+# Main class
+class Main
+ def initialize(queries, debug = false)
+ xml = FontAwesome.new(queries).to_alfred
+ logging(xml) if debug
end
-end
-
-Alfred.with_friendly_error do |alfred|
- alfred.with_rescue_feedback = true
- query = ARGV.join(' ').strip
- puts FontAwesome.new(query).to_alfred(alfred)
+ def logging(string)
+ log_file = File.expand_path('~/Library/Logs/font-awesome-workflow.log')
+ logger = Logger.new(log_file, 'daily')
+ logger.progname = 'Font Awesome Workflow'
+ logger.debug(string)
+ end
end
+
+# entry point
+Main.new(ARGV)
View
74 workflow/test/font_awesome_test.rb
@@ -109,47 +109,75 @@
describe '#item_hash' do
before do
- @icon = FontAwesome::Icon.new('apple')
- @item_hash = FontAwesome.new.item_hash(@icon)
+ icon = FontAwesome::Icon.new('apple')
+ @item_hash = FontAwesome.new.item_hash(icon)
end
it { @item_hash[:uid].must_equal '' }
it { @item_hash[:title].must_equal 'apple' }
- it { @item_hash[:subtitle].must_equal "Paste class name: fa-apple" }
+ it { @item_hash[:subtitle].must_equal 'Paste class name: fa-apple' }
it { @item_hash[:arg].must_equal 'apple|||f179' }
it { @item_hash[:icon][:type].must_equal 'default' }
- it { @item_hash[:icon][:name].must_equal "./icons/fa-apple.png" }
+ it { @item_hash[:icon][:name].must_equal './icons/fa-apple.png' }
it { @item_hash[:valid].must_equal 'yes' }
it { @item_hash.size.must_equal 6 }
end
- describe '#add_items' do
+ describe '#item_xml' do
before do
- feedback = Alfred::Core.new.feedback
- icon_ids = %w(beer cloud apple)
- icons = icon_ids.map { |name| FontAwesome::Icon.new(name) }
- @feedback = FontAwesome.new.add_items(feedback, icons)
+ icon = FontAwesome::Icon.new('apple')
+ item_hash = FontAwesome.new.item_hash(icon)
+ @item_xml = FontAwesome.new.item_xml(item_hash)
end
- it { @feedback.items.first.title.must_equal 'beer' }
- it { @feedback.items.last.title.must_equal 'apple' }
+ it do
+ expectation = <<-XML
+<item arg="apple|||f179" uid="">
+ <title>apple</title>
+ <subtitle>Paste class name: fa-apple</subtitle>
+ <icon>./icons/fa-apple.png</icon>
+</item>
+ XML
+ @item_xml.must_equal expectation
+ end
end
describe '#to_alfred' do
before do
- alfred = Alfred::Core.new
- query = 'bookmark'
- xml = FontAwesome.new(query).to_alfred(alfred)
+ queries = ['bookmark']
+ xml = FontAwesome.new(queries).to_alfred
@doc = REXML::Document.new(xml)
+ # TODO: mute puts
end
- it { @doc.elements['items'].size.must_equal 2 }
+ it { @doc.elements['items'].elements.size.must_equal 2 }
+ it { @doc.elements['items/item[1]'].attributes['arg'].must_equal 'bookmark|||f02e' }
it { @doc.elements['items/item[1]/title'].text.must_equal 'bookmark' }
- it { @doc.elements['items/item[1]/arg'].text.must_equal 'bookmark|||f02e' }
it { @doc.elements['items/item[1]/icon'].text.must_equal './icons/fa-bookmark.png' }
+ it { @doc.elements['items/item[2]'].attributes['arg'].must_equal 'bookmark-o|||f097' }
it { @doc.elements['items/item[2]/title'].text.must_equal 'bookmark-o' }
- it { @doc.elements['items/item[2]/arg'].text.must_equal 'bookmark-o|||f097' }
it { @doc.elements['items/item[2]/icon'].text.must_equal './icons/fa-bookmark-o.png' }
+
+ it 'must equal $stdout (test for puts)' do
+ expectation = <<-XML
+<?xml version='1.0'?>
+<items>
+<item arg="bookmark|||f02e" uid="">
+ <title>bookmark</title>
+ <subtitle>Paste class name: fa-bookmark</subtitle>
+ <icon>./icons/fa-bookmark.png</icon>
+</item>
+<item arg="bookmark-o|||f097" uid="">
+ <title>bookmark-o</title>
+ <subtitle>Paste class name: fa-bookmark-o</subtitle>
+ <icon>./icons/fa-bookmark-o.png</icon>
+</item>
+</items>
+ XML
+
+ capture(:stdout) { FontAwesome.new(['bookmark']).to_alfred }.must_equal \
+ expectation
+ end
end
describe '::Icon' do
@@ -178,3 +206,15 @@
end
end
end
+
+def capture(stream)
+ begin
+ stream = stream.to_s
+ eval "$#{stream} = StringIO.new"
+ yield
+ result = eval("$#{stream}").string
+ ensure
+ eval "$#{stream} = #{stream.upcase}"
+ end
+ result
+end
View
2 workflow/test/test_helper.rb
@@ -13,8 +13,8 @@
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
require 'bundle/bundler/setup'
-require 'alfred'
require 'lib/font_awesome'
+require 'rexml/document'
require 'test/fixtures'
require 'minitest/autorun'

0 comments on commit 9bb88ca

Please sign in to comment.
Something went wrong with that request. Please try again.