mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2024-12-22 15:43:14 +01:00
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:
parent
f35aa2a9d3
commit
2b4281ad87
2 changed files with 15 additions and 11 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue