### Install Toys Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md To run the commands here, first install Toys. ```sh gem install toys ``` -------------------------------- ### Install and Run Framework Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md Installs dependencies and starts the local web server running the 'hello' function. ```shell bundle install # ...installs the functions_framework gem and other dependencies bundle exec functions-framework-ruby --target hello # ...starts the functions server in the foreground ``` -------------------------------- ### Running the examples locally Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md To perform integration tests of the functions, start the server in a shell by running: ```sh toys server ``` -------------------------------- ### Install and Run Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/README.md Commands to install dependencies and start the local Functions Framework server. ```sh bundle install # ...installs the functions_framework gem and other dependencies bundle exec functions-framework-ruby --target hello # ...starts the web server in the foreground ``` -------------------------------- ### Deploying to Cloud Run Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md To deploy to Cloud Run, first create a project and enable billing. You will also need to install the [Google Cloud SDK](https://cloud.google.com/sdk) if you have not already done so. Then run the `toys run deploy` command: ```sh toys run deploy --project=[PROJECT NAME] ``` -------------------------------- ### Deploy to Cloud Run Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md To deploy to Cloud Run, first create a project and enable billing. Then run the `toys run deploy` command, providing the function name, and the project. For example: ```sh toys run deploy http_example --project=[PROJECT NAME] ``` -------------------------------- ### lib/hello.rb example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example lib/hello.rb file defining a class used by the HTTP function. ```ruby class Hello def initialize request @request = request end def build_response "Received request: #{@request.request_method} #{@request.url}\n" end end ``` -------------------------------- ### app.rb example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example app.rb file defining an HTTP function. ```ruby require "functions_framework" FunctionsFramework.on_startup do require_relative "lib/hello" end FunctionsFramework.http "hello" do |request| Hello.new(request).build_response end ``` -------------------------------- ### Running the Docker image Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md Then you can run it in Docker: ```sh toys image server ``` -------------------------------- ### Gemfile example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example Gemfile specifying dependencies for a Functions Framework project. ```ruby source "https://rubygems.org" gem "functions_framework", "~> 1.0" ``` -------------------------------- ### Serve the HTTP sample function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md Here is how to serve the HTTP sample function. ```sh toys server http_example ``` -------------------------------- ### Send requests to Cloud Run service Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md At the end of the deployment process, the command will display the hostname for the Cloud Run service. Use that hostname to send requests via the commands `toys run request` or `toys run event`. For example: ```sh toys run request echo-abcdefghij-uc.a.run.app ``` -------------------------------- ### Serve the event sample function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md To serve the event sample function. ```sh toys server event_example ``` -------------------------------- ### Run the Docker image Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md Then you can run it in Docker by passing the function name to `toys image server`, as below: ```sh toys image server http_example ``` -------------------------------- ### Sending requests to Cloud Run Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md At the end of the deployment process, the command will display the hostname for the Cloud Run service. Use that hostname to send requests via the command `toys run request`. For example: ```sh toys run request sinatra-abcdefghij-uc.a.run.app --path=/hello/Sinatra ``` -------------------------------- ### Define a startup task Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example of defining a startup task using FunctionsFramework.on_startup to perform initialization before function execution. ```ruby require "functions_framework" FunctionsFramework.on_startup do |function| # Perform initialization here. require "my_cache" MyCache.warmup end FunctionsFramework.http "hello" do |request| # Initialization will be done by the time a normal function is called. end ``` -------------------------------- ### Building the Docker image Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md To test a server running in a Docker container, you need to build the Docker image and then run it. To build the image: ```sh toys image build ``` -------------------------------- ### Running unit tests Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md To run unit tests, run the "test" toys script. ```sh toys test ``` -------------------------------- ### Lazy initialization of shared resources Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example demonstrating lazy initialization of a Google Cloud Storage client using `on_startup` and `set_global`. ```ruby FunctionsFramework.on_startup do require "google/cloud/storage" set_global :storage_client do Google::Cloud::Storage.new end end # The first time this function is invoked, it will call the above block # to construct the storage client. Subsequent invocations will not need # to construct it again, but will reuse the same shared object. FunctionsFramework.http "storage_example" do |request| bucket = global(:storage_client).bucket "my-bucket" file = bucket.file "path/to/my-file.txt" file.download.to_s end ``` -------------------------------- ### Typical project layout Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Example of a typical project structure for a Functions Framework based Ruby application. ```text (project directory) | +- Gemfile | +- app.rb | +- lib/ | | | +- hello.rb | +- test/ | ... ``` -------------------------------- ### Send events to the CloudEvents function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md Then, in a separate shell, you can send events to it. ```sh toys event ``` -------------------------------- ### Sending requests locally Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/sinatra/README.md Then, in a separate shell, you can send requests to it: ```sh toys request # or toys request --path /hello/Sinatra ``` -------------------------------- ### Lazy Initialization of Shared Client Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md An example demonstrating lazy initialization of a shared storage client within a startup block. ```ruby require "functions_framework" # This startup block describes _how_ to initialize a shared client, but ``` -------------------------------- ### Send requests to the HTTP function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/examples/echo/README.md Then, in a separate shell, you can send requests to it. ```sh toys request ``` -------------------------------- ### Dockerfile for a Ruby functions project Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/running-a-functions-server.md A basic Dockerfile for a Ruby functions project, setting up the environment and installing dependencies. ```dockerfile FROM ruby:2.6 WORKDIR /app COPY . . RUN gem install --no-document bundler \ && bundle config --local frozen true \ && bundle config --local without "development test" \ && bundle install ENV PORT=8080 ENTRYPOINT ["bundle", "exec", "functions-framework-ruby"] ``` -------------------------------- ### Running functions locally Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/running-a-functions-server.md This command installs dependencies, then runs the functions-framework-ruby executable to serve functions from `foo.rb` targeting the `hello` function. ```sh bundle install bundle exec functions-framework-ruby --source=foo.rb --target=hello ``` -------------------------------- ### Local Development Server Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md Example of using curl to send a request to the locally running function. ```shell curl http://my-url ``` -------------------------------- ### Simple Event Function Example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md A basic event handler that receives a CloudEvent and logs its type. ```ruby require "functions_framework" FunctionsFramework.cloud_event "hello" do |event| logger.info "I received an event of type #{event.type}!" end ``` -------------------------------- ### Testing startup tasks explicitly Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example test for startup tasks, showing how to run them explicitly and assert against the resulting globals, followed by calling an HTTP function. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing def test_startup_tasks load_temporary "app.rb" do globals = run_startup_tasks "my_function" assert_equal "foo", globals[:my_global] request = make_get_request "https://example.com/foo" response = call_http "my_function", request assert_equal 200, response.status end end end ``` -------------------------------- ### HTTP function example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Defines an HTTP function named "hello" that returns a simple message. ```ruby require "functions_framework" FunctionsFramework.http "hello" do |request| # Return the response body. "Hello, world!\n" end ``` -------------------------------- ### HTTP Function with Shared Storage Client Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md An example of an HTTP function that accesses a shared Google Cloud Storage client initialized during startup. ```ruby require "functions_framework" # Use an on_startup block to initialize a shared client and store it in # the global shared data. FunctionsFramework.on_startup do require "google/cloud/storage" set_global :storage_client, Google::Cloud::Storage.new end # The shared storage_client can be accessed by all function invocations # via the global shared data. FunctionsFramework.http "storage_example" do |request| bucket = global(:storage_client).bucket "my-bucket" file = bucket.file "path/to/my-file.txt" file.download.to_s end ``` -------------------------------- ### Dockerfile for Ruby Functions Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/deploying-functions.md A basic Dockerfile to build a container image for a Ruby Cloud Function. It installs dependencies and sets the entrypoint. ```dockerfile FROM ruby:3.3 WORKDIR /app COPY . . RUN gem install --no-document bundler \ && bundle config --local frozen true \ && bundle config --local without "development test" \ && bundle install ENTRYPOINT ["bundle", "exec", "functions-framework-ruby"] ``` -------------------------------- ### RSpec Describe Block Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example of including the FunctionsFramework::Testing module in an RSpec describe block. ```ruby require "rspec" require "functions_framework/testing" describe "My functions" do include FunctionsFramework::Testing # define examples... end ``` -------------------------------- ### Loading Functions Temporarily Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example of using load_temporary to load Ruby files for testing specific functions. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing def test_a_function load_temporary "foo.rb" do # Test a function defined in foo.rb end end def test_another_function load_temporary "bar.rb" do # Test a function defined in bar.rb end end end ``` -------------------------------- ### Minitest Test Class Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example of including the FunctionsFramework::Testing module in a Minitest test class. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing # define tests... end ``` -------------------------------- ### Testing an HTTP Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example of testing an HTTP function by creating a POST request and calling the function. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing def test_http_function load_temporary "app.rb" do request = make_post_request "https://example.com/foo", "{\"name\":\"Ruby\"}", ["Content-Type: application/json"] response = call_http "my_function", request assert_equal 200, response.status assert_equal "Hello, Ruby!", response.body.join end end end ``` -------------------------------- ### Testing a CloudEvent function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example test for a CloudEvent function, demonstrating how to construct a CloudEvent, capture stderr, and assert against logs. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing def test_event_function load_temporary "app.rb" do event = make_cloud_event "Hello, world!", type: "my-type" _out, err = capture_subprocess_io do call_event "my_function", event end assert_match(/Received: \"Hello, world!\"/, err) end end end ``` -------------------------------- ### Basic Sinatra HTTP Function Example Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Connects an HTTP function to a modular Sinatra application, allowing access to Sinatra's features. ```ruby require "functions_framework" require "sinatra/base" class App < Sinatra::Base get "/hello/:name" do "Hello, #{params[:name]}!" end end FunctionsFramework.http "sinatra_example" do |request| App.call request.env end ``` -------------------------------- ### Testing an Erroring HTTP Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/testing-functions.md Example of testing an HTTP function that is expected to raise an error, checking the 500 response. ```ruby require "minitest/autorun" require "functions_framework/testing" class MyTest < Minitest::Test include FunctionsFramework::Testing def test_erroring_http_function load_temporary "app.rb" do request = make_post_request "https://example.com/foo", "{\"name\":\"Ruby\"}", ["Content-Type: application/json"] response = call_http "error_function", request assert_equal 500, response.status assert_match(/ArgumentError/, response.body.join) end end end ``` -------------------------------- ### Cloud Event Function with Logger and Global Data Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md An example of a Cloud Event function that uses the logger and accesses global data for the function name. ```ruby require "functions_framework" FunctionsFramework.cloud_event "hello" do |event| logger.info "Now running the function called #{global(:function_name)}" end ``` -------------------------------- ### Avoid top-level initialization Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Illustrates the correct way to perform initialization within an on_startup block, contrasting it with incorrect top-level initialization that might run at build time. ```ruby require "functions_framework" # DO NOT perform initialization here because this could get run at build time. # require "my_cache" # MyCache.warmup # Instead initialize in an on_startup block, which is executed only when a # runtime server is starting up. FunctionsFramework.on_startup do # Perform initialization here. require "my_cache" MyCache.warmup end # ... ``` -------------------------------- ### Building a Docker image Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/running-a-functions-server.md This command builds a Docker image for the project using the specified Dockerfile. ```sh docker build --tag my-image . ``` -------------------------------- ### Running the release proposal script Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/RELEASING.md Command to manually propose a release using the release-please tool. ```bash toys release please functions_framework:1.2.3 ``` -------------------------------- ### App.rb - Hello World Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md Defines a simple function called 'hello' in app.rb. ```ruby # app.rb require "functions_framework" FunctionsFramework.http("hello") do |request| "Hello, world!\n" end ``` -------------------------------- ### Build Docker Image with Cloud Build Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/deploying-functions.md Command to build the Docker image using Cloud Build and tag it for container registry. ```sh gcloud builds submit --tag=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID . ``` -------------------------------- ### Configuring Cloud Functions deployments with environment variables Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/deploying-functions.md This command deploys a Ruby function to Google Cloud Functions and sets an environment variable to limit logging to WARN level and above. ```sh gcloud functions deploy $YOUR_FUNCTION_NAME --project=$YOUR_PROJECT_ID \ --runtime=ruby33 --trigger-http --source=$YOUR_FUNCTION_SOURCE \ --entry-point=$YOUR_FUNCTION_TARGET \ --set-env-vars=FUNCTION_LOGGING_LEVEL=WARN ``` -------------------------------- ### Hello World Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md A simple HTTP function that returns 'Hello, world!'. ```ruby FunctionsFramework.http("hello") do |request| "Hello, world!\n" end ``` -------------------------------- ### Running the Docker container locally Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/running-a-functions-server.md This command runs the built Docker image, mapping port 8080 and passing arguments to the functions-framework-ruby executable. ```sh docker run --rm -it -p 8080:8080 my-image --source=foo.rb --target=hello ``` -------------------------------- ### Deploy to Cloud Run Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/deploying-functions.md Command to deploy the built Docker image to Cloud Run, specifying environment variables for function source and target. ```sh gcloud run deploy $YOUR_APP_NAME --project=$YOUR_PROJECT_ID \ --image=gcr.io/$YOUR_PROJECT_ID/$YOUR_APP_NAME:$YOUR_BUILD_ID \ --platform=managed --allow-unauthenticated --region=us-central1 \ --set-env-vars=FUNCTION_SOURCE=$YOUR_SOURCE,FUNCTION_TARGET=$YOUR_TARGET ``` -------------------------------- ### Gemfile for Sinatra Integration Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Declares dependencies on the Functions Framework and Sinatra in the Gemfile. ```ruby source "https://rubygems.org" gem "functions_framework", "~> 1.0" gem "sinatra", "~> 2.0" ``` -------------------------------- ### HTTP function with logging Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Logs some request info and returns a simple response. ```ruby require "functions_framework" FunctionsFramework.http "logging_example" do |request| # Log some request info. request.logger.info "I received #{request.request_method} from #{request.url}!" # A simple response body. "ok" end ``` -------------------------------- ### Deploying a function to Cloud Functions Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/deploying-functions.md This command deploys a Ruby function to Google Cloud Functions. It specifies the project, runtime, trigger type, and the entry point for the function. ```sh gcloud functions deploy $YOUR_FUNCTION_NAME \ --project=$YOUR_PROJECT_ID \ --runtime=ruby33 \ --trigger-http \ --entry-point=$YOUR_FUNCTION_TARGET ``` -------------------------------- ### Sending requests locally Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/running-a-functions-server.md This command sends a request to the locally running functions server. ```sh curl http://localhost:8080/ ``` -------------------------------- ### Custom Error Handling in HTTP Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Demonstrates how to construct a custom HTTP response for errors within an HTTP function. ```ruby require "functions_framework" FunctionsFramework.http "error_reporter" do |request| begin raise "whoops!" rescue RuntimeError => e [500, {}, ["Uh, oh, got an error message: #{e.message}."]] end end ``` -------------------------------- ### HTTP function with request information Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/writing-functions.md Includes some request information in the response body. ```ruby require "functions_framework" FunctionsFramework.http "request_info_example" do |request| # Include some request info in the response body. "Received #{request.request_method} from #{request.url}!\n" end ``` -------------------------------- ### Gemfile Dependency Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md Specifies the Functions Framework as a dependency in the Gemfile. ```ruby # Gemfile source "https://rubygems.org" gem "functions_framework", "~> 1.0" ``` -------------------------------- ### Send Request to Local Function Source: https://github.com/googlecloudplatform/functions-framework-ruby/blob/main/docs/overview.md Sends a request to the locally running function using curl. ```shell curl http://localhost:8080 # Output: Hello, world! ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.