Module: Dolt::Sinatra::Actions

Included in:
Base
Defined in:
lib/dolt/sinatra/actions.rb

Instance Method Summary collapse

Instance Method Details

#blame(repo, ref, path) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/dolt/sinatra/actions.rb', line 101

def blame(repo, ref, path)
  actions.blame(repo, ref, path) do |err, data|
    next error(err, repo, ref) if !err.nil?
    add_headers(response, :ref => ref)
    body(renderer.render(:blame, data))
  end
end

#blob(repo, ref, path, options = { :template => :blob }) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/dolt/sinatra/actions.rb', line 64

def blob(repo, ref, path, options = { :template => :blob })
  actions.blob(repo, ref, path) do |err, data|
    next error(err, repo, ref) if !err.nil?
    blob = data[:blob]
    next redirect(tree_url(repo, ref, path)) if blob.class.to_s !~ /\bBlob/
    add_headers(response, options.merge(:ref => ref))
    tpl_options = options[:template_options] || {}
    body(renderer.render(options[:template], data, tpl_options))
  end
end

#error(error, repo, ref) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dolt/sinatra/actions.rb', line 32

def error(error, repo, ref)
  add_headers(response)
  body(renderer.render(:"500", {
                         :error => error,
                         :repository_slug => repo,
                         :ref => ref
                       }))
rescue Exception => err
  err_backtrace = err.backtrace.map { |s| "<li>#{s}</li>" }
  error_backtrace = error.backtrace.map { |s| "<li>#{s}</li>" }

  body(<<-HTML)
  <h1>Fatal Dolt Error</h1>
  <p>
    Dolt encountered an exception, and additionally
    triggered another exception trying to render the error.
  </p>
  <h2>Error: #{err.class} #{err.message}</h2>
  <ul>#{err_backtrace.join()}</ul>
  <h2>Original error: #{error.class} #{error.message}</h2>
  <ul>#{error_backtrace.join()}</ul>
  HTML
end

#history(repo, ref, path, count) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/dolt/sinatra/actions.rb', line 109

def history(repo, ref, path, count)
  actions.history(repo, ref, path, count) do |err, data|
    next error(err, repo, ref) if !err.nil?
    add_headers(response, :ref => ref)
    body(renderer.render(:commits, data))
  end
end

#raw(repo, ref, path) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/dolt/sinatra/actions.rb', line 56

def raw(repo, ref, path)
  blob(repo, ref, path, {
         :template => :raw,
         :content_type => "text/plain",
         :template_options => { :layout => nil }
       })
end

#redirect(url) ⇒ Object

Built-in redirect seems to not work with Sinatra::Async, it throws an error.



26
27
28
29
30
# File 'lib/dolt/sinatra/actions.rb', line 26

def redirect(url)
  response.status = 302
  response["Location"] = url
  body ""
end

#refs(repo) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/dolt/sinatra/actions.rb', line 117

def refs(repo)
  actions.refs(repo) do |err, data|
    next error(err, repo, ref) if !err.nil?
    add_headers(response, :content_type => "application/json")
    body(renderer.render(:refs, data, :layout => nil))
  end
end

#tree(repo, ref, path) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dolt/sinatra/actions.rb', line 75

def tree(repo, ref, path)
  actions.tree(repo, ref, path) do |err, data|
    begin
      next error(err, repo, ref) if !err.nil?
      tree = data[:tree]
      next redirect(blob_url(repo, ref, path)) if tree.class.to_s !~ /\bTree/
      add_headers(response, :ref => ref)
      body(renderer.render(:tree, data))
    rescue Exception => err
      error(err, repo, ref)
    end
  end
end

#tree_entry(repo, ref, path) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dolt/sinatra/actions.rb', line 89

def tree_entry(repo, ref, path)
  actions.tree_entry(repo, ref, path) do |err, data|
    begin
      next error(err, repo, ref) if !err.nil?
      add_headers(response, :ref => ref)
      body(renderer.render(data.key?(:tree) ? :tree : :blob, data))
    rescue Exception => err
      error(err, repo, ref)
    end
  end
end

#tree_history(repo, ref, path, count = 1) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dolt/sinatra/actions.rb', line 125

def tree_history(repo, ref, path, count = 1)
  actions.tree_history(repo, ref, path, count) do |err, data|
    begin
      if !err.nil?
        error(err, repo, ref)
      else
        add_headers(response, :content_type => "application/json", :ref => ref)
        body(renderer.render(:tree_history, data, :layout => nil))
      end
    rescue Exception => err
      error(err, repo, ref)
    end
  end
end