Class: RevisionPlate::App

Inherits:
Object
  • Object
show all
Defined in:
lib/revision_plate/app.rb

Constant Summary collapse

ACCEPT_METHODS =
['GET', 'HEAD'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, path: nil) ⇒ App

Returns a new instance of App.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/revision_plate/app.rb', line 5

def initialize(file = nil, path: nil)
  @file = file
  @path = path
  if @file
    unless @file.kind_of?(Pathname)
      @file = Pathname.new(@file)
    end
  else
    if defined? Rails
      @file = Rails.root.join('REVISION')
    else
      raise ArgumentError, "couldn't locate REVISION file"
    end
  end

  if @file.exist?
    @revision = @file.read.chomp
  else
    @revision = nil
  end
end

Instance Method Details

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/revision_plate/app.rb', line 29

def call(env)
  unless ACCEPT_METHODS.include?(env['REQUEST_METHOD']) && (@path ? env['PATH_INFO'] == @path : true)
    return [404, {'Content-Type' => 'text/plain'}, []]
  end

  if @revision
    if @file.exist?
      status = 200
      body = [@revision, ?\n]
    else
      status = 404
      body = ["REVISION_FILE_REMOVED\n"]
    end
  else
    status = 404
    body = ["REVISION_FILE_NOT_FOUND\n"]
  end

  headers = {'Content-Type' => 'text/plain'}
  if env['REQUEST_METHOD'] == 'HEAD'
    [status, headers, []]
  else
    [status, headers, body]
  end
end