./lib/dolt/sinatra/actions.rb in dolt-0.1.1 vs ./lib/dolt/sinatra/actions.rb in dolt-0.2.0

- old
+ new

@@ -13,36 +13,69 @@ # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. #++ +require "em_rugged" + module Dolt module Sinatra module Actions - def error(status) + # Built-in redirect seems to not work with Sinatra::Async, it throws + # an error. + def redirect(url) + response.status = 302 + response["Location"] = url + body "" + end + + def error(error) response["Content-Type"] = "text/plain" - body("Process failed with exit code \n#{status.exitstatus}") + body("Process failed with exit code #{error.exit_code}:\n#{error.message}") end - def blob(repo, path, ref) - actions.blob(repo, path, ref) do |status, data| - if status.nil? - response["Content-Type"] = "text/html" - body(renderer.render(:blob, data)) - else - error(status) - end + def raw(repo, ref, path) + blob(repo, ref, path, { + :template => :raw, + :content_type => "text/plain", + :template_options => { :layout => nil } + }) + end + + def blob(repo, ref, path, options = { :template => :blob, :content_type => "text/html" }) + actions.blob(repo, ref, path) do |err, data| + return error(err) if !err.nil? + blob = data[:blob] + return redirect(tree_url(repo, ref, path)) if !blob.is_a?(Rugged::Blob) + response["Content-Type"] = options[:content_type] + tpl_options = options[:template_options] || {} + body(renderer.render(options[:template], data, tpl_options)) end end - def tree(repo, path, ref) - actions.tree(repo, path, ref) do |status, data| - if status.nil? - response["Content-Type"] = "text/html" - body(renderer.render(:tree, data)) - else - error(status) - end + def tree(repo, ref, path) + actions.tree(repo, ref, path) do |err, data| + return error(err) if !err.nil? + tree = data[:tree] + return redirect(blob_url(repo, ref, path)) if !tree.is_a?(Rugged::Tree) + response["Content-Type"] = "text/html" + body(renderer.render(:tree, data)) + end + end + + def blame(repo, ref, path) + actions.blame(repo, ref, path) do |err, data| + return error(err) if !err.nil? + response["Content-Type"] = "text/html" + body(renderer.render(:blame, data)) + end + end + + def history(repo, ref, path, count) + actions.history(repo, ref, path, count) do |err, data| + return error(err) if !err.nil? + response["Content-Type"] = "text/html" + body(renderer.render(:commits, data)) end end end end end