Permalink
Browse files

added first package

  • Loading branch information...
1 parent a01ac0d commit 7296e072395404b36c532992495224dfce9a6d83 Justin Marney committed Jun 10, 2008
View
@@ -2,7 +2,6 @@ History.txt
Manifest.txt
README.txt
Rakefile
-bin/simple_importer
lib/simple_importer.rb
tasks/ann.rake
tasks/annotations.rake
View
@@ -16,5 +16,6 @@ PROJ.url = 'FIXME (project homepage)'
PROJ.rubyforge_name = 'simple_importer'
PROJ.spec_opts << '--color'
+PROJ.version = SimpleImporter::VERSION
# EOF
View
@@ -4,6 +4,8 @@
# Used to prevent the class/module from being loaded more than once
unless defined? SimpleImporter
+require 'csv'
+
module SimpleImporter
def file(path)
path = File.join(RAILS_ROOT, 'data', path)
@@ -13,7 +15,16 @@ def file(path)
end
def csv(path)
- require 'csv' unless defined? CSV
+ file(path) do |f|
+ parsed_file = CSV::Reader.parse(f)
+ parsed_file.each do |row|
+ row.each {|v| v.trim! if v.respond_to? :trim! }
+ yield row if block_given?
+ end
+ end
+ end
+
+ def tsv(path)
file(path) do |f|
parsed_file = CSV::Reader.parse(f)
parsed_file.each do |row|
@@ -31,8 +42,8 @@ def xml(path)
end
end
- def run
- reset if respond_to? :reset
+ def run(reset_flag = false)
+ reset if reset_flag && respond_to?(:reset)
import
end
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
+== 1.0.0 / 2008-06-10
+
+* 1 major enhancement
+ * Birthday!
@@ -0,0 +1,17 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+lib/simple_importer.rb
+tasks/ann.rake
+tasks/annotations.rake
+tasks/bones.rake
+tasks/doc.rake
+tasks/gem.rake
+tasks/manifest.rake
+tasks/post_load.rake
+tasks/rubyforge.rake
+tasks/setup.rb
+tasks/test.rake
+test/test_helper.rb
+test/test_simple_importer.rb
@@ -0,0 +1,74 @@
+simple_importer
+ by Justin Marney
+ FIXME (url)
+
+== DESCRIPTION:
+
+Simple API for importing from csv, tsv and xml.
+
+== FEATURES/PROBLEMS:
+
+* FIXME (list of features or problems)
+
+== SYNOPSIS:
+
+# extend your import modules and define import methods
+module XmlImporter
+ extend SimpleImporter
+
+ # if true is passed to run, reset will get called first if it exists.
+ def self.reset
+ Accessory.find(:all).each {|a| a.destroy }
+ end
+
+ def self.import
+ csv('Accessory Schema.csv') do |row|
+ unless row[0].nil? || row[0] == 'SKU'
+ accessory = Accessory.create!(:sku => row[0],
+ :image => row[1],
+ :thumbnail_image => row[2],
+ :name => row[3],
+ :supplier_cost => row[4],
+ :retail_cost => row[5],
+ :description => row[6])
+ end
+ end
+ end
+end
+
+# run your import modules (from a rake task perhaps)
+XmlImporter.run
+
+== REQUIREMENTS:
+
+Hpricot is required if you intend to import xml.
+test/spec and mocha are required to run the tests.
+
+== INSTALL:
+
+* FIXME (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 Justin Marney
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
+# Look in the tasks/setup.rb file for the various options that can be
+# configured in this Rakefile. The .rake files in the tasks directory
+# are where the options are used.
+
+load 'tasks/setup.rb'
+
+ensure_in_path 'lib'
+require 'simple_importer'
+
+task :default => 'spec:run'
+
+PROJ.name = 'simple_importer'
+PROJ.authors = 'Justin Marney'
+PROJ.email = 'justin.marney@viget.com'
+PROJ.url = 'FIXME (project homepage)'
+PROJ.rubyforge_name = 'simple_importer'
+
+PROJ.spec_opts << '--color'
+PROJ.version = SimpleImporter::VERSION
+
+# EOF
@@ -0,0 +1,97 @@
+# $Id$
+
+# Equivalent to a header guard in C/C++
+# Used to prevent the class/module from being loaded more than once
+unless defined? SimpleImporter
+
+require 'csv'
+
+module SimpleImporter
+ def file(path)
+ path = File.join(RAILS_ROOT, 'data', path)
+ file = File.open(path)
+ yield file if block_given?
+ file.close
+ end
+
+ def csv(path)
+ file(path) do |f|
+ parsed_file = CSV::Reader.parse(f)
+ parsed_file.each do |row|
+ row.each {|v| v.trim! if v.respond_to? :trim! }
+ yield row if block_given?
+ end
+ end
+ end
+
+ def tsv(path)
+ file(path) do |f|
+ parsed_file = CSV::Reader.parse(f)
+ parsed_file.each do |row|
+ row.each {|v| v.trim! if v.respond_to? :trim! }
+ yield row if block_given?
+ end
+ end
+ end
+
+ def xml(path)
+ require 'hpricot' unless defined? Hpricot
+ file(path) do |f|
+ doc = Hpricot(f)
+ yield doc if block_given?
+ end
+ end
+
+ def run(reset_flag = false)
+ reset if reset_flag && respond_to?(:reset)
+ import
+ end
+
+ # :stopdoc:
+ VERSION = '1.0.0'
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
+ # :startdoc:
+
+ # Returns the version string for the library.
+ #
+ def self.version
+ VERSION
+ end
+
+ # Returns the library path for the module. If any arguments are given,
+ # they will be joined to the end of the libray path using
+ # <tt>File.join</tt>.
+ #
+ def self.libpath( *args )
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, *args)
+ end
+
+ # Returns the lpath for the module. If any arguments are given,
+ # they will be joined to the end of the path using
+ # <tt>File.join</tt>.
+ #
+ def self.path( *args )
+ args.empty? ? PATH : ::File.join(PATH, *args)
+ end
+
+ # Utility method used to rquire all files ending in .rb that lie in the
+ # directory below this file that has the same name as the filename passed
+ # in. Optionally, a specific _directory_ name can be passed in such that
+ # the _filename_ does not have to be equivalent to the directory.
+ #
+ def self.require_all_libs_relative_to( fname, dir = nil )
+ dir ||= ::File.basename(fname, '.*')
+ search_me = ::File.expand_path(
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
+
+ Dir.glob(search_me).sort.each {|rb| require rb}
+ end
+
+end # module SimpleImporter
+
+SimpleImporter.require_all_libs_relative_to __FILE__
+
+end # unless defined?
+
+# EOF
@@ -0,0 +1,76 @@
+# $Id$
+
+begin
+ require 'bones/smtp_tls'
+rescue LoadError
+ require 'net/smtp'
+end
+require 'time'
+
+namespace :ann do
+
+ file PROJ.ann_file do
+ puts "Generating #{PROJ.ann_file}"
+ File.open(PROJ.ann_file,'w') do |fd|
+ fd.puts("#{PROJ.name} version #{PROJ.version}")
+ fd.puts(" by #{Array(PROJ.authors).first}") if PROJ.authors
+ fd.puts(" #{PROJ.url}") if PROJ.url
+ fd.puts(" (the \"#{PROJ.release_name}\" release)") if PROJ.release_name
+ fd.puts
+ fd.puts("== DESCRIPTION")
+ fd.puts
+ fd.puts(PROJ.description)
+ fd.puts
+ fd.puts(PROJ.changes.sub(%r/^.*$/, '== CHANGES'))
+ fd.puts
+ PROJ.ann_paragraphs.each do |p|
+ fd.puts "== #{p.upcase}"
+ fd.puts
+ fd.puts paragraphs_of(PROJ.readme_file, p).join("\n\n")
+ fd.puts
+ end
+ fd.puts PROJ.ann_text if PROJ.ann_text
+ end
+ end
+
+ desc "Create an announcement file"
+ task :announcement => PROJ.ann_file
+
+ desc "Send an email announcement"
+ task :email => PROJ.ann_file do
+ from = PROJ.ann_email[:from] || PROJ.email
+ to = Array(PROJ.ann_email[:to])
+
+ ### build a mail header for RFC 822
+ rfc822msg = "From: #{from}\n"
+ rfc822msg << "To: #{to.join(',')}\n"
+ rfc822msg << "Subject: [ANN] #{PROJ.name} #{PROJ.version}"
+ rfc822msg << " (#{PROJ.release_name})" if PROJ.release_name
+ rfc822msg << "\n"
+ rfc822msg << "Date: #{Time.new.rfc822}\n"
+ rfc822msg << "Message-Id: "
+ rfc822msg << "<#{"%.8f" % Time.now.to_f}@#{PROJ.ann_email[:domain]}>\n\n"
+ rfc822msg << File.read(PROJ.ann_file)
+
+ params = [:server, :port, :domain, :acct, :passwd, :authtype].map do |key|
+ PROJ.ann_email[key]
+ end
+
+ params[3] = PROJ.email if params[3].nil?
+
+ if params[4].nil?
+ STDOUT.write "Please enter your e-mail password (#{params[3]}): "
+ params[4] = STDIN.gets.chomp
+ end
+
+ ### send email
+ Net::SMTP.start(*params) {|smtp| smtp.sendmail(rfc822msg, from, to)}
+ end
+end # namespace :ann
+
+desc 'Alias to ann:announcement'
+task :ann => 'ann:announcement'
+
+CLOBBER << PROJ.ann_file
+
+# EOF
@@ -0,0 +1,22 @@
+# $Id$
+
+if HAVE_BONES
+
+desc "Enumerate all annotations"
+task :notes do
+ Bones::AnnotationExtractor.enumerate(
+ PROJ, PROJ.annotation_tags.join('|'), :tag => true)
+end
+
+namespace :notes do
+ PROJ.annotation_tags.each do |tag|
+ desc "Enumerate all #{tag} annotations"
+ task tag.downcase.to_sym do
+ Bones::AnnotationExtractor.enumerate(PROJ, tag)
+ end
+ end
+end
+
+end # if HAVE_BONES
+
+# EOF
Oops, something went wrong.

0 comments on commit 7296e07

Please sign in to comment.