Sha256: 5678780a83fd3e48e00187543a9ad45e169de6ff7ef618525b97039f6c9287c8

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module FaaStRuby
  require 'base64'
  class InvalidResponseError < StandardError
    def initialize(msg)
      msg = "You must use the method 'render' within your function handler."
    end
  end
  class Response
    def self.error(error)
      new(
        body: error,
        status: 500,
        headers: {'Content-Type' => 'application/json'}
      )
    end
    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

    def self.from_payload(payload)
      from_json Base64.urlsafe_decode64(payload)
    end
    def self.from_json(json)
      hash = Oj.load(json)
      new(
        body: hash['body'],
        status: hash['status'],
        headers: hash['headers'],
        binary: hash['binary']
      )
    end
    attr_accessor :body, :status, :headers, :binary
    def initialize(body:, status: 200, headers: {}, binary: false)
      @body = body
      @status = status
      @headers = headers
      @binary = binary
    end

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

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

    def binary?
      @binary
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
faastruby-0.5.4 lib/faastruby/server/response.rb
faastruby-0.5.3 lib/faastruby/server/response.rb
faastruby-0.5.2 lib/faastruby/server/response.rb
faastruby-0.5.0 lib/faastruby/server/response.rb