2015-04-27 21:26:37 +02:00
|
|
|
require 'elasticsearch/model'
|
2015-04-27 21:03:58 +02:00
|
|
|
require 'json'
|
2015-04-13 22:09:28 +02:00
|
|
|
|
|
|
|
class Paper < ActiveRecord::Base
|
2015-04-27 21:26:37 +02:00
|
|
|
include Elasticsearch::Model
|
|
|
|
include Elasticsearch::Model::Callbacks
|
|
|
|
|
2015-05-17 17:49:09 +02:00
|
|
|
validates_presence_of :body, :content, :name, :originator, :paper_type, :published_at, :reference, :url
|
|
|
|
validates :url, uniqueness: true
|
|
|
|
|
2015-04-13 22:09:28 +02:00
|
|
|
class << self
|
2015-04-27 21:03:58 +02:00
|
|
|
def import_from_json(json_string)
|
2015-05-17 17:49:09 +02:00
|
|
|
old_count = count
|
2015-04-27 21:03:58 +02:00
|
|
|
JSON.parse(json_string).each do |record|
|
2015-04-13 22:09:28 +02:00
|
|
|
attributes = {
|
2015-05-17 17:49:09 +02:00
|
|
|
body: record['body'],
|
|
|
|
content: record['content'],
|
2015-04-27 21:03:58 +02:00
|
|
|
name: record['name'],
|
2015-05-17 17:49:09 +02:00
|
|
|
resolution: record['resolution'],
|
2015-04-27 21:03:58 +02:00
|
|
|
originator: record['originator'],
|
2015-05-17 17:49:09 +02:00
|
|
|
paper_type: record['paper_type'],
|
2015-04-27 21:03:58 +02:00
|
|
|
published_at: record['published_at'],
|
2015-05-17 17:49:09 +02:00
|
|
|
reference: record['reference'],
|
|
|
|
url: record['url'],
|
2015-04-13 22:09:28 +02:00
|
|
|
}
|
2015-04-28 00:47:26 +02:00
|
|
|
record = find_or_initialize_by(url: attributes[:url])
|
2015-05-17 17:49:09 +02:00
|
|
|
record.update_attributes(attributes)
|
2015-04-13 22:09:28 +02:00
|
|
|
end
|
2015-05-17 17:49:09 +02:00
|
|
|
puts "Imported #{count - old_count} Papers!"
|
2015-04-13 22:09:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|