mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2024-12-22 23:53:15 +01:00
32 lines
993 B
Ruby
32 lines
993 B
Ruby
require 'elasticsearch/model'
|
|
require 'json'
|
|
|
|
class Paper < ActiveRecord::Base
|
|
include Elasticsearch::Model
|
|
include Elasticsearch::Model::Callbacks
|
|
|
|
validates_presence_of :body, :content, :name, :originator, :paper_type, :published_at, :reference, :url
|
|
validates :url, uniqueness: true
|
|
|
|
class << self
|
|
def import_from_json(json_string)
|
|
old_count = count
|
|
JSON.parse(json_string).each do |record|
|
|
attributes = {
|
|
body: record['body'],
|
|
content: record['content'],
|
|
name: record['name'],
|
|
resolution: record['resolution'],
|
|
originator: record['originator'],
|
|
paper_type: record['paper_type'],
|
|
published_at: record['published_at'],
|
|
reference: record['reference'],
|
|
url: record['url'],
|
|
}
|
|
record = find_or_initialize_by(url: attributes[:url])
|
|
record.update_attributes(attributes)
|
|
end
|
|
puts "Imported #{count - old_count} Papers!"
|
|
end
|
|
end
|
|
end
|