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 << 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