Module: FaaStRuby::RunnerMethods

Included in:
FunctionObject, SpecHelper
Defined in:
lib/faastruby/server/runner_methods.rb

Instance Method Summary collapse

Instance Method Details

#puts(msg) ⇒ Object



92
93
94
# File 'lib/faastruby/server/runner_methods.rb', line 92

def puts(msg)
  super "[#{@path}] #{msg}".green
end

#redirect_to(function: nil, url: nil, status: 303) ⇒ Object



87
88
89
90
# File 'lib/faastruby/server/runner_methods.rb', line 87

def redirect_to(function: nil, url: nil, status: 303)
  headers = {"Location" => function || url}
  respond_with(nil, status: status, headers: headers, binary: false)
end

#render(js: nil, css: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, data: nil, png: nil, svg: nil, jpeg: nil, gif: nil, icon: nil, status: 200, headers: {}, content_type: nil, binary: false) ⇒ Object



18
19
20
21
22
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/faastruby/server/runner_methods.rb', line 18

def render(
    js: nil,
    css: nil,
    body: nil,
    inline: nil,
    html: nil,
    json: nil,
    yaml: nil,
    text: nil,
    data: nil,
    png: nil,
    svg: nil,
    jpeg: nil,
    gif: nil,
    icon: nil,
    status: 200, headers: {}, content_type: nil, binary: false
  )
  headers["Content-Type"] = content_type if content_type
  bin = false
  case
  when json
    headers["Content-Type"] ||= "application/json"
    resp_body = json.is_a?(String) ? json : Oj.dump(json)
  when html, inline
    headers["Content-Type"] ||= "text/html"
    resp_body = html
  when text
    headers["Content-Type"] ||= "text/plain"
    resp_body = text
  when yaml
    headers["Content-Type"] ||= "application/yaml"
    resp_body = yaml.is_a?(String) ? yaml : YAML.load(yaml)
  when body
    headers["Content-Type"] ||= "application/octet-stream"
    bin = binary
    resp_body = bin ? Base64.urlsafe_encode64(body) : body
  when data
    headers["Content-Type"] ||= "application/octet-stream"
    resp_body = Base64.urlsafe_encode64(data, padding: false)
    bin = true
  when js
    headers["Content-Type"] ||= "text/javascript"
    resp_body = js
  when css
    headers["Content-Type"] ||= "text/css"
    resp_body = css
  when png
    headers["Content-Type"] ||= "image/png"
    resp_body = Base64.urlsafe_encode64(File.binread(png), padding: false)
    bin = true
  when svg
    headers["Content-Type"] ||= "image/svg+xml"
    resp_body = svg
  when jpeg
    headers["Content-Type"] ||= "image/jpeg"
    resp_body = Base64.urlsafe_encode64(File.binread(jpeg), padding: false)
    bin = true
  when gif
    headers["Content-Type"] ||= "image/gif"
    resp_body = Base64.urlsafe_encode64(File.binread(gif), padding: false)
    bin = true
  when icon
    headers["Content-Type"] ||= "image/x-icon"
    resp_body = Base64.urlsafe_encode64(File.binread(icon), padding: false)
    bin = true
  end
  respond_with(resp_body, status: status, headers: headers, binary: bin)
end

#rendered!Object



4
5
6
# File 'lib/faastruby/server/runner_methods.rb', line 4

def rendered!
  @rendered = true
end

#rendered?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/faastruby/server/runner_methods.rb', line 7

def rendered?
  @rendered
end

#respond_with(body, status: 200, headers: {}, binary: false) ⇒ Object



11
12
13
14
15
16
# File 'lib/faastruby/server/runner_methods.rb', line 11

def respond_with(body, status: 200, headers: {}, binary: false)
  raise FaaStRuby::DoubleRenderError.new("You called 'render/respond_with/redirect_to' twice in your handler method") if rendered?
  response = FaaStRuby::Response.new(body: body, status: status, headers: headers, binary: binary)
  rendered!
  response
end