Skip to content

Commit

Permalink
Remove ruby template
Browse files Browse the repository at this point in the history
  • Loading branch information
ruedap committed Dec 8, 2013
1 parent 819c148 commit 4dd1cf5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 44 deletions.
1 change: 0 additions & 1 deletion 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
Expand Down
3 changes: 0 additions & 3 deletions 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
Expand Down Expand Up @@ -30,7 +28,6 @@ PLATFORMS
ruby

DEPENDENCIES
alfred-workflow (= 1.11.3)
coveralls (= 0.7.0)
htmlentities (= 4.3.1)
minitest (= 5.0.8)
Expand Down
3 changes: 1 addition & 2 deletions workflow/encode.rb
Expand Up @@ -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)
26 changes: 18 additions & 8 deletions 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

Expand All @@ -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)
Expand All @@ -58,21 +61,28 @@ 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

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
27 changes: 15 additions & 12 deletions workflow/main.rb
Expand Up @@ -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(args, debug = false)
result = FontAwesome.new(args).to_alfred
logging(result) 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(result)
log_file = File.expand_path('~/Library/Logs/font-awesome-workflow.log')
logger = Logger.new(log_file, 'daily')
logger.progname = 'Font Awesome Workflow'
logger.debug(result)
end
end

# entry point
Main.new(ARGV)
74 changes: 57 additions & 17 deletions workflow/test/font_awesome_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion workflow/test/test_helper.rb
Expand Up @@ -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 4dd1cf5

Please sign in to comment.