Skip to content

Commit c22b9e2

Browse files
affixalexellis
authored andcommitted
Add Ruby to Template Documentation
Signed-off-by: Keiran Smith <contact@keiran.scot>
1 parent dec0c31 commit c22b9e2

1 file changed

Lines changed: 79 additions & 4 deletions

File tree

docs/cli/templates.md

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pycon/requirements.txt
7676
7777
### 2.1 Python: dependencies
7878

79-
You should edit `pycon/requirements.txt` and add any pip modules you want with each one on a new line, for instance `requests`.
79+
You should edit `pycon/requirements.txt` and add any pip modules you want with each one on a new line, for instance `requests`.
8080

8181
The primary Python template uses Alpine Linux as a runtime environment due to its minimal size, but if you need a Debian environment so that you can compile `numpy` or other modules then read on to the next section.
8282

@@ -145,11 +145,86 @@ Now you can open your current folder in a tool such as Visual Studio Code and ad
145145

146146
## 5.0 Ruby
147147

148-
OpenFaaS has first-class support for Ruby.
149148

150-
Just create a new function by passing in `--lang ruby` as an argument.
149+
Create a function called `ruby-function`:
150+
151+
```
152+
$ faas-cli new --lang ruby ruby-function
153+
```
154+
155+
The directory structure is:
156+
157+
```
158+
├── ruby-function
159+
│   ├── Gemfile
160+
│   └── handler.rb
161+
├── ruby-function.yml
162+
```
163+
164+
Your code should be in the handler.rb file
165+
166+
### 5.1 Adding a Gem (Library)
167+
168+
Open the `Gemfile` in the ruby-function directory
169+
170+
Add the following line
171+
172+
```
173+
gem 'httparty'
174+
```
175+
176+
### 5.1 Using our Gem
177+
178+
Replace your `handler.rb` code with the following
179+
180+
```
181+
require 'httparty'
182+
183+
class Handler
184+
def run(req)
185+
return HTTParty.get("http://api.stackexchange.com/2.2/questions?site=stackoverflow&tagged=#{req}")
186+
end
187+
end
188+
```
189+
190+
### 5.3 Building / Deploy / Run
191+
192+
Edit the `ruby-function.yml` and point your image to your dockerhub, for example
193+
`${your_user}/ruby-function`
194+
195+
```
196+
$ faas-cli up -f ruby-function.yml
197+
...
198+
Using bundler 1.16.4
199+
Fetching multi_xml 0.6.0
200+
Installing multi_xml 0.6.0
201+
Fetching httparty 0.16.2
202+
Installing httparty 0.16.2
203+
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
204+
Bundled gems are installed into `/usr/local/bundle`
205+
Post-install message from httparty:
206+
When you HTTParty, you must party hard!
207+
...
208+
```
209+
210+
### 5.4 Invoke!
211+
212+
```
213+
$ echo 'OpenFaaS' | faas-cli invoke ruby-function
214+
{
215+
"quota_remaining" : 298,
216+
"quota_max" : 300,
217+
"has_more" : false,
218+
"items" : [
219+
{
220+
"title" : "Scaling with GPU usage",
221+
"creation_date" : 1536315498,
222+
"answer_count" : 0,
223+
"view_count" : 10,
224+
"is_answered" : false,
225+
...
226+
```
151227

152-
Manage your dependencies through your `Gemfile` placed in the function's folder.
153228

154229
## 6.0 Java
155230

0 commit comments

Comments
 (0)