Class: RailsHttpLab::RequestsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_http_lab/requests_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



16
17
18
19
20
21
# File 'app/controllers/rails_http_lab/requests_controller.rb', line 16

def create
  rel = params.require(:path).to_s
  doc = doc_from_params
  storage.write_bru(rel, doc)
  render json: serialize(doc, rel)
end

#destroyObject



23
24
25
26
# File 'app/controllers/rails_http_lab/requests_controller.rb', line 23

def destroy
  storage.delete(path_param)
  head :no_content
end

#renameObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/rails_http_lab/requests_controller.rb', line 28

def rename
  from     = params.require(:path).to_s
  new_name = params.require(:name).to_s.strip
  raise RailsHttpLab::Error, "name cannot be empty" if new_name.empty?
  raise RailsHttpLab::Error, "name cannot contain '/' or '\\'" if new_name.match?(%r{[/\\]})

  base   = new_name.sub(/\.bru\z/i, "")
  parent = File.dirname(from)
  parent = "" if parent == "."
  to = parent.empty? ? "#{base}.bru" : "#{parent}/#{base}.bru"

  storage.rename(from, to)
  doc  = storage.read_bru(to)
  meta = doc.block("meta")
  if meta&.kv?
    meta["name"] = base
    storage.write_bru(to, doc)
  end

  render json: serialize(doc, to)
end

#showObject



5
6
7
8
# File 'app/controllers/rails_http_lab/requests_controller.rb', line 5

def show
  doc = storage.read_bru(path_param)
  render json: serialize(doc, path_param)
end

#updateObject



10
11
12
13
14
# File 'app/controllers/rails_http_lab/requests_controller.rb', line 10

def update
  doc = doc_from_params
  storage.write_bru(path_param, doc)
  render json: serialize(doc, path_param)
end