Import JSON data from morph.io via rake task

Usage: 
MORPH_API_KEY=‘your key’ b rake import_papers:from_morph
This commit is contained in:
Andreas Haller 2015-04-27 21:03:58 +02:00
parent f35aa2a9d3
commit 2b4281ad87
2 changed files with 15 additions and 11 deletions

View file

@ -1,16 +1,16 @@
require 'csv' require 'json'
class Paper < ActiveRecord::Base class Paper < ActiveRecord::Base
class << self class << self
def import_from_csv(csv_string) def import_from_json(json_string)
CSV.parse(csv_string, headers: true) do |row| JSON.parse(json_string).each do |record|
attributes = { attributes = {
name: row['name'], name: record['name'],
url: row['url'], url: record['url'],
reference: row['reference'], reference: record['reference'],
paper_type: row['paper_type'], paper_type: record['paper_type'],
originator: row['originator'], originator: record['originator'],
published_at: row['published_at'], published_at: record['published_at'],
} }
create!(attributes) create!(attributes)
end end

View file

@ -1,7 +1,11 @@
namespace :import_papers do namespace :import_papers do
desc 'Import Paper records from CSV' desc 'Import Paper records from CSV'
task :from_csv, [:csv_file] => :environment do |t, args| task :from_morph => :environment do |t, args|
Paper.import_from_csv(File.read(args[:csv_file])) require 'open-uri'
api_key = ENV['MORPH_API_KEY']
uri = URI.parse "https://api.morph.io/ahx/city_council_leipzig_recent_papers/data.json?key=#{api_key}&query=select%20*%20from%20%27data%27%20limit%2010"
puts "Download files from #{uri}"
Paper.import_from_json(uri.read)
end end
end end