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