The New Hello World for faastRuby - The Serverless Platform for Ruby & Crystal - Part 2.
Tags: ruby, crystal, faastruby, serverless
Published: 2019-04-13 20:23:19

---
title: The New Hello World for faastRuby - The Serverless Platform for Ruby & Crystal - Part 2. 
published: true
description: 
tags: ruby, crystal, faastruby, serverless
---

![alt text](https://faastruby.io/wp-content/uploads/2019/03/logo-negative-1.png "faastRuby logo")

Hi folks! Before we get started, if you have not already read my previous article - The New Hello World for faastRuby. The Serverless Platform for Ruby & Crystal: [Part 1](https://dev.to/sergiosouzalima/the-new-hello-world-for-faastruby-the-serverless-platform-for-ruby-crystal-part-1-2o95) -  you should definitely give it a look over as we’re going to be building on what we learned there.

### What are we going to create?

Using the [faastRuby](https://www.faastruby.io) Platform, we'll create a function in Ruby and make it quickly available on the internet.

## Step by Step Function Creation

### Login

Enter the folder we created earlier [Part 1](https://dev.to/sergiosouzalima/the-new-hello-world-for-faastruby-the-serverless-platform-for-ruby-crystal-part-1-2o95).

```Bash
$ cd faastruby
$ faastruby/> 
```

Log in with your account you previously created. ([Part 1](https://dev.to/sergiosouzalima/the-new-hello-world-for-faastruby-the-serverless-platform-for-ruby-crystal-part-1-2o95)).

```Bash
$ faastruby/>faastruby login       
Email: you@your-email.com
Password: ********
Login successful.
```

### Creating the Hello World Project

Now let's create our project named, Hello World.


```Bash
$ faastruby/>faastruby new-project hello-world --api
```

The *faastruby new-project* command will create the *hello-world* folder with subfolders, static files, and initial functions. You will see the output below:

```Bash
+ d ./hello-world
+ f ./hello-world/project.yml
+ f ./hello-world/secrets.yml
+ d ./hello-world/functions/root
+ f ./hello-world/functions/root/handler.rb
+ f ./hello-world/functions/root/faastruby.yml
+ d ./hello-world/functions/catch-all
+ f ./hello-world/functions/catch-all/handler.rb
+ f ./hello-world/functions/catch-all/faastruby.yml
+ d ./hello-world/public
+ f ./hello-world/public/faastruby.yml
+ f ./hello-world/.gitignore
Initialized empty Git repository in /Users/you/workspace/faastruby/hello-world/.git/
Project 'hello-world' initialized.
Now run:
$ cd hello-world
$ faastruby local

Then visit http://localhost:3000
```
Enter into the *hello-world* folder we just created.

```Bash
$ faastruby/> cd hello-world
$ faastruby/hello-world/>
```
### Folder & files explanation

```Bash
├── functions <---------------- Put your functions inside this directory
│   ├── catch-all <------------ This function will run when a function is not found.
│   │   ├── faastruby.yml
│   │   └── handler.rb
│   └── root <----------------- This function will run when a request ie made to the root path '/'
│       ├── faastruby.yml 
│       └── handler.rb
├── project.yml <-------------- Project configuration file.
├── public <------------------- Put static files inside this folder.
│   └── faastruby.yml <-------- Static files configuration.
└── secrets.yml <-------------- Put your secrets here.
```

### Starting faastRuby local

Now let's start the faastRuby local server and check if our project is really working.

```Bash
$ faastruby/hello-world/> faastruby local
```

You'll see the output

```Bash
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.0-p0), codename: Llamas in Pajamas
* Min threads: 0, max threads: 32
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
2019-03-23 16:21:23 -0300 | Detecting existing functions.
---
2019-03-23 16:21:23 -0300 | Ruby functions: ["root", "catch-all"]
---
2019-03-23 16:21:23 -0300 | Crystal functions: []
---
2019-03-23 16:21:23 -0300 | Listening for changes.
---
2019-03-23 16:21:23 -0300 | faastRuby Local is ready at http://localhost:3000
```
Then, visit [http://localhost:3000](). 

If everything went well, you now have the *root* function response in your browser.

```javascript
{
"message": "Welcome to FaaStRuby Local! Edit the function 'root' to customize this response."
}
``` 
### Creating your first function

Your first function will be called *hello* and it will respond in json format with the message "Hello, World!".

**Note**: to create functions, you must be inside the *functions* folder of your project.

```Bash
$ faastruby/hello-world/> cd functions
$ faastruby/hello-world/functions/>
```

Use *faastruby new hello* command to create the *hello* function. 

```Bash
$ faastruby/hello-world/functions> faastruby new hello
```

```Bash
+ d hello
+ d hello/spec
+ f hello/spec/spec_helper.rb
+ f hello/spec/handler_spec.rb
+ f hello/README.md
+ f hello/Gemfile
+ f hello/handler.rb
+ f hello/faastruby.yml
✔ Installing gems...
```

The *faastruby new* command, created specific folders and files for your *hello* function.

Let's go inside the *hello* folder to implement our solution in Ruby.

```Bash
$ faastruby/hello-world/functions/> cd hello
$ faastruby/hello-world/functions/hello/>
```

Make the file, *handler.rb* look like:

```ruby
# hello-world/functions/hello/handler.rb
require 'json'

def handler event
  render json: { 'message' => 'Hello, World!' }
end
```

We have to modify the unit test:

```ruby
# hello-world/functions/hello/spec/handler_spec.rb
require 'spec_helper'
require 'handler'
require 'json'

describe '#handler' do
  let(:body) { handler(event).body }
  let(:event) {
    Event.new(
      body: nil, query_params: {},
      headers: {}, context: nil
    )
  }

  context 'when function is requested' do
    let(:response_value) {
      JSON.parse(body).values.first
    }
    it 'returns a String' do
      expect(body).to be_a(String)
    end
    it "returns 'Hello, World!'" do
      expect(response_value).to eq('Hello, World!')
    end
  end
end
```

Now we need to include a gem in the Gemfile file of this function. 

Since we are using *json* gem within the *hello* function, we need to include it in Gemfile.

```ruby
# hello-world/functions/hello/Gemfile
source 'https://rubygems.org'

gem 'json'

group :test do
  gem 'rspec'
  gem 'faastruby-rpc'
  gem 'faastruby'
end
``` 

Don't forget to run *bundle install*

```Bash
$ faastruby/hello-world/functions/hello/> bundle install
```

Are our unit tests passing?

```Bash
faastruby/hello-world/functions/hello/> rspec -fd
```

```Bash
handler
  when function is requested
    returns 'Hello, World!'
    returns a String

Finished in 3.89 seconds (files took 0.6736 seconds to load)
2 examples, 0 failures
```

### Starting the local server again.

**Remember**: to upload the local server, the *faastruby local* command, must be used in the main project folder.

So, go to the root folder of the *hello-world* project, and start the server.

```Bash
$ faastruby/hello-world/> faastruby local
```

Let's see how our *hello* function works.

Visit in your browser: [http://localhost:3000/hello]()

You will see the following result in your browser.

```javascript
{
"message": "Hello, World!"
}
``` 

## Your first deploy

When we deploy the project to the faastRuby platform, the functions and static files go to the Workspace.

In order to deploy the project, the *faastruby deploy* command must be used in the main project folder.

So make sure you are in the *hello-world* folder and run the deploy command.


```Bash
$ faastruby/hello-world/> faastruby deploy
```

```Bash
┌ Deploying project 'hello-world' to workspace 'hello-world-stage-8f6999'
├── [✔] Connecting to workspace 'hello-world-stage-999999'
├── [✔] Uploading static assets in 'public'
├── [✔] Deploying function from 'functions/hello'
├── [✔] Deploying function from 'functions/catch-all'
└── [✔] Deploying function from 'functions/root'
* Project URL: https://hello-world-stage-999999.tor1.faast.cloud
```

Finally, your function is available in the cloud! Your URL will look like the following URL.

Visit in your browser: [https://hello-world-stage-999999.tor1.faast.cloud/hello]()

You'll see the output below:

```javascript
{
"message": "Hello, World!"
}
``` 

## Conclusion

* We learned how to build a faastRuby project from scratch and send it to the cloud.
* We learned how to create, test and execute a Ruby function locally using faastRuby local server.


I hope you enjoyed the article!