Ruby Function Developer Kit (FDK)

This provides a Ruby framework for deleloping functions for us with Fn.

Function Handler

To use this FDK, you simply need to require this gem.

require 'fdk`

Then create a function with with the following syntax:

def myfunc(context, input)
    # Do some work here
    return output
end
  • context - provides runtime information for your function, such as configuration values, headers, etc.
  • input – This parameter is a string containing body of the request.
  • output - is where you can return data back to the caller. Whatever you return will be sent back to the caller. If async, this value is ignored.
    • Default output format should be in JSON, as Content-Type header will be application/json by default. You can be more flexible if you create and return an FDK::Response object instead of a string.

Then simply pass that function to the FDK:

FDK.handle(myfunction)

Full Example

require 'fdk'

def myfunction(context:, input:)
    STDERR.puts "request_url: " + context.protocol['request_url']
    STDERR.puts "call_id: " + context.call_id
    STDERR.puts "input: " + input.to_s
    { message: "Hello #{input['name']}!" }
end

FDK.handle(:myfunction)

Running the example that is in the root directory of this repo

$ echo '{"name":"coolio"}' | fn run
{"message":"Hello coolio!"}

You can also specify the format (the default is JSON)

$ echo '{"name":"coolio"}' | fn run --format json
{"message":"Hello coolio!"}

If you want to just pass plain text to the function, specify a format of default:

$ echo 'coolio' | fn run --format default
{"message":"Hello coolio!"}

Deploy:

fn deploy --app myapp --local && echo '{"name":"coolio"}' | fn call myapp /fdk-ruby

Change to hot:

Update func.yaml: format: json

fn deploy --app myapp --local && echo '{"name":"coolio"}' | fn call myapp /fdk-ruby

Compare cold and hot

Run

ruby loop.rb