Permalink
Browse files

Normalize source loading

  • Loading branch information...
1 parent 8ffe72d commit 29e04d7b72b5c739c3eba14c04c5a6af024d6ef2 @rwdaigle committed May 11, 2012
Showing with 43 additions and 29 deletions.
  1. +31 −0 app/loaders/csv_loader.rb
  2. +9 −4 app/loaders/wordnik_wod_loader.rb
  3. +3 −25 lib/tasks/ptls.rake
@@ -0,0 +1,31 @@
+require 'csv'
+
+class CSVLoader
+
+ class << self
+
+ def load!
+
+ # Get the file to filesystem
+ log({event: "csv-import", url: ENV['VOCABULARY_CSV_URL']}) do
+
+ file = "tmp/#{Time.now.utc.to_f}-import.csv"
+ `curl -q "#{ENV['VOCABULARY_CSV_URL']}" > #{file}`
+
+ load_csv_file(file)
+ end
+ end
+
+ def load_csv_file(file)
+ vocab = Subject.vocabulary
+ CSV.foreach(file) do |row|
+ unit = vocab.units.create(question: row[0])
+ if(unit && unit.valid?)
+ log({event: "csv-import-unit" }, unit, { status: "success" })
+ else
+ log({event: "csv-import-unit" }, unit, { status: "failure" }, { message: unit.errors.full_messages.join(", ") })
+ end
+ end
+ end
+ end
+end
@@ -17,12 +17,17 @@ def load!(from = Date.today)
def load_wod(subject, day)
result = question = nil
- log(subject, day: day) do
+ log({event: "wod-import", day: day}) do
+
result = Wordnik.words.get_word_of_the_day(date: day.strftime("%Y-%m-%d"))
question = result && !result.empty? ? result['word'] : nil
- existing_question = subject.units.where(question: question).first
- log(subject, event: "wod", day: day, word: question, already_exists: !!existing_question)
- subject.units.create(question: question) unless existing_question
+
+ unit = subject.units.create(question: question)
+ if(unit && unit.valid?)
+ log({event: "wod-import-unit", day: day }, unit, { status: "success" })
+ else
+ log({event: "wod-import-unit", day: day }, unit, { status: "failure" }, { message: unit.errors.full_messages.join(", ") })
+ end
end
end
end
View
@@ -1,4 +1,4 @@
-require 'csv'
+
namespace :ptls do
@@ -10,30 +10,8 @@ namespace :ptls do
desc "Load words from external sources"
task :load => :environment do
- # puts "Enqueuing WordnikWODLoader.load!"
- # $queue.enqueue("WordnikWODLoader.load!")
- WordnikWODLoader.load!(Date.today)
- end
-
- desc "Import words from the csv file located at VOCABULARY_CSV_URL"
- task :import => :environment do
-
- # Get the file to filesystem
- Object.log({caller: "ptls.rake#import", event: "csv-import", file: ENV['VOCABULARY_CSV_URL']}) do
-
- file = "tmp/#{Time.now.utc.to_i}-import.csv"
- `curl "#{ENV['VOCABULARY_CSV_URL']}" > #{file}`
-
- # Parse
- vocab = Subject.vocabulary
- CSV.foreach(file) do |row|
- unit = vocab.units.create(question: row[0])
- if(unit && unit.valid?)
- Object.log({caller: "ptls.rake#import", event: "csv-import-unit" }, unit, { status: "success" })
- else
- Object.log({caller: "ptls.rake#import", event: "csv-import-unit" }, unit, { status: "failure" }, { message: unit.errors.full_messages.join(", ") })
- end
- end
+ [WordnikWODLoader, CSVLoader].each do |klass|
+ klass.load!
end
end
end

0 comments on commit 29e04d7

Please sign in to comment.