Class: Cuba

Inherits:
Object
  • Object
show all
Defined in:
lib/cuba/sugar.rb

Instance Method Summary collapse

Instance Method Details

#as(http_code = 200, extra_headers = {}) ⇒ Object

Sugar to do some common response tasks

on post, “users” do

as 201 do
  "User successfully created!"
end

end



19
20
21
22
23
# File 'lib/cuba/sugar.rb', line 19

def as(http_code = 200, extra_headers = {})
  res.status = http_code
  res.headers.merge! extra_headers
  res.write yield if block_given?
end

#as_json(http_code = 200, extra_headers = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/cuba/sugar.rb', line 25

def as_json(http_code = 200, extra_headers = {})
  extra_headers["Content-Type"] ||= "application/json"
  as(http_code, extra_headers) do
    (yield).to_json if block_given?
  end
end

#csrf_tagObject



51
52
53
# File 'lib/cuba/sugar.rb', line 51

def csrf_tag
  Rack::Csrf.tag(env)
end

#csrf_tokenObject



55
56
57
# File 'lib/cuba/sugar.rb', line 55

def csrf_token
  Rack::Csrf.token(env)
end

#helpers(*extensions, &block) ⇒ Object

From sinatra/base



46
47
48
49
# File 'lib/cuba/sugar.rb', line 46

def helpers(*extensions, &block)
  instance_eval(&block) if block_given?
  extend(*extensions)   if extensions.any?
end

#optionsObject



7
# File 'lib/cuba/sugar.rb', line 7

def options; req.options? end

#paramsObject



9
# File 'lib/cuba/sugar.rb', line 9

def params; req.params end

#redirect(*args) ⇒ Object



40
41
42
43
# File 'lib/cuba/sugar.rb', line 40

def redirect(*args)
  res.redirect(*args)
  @matched = true
end

#rootObject



6
# File 'lib/cuba/sugar.rb', line 6

def root; "" end

#sessionObject



36
37
38
# File 'lib/cuba/sugar.rb', line 36

def session
  env["rack.session"]
end

#subdomain(sub) ⇒ Object



32
33
34
# File 'lib/cuba/sugar.rb', line 32

def subdomain(sub)
  sub == req.host.split(".").first
end