Class: FaaStRuby::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/faastruby/spec_helper.rb,
lib/faastruby/server/response.rb

Overview

Add call method to the Response class for backwards compatibility. This will be removed on v0.6

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, status: 200, headers: {}, binary: false) ⇒ Response

Returns a new instance of Response.



50
51
52
53
54
55
56
57
58
59
# File 'lib/faastruby/server/response.rb', line 50

def initialize(body:, status: 200, headers: {}, binary: false)
  if body.is_a?(String) || body.nil?
    @body = body
  else
    @body = body.inspect
  end
  @status = status
  @headers = headers
  @binary = binary
end

Instance Attribute Details

#binaryObject

Returns the value of attribute binary.



49
50
51
# File 'lib/faastruby/server/response.rb', line 49

def binary
  @binary
end

#bodyObject

Returns the value of attribute body.



49
50
51
# File 'lib/faastruby/server/response.rb', line 49

def body
  @body
end

#headersObject

Returns the value of attribute headers.



49
50
51
# File 'lib/faastruby/server/response.rb', line 49

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



49
50
51
# File 'lib/faastruby/server/response.rb', line 49

def status
  @status
end

Class Method Details

.error(error) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/faastruby/server/response.rb', line 9

def self.error(error)
  new(
    body: error,
    status: 500,
    headers: {'Content-Type' => 'application/json'}
  )
end

.from_json(json) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/faastruby/server/response.rb', line 40

def self.from_json(json)
  hash = Oj.load(json)
  new(
    body: hash['body'],
    status: hash['status'],
    headers: hash['headers'],
    binary: hash['binary']
  )
end

.from_payload(payload) ⇒ Object



37
38
39
# File 'lib/faastruby/server/response.rb', line 37

def self.from_payload(payload)
  from_json Base64.urlsafe_decode64(payload)
end

.invalid_responseObject



27
28
29
30
31
32
33
34
35
# File 'lib/faastruby/server/response.rb', line 27

def self.invalid_response
  body = {'error' => "Your function must render a response. For example, render text: \"Hello World!\". If you want to respond with an empty body, use 'render_nothing'."}
  body = Oj.dump(body)
  new(
    body: body,
    status: 500,
    headers: {'Content-Type' => 'application/json'}
  )
end

.request_limit_reached(workspace: nil, function: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/faastruby/server/response.rb', line 16

def self.request_limit_reached(workspace: nil, function: nil)
  body = {'error' => "Concurrent requests limit reached. Please add more runners to the workspace #{workspace}."} if workspace
  # body = {'error' => "Concurrent requests limit reached for function '#{workspace}/#{function}'. Please associate more runners."} if function
  body = Oj.dump(body)
  new(
    body: body,
    status: 422,
    headers: {'Content-Type' => 'application/json'}
  )
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/faastruby/server/response.rb', line 75

def binary?
  @binary
end

#callObject



14
15
16
# File 'lib/faastruby/spec_helper.rb', line 14

def call
  self
end

#payloadObject



71
72
73
# File 'lib/faastruby/server/response.rb', line 71

def payload
  Base64.urlsafe_encode64(to_json, padding: false)
end

#to_jsonObject



61
62
63
64
65
66
67
68
69
# File 'lib/faastruby/server/response.rb', line 61

def to_json
  hash = {
    'body' => body,
    'status' => status,
    'headers' => headers,
    'binary' => binary
  }
  Oj.dump(hash)
end