From 2b4281ad873920450516b02653c25e9095dac064 Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Mon, 27 Apr 2015 21:03:58 +0200 Subject: [PATCH] Import JSON data from morph.io via rake task MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Usage: MORPH_API_KEY=‘your key’ b rake import_papers:from_morph --- app/models/paper.rb | 18 +++++++++--------- lib/tasks/import_papers.rake | 8 ++++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/models/paper.rb b/app/models/paper.rb index 2ec25f2..6758828 100644 --- a/app/models/paper.rb +++ b/app/models/paper.rb @@ -1,16 +1,16 @@ -require 'csv' +require 'json' class Paper < ActiveRecord::Base class << self - def import_from_csv(csv_string) - CSV.parse(csv_string, headers: true) do |row| + def import_from_json(json_string) + JSON.parse(json_string).each do |record| attributes = { - name: row['name'], - url: row['url'], - reference: row['reference'], - paper_type: row['paper_type'], - originator: row['originator'], - published_at: row['published_at'], + name: record['name'], + url: record['url'], + reference: record['reference'], + paper_type: record['paper_type'], + originator: record['originator'], + published_at: record['published_at'], } create!(attributes) end diff --git a/lib/tasks/import_papers.rake b/lib/tasks/import_papers.rake index 87bd129..7283452 100644 --- a/lib/tasks/import_papers.rake +++ b/lib/tasks/import_papers.rake @@ -1,7 +1,11 @@ namespace :import_papers do desc 'Import Paper records from CSV' - task :from_csv, [:csv_file] => :environment do |t, args| - Paper.import_from_csv(File.read(args[:csv_file])) + task :from_morph => :environment do |t, args| + 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