https://github.com/fnproject/fdk-ruby
Ruby Function Developer Kit
https://github.com/fnproject/fdk-ruby
Last synced: 12 months ago
JSON representation
Ruby Function Developer Kit
- Host: GitHub
- URL: https://github.com/fnproject/fdk-ruby
- Owner: fnproject
- License: apache-2.0
- Created: 2017-09-28T18:04:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-04-29T13:43:38.000Z (about 1 year ago)
- Last Synced: 2025-06-15T02:18:45.676Z (about 1 year ago)
- Language: Ruby
- Homepage: https://fnproject.github.io
- Size: 246 KB
- Stars: 9
- Watchers: 31
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Function Development Kit for Ruby
The Function Development Kit for Ruby (FDK for Ruby) provides a Ruby framework for developing functions for use with [Fn](https://fnproject.github.io).
[](https://circleci.com/gh/fnproject/fdk-ruby)
## Function Handler
To use this FDK, you simply need to require this gem.
```ruby
require 'fdk'
```
Then create a function with with the following syntax:
```ruby
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:
```ruby
FDK.handle(target: :myfunction)
```
## Examples
See the [examples](examples) folder of this repo for code examples.
### Hello World Example
In the [hello-ruby](examples/hello-ruby) folder there is a traditional "Hello World" example. The code is found in [func.rb](examples/hello-ruby/func.rb):
```ruby
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](https://github.com/fnproject/fn/blob/master/README.md) if you need instructions):
`cd` to the [hello-ruby](examples/hello-ruby) folder and run:
```sh
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](examples/hello-ruby) example:
```sh
$ 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!"}
```