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 db61821
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 39 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)
19 changes: 14 additions & 5 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 Down Expand Up @@ -58,13 +61,19 @@ 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
xml = @icons.map { |icon| item_xml(item_hash(icon)) }.join
"<?xml version='1.0'?>\n<items>\n#{xml}</items>"
end

private
Expand Down
28 changes: 16 additions & 12 deletions workflow/main.rb
Expand Up @@ -4,21 +4,25 @@
$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(query, debug = false)
puts result = FontAwesome.new(query).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
query = ARGV.join(' ').strip
Main.new(query)
38 changes: 22 additions & 16 deletions workflow/test/font_awesome_test.rb
Expand Up @@ -109,46 +109,52 @@

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)
xml = FontAwesome.new(query).to_alfred
@doc = REXML::Document.new(xml)
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' }
end

Expand Down

0 comments on commit db61821

Please sign in to comment.