Module: Sinatra::AlfRest

Defined in:
lib/sinatra/alf-rest.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



64
65
66
67
68
69
# File 'lib/sinatra/alf-rest.rb', line 64

def self.registered(app)
  config = Alf::Rest::Config.new
  app.set :alf_configuration, config
  app.use Alf::Rest::Middleware, config
  app.helpers Alf::Rest::Helpers
end

Instance Method Details

#alf_restObject



56
57
58
59
60
61
62
# File 'lib/sinatra/alf-rest.rb', line 56

def alf_rest
  if block_given?
    yield(settings.alf_configuration)
  else
    settings.alf_configuration
  end
end

#rest_delete(url, heading, &bl) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sinatra/alf-rest.rb', line 41

def rest_delete(url, heading, &bl)
  delete(url) do
    input  = Tuple[heading].coerce(params.select{|k| heading[k.to_sym]})
    result = instance_exec(input, &bl)

    Alf::Rest::Response.new(env){|r|
      r.status = result.rack_status
      r.body   = result.rack_body
      if not(location_set?) and (loc = result.rack_location(request))
        r["Location"] = loc
      end
    }.finish
  end
end

#rest_get(url, &bl) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/sinatra/alf-rest.rb', line 5

def rest_get(url, &bl)
  get(url) do
    Alf::Rest::Response.new(env){|r|
      r.body = instance_exec(&bl)
    }.finish
  end
end

#rest_post(url, heading, &bl) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sinatra/alf-rest.rb', line 13

def rest_post(url, heading, &bl)
  post(url) do
    input   = Alf::Rest::Request.new(env, heading).to_tuple
    result  = instance_exec(input, &bl)

    Alf::Rest::Response.new(env){|r|
      r.status = result.rack_status
      r.body   = result.rack_body
      if not(location_set?) and (loc = result.rack_location(request))
        r["Location"] = loc
      end
    }.finish
  end
end

#rest_put(url, heading, &bl) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sinatra/alf-rest.rb', line 28

def rest_put(url, heading, &bl)
  put(url) do
    input   = Alf::Rest::Request.new(env, heading).to_tuple
    result  = instance_exec(input, &bl)

    Alf::Rest::Response.new(env){|r|
      r.status = result.rack_status
      r.body   = result.rack_body
      r["Location"] = request.path unless location_set?
    }.finish
  end
end