Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improve RfcEntry storage model

  • Loading branch information...
commit d94e1749d66f8ced721f2b4d0f53fe2d20b6b61d 1 parent a9eceb7
@mislav authored
Showing with 54 additions and 27 deletions.
  1. +24 −5 Rakefile
  2. +3 −22 app.rb
  3. +27 −0 models.rb
View
29 Rakefile
@@ -17,19 +17,38 @@ end
task :import_index => ['tmp/rfc-index.xml', :environment] do |task|
require 'nokogiri'
require 'active_support/core_ext/object/try'
+ require 'date'
DataMapper.logger.set_log($stderr, :warn)
index = Nokogiri File.open(task.prerequisites.first)
+ num = 0
+
+ date_from_xml = ->(xml_date) {
+ if xml_date
+ year = xml_date.at('./year').text
+ month_name = xml_date.at('./month').text
+ day = xml_date.at('./day').try(:text)
+ Date.parse [year, month_name, day].join(' ')
+ end
+ }
index.search('rfc-entry').each do |xml_entry|
- entry = RfcEntry.new
- entry.document_id = xml_entry.at('./doc-id').text
- entry.title = xml_entry.at('./title').text
- entry.abstract = xml_entry.at('./abstract').try(:inner_html)
- entry.keywords = xml_entry.search('./keywords/*').map(&:text)
+ doc_id = xml_entry.at('./doc-id').text
+ unless entry = RfcEntry.get(doc_id)
+ entry = RfcEntry.new
+ entry.document_id = doc_id
+ entry.title = xml_entry.at('./title').text
+ entry.abstract = xml_entry.at('./abstract').try(:inner_html)
+ entry.keywords = xml_entry.search('./keywords/*').map(&:text)
+ end
+ entry.obsoleted = xml_entry.search('./obsoleted-by').any?
+ entry.publish_date = date_from_xml.(xml_entry.at('./date'))
+ num += 1 if entry.dirty?
entry.save!
end
+
+ puts "updated #{num} entries."
end
file 'tmp/rfc-index.xml' do |task|
View
25 app.rb
@@ -27,8 +27,7 @@
:entitystore => "file:#{ENV['TMPDIR']}/rack/body"
end
-require 'dm-migrations'
-require_relative 'searchable'
+require 'dm-core'
configure :development do
DataMapper::Logger.new($stderr, :debug)
@@ -38,25 +37,7 @@
DataMapper.setup(:default, ENV['DATABASE_URL'])
end
-class RfcEntry
- include DataMapper::Resource
- extend Searchable
-
- property :document_id, String, length: 10, key: true
- property :title, String, length: 255
- property :abstract, Text, length: 2200
- property :keywords, Text, length: 500
-
- def keywords=(value)
- if Array === value
- super(value.empty?? nil : value.join(', '))
- else
- super
- end
- end
-
- searchable [:title, :abstract, :keywords]
-end
+require_relative 'models'
get "/" do
cache_control :public
@@ -66,7 +47,7 @@ def keywords=(value)
get "/search" do
@query = params[:q]
- @results = RfcEntry.search @query, limit: 50
+ @results = RfcEntry.search_raw @query, page: params[:page], limit: 50
erb :search, {}, title: "RFC search"
end
View
27 models.rb
@@ -0,0 +1,27 @@
+require 'dm-migrations'
+require_relative 'searchable'
+
+class RfcEntry
+ include DataMapper::Resource
+ extend Searchable
+
+ property :document_id, String, length: 10, key: true
+ property :title, String, length: 255
+ property :abstract, Text, length: 2200
+ property :keywords, Text, length: 500
+ property :body, Text
+ property :obsoleted, Boolean, default: false
+ property :publish_date, Date
+
+ def keywords=(value)
+ if Array === value
+ super(value.empty?? nil : value.join(', '))
+ else
+ super
+ end
+ end
+
+ searchable title: 'A', keywords: 'B',
+ abstract: 'C', body: 'D'
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.