Permalink
Browse files

add tsync:fetch rake task that calls TenderSync::Sources::FileSystem#…

…write_dir
  • Loading branch information...
1 parent f2676d7 commit 60afa62d3a9b19ba228b3a39c49cb96a3427d8a4 @technoweenie committed Feb 18, 2010
Showing with 96 additions and 15 deletions.
  1. +2 −15 .gitignore
  2. +61 −0 Rakefile
  3. +17 −0 lib/tender_sync/sources/filesystem.rb
  4. +16 −0 test/filesystem_source_test.rb
View
17 .gitignore
@@ -1,21 +1,8 @@
-## MAC OS
-.DS_Store
-
-## TEXTMATE
-*.tmproj
-tmtags
-
-## EMACS
-*~
-\#*
-.\#*
-
-## VIM
-*.swp
-
## PROJECT::GENERAL
coverage
rdoc
pkg
## PROJECT::SPECIFIC
+config.yml
+faqs
View
61 Rakefile
@@ -1,6 +1,67 @@
require 'rubygems'
require 'rake'
+namespace :tsync do
+ desc "fetches FAQ articles from Tender into the local filesystem"
+ task :fetch => :load_config do
+ api = TenderSync::Sources::Api.new($config)
+ fs = TenderSync::Sources::Filesystem.new($config)
+ dir = api.fetch_dir
+ fs.write_dir(dir)
+ end
+
+ desc "helps you configure your tender-sync settings"
+ task :config do
+ sample = <<-SAMPLE
+tender:
+ path: "faqs/tender_support"
+ site: "help"
+ api_key: "abc123foo"
+ SAMPLE
+ if File.exist?("config.yml")
+ puts "A sample config file might look like:"
+ puts sample
+ else
+ File.open('config.yml', 'w') do |f|
+ f << sample
+ end
+ puts "This has been written to config.yml, your shiny new config file:"
+ puts sample
+ end
+ puts
+ puts "Now, you can fetch FAQs from tender with:"
+ puts " rake tsync:fetch SITE=tender"
+ end
+
+ task :load_config do
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
+ require 'tender_sync'
+
+ config = nil
+ if File.exist?("config.yml")
+ require 'yaml'
+ global_config = YAML.load_file("config.yml")
+ config = global_config[ENV['SITE'].to_s]
+ end
+
+ if config
+ $config = TenderSync::Config.new
+ config.each do |key, value|
+ $config.send "#{key}=", value
+ end
+ else
+ puts "Invalid Site: #{ENV['SITE'].inspect}"
+ if File.exist?("config.yml")
+ puts "Check config.yml"
+ else
+ puts
+ Rake::Task['tsync:config'].execute
+ end
+ exit
+ end
+ end
+end
+
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
View
17 lib/tender_sync/sources/filesystem.rb
@@ -1,3 +1,4 @@
+require 'fileutils'
class TenderSync::Sources::Filesystem
attr_reader :path
attr_accessor :config
@@ -31,4 +32,20 @@ def fetch_dir
end
dir
end
+
+ def write_dir(dir)
+ if !path
+ raise ArgumentError, "Path cannot be nil"
+ end
+
+ dir.each do |section, files|
+ section_path = File.join(path, section)
+ FileUtils.mkdir_p section_path
+ files.each do |file|
+ File.open(File.join(section_path, file.filename + ".mkdn"), 'w') do |f|
+ f << file.body
+ end
+ end
+ end
+ end
end
View
16 test/filesystem_source_test.rb
@@ -16,4 +16,20 @@ def test_builds_tender_sync_dir_from_fetched_faqs
assert_equal 'def', faqs[1].filename
assert_equal 'def', faqs[1].body
end
+
+ def test_srites_tender_sync_dir_to_filesystem
+ dir = TenderSync::Dir.new
+ dir.add 'foo', TenderSync::Dir::File.new('foo', 'foo')
+ dir.add 'foo', TenderSync::Dir::File.new('foobar', 'foobar')
+ dir.add 'bar', TenderSync::Dir::File.new('foobar', 'foobar')
+
+ @source.write_dir(dir)
+
+ assert_equal 'foo', IO.read(File.join(@config.path, 'foo', 'foo.mkdn'))
+ assert_equal 'foobar', IO.read(File.join(@config.path, 'foo', 'foobar.mkdn'))
+ assert_equal 'foobar', IO.read(File.join(@config.path, 'bar', 'foobar.mkdn'))
+ ensure
+ FileUtils.rm_rf File.join(@config.path, 'foo')
+ FileUtils.rm_rf File.join(@config.path, 'bar')
+ end
end

0 comments on commit 60afa62

Please sign in to comment.