Skip to content

Commit

Permalink
moves create_template method inline, since it's used only once
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybmerrill committed Aug 10, 2017
1 parent 2f44389 commit e9fd394
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions webapp/tabula_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,6 @@ def upload(file)
return [job_batch, file_id]
end


def create_template(template_info)
template_name = template_info["name"] || "Unnamed Template #{Time.now.to_s}"
template_id = Digest::SHA1.hexdigest(Time.now.to_s + template_name) # just SHA1 of time isn't unique with multiple uploads
template_filename = template_id + ".tabula-template.json"
file_path = File.join(TabulaSettings::DOCUMENTS_BASEPATH, "..", "templates")
# write to file
FileUtils.mkdir_p(file_path)
open(File.join(file_path, template_filename), 'w'){|f| f << JSON.dump(template_info["template"])}
page_count = template_info.has_key?("page_count") ? template_info["page_count"] : template_info["template"].map{|f| f["page"]}.uniq.count
selection_count = template_info.has_key?("selection_count") ? template_info["selection_count"] : template_info["template"].count
Tabula::Workspace.instance.add_template({
"id" => template_id,
"name" => template_name,
"page_count" => page_count,
"time" => Time.now.to_i,
"selection_count" => selection_count,
"template" => template_info["template"]
})
return template_id
end



class InvalidTemplateError < StandardError; end
TEMPLATE_REQUIRED_KEYS = ["page", "extraction_method", "x1", "x2", "y1", "y2", "width", "height"]
def upload_template(template_file)
Expand Down Expand Up @@ -163,7 +139,24 @@ def upload_template(template_file)

# create a template from the GUI
on post do
template_id = create_template(JSON.parse(req.params["model"]))
template_info = JSON.parse(req.params["model"])
template_name = template_info["name"] || "Unnamed Template #{Time.now.to_s}"
template_id = Digest::SHA1.hexdigest(Time.now.to_s + template_name) # just SHA1 of time isn't unique with multiple uploads
template_filename = template_id + ".tabula-template.json"
file_path = File.join(TabulaSettings::DOCUMENTS_BASEPATH, "..", "templates")
# write to file
FileUtils.mkdir_p(file_path)
open(File.join(file_path, template_filename), 'w'){|f| f << JSON.dump(template_info["template"])}
page_count = template_info.has_key?("page_count") ? template_info["page_count"] : template_info["template"].map{|f| f["page"]}.uniq.count
selection_count = template_info.has_key?("selection_count") ? template_info["selection_count"] : template_info["template"].count
Tabula::Workspace.instance.add_template({
"id" => template_id,
"name" => template_name,
"page_count" => page_count,
"time" => Time.now.to_i,
"selection_count" => selection_count,
"template" => template_info["template"]
})
res.status = 200
res['Content-Type'] = 'application/json'
res.write(JSON.dump({template_id: template_id}))
Expand Down

0 comments on commit e9fd394

Please sign in to comment.