Skip to content
Ruby Function Developer Kit
Ruby Shell
Branch: master
Clone or download
crush-157 Merge pull request #29 from fnproject/feature/log-frames
If enabled, print log framing content to stdout/stderr
Latest commit 46dc1be May 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci updated circleci/config.yml after chat with Owen Oct 29, 2018
examples Bump version to 0.0.20 May 15, 2019
lib
tests If enabled, print log framing content to stdout/stderr May 13, 2019
.gitignore Support for CloudEvents format. (#10) Apr 24, 2018
.rubocop.yml explicity disabling the Style/DateTime cop (should be disabled by def… Oct 29, 2018
LICENSE Initial commit Sep 28, 2017
README.md add circle releases Oct 5, 2018
Rakefile Added RuboCop task to Rakefile Oct 29, 2018
fdk.gemspec added webrick as runtime dependency to Gemfile Jan 29, 2019
release.sh Fix tag Nov 17, 2017

README.md

Ruby Function Developer Kit (FDK)

This provides a Ruby framework for developing functions for use with Fn.

CircleCI

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 myfunction(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(target: :myfunction)

Examples

See the examples folder of this repo for code examples.

Hello World Example

In the hello-ruby folder there is a traditional "Hello World" example. The code is found in func.rb:

require 'fdk'

def myfunction(context:, input:)
  input_value = input.respond_to?(:fetch) ? input.fetch('name') : input
  name = input_value.to_s.strip.empty? ? 'World' : input_value
  { message: "Hello #{name}!" }
end

FDK.handle(target: :myfunction)

Deploying functions

To use a function we need to deploy it to an fn server.

In fn an app consist of one or more functions and each function is deployed as part of an app.

We're going to deploy the hello world example as part of the app examples.

With an fn server running (see Quickstart if you need instructions):

cd to the hello-ruby folder and run:

fn deploy --app examples --local

The --app examples option tells fn to deploy the function as part of the app named examples.

The --local option tells fn not to push the function image to Docker Hub.

Invoking functions

Once we have deployed a function we can invoke it using fn invoke.

Running the Hello World example:

$ fn invoke examples hello
{"message":"Hello World!"}

To get a more personal message, send a name in a JSON format and set the content-type 'application/json':

echo '{"name":"Joe"}' | fn invoke examples hello --content-type 'application/json'
{"message":"Hello Joe!"}
You can’t perform that action at this time.