Class: GitNoted::Application

Inherits:
Sinatra::Base
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/git_noted/application.rb

Constant Summary collapse

TRANSPARENT_1PX_PNG =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=".unpack('m').first

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository:, renderer: Application.default_renderer) ⇒ Application

Returns a new instance of Application.



51
52
53
54
55
# File 'lib/git_noted/application.rb', line 51

def initialize(repository:, renderer: Application.default_renderer)
  super()
  @repository = repository
  @renderer = renderer
end

Instance Attribute Details

#rendererObject

Returns the value of attribute renderer.



58
59
60
# File 'lib/git_noted/application.rb', line 58

def renderer
  @renderer
end

#repositoryObject

Returns the value of attribute repository.



57
58
59
# File 'lib/git_noted/application.rb', line 57

def repository
  @repository
end

Class Method Details

.default_rendererObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/git_noted/application.rb', line 11

def self.default_renderer
  renderer = Redcarpet::Render::HTML.new({
    escape_html: true,
    safe_links_only: true,
  })
  redcarpet = Redcarpet::Markdown.new(renderer, {
    tables: true,
    no_intra_emphasis: true
  })
  redcarpet.method(:render)
end

.with(allow_origins: [], **options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/git_noted/application.rb', line 23

def self.with(allow_origins: [], **options)
  Class.new(self) do
    alias_method :initialize_saved, :initialize
    define_method(:initialize) do
      initialize_saved(**options)
    end

    use Rack::Cors do
      allow do
        origins *allow_origins unless allow_origins.empty?

        resource '/api/*', {
            methods: [:get, :options, :head],
            headers: :any,
            expose:  [],
            credentials: true,
            max_age: 600,
        }
      end

      allow do
        origins '*'
        resource '/public/*', :headers => :any, :methods => :get
      end
    end
  end
end

Instance Method Details

#load_labels(params) ⇒ Object



156
157
158
159
160
161
# File 'lib/git_noted/application.rb', line 156

def load_labels(params)
  used_with_label_names = (params[:used_with] || '').split(',')
  prefix = params[:prefix]
  prefix = nil if prefix == ''
  @repository.search_labels(prefix: prefix, used_with: used_with_label_names)
end

#load_notes(params) ⇒ Object



150
151
152
153
154
# File 'lib/git_noted/application.rb', line 150

def load_notes(params)
  label_names = (params[:labels] || '').split(",")
  exclude_label_names = (params[:exclude_labels] || '').split(",")
  @repository.search_notes(labels: label_names, exclude_labels: exclude_label_names)
end

#read_note(note) ⇒ Object



142
143
144
# File 'lib/git_noted/application.rb', line 142

def read_note(note)
  @repository.read(note)
end

#render_note(note) ⇒ Object



146
147
148
# File 'lib/git_noted/application.rb', line 146

def render_note(note)
  @renderer.call(read_note(note))
end