factor out xml building and remove duplication

This commit is contained in:
Reinier Balt 2013-09-18 19:50:08 +02:00
parent 871e18922b
commit 04560370f8
3 changed files with 84 additions and 50 deletions

View file

@ -1,5 +1,5 @@
require 'time'
require 'net/https'
require File.expand_path(File.dirname(__FILE__) + '/tracks_xml_builder')
module TracksCli
@ -33,60 +33,29 @@ module TracksCli
URI.parse(@options[:projects_uri])
end
def post_todo(todo)
req = Net::HTTP::Post.new(todo_uri.path, "Content-Type" => "text/xml")
def post(uri, body)
req = Net::HTTP::Post.new(uri.path, "Content-Type" => "text/xml")
req.basic_auth @options[:login], @options[:password]
req.body = build_todo_body(todo)
get_http(todo_uri).request(req)
req.body = body
get_http(uri).request(req)
end
def get(uri)
req = Net::HTTP::Get.new(uri.path)
req.basic_auth @options[:login], @options[:password]
get_http(uri).request(req)
end
def post_todo(todo)
post(todo_uri, TracksXmlBuilder.new.build_todo_xml(todo))
end
def post_project(project)
req = Net::HTTP::Post.new(project_uri.path, "Content-Type" => "text/xml")
req.basic_auth @options[:login], @options[:password]
req.body = build_project_body(project)
get_http(project_uri).request(req)
post(project_uri, TracksXmlBuilder.new.build_project_xml(project))
end
def get_context(context_id)
req = Net::HTTP::Get.new(context_uri_for(context_id).path)
req.basic_auth @options[:login], @options[:password]
get_http(context_uri_for(context_id)).request(req)
end
def build_todo_body(todo)
props = "<description>#{todo[:description]}</description><project_id>#{todo[:project_id]}</project_id>"
unless todo[:show_from].nil?
props << "<show-from type=\"datetime\">#{Time.at(todo[:show_from]).xmlschema}</show-from>"
end
unless todo[:notes].nil?
props << "<notes>#{todo[:notes]}</notes>"
end
unless todo[:taglist].nil?
tags = todo[:taglist].split(",")
if tags.length() > 0
tags = tags.collect { |tag| "<tag><name>#{tag.strip}</name></tag>" unless tag.strip.empty?}.join('')
props << "<tags>#{tags}</tags>"
end
end
if todo[:context_name] && !todo[:context_name].empty?
props << "<context><name>#{todo[:context_name]}</name></context>"
else
props << "<context_id>#{todo[:context_id]}</context_id>"
end
if todo[:is_dependend]
props << "<predecessor_dependencies><predecessor>#{todo[:predecessor]}</predecessor></predecessor_dependencies>"
end
"<todo>#{props}</todo>"
end
def build_project_body(project)
"<project><name>#{project[:description]}</name><default-context-id>#{project[:default_context_id]}</default-context-id></project>"
get(context_uri_for(context_id))
end
end

View file

@ -0,0 +1,66 @@
require 'time'
module TracksCli
class TracksXmlBuilder
def xml_for_description(description)
"<description>#{description}</description>"
end
def xml_for_project_id(project_id)
"<project_id>#{project_id}</project_id>"
end
def xml_for_show_from(show_from)
show_from.nil? ? "" : "<show-from type=\"datetime\">#{Time.at(show_from).xmlschema}</show-from>"
end
def xml_for_notes(notes)
notes.nil? ? "" : "<notes>#{notes}</notes>"
end
def xml_for_taglist(taglist)
unless taglist.nil?
tags = taglist.split(",")
if tags.length() > 0
tags = tags.collect { |tag| "<tag><name>#{tag.strip}</name></tag>" unless tag.strip.empty?}.join('')
return "<tags>#{tags}</tags>"
end
else
return ""
end
end
def xml_for_context(context_name, context_id)
if context_name && context_name.empty?
return "<context><name>#{context_name}</name></context>"
else
return "<context_id>#{context_id}</context_id>"
end
end
def xml_for_predecessor(dependend, predecessor)
dependend ? "<predecessor_dependencies><predecessor>#{predecessor}</predecessor></predecessor_dependencies>" : ""
end
def build_todo_xml(todo)
props = [
xml_for_description(todo[:description]),
xml_for_project_id(todo[:project_id]),
xml_for_show_from(todo[:show_from]),
xml_for_notes(todo[:notes]),
xml_for_taglist(todo[:taglist]),
xml_for_context(todo[:context_name], todo[:context_id]),
xml_for_predecessor(todo[:is_dependend], todo[:predecessor])
]
"<todo>#{props.join("")}</todo>"
end
def build_project_xml(project)
"<project><name>#{project[:description]}</name><default-context-id>#{project[:default_context_id]}</default-context-id></project>"
end
end
end

View file

@ -95,7 +95,6 @@ class TemplateParser
options[:depend]= line[0].chr == "^" ? true : false;
line = line[1..line.length] # remove first char
# find notes
tmp= line.split("|")
if tmp.length > 5
puts "Formatting error: found too many |"
@ -113,7 +112,7 @@ class TemplateParser
options[:context]=tmp[1]
options[:taglist]=tmp[2]
options[:notes]=tmp[3]
options[:notes] =tmp[3]
options
end