lib/mtif/posts.rb in mtif-0.0.1 vs lib/mtif/posts.rb in mtif-0.0.2
- old
+ new
@@ -1,21 +1,23 @@
require "time"
class MTIF
class Post
attr_accessor :source, :data
-
+
SINGLE_VALUE_KEYS = %w(author title status basename date unique_url body extended_body excerpt
- keywords allow_comments allow_pings convert_breaks no_entry).map(&:to_sym)
-
+ keywords allow_comments allow_pings convert_breaks no_entry primary_category).map(&:to_sym)
MULTILINE_KEYS = %w(body extended_body excerpt keywords comment ping).map(&:to_sym)
-
- MULTIVALUE_KEYS = %w(category tag comment ping).map(&:to_sym)
-
+ MULTIVALUE_KEYS = %w(category tags comment ping).map(&:to_sym)
+ CSV_KEYS = %w(tags).map(&:to_sym)
+
VALID_KEYS = (SINGLE_VALUE_KEYS + MULTILINE_KEYS + MULTIVALUE_KEYS).sort.uniq
DATE_FORMAT = "%m/%d/%Y %I:%M:%S %p"
+
+ FIELD_SEPARATOR = '-----'
+ POST_SEPARATOR = '--------'
def valid_keys
VALID_KEYS
end
@@ -40,18 +42,18 @@
end
def initialize(content)
@source = content
@data = {}
-
+
MULTIVALUE_KEYS.each do |key|
@data[key] = []
end
-
+
parse_source
end
-
+
def to_mtif
result = []
single_line_single_value_keys.each do |key|
value = self.send(key)
next if value.nil? || (value.respond_to?(:empty) && value.empty?)
@@ -61,45 +63,53 @@
single_line_multivalue_keys.each do |key|
values = self.send(key)
next if values.nil? || (values.respond_to?(:empty) && values.empty?)
+ if CSV_KEYS.include?(key)
+ values = [
+ values.map{|v|
+ v.include?("\s") ? "\"#{v}\"" : v
+ }.join(',')
+ ]
+ end
+
values.each do |value|
result << "#{mtif_key(key)}: #{mtif_value(value)}"
end
end
multiline_single_value_keys.each do |key|
value = self.send(key)
next if value.nil? || (value.respond_to?(:empty) && value.empty?)
- result << '-----'
+ result << FIELD_SEPARATOR
result << "#{mtif_key(key)}:\n#{mtif_value(value)}"
end
-
+
multiline_multivalue_keys.each do |key|
values = self.send(key)
next if values.nil? || (values.respond_to?(:empty) && values.empty?)
values.each do |value|
- result << '-----'
+ result << FIELD_SEPARATOR
result << "#{mtif_key(key)}:\n#{mtif_value(value)}"
end
end
- result << '-----' unless result.last == '-----' #close the final field
- result << '--------' # close the post
+ result << FIELD_SEPARATOR unless result.last == FIELD_SEPARATOR #close the final field
+ result << POST_SEPARATOR # close the post
result.join("\n") + "\n"
end
private
def method_missing(method, *args, &block)
key = method.to_s.chomp('=').to_sym
if valid_key?(key)
- if key = method
+ if key == method
data[key]
else
data[key] = args.first
end
else
@@ -114,11 +124,11 @@
end
def mtif_key_to_key(raw_key)
raw_key.strip.downcase.tr(' ','_').to_sym unless raw_key.nil?
end
-
+
def mtif_key(key)
key.to_s.tr('_', ' ').upcase
end
def mtif_value(value)
@@ -133,24 +143,30 @@
Time.strptime(raw_value, DATE_FORMAT)
else
raw_value
end
end
-
+
def store_data(raw_key, raw_value)
key = mtif_key_to_key(raw_key)
value = convert_to_native_type(raw_value)
if MULTIVALUE_KEYS.include?(key)
- self.data[key] << value unless value.empty?
+ if CSV_KEYS.include?(key)
+ value.split(',').each do |v|
+ self.data[key] << v.gsub(/^\"|\"$/, '') unless v.empty?
+ end
+ else
+ self.data[key] << value unless value.empty?
+ end
else
self.data[key] = value
end
end
def parse_source
- source.slice_before(/-----/).each do |lines|
- if lines.first =~ /^-----/ && lines.size > 1
+ source.slice_before(/^#{FIELD_SEPARATOR}/).each do |lines|
+ if lines.first =~ /^#{FIELD_SEPARATOR}/ && lines.size > 1
# Multiline data
store_data(lines.shift(2).last.chomp(":\n"), lines.join.strip)
elsif lines.first =~ /^[A-Z ]+: /
# Single-line data
lines.each do |line|