Skip to content

Commit

Permalink
Add Ruby2.5 runtime to runtimes manifest.
Browse files Browse the repository at this point in the history
Update documentation with examples.
  • Loading branch information
remore authored and rabbah committed Jun 26, 2018
1 parent d89e1d0 commit b9699cf
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ansible/files/runtimes.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
"name": "action-php-v7.1"
}
}
],
"ruby": [
{
"kind": "ruby:2.5",
"default": true,
"deprecated": false,
"image": {
"prefix": "remore",
"name": "openwhisk-runtime-ruby",
"tag": "latest"
}
}
]
},
"blackboxes": [
Expand Down
61 changes: 60 additions & 1 deletion docs/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Actions are stateless code snippets that run on the OpenWhisk platform.
For example, an action can be used to detect the faces in an image, respond to a database change,
aggregate a set of API calls, or post a Tweet.
An action can be written as a JavaScript, Swift, Python or PHP function, a Java method,
An action can be written as a JavaScript, Swift, Python or PHP function, a Java or Ruby method,
any binary-compatible executable including Go programs and custom executables packaged as Docker containers.

Actions can be explicitly invoked, or run in response to an event.
Expand All @@ -42,6 +42,7 @@ Learn how to create, invoke, and debug actions in your preferred development env
* [Python](#creating-python-actions)
* [Java](#creating-java-actions)
* [PHP](#creating-php-actions)
* [Ruby](#creating-ruby-actions)
* [Docker](#creating-docker-actions)
* [Go](#creating-go-actions)
* [Native binaries](#creating-native-actions)
Expand Down Expand Up @@ -645,6 +646,64 @@ wsk action create helloPHP --kind php:7.1 helloPHP.zip
```


## Creating Ruby actions

The process of creating Ruby actions is similar to that of JavaScript actions. The following sections guide you through creating and invoking a single Ruby action, and demonstrate how to zip your Ruby actions.

### Creating and invoking a Ruby action

An action is simply a top-level Ruby method. For example, create a file called `hello.rb` with the following source code:

```ruby
def main(args)
name = args["name"] || "stranger"
greeting = "Hello #{name}!"
print greeting
{ "greeting" => greeting }
end
```

Ruby actions always consume a Hash and return a Hash. The entry method for the action is `main` by default but may be specified explicitly when creating the action with the `wsk` CLI using `--main`, as with any other action type.

You can create an OpenWhisk action called `helloRuby` from this function as follows:

```
wsk action create helloRuby hello.rb
```

The CLI automatically infers the type of the action from the source file extension. For `.rb` source files, the action runs using a Ruby 2.5 runtime. See the Ruby [reference](./reference.md#ruby-actions) for more information.

Action invocation is the same for Ruby actions as it is for JavaScript actions:

```
wsk action invoke --result helloRuby --param name World
```

```json
{
"greeting": "Hello World!"
}
```

Find out more about parameters in the [Working with parameters](./parameters.md) section.

### Packaging Ruby actions in zip files

You can package a Ruby action along with other files and dependent packages in a zip file.
The filename of the source file containing the entry point (e.g., `main`) must be `main.rb`.
For example, to create an action that includes a second file called `helper.rb`, first create an archive containing your source files:

```bash
zip -r hello_ruby.zip main.rb helper.rb
```

and then create the action:

```bash
wsk action create helloRuby --kind ruby:2.5 hello_ruby.zip
```


## Creating Swift actions

The process of creating Swift actions is similar to that of JavaScript actions. The following sections guide you through creating and invoking a single swift action, and packaging an action in a zip file.
Expand Down
2 changes: 1 addition & 1 deletion docs/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This page outlines how to configure parameters when deploying packages and actio

### Passing parameters to an action at invoke time

Parameters can be passed to the action when it is invoked. These examples use JavaScript but all the other languages work the same way (see documentation on [Swift actions](./actions.md#creating-swift-actions), [Python actions](./actions.mdcreating-python-actions), [Java actions](./actions.mdcreating-java-actions), [PHP actions](./actions.mdcreating-php-actions), [Docker actions](./actions.mdcreating-docker-actions) or [Go actions](./actions.mdcreating-go-actions) as appropriate for more detailed examples).
Parameters can be passed to the action when it is invoked. These examples use JavaScript but all the other languages work the same way (see documentation on [this page](./actions.md) as appropriate for more detailed examples that are language specific).

1. Use parameters in the action. For example, create 'hello.js' file with the following content:

Expand Down
8 changes: 8 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ The following Composer packages are also available:
- guzzlehttp/guzzle v6.3.0
- ramsey/uuid v3.6.1

## Ruby actions

Ruby actions are executed using Ruby 2.5. To use this runtime, specify the `wsk` CLI parameter `--kind ruby:2.5` when creating or updating an action. This is the default when creating an action with file that has a `.rb` extension.

A few Ruby gems such as `mechanize` and `jwt` are available in addition to the default and bundled gems. See Dockerfile of ruby runtime image for more information.

Last but not least, you can use arbitrary gem so long as you use zipped actions to package all the dependencies. [this page](./actions.md) will explain how to do this.

## Docker actions

Docker actions run a user-supplied binary in a Docker container. The binary runs in a Docker image based on [python:3.6.1-alpine](https://hub.docker.com/r/library/python), so the binary must be compatible with this distribution.
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/common/rest/WskRest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ class WskRestAction
case ".php" => {
("php:default", FileUtils.readFileToString(new File(artifactFile), StandardCharsets.UTF_8), artifactFile)
}
case ".rb" => {
("ruby:default", FileUtils.readFileToString(new File(artifactFile), StandardCharsets.UTF_8), artifactFile)
}
case _ => ("", "", artifactFile)
}
}
Expand Down

0 comments on commit b9699cf

Please sign in to comment.