diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 5820e48e..00000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -version: 2.1 -executors: - ruby24: - docker: - - image: "circleci/ruby:2.4-buster" - ruby25: - docker: - - image: "circleci/ruby:2.5-buster" - ruby26: - docker: - - image: "circleci/ruby:2.6-buster" - ruby27: - docker: - - image: "circleci/ruby:2.7-buster" -commands: - rake-test: - steps: - - checkout - - run: - name: Bundle - command: "gem install --no-document bundler && bundle install --jobs=3 --retry=3" - - run: - name: CI - command: "bundle exec toys ci" -jobs: - test-ruby24: - executor: ruby24 - steps: - - rake-test - test-ruby25: - executor: ruby25 - steps: - - rake-test - test-ruby26: - executor: ruby26 - steps: - - rake-test - test-ruby27: - executor: ruby27 - steps: - - rake-test -workflows: - version: 2 - builds: - jobs: - - test-ruby24: - - test-ruby25: - - test-ruby26: - - test-ruby27: diff --git a/.gitignore b/.gitignore index 7a8a4ca5..54946e14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,11 @@ -/.bundle/ -/.yardoc -/Gemfile.lock -/coverage/ -/doc/ -/pkg/ -/tmp/ +.DS_Store +.bundle/ +.yardoc/ +Gemfile.lock +coverage/ +examples/ +doc/ +lib/ +pkg/ +test/ +tmp/ diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000..e69de29b diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index 5b3d10c4..00000000 --- a/.rubocop.yml +++ /dev/null @@ -1,2 +0,0 @@ -inherit_gem: - google-style: google-style.yml diff --git a/.toys.rb b/.toys.rb deleted file mode 100644 index 68fc3e07..00000000 --- a/.toys.rb +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -expand :clean, paths: ["pkg", "doc", ".yardoc", "tmp"] - -expand :minitest, libs: ["lib", "test"] - -expand :rubocop - -expand :yardoc do |t| - t.generate_output_flag = true - t.fail_on_warning = true - t.fail_on_undocumented_objects = true -end - -expand :gem_build - -expand :gem_build, name: "release", push_gem: true - -expand :gem_build, name: "install", install_gem: true - -tool "ci" do - desc "Run all CI checks" - - include :exec, result_callback: :handle_result - include :terminal - - def handle_result result - if result.success? - puts "** #{result.name} passed\n\n", :green, :bold - else - puts "** CI terminated: #{result.name} failed!", :red, :bold - exit 1 - end - end - - def run - exec_tool ["test"], name: "Tests" - exec_tool ["rubocop"], name: "Style checker" - exec_tool ["yardoc"], name: "Docs generation" - end -end diff --git a/.yardopts b/.yardopts deleted file mode 100644 index 2805ab58..00000000 --- a/.yardopts +++ /dev/null @@ -1,11 +0,0 @@ ---no-private ---title=Functions Framework ---markup markdown ---markup-provider redcarpet ---main=README.md -./lib/functions_framework/**/*.rb -./lib/functions_framework.rb -- -README.md -CONTRIBUTING.md -CHANGELOG.md diff --git a/404.html b/404.html new file mode 100644 index 00000000..9c758fa6 --- /dev/null +++ b/404.html @@ -0,0 +1,22 @@ + + + + + + +

+ Ruby Functions Framework documentation +

+ + + diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 4dc68c6f..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,2 +0,0 @@ -# Changelog - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 57a8ac3c..00000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,32 +0,0 @@ -# Contributing - -Want to contribute? Great! First, read this page (including the small print at the end). - -### Before you contribute - -Before we can use your code, you must sign the -[Google Individual Contributor License Agreement] -(https://cla.developers.google.com/about/google-individual) -(CLA), which you can do online. The CLA is necessary mainly because you own the -copyright to your changes, even after your contribution becomes part of our -codebase, so we need your permission to use and distribute your code. We also -need to be sure of various other things—for instance that you'll tell us if you -know that your code infringes on other people's patents. You don't have to sign -the CLA until after you've submitted your code for review and a member has -approved it, but you must do it before we can put your code into our codebase. -Before you start working on a larger contribution, you should get in touch with -us first through the issue tracker with your idea so that we can help out and -possibly guide you. Coordinating up front makes it much easier to avoid -frustration later on. - -### Code reviews - -All submissions, including submissions by project members, require review. We -use Github pull requests for this purpose. - -### The small print - -Contributions made by corporations are covered by a different agreement than -the one above, the -[Software Grant and Corporate Contributor License Agreement] -(https://cla.developers.google.com/about/google-corporate). diff --git a/Gemfile b/Gemfile deleted file mode 100644 index fdadff04..00000000 --- a/Gemfile +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -source "https://rubygems.org" - -gemspec diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 7f8889ba..00000000 --- a/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "{}" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright {yyyy} {name of copyright owner} - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 46f633ce..00000000 --- a/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# Functions Framework - -## Installation - -``` -$ gem install functions_framework -``` - -## Supported Ruby Versions - -This library is supported on Ruby 2.4+. - -Google provides official support for Ruby versions that are actively supported -by Ruby Core—that is, Ruby versions that are either in normal maintenance or -in security maintenance, and not end of life. Currently, this means Ruby 2.4 -and later. Older versions of Ruby _may_ still work, but are unsupported and not -recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details -about the Ruby support schedule. diff --git a/functions_framework.gemspec b/functions_framework.gemspec deleted file mode 100644 index 5ae72070..00000000 --- a/functions_framework.gemspec +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -lib = File.expand_path "lib", __dir__ -$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib -require "functions_framework/version" - -::Gem::Specification.new do |spec| - spec.name = "functions_framework" - spec.version = ::FunctionsFramework::VERSION - spec.authors = ["Daniel Azuma"] - spec.email = ["dazuma@google.com"] - - spec.summary = "Functions Framework for Ruby" - spec.description = - "The Functions Framework implementation for Ruby." - spec.license = "Apache-2.0" - spec.homepage = "https://github.com/GoogleCloudPlatform/functions-framework-ruby" - - spec.files = ::Dir.glob("lib/**/*.rb") + - ::Dir.glob("*.md") + - ["LICENSE", ".yardopts"] - spec.required_ruby_version = ">= 2.4.0" - spec.require_paths = ["lib"] - - spec.add_dependency "puma", "~> 4.3" - spec.add_dependency "rack", "~> 2.1" - - spec.add_development_dependency "google-style", "~> 1.24.0" - spec.add_development_dependency "minitest", "~> 5.13" - spec.add_development_dependency "minitest-focus", "~> 1.1" - spec.add_development_dependency "minitest-rg", "~> 5.2" - spec.add_development_dependency "redcarpet", "~> 3.5" - spec.add_development_dependency "toys", "~> 0.9.3" - spec.add_development_dependency "yard", "~> 0.9.24" -end diff --git a/index.html b/index.html new file mode 100644 index 00000000..dc155b1f --- /dev/null +++ b/index.html @@ -0,0 +1,15 @@ + + + + + + + +

+ Ruby Functions Framework documentation +

+ + + diff --git a/lib/functions_framework.rb b/lib/functions_framework.rb deleted file mode 100644 index b2d0abf8..00000000 --- a/lib/functions_framework.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require "functions_framework/version" - -## -# The Functions Framework for Ruby -# -module FunctionsFramework -end diff --git a/lib/functions_framework/version.rb b/lib/functions_framework/version.rb deleted file mode 100644 index 78569202..00000000 --- a/lib/functions_framework/version.rb +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -module FunctionsFramework - ## - # Version of the Ruby Functions Framework - # @return [String] - # - VERSION = "0.0.0".freeze -end diff --git a/test/helper.rb b/test/helper.rb deleted file mode 100644 index 85eebdf5..00000000 --- a/test/helper.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require "minitest/autorun" -require "minitest/focus" -require "minitest/rg" - -require "functions_framework" diff --git a/test/test_version.rb b/test/test_version.rb deleted file mode 100644 index bd1435f2..00000000 --- a/test/test_version.rb +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright 2020 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -require "helper" - -describe FunctionsFramework::VERSION do - let(:version) { FunctionsFramework::VERSION } - it "is a legal version" do - assert_instance_of String, version - assert version.frozen? - Gem::Version.new version - end -end diff --git a/v0.10.0/FunctionsFramework.html b/v0.10.0/FunctionsFramework.html new file mode 100644 index 00000000..bf164056 --- /dev/null +++ b/v0.10.0/FunctionsFramework.html @@ -0,0 +1,1111 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Module: FunctionsFramework + + + +

+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/version.rb,
+ lib/functions_framework/cli.rb,
lib/functions_framework/server.rb,
lib/functions_framework/testing.rb,
lib/functions_framework/function.rb,
lib/functions_framework/registry.rb,
lib/functions_framework/legacy_event_converter.rb,
lib/functions_framework.rb
+
+
+ +
+ +

Overview

+
+

The Functions Framework for Ruby.

+ +

Functions Framework is an open source framework for writing lightweight, +portable Ruby functions that run in a serverless environment. For general +information about the Functions Framework, see +https://github.com/GoogleCloudPlatform/functions-framework. +To get started with the functions framework for Ruby, see +https://github.com/GoogleCloudPlatform/functions-framework-ruby for basic +examples.

+ +

Inside the FunctionsFramework module

+ +

The FunctionsFramework module includes the main entry points for the +functions framework. Use the FunctionsFramework.http, +event, or FunctionsFramework.cloud_event methods to +define functions. To serve functions via a web service, invoke the +functions-framework-ruby executable, or use the FunctionsFramework.start +or FunctionsFramework.run methods.

+ +

Internal modules

+ +

Here is a roadmap to the internal modules in the Ruby functions framework.

+ +
    +
  • CLI is the implementation of the +functions-framework-ruby executable. Most apps will not need to interact +with this class directly.
  • +
  • Function is the internal representation of a +function, indicating the type of function (http or cloud event), the +name of the function, and the block of code implementing it. Most apps +do not need to interact with this class directly.
  • +
  • Registry looks up functions by name. When you +define a set of named functions, they are added to a registry, and when +you start a server and specify the target function by name, it is looked +up from the registry. Most apps do not need to interact with this class +directly.
  • +
  • Server is a web server that makes a function +available via HTTP. It wraps the Puma web server and runs a specific +Function. Many apps can simply run the +functions-framework-ruby executable to spin up a server. However, if you +need closer control over your execution environment, you can use the +Server class to run a server. Note that, in most +cases, it is easier to use the FunctionsFramework.start or +FunctionsFramework.run wrapper methods rather than instantiate a +Server class directly.
  • +
  • Testing provides helpers that are useful when +writing unit tests for functions.
  • +
+ + +
+
+
+ + +

Defined Under Namespace

+

+ + + Modules: Testing + + + + Classes: CLI, Function, LegacyEventConverter, Registry, Server + + +

+ + +

+ Constant Summary + collapse +

+ +
+ +
VERSION = +
+
+

Version of the Ruby Functions Framework

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + +
  • + +
+ +
+
+
"0.10.0".freeze
+ +
DEFAULT_TARGET = +
+
+

The default target function name. If you define a function without +specifying a name, or run the framework without giving a target, this name +is used.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + +
  • + +
+ +
+
+
"function".freeze
+ +
DEFAULT_SOURCE = +
+
+

The default source file path. The CLI loads functions from this file if no +source file is given explicitly.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + +
  • + +
+ +
+
+
"./app.rb".freeze
+ +
CloudEvents = +
+
+

The CloudEvents implementation was extracted to become the official +CloudEvents SDK. This alias is left here for backward compatibility.

+ + +
+
+
+ + +
+
+
::CloudEvents
+ +
+ + + + + +

Class Attribute Summary collapse

+ + + + + + +

+ Class Method Summary + collapse +

+ + + + + +
+

Class Attribute Details

+ + + +
+

+ + .global_registryFunctionsFramework::Registry + + + + + +

+
+

The "global" registry that holds events defined by the +FunctionsFramework class methods.

+ + +
+
+
+ +

Returns:

+ + +
+ + + + +
+
+
+
+107
+108
+109
+
+
# File 'lib/functions_framework.rb', line 107
+
+def global_registry
+  @global_registry
+end
+
+
+ + + +
+

+ + .loggerLogger + + + + + +

+
+

A "global" logger that is used by the framework's web server, and can +also be used by functions.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Logger) + + + +
  • + +
+ +
+ + + + +
+
+
+
+115
+116
+117
+
+
# File 'lib/functions_framework.rb', line 115
+
+def logger
+  @logger
+end
+
+
+ +
+ + +
+

Class Method Details

+ + +
+

+ + .cloud_event(name = DEFAULT_TARGET, &block) ⇒ self + + + + + +

+
+

Define a function that responds to CloudEvents.

+ +

You must provide a name for the function, and a block that implemets the +function. The block should take one argument: the event object of type +CloudEvents::Event. +Any return value is ignored.

+ +

Example

+ +
FunctionsFramework.cloud_event "my-function" do |event|
+  FunctionsFramework.logger.info "Event data: #{event.data.inspect}"
+end
+
+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + (defaults to: DEFAULT_TARGET) + + + — +

    The function name. Defaults to DEFAULT_TARGET.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a proc.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+163
+164
+165
+166
+
+
# File 'lib/functions_framework.rb', line 163
+
+def cloud_event name = DEFAULT_TARGET, &block
+  global_registry.add_cloud_event name, &block
+  self
+end
+
+
+ +
+

+ + .http(name = DEFAULT_TARGET, &block) ⇒ self + + + + + +

+
+

Define a function that response to HTTP requests.

+ +

You must provide a name for the function, and a block that implemets the +function. The block should take a single Rack::Request argument. It +should return one of the following:

+ +
    +
  • A standard 3-element Rack response array. See +https://github.com/rack/rack/blob/master/SPEC
  • +
  • A Rack::Response object.
  • +
  • A simple String that will be sent as the response body.
  • +
  • A Hash object that will be encoded as JSON and sent as the response +body.
  • +
+ +

Example

+ +
FunctionsFramework.http "my-function" do |request|
+  "I received a request for #{request.url}"
+end
+
+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + (defaults to: DEFAULT_TARGET) + + + — +

    The function name. Defaults to DEFAULT_TARGET.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a proc.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+140
+141
+142
+143
+
+
# File 'lib/functions_framework.rb', line 140
+
+def http name = DEFAULT_TARGET, &block
+  global_registry.add_http name, &block
+  self
+end
+
+
+ +
+

+ + .on_startup(&block) ⇒ self + + + + + +

+
+

Define a server startup task. This is useful for initializing shared +resources that should be accessible across all function invocations in +this Ruby VM.

+ +

Startup tasks are run just before a server starts. All startup tasks are +guaranteed to complete before any function executes. However, they are +run only when preparing to run functions. They are not run, for example, +if an app is loaded to verify its integrity during deployment.

+ +

Startup tasks are passed the Function identifying +the function to execute, and have no return value.

+ + +
+
+
+

Parameters:

+
    + +
  • + + block + + + (Proc) + + + + — +

    The startup task

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+184
+185
+186
+187
+
+
# File 'lib/functions_framework.rb', line 184
+
+def on_startup &block
+  global_registry.add_startup_task(&block)
+  self
+end
+
+
+ +
+

+ + .run(target) {|FunctionsFramework::Server::Config| ... } ⇒ self + + + + + +

+
+

Run the functions framework server and block until it stops. The server +will look up the given target function name in the global registry.

+ + +
+
+
+

Parameters:

+
    + +
  • + + target + + + (FunctionsFramework::Function, String) + + + + — +

    The function to run, +or the name of the function to look up in the global registry.

    +
    + +
  • + +
+ +

Yields:

+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+228
+229
+230
+231
+232
+
+
# File 'lib/functions_framework.rb', line 228
+
+def run target, &block
+  server = start target, &block
+  server.wait_until_stopped
+  self
+end
+
+
+ +
+

+ + .start(target) {|FunctionsFramework::Server::Config| ... } ⇒ FunctionsFramework::Server + + + + + +

+
+

Run startup tasks, then start the functions framework server in the +background. The startup tasks and target function will be looked up in +the global registry.

+ + +
+
+
+

Parameters:

+
    + +
  • + + target + + + (FunctionsFramework::Function, String) + + + + — +

    The function to run, +or the name of the function to look up in the global registry.

    +
    + +
  • + +
+ +

Yields:

+ +

Returns:

+ + +
+ + + + +
+
+
+
+200
+201
+202
+203
+204
+205
+206
+207
+208
+209
+210
+211
+212
+213
+214
+215
+216
+
+
# File 'lib/functions_framework.rb', line 200
+
+def start target, &block
+  require "functions_framework/server"
+  if target.is_a? ::FunctionsFramework::Function
+    function = target
+  else
+    function = global_registry[target]
+    raise ::ArgumentError, "Undefined function: #{target.inspect}" if function.nil?
+  end
+  globals = function.populate_globals
+  server = Server.new function, globals, &block
+  global_registry.startup_tasks.each do |task|
+    task.call function, globals: globals, logger: server.config.logger
+  end
+  globals.freeze
+  server.respond_to_signals
+  server.start
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/CLI.html b/v0.10.0/FunctionsFramework/CLI.html new file mode 100644 index 00000000..d4b0a1df --- /dev/null +++ b/v0.10.0/FunctionsFramework/CLI.html @@ -0,0 +1,941 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::CLI + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/cli.rb
+
+ +
+ +

Overview

+
+

Implementation of the functions-framework-ruby executable.

+ + +
+
+
+ + +
+ +

+ Constant Summary + collapse +

+ +
+ +
DEFAULT_LOGGING_LEVEL = +
+
+

The default logging level, if not given in the environment variable.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Integer) + + + +
  • + +
+ +
+
+
::Logger::Severity::INFO
+ +
+ + + + + +

Instance Attribute Summary collapse

+ + + + + + +

+ Instance Method Summary + collapse +

+ + + + +
+

Constructor Details

+ +
+

+ + #initializeCLI + + + + + +

+
+

Create a new CLI, setting arguments to their defaults.

+ + +
+
+
+ + +
+ + + + +
+
+
+
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+
+
# File 'lib/functions_framework/cli.rb', line 34
+
+def initialize
+  @target = ::ENV["FUNCTION_TARGET"] || ::FunctionsFramework::DEFAULT_TARGET
+  @source = ::ENV["FUNCTION_SOURCE"] || ::FunctionsFramework::DEFAULT_SOURCE
+  @env = nil
+  @port = nil
+  @bind = nil
+  @min_threads = nil
+  @max_threads = nil
+  @detailed_errors = nil
+  @signature_type = ::ENV["FUNCTION_SIGNATURE_TYPE"]
+  @logging_level = init_logging_level
+  @what_to_do = nil
+  @error_message = nil
+  @exit_code = 0
+end
+
+
+ +
+ +
+

Instance Attribute Details

+ + + +
+

+ + #error_messageString? (readonly) + + + + + +

+
+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + + — +

    The current error message.

    +
    + +
  • + +
  • + + + (nil) + + + + — +

    if no error has occurred.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+68
+69
+70
+
+
# File 'lib/functions_framework/cli.rb', line 68
+
+def error_message
+  @error_message
+end
+
+
+ + + +
+

+ + #exit_codeInteger (readonly) + + + + + +

+
+

Returns The current exit status.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Integer) + + + + — +

    The current exit status.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+62
+63
+64
+
+
# File 'lib/functions_framework/cli.rb', line 62
+
+def exit_code
+  @exit_code
+end
+
+
+ +
+ + +
+

Instance Method Details

+ + +
+

+ + #completeObject + + + + + +

+
+

Finish the CLI, displaying any error status and exiting with the current +exit code. Never returns.

+ + +
+
+
+ + +
+ + + + +
+
+
+
+174
+175
+176
+177
+
+
# File 'lib/functions_framework/cli.rb', line 174
+
+def complete
+  warn @error_message if @error_message
+  exit @exit_code
+end
+
+
+ +
+

+ + #error?boolean + + + + + +

+
+

Determine if an error has occurred

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (boolean) + + + +
  • + +
+ +
+ + + + +
+
+
+
+55
+56
+57
+
+
# File 'lib/functions_framework/cli.rb', line 55
+
+def error?
+  !@error_message.nil?
+end
+
+
+ +
+

+ + #parse_args(argv) ⇒ self + + + + + +

+
+

Parse the given command line arguments. +Exits if argument parsing failed.

+ + +
+
+
+

Parameters:

+
    + +
  • + + argv + + + (Array<String>) + + + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+
+
# File 'lib/functions_framework/cli.rb', line 77
+
+def parse_args argv # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
+  @option_parser = ::OptionParser.new do |op| # rubocop:disable Metrics/BlockLength
+    op.on "-t", "--target TARGET",
+          "Set the name of the function to execute (defaults to #{DEFAULT_TARGET})" do |val|
+      @target = val
+    end
+    op.on "-s", "--source SOURCE",
+          "Set the source file to load (defaults to #{DEFAULT_SOURCE})" do |val|
+      @source = val
+    end
+    op.on "--signature-type TYPE",
+          "Asserts that the function has the given signature type." \
+          " Supported values are 'http' and 'cloudevent'." do |val|
+      @signature_type = val
+    end
+    op.on "-p", "--port PORT", "Set the port to listen to (defaults to 8080)" do |val|
+      @port = val.to_i
+    end
+    op.on "-b", "--bind BIND", "Set the address to bind to (defaults to 0.0.0.0)" do |val|
+      @bind = val
+    end
+    op.on "-e", "--environment ENV", "Set the Rack environment" do |val|
+      @env = val
+    end
+    op.on "--min-threads NUM", "Set the minimum threead pool size" do |val|
+      @min_threads = val
+    end
+    op.on "--max-threads NUM", "Set the maximum threead pool size" do |val|
+      @max_threads = val
+    end
+    op.on "--[no-]detailed-errors", "Set whether to show error details" do |val|
+      @detailed_errors = val
+    end
+    op.on "--verify", "Verify the app only, but do not run the server." do
+      @what_to_do ||= :verify
+    end
+    op.on "-v", "--verbose", "Increase log verbosity" do
+      @logging_level -= 1
+    end
+    op.on "-q", "--quiet", "Decrease log verbosity" do
+      @logging_level += 1
+    end
+    op.on "--version", "Display the framework version" do
+      @what_to_do ||= :version
+    end
+    op.on "--help", "Display help" do
+      @what_to_do ||= :help
+    end
+  end
+  begin
+    @option_parser.parse! argv
+    error! "Unrecognized arguments: #{argv}\n#{@option_parser}", 2 unless argv.empty?
+  rescue ::OptionParser::ParseError => e
+    error! "#{e.message}\n#{@option_parser}", 2
+  end
+  self
+end
+
+
+ +
+

+ + #runself + + + + + +

+
+

Perform the requested function.

+ +
    +
  • If the --version flag was given, display the version.
  • +
  • If the --help flag was given, display online help.
  • +
  • If the --verify flag was given, load and verify the function, +displaying any errors, then exit without starting a server.
  • +
  • Otherwise, start the configured server and block until it stops.
  • +
+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+146
+147
+148
+149
+150
+151
+152
+153
+154
+155
+156
+157
+158
+159
+160
+161
+162
+163
+164
+165
+166
+167
+168
+
+
# File 'lib/functions_framework/cli.rb', line 146
+
+def run
+  return self if error?
+  case @what_to_do
+  when :version
+    puts ::FunctionsFramework::VERSION
+  when :help
+    puts @option_parser
+  when :verify
+    begin
+      load_function
+      puts "OK"
+    rescue ::StandardError => e
+      error! e.message
+    end
+  else
+    begin
+      start_server.wait_until_stopped
+    rescue ::StandardError => e
+      error! e.message
+    end
+  end
+  self
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Function.html b/v0.10.0/FunctionsFramework/Function.html new file mode 100644 index 00000000..2247b77b --- /dev/null +++ b/v0.10.0/FunctionsFramework/Function.html @@ -0,0 +1,1185 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::Function + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/function.rb
+
+ +
+ +

Overview

+
+

Representation of a function.

+ +

A function has a name, a type, and an implementation.

+ +

Function implementations

+ +

The implementation in general is an object that responds to the call +method.

+ +
    +
  • For a function of type :http, the call method takes a single +Rack::Request argument and returns one of various HTTP response +types. See Registry.add_http.
  • +
  • For a function of type :cloud_event, the call method takes a single +CloudEvent +argument, and does not return a value. See +Registry.add_cloud_event.
  • +
  • For a function of type :startup_task, the call method takes a +single Function argument, and does not return a +value. See Registry.add_startup_task.
  • +
+ +

The implementation can be specified in one of three ways:

+ +
    +
  • A callable object can be passed in the callable keyword argument. The +object's call method will be invoked for every function execution. +Note that this means it may be called multiple times concurrently in +separate threads.
  • +
  • A callable class can be passed in the callable keyword argument. +This class should subclass Callable and +define the call method. A separate instance of this class will be +created for each function invocation.
  • +
  • A block can be provided. It will be used to define the call method in +an anonymous subclass of Callable. +Thus, providing a block is really just syntactic sugar for providing a +class. (This means, for example, that the return keyword will work +as expected within the block because it is treated as a method.)
  • +
+ +

When the implementation is provided as a callable class or block, it is +executed in the context of a Callable +object. This object provides a convenience accessor for the Logger, and +access to globals, which are data defined by the application startup +process and available to each function invocation. Typically, globals are +used for shared global resources such as service connections and clients.

+ + +
+
+
+ + +

Defined Under Namespace

+

+ + + + + Classes: Callable + + +

+ + + + +

Instance Attribute Summary collapse

+ + + + + + +

+ Class Method Summary + collapse +

+ + + +

+ Instance Method Summary + collapse +

+ + + + +
+

Constructor Details

+ +
+

+ + #initialize(name, type, callable: nil, &block) ⇒ Function + + + + + +

+
+

Create a new function definition.

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
  • + + type + + + (Symbol) + + + + — +

    The type of function. Valid types are :http, +:cloud_event, and :startup_task.

    +
    + +
  • + +
  • + + callable + + + (Class, #call) + + + (defaults to: nil) + + + — +

    A callable object or class.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a block.

    +
    + +
  • + +
+ + +
+ + + + +
+
+
+
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+
+
# File 'lib/functions_framework/function.rb', line 105
+
+def initialize name, type, callable: nil, &block
+  @name = name
+  @type = type
+  @callable = @callable_class = nil
+  if callable.respond_to? :call
+    @callable = callable
+  elsif callable.is_a? ::Class
+    @callable_class = callable
+  elsif block_given?
+    @callable_class = ::Class.new Callable do
+      define_method :call, &block
+    end
+  else
+    raise ::ArgumentError, "No callable given for function"
+  end
+end
+
+
+ +
+ +
+

Instance Attribute Details

+ + + +
+

+ + #nameString (readonly) + + + + + +

+
+

Returns The function name.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+125
+126
+127
+
+
# File 'lib/functions_framework/function.rb', line 125
+
+def name
+  @name
+end
+
+
+ + + +
+

+ + #typeSymbol (readonly) + + + + + +

+
+

Returns The function type.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Symbol) + + + + — +

    The function type

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+130
+131
+132
+
+
# File 'lib/functions_framework/function.rb', line 130
+
+def type
+  @type
+end
+
+
+ +
+ + +
+

Class Method Details

+ + +
+

+ + .cloud_event(name, callable: nil, &block) ⇒ FunctionsFramework::Function + + + + + +

+
+

Create a new CloudEvents function definition.

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
  • + + callable + + + (Class, #call) + + + (defaults to: nil) + + + — +

    A callable object or class.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a block.

    +
    + +
  • + +
+ +

Returns:

+ + +
+ + + + +
+
+
+
+81
+82
+83
+
+
# File 'lib/functions_framework/function.rb', line 81
+
+def self.cloud_event name, callable: nil, &block
+  new name, :cloud_event, callable: callable, &block
+end
+
+
+ +
+

+ + .http(name, callable: nil, &block) ⇒ FunctionsFramework::Function + + + + + +

+
+

Create a new HTTP function definition.

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
  • + + callable + + + (Class, #call) + + + (defaults to: nil) + + + — +

    A callable object or class.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a block.

    +
    + +
  • + +
+ +

Returns:

+ + +
+ + + + +
+
+
+
+69
+70
+71
+
+
# File 'lib/functions_framework/function.rb', line 69
+
+def self.http name, callable: nil, &block
+  new name, :http, callable: callable, &block
+end
+
+
+ +
+

+ + .startup_task(callable: nil, &block) ⇒ FunctionsFramework::Function + + + + + +

+
+

Create a new startup task function definition.

+ + +
+
+
+

Parameters:

+
    + +
  • + + callable + + + (Class, #call) + + + (defaults to: nil) + + + — +

    A callable object or class.

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a block.

    +
    + +
  • + +
+ +

Returns:

+ + +
+ + + + +
+
+
+
+92
+93
+94
+
+
# File 'lib/functions_framework/function.rb', line 92
+
+def self.startup_task callable: nil, &block
+  new nil, :startup_task, callable: callable, &block
+end
+
+
+ +
+ +
+

Instance Method Details

+ + +
+

+ + #call(*args, globals: nil, logger: nil) ⇒ Object + + + + + +

+
+

Call the function given a set of arguments. Set the given logger and/or +globals in the context if the callable supports it.

+ +

If the given arguments exceeds what the function will accept, the args +are silently truncated. However, if the function requires more arguments +than are provided, an ArgumentError is raised.

+ + +
+
+
+

Parameters:

+
    + +
  • + + args + + + (Array) + + + + — +

    Argument to pass to the function.

    +
    + +
  • + +
  • + + logger + + + (Logger) + + + (defaults to: nil) + + + — +

    Logger for use by function executions.

    +
    + +
  • + +
  • + + globals + + + (Hash) + + + (defaults to: nil) + + + — +

    Globals for the function execution context

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Object) + + + + — +

    The function return value.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+157
+158
+159
+160
+161
+162
+163
+164
+165
+
+
# File 'lib/functions_framework/function.rb', line 157
+
+def call *args, globals: nil, logger: nil
+  callable = @callable || @callable_class.new(globals: globals, logger: logger)
+  params = callable.method(:call).parameters.map(&:first)
+  unless params.include? :rest
+    max_params = params.count(:req) + params.count(:opt)
+    args = args.take max_params
+  end
+  callable.call(*args)
+end
+
+
+ +
+

+ + #populate_globals(globals = nil) ⇒ Hash + + + + + +

+
+

Populate the given globals hash with this function's info.

+ + +
+
+
+

Parameters:

+
    + +
  • + + globals + + + (Hash) + + + (defaults to: nil) + + + — +

    Initial globals hash (optional).

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Hash) + + + + — +

    A new globals hash with this function's info included.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+138
+139
+140
+141
+142
+
+
# File 'lib/functions_framework/function.rb', line 138
+
+def populate_globals globals = nil
+  result = { function_name: name, function_type: type }
+  result.merge! globals if globals
+  result
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Function/Callable.html b/v0.10.0/FunctionsFramework/Function/Callable.html new file mode 100644 index 00000000..b27f9afc --- /dev/null +++ b/v0.10.0/FunctionsFramework/Function/Callable.html @@ -0,0 +1,664 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::Function::Callable + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/function.rb
+
+ +
+ +

Overview

+
+

A base class for a callable object that provides calling context.

+ +

An object of this class is self while a function block is running.

+ + +
+
+
+ + +
+ + + + + + + +

+ Instance Method Summary + collapse +

+ + + + +
+

Constructor Details

+ +
+

+ + #initialize(globals: nil, logger: nil) ⇒ Callable + + + + + +

+
+

Create a callable object with the given context.

+ + +
+
+
+

Parameters:

+
    + +
  • + + globals + + + (Hash) + + + (defaults to: nil) + + + — +

    A set of globals available to the call.

    +
    + +
  • + +
  • + + logger + + + (Logger) + + + (defaults to: nil) + + + — +

    A logger for use by the function call.

    +
    + +
  • + +
+ + +
+ + + + +
+
+
+
+201
+202
+203
+204
+
+
# File 'lib/functions_framework/function.rb', line 201
+
+def initialize globals: nil, logger: nil
+  @__globals = globals || {}
+  @__logger = logger || FunctionsFramework.logger
+end
+
+
+ +
+ + +
+

Instance Method Details

+ + +
+

+ + #global(key) ⇒ Object + + + + + +

+
+

Get the given named global.

+ +

For most function calls, the following globals will be defined:

+ +
    +
  • :function_name (String) The name of the running function.
  • +
  • :function_type (Symbol) The type of the running function, +either :http or :cloud_event.
  • +
+ +

You can also set additional globals from a startup task.

+ + +
+
+
+

Parameters:

+
    + +
  • + + key + + + (Symbol, String) + + + + — +

    The name of the global to get.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Object) + + + +
  • + +
+ +
+ + + + +
+
+
+
+220
+221
+222
+223
+224
+
+
# File 'lib/functions_framework/function.rb', line 220
+
+def global key
+  value = @__globals[key]
+  value = value.value if LazyGlobal === value
+  value
+end
+
+
+ +
+

+ + #loggerLogger + + + + + +

+
+

A logger for use by this call.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Logger) + + + +
  • + +
+ +
+ + + + +
+
+
+
+267
+268
+269
+
+
# File 'lib/functions_framework/function.rb', line 267
+
+def logger
+  @__logger
+end
+
+
+ +
+

+ + + #set_global(key, value) ⇒ self + + #set_global(key, &block) ⇒ self + + + + + + +

+
+

Set a global. This can be called from startup tasks, but the globals +are frozen when the server starts, so this call will raise an exception +if called from a normal function.

+ +

You can set a global to a final value, or you can provide a block that +lazily computes the global the first time it is requested.

+ + +
+
+
+ +

Overloads:

+
    + + +
  • + #set_global(key, value) ⇒ self +
    +
    +

    Set the given global to the given value. For example:

    + +
    set_global(:project_id, "my-project-id")
    +
    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + key + + + (Symbol, String) + + + +
    • + +
    • + + value + + + (Object) + + + +
    • + +
    + +

    Returns:

    +
      + +
    • + + + (self) + + + +
    • + +
    + +
    +
  • + + +
  • + #set_global(key, &block) ⇒ self +
    +
    +

    Call the given block to compute the global's value only when the +value is actually requested. This block will be called at most once, +and its result reused for subsequent calls. For example:

    + +
    set_global(:connection_pool) do
    +  ExpensiveConnectionPool.new
    +end
    +
    + + +
    +
    +
    +

    Parameters:

    +
      + +
    • + + key + + + (Symbol, String) + + + +
    • + +
    • + + block + + + (Proc) + + + + — +

      A block that lazily computes a value

      +
      + +
    • + +
    + +

    Yield Returns:

    +
      + +
    • + + + (Object) + + + + — +

      The value

      +
      + +
    • + +
    +

    Returns:

    +
      + +
    • + + + (self) + + + +
    • + +
    + +
    +
  • + +
+ + +
+ + + + +
+
+
+
+257
+258
+259
+260
+
+
# File 'lib/functions_framework/function.rb', line 257
+
+def set_global key, value = nil, &block
+  @__globals[key] = block ? LazyGlobal.new(block) : value
+  self
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/LegacyEventConverter.html b/v0.10.0/FunctionsFramework/LegacyEventConverter.html new file mode 100644 index 00000000..aa1a03e5 --- /dev/null +++ b/v0.10.0/FunctionsFramework/LegacyEventConverter.html @@ -0,0 +1,274 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::LegacyEventConverter + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/legacy_event_converter.rb
+
+ +
+ +

Overview

+
+

Converter from legacy GCF event formats to CloudEvents.

+ + +
+
+
+ + +
+ + + + + + + +

+ Instance Method Summary + collapse +

+ + + + + + +
+

Instance Method Details

+ + +
+

+ + #decode_rack_env(env) ⇒ ::CloudEvents::Event? + + + + + +

+
+

Decode an event from the given Rack environment hash.

+ + +
+
+
+

Parameters:

+
    + +
  • + + env + + + (Hash) + + + + — +

    The Rack environment

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (::CloudEvents::Event) + + + + — +

    if the request could be converted

    +
    + +
  • + +
  • + + + (nil) + + + + — +

    if the event format was not recognized.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+
+
# File 'lib/functions_framework/legacy_event_converter.rb', line 29
+
+def decode_rack_env env
+  content_type = ::CloudEvents::ContentType.new env["CONTENT_TYPE"], default_charset: "utf-8"
+  return nil unless content_type.media_type == "application" && content_type.subtype_base == "json"
+  input = read_input_json env["rack.input"], content_type.charset
+  return nil unless input
+  input = convert_raw_pubsub_event input, env if raw_pubsub_payload? input
+  context = normalized_context input
+  return nil unless context
+  construct_cloud_event context, input["data"]
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Registry.html b/v0.10.0/FunctionsFramework/Registry.html new file mode 100644 index 00000000..f7b09768 --- /dev/null +++ b/v0.10.0/FunctionsFramework/Registry.html @@ -0,0 +1,848 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::Registry + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/registry.rb
+
+ +
+ +

Overview

+
+

Registry providing lookup of functions by name.

+ + +
+
+
+ + +
+ + + + + + + +

+ Instance Method Summary + collapse +

+ + + + +
+

Constructor Details

+ +
+

+ + #initializeRegistry + + + + + +

+
+

Create a new empty registry.

+ + +
+
+
+ + +
+ + + + +
+
+
+
+23
+24
+25
+26
+27
+
+
# File 'lib/functions_framework/registry.rb', line 23
+
+def initialize
+  @mutex = ::Mutex.new
+  @functions = {}
+  @start_tasks = []
+end
+
+
+ +
+ + +
+

Instance Method Details

+ + +
+

+ + #[](name) ⇒ FunctionsFramework::Function? + + + + + +

+
+

Look up a function definition by name.

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (FunctionsFramework::Function) + + + + — +

    if the function is found

    +
    + +
  • + +
  • + + + (nil) + + + + — +

    if the function is not found

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+36
+37
+38
+
+
# File 'lib/functions_framework/registry.rb', line 36
+
+def [] name
+  @mutex.synchronize { @functions[name.to_s] }
+end
+
+
+ +
+

+ + #add_cloud_event(name, &block) ⇒ self + + + + + +

+
+

Add a CloudEvent function to the registry.

+ +

You must provide a name for the function, and a block that implemets the +function. The block should take one argument: the event object of type +CloudEvents::Event. +Any return value is ignored.

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a proc

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+96
+97
+98
+99
+100
+101
+102
+103
+
+
# File 'lib/functions_framework/registry.rb', line 96
+
+def add_cloud_event name, &block
+  name = name.to_s
+  @mutex.synchronize do
+    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
+    @functions[name] = Function.cloud_event name, &block
+  end
+  self
+end
+
+
+ +
+

+ + #add_http(name, &block) ⇒ self + + + + + +

+
+

Add an HTTP function to the registry.

+ +

You must provide a name for the function, and a block that implemets the +function. The block should take a single Rack::Request argument. It +should return one of the following:

+ +
    +
  • A standard 3-element Rack response array. See +https://github.com/rack/rack/blob/master/SPEC
  • +
  • A Rack::Response object.
  • +
  • A simple String that will be sent as the response body.
  • +
  • A Hash object that will be encoded as JSON and sent as the response +body.
  • +
+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The function name

    +
    + +
  • + +
  • + + block + + + (Proc) + + + + — +

    The function code as a proc

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+75
+76
+77
+78
+79
+80
+81
+82
+
+
# File 'lib/functions_framework/registry.rb', line 75
+
+def add_http name, &block
+  name = name.to_s
+  @mutex.synchronize do
+    raise ::ArgumentError, "Function already defined: #{name}" if @functions.key? name
+    @functions[name] = Function.http name, &block
+  end
+  self
+end
+
+
+ +
+

+ + #add_startup_task(&block) ⇒ self + + + + + +

+
+

Add a startup task.

+ +

Startup tasks are generally run just before a server starts. They are +passed the Function identifying the function to +execute, and have no return value.

+ + +
+
+
+

Parameters:

+
    + +
  • + + block + + + (Proc) + + + + — +

    The startup task

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+115
+116
+117
+118
+119
+120
+
+
# File 'lib/functions_framework/registry.rb', line 115
+
+def add_startup_task &block
+  @mutex.synchronize do
+    @start_tasks << Function.startup_task(&block)
+  end
+  self
+end
+
+
+ +
+

+ + #namesArray<String> + + + + + +

+
+

Returns the list of defined names

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Array<String>) + + + +
  • + +
+ +
+ + + + +
+
+
+
+45
+46
+47
+
+
# File 'lib/functions_framework/registry.rb', line 45
+
+def names
+  @mutex.synchronize { @functions.keys.sort }
+end
+
+
+ +
+

+ + #startup_tasksArray<FunctionsFramework::Function> + + + + + +

+
+

Return an array of startup tasks.

+ + +
+
+
+ +

Returns:

+ + +
+ + + + +
+
+
+
+54
+55
+56
+
+
# File 'lib/functions_framework/registry.rb', line 54
+
+def startup_tasks
+  @mutex.synchronize { @start_tasks.dup }
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Server.html b/v0.10.0/FunctionsFramework/Server.html new file mode 100644 index 00000000..3641d338 --- /dev/null +++ b/v0.10.0/FunctionsFramework/Server.html @@ -0,0 +1,1023 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::Server + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + +
+
Includes:
+
MonitorMixin
+
+ + + + + + +
+
Defined in:
+
lib/functions_framework/server.rb
+
+ +
+ +

Overview

+
+

A web server that wraps a function.

+ + +
+
+
+ + +

Defined Under Namespace

+

+ + + + + Classes: Config + + +

+ + + + +

Instance Attribute Summary collapse

+ + + + + + +

+ Instance Method Summary + collapse +

+ + + + + +
+

Constructor Details

+ +
+

+ + #initialize(function, globals) {|FunctionsFramework::Server::Config| ... } ⇒ Server + + + + + +

+
+

Create a new web server given a function definition, a set of application +globals, and server configuration.

+ +

To configure the server, pass a block that takes a +Config object as the parameter. This block +is the only opportunity to modify the configuration; once the server is +initialized, configuration is frozen.

+ + +
+
+
+

Parameters:

+
    + +
  • + + function + + + (FunctionsFramework::Function) + + + + — +

    The function to execute.

    +
    + +
  • + +
  • + + globals + + + (Hash) + + + + — +

    Globals to pass to invocations. This hash should +normally be frozen so separate function invocations cannot interfere +with one another's globals.

    +
    + +
  • + +
+ +

Yields:

+ + +
+ + + + +
+
+
+
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+
+
# File 'lib/functions_framework/server.rb', line 45
+
+def initialize function, globals
+  super()
+  @config = Config.new
+  yield @config if block_given?
+  @config.freeze
+  @function = function
+  @app =
+    case function.type
+    when :http
+      HttpApp.new function, globals, @config
+    when :cloud_event
+      EventApp.new function, globals, @config
+    else
+      raise "Unrecognized function type: #{function.type}"
+    end
+  @server = nil
+  @signals_installed = false
+end
+
+
+ +
+ +
+

Instance Attribute Details

+ + + +
+

+ + #configFunctionsFramework::Server::Config (readonly) + + + + + +

+
+

The final configuration. This is a frozen object that cannot be modified.

+ + +
+
+
+ +

Returns:

+ + +
+ + + + +
+
+
+
+74
+75
+76
+
+
# File 'lib/functions_framework/server.rb', line 74
+
+def config
+  @config
+end
+
+
+ + + +
+

+ + #functionFunctionsFramework::Function (readonly) + + + + + +

+
+

The function to execute.

+ + +
+
+
+ +

Returns:

+ + +
+ + + + +
+
+
+
+68
+69
+70
+
+
# File 'lib/functions_framework/server.rb', line 68
+
+def function
+  @function
+end
+
+
+ +
+ + +
+

Instance Method Details

+ + +
+

+ + #respond_to_signalsself + + + + + +

+
+

Cause this server to respond to SIGTERM, SIGINT, and SIGHUP by shutting +down gracefully.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+148
+149
+150
+151
+152
+153
+154
+155
+156
+157
+158
+159
+160
+161
+162
+163
+164
+165
+166
+167
+
+
# File 'lib/functions_framework/server.rb', line 148
+
+def respond_to_signals
+  synchronize do
+    return self if @signals_installed
+    ::Signal.trap "SIGTERM" do
+      Server.signal_enqueue "SIGTERM", @config.logger, @server
+    end
+    ::Signal.trap "SIGINT" do
+      Server.signal_enqueue "SIGINT", @config.logger, @server
+    end
+    begin
+      ::Signal.trap "SIGHUP" do
+        Server.signal_enqueue "SIGHUP", @config.logger, @server
+      end
+    rescue ::ArgumentError
+      # Not available on all systems
+    end
+    @signals_installed = true
+  end
+  self
+end
+
+
+ +
+

+ + #running?Boolean + + + + + +

+
+

Determine if the web server is currently running

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Boolean) + + + +
  • + +
+ +
+ + + + +
+
+
+
+138
+139
+140
+
+
# File 'lib/functions_framework/server.rb', line 138
+
+def running?
+  @server&.thread&.alive?
+end
+
+
+ +
+

+ + #startself + + + + + +

+
+

Start the web server in the background. Does nothing if the web server +is already running.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+
+
# File 'lib/functions_framework/server.rb', line 82
+
+def start
+  synchronize do
+    unless running?
+      @server = ::Puma::Server.new @app
+      @server.min_threads = @config.min_threads
+      @server.max_threads = @config.max_threads
+      @server.leak_stack_on_error = @config.show_error_details?
+      @server.binder.add_tcp_listener @config.bind_addr, @config.port
+      @config.logger.info "FunctionsFramework: Serving function #{@function.name.inspect}" \
+                          " on port #{@config.port}..."
+      @server.run true
+    end
+  end
+  self
+end
+
+
+ +
+

+ + #stop(force: false, wait: false) ⇒ self + + + + + +

+
+

Stop the web server in the background. Does nothing if the web server +is not running.

+ + +
+
+
+

Parameters:

+
    + +
  • + + force + + + (Boolean) + + + (defaults to: false) + + + — +

    Use a forced halt instead of a graceful shutdown

    +
    + +
  • + +
  • + + wait + + + (Boolean) + + + (defaults to: false) + + + — +

    Block until shutdown is complete

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+
+
# File 'lib/functions_framework/server.rb', line 106
+
+def stop force: false, wait: false
+  synchronize do
+    if running?
+      @config.logger.info "FunctionsFramework: Shutting down server..."
+      if force
+        @server.halt wait
+      else
+        @server.stop wait
+      end
+    end
+  end
+  self
+end
+
+
+ +
+

+ + #wait_until_stopped(timeout: nil) ⇒ self + + + + + +

+
+

Wait for the server to stop. Returns immediately if the server is not +running.

+ + +
+
+
+

Parameters:

+
    + +
  • + + timeout + + + (nil, Numeric) + + + (defaults to: nil) + + + — +

    The timeout. If nil (the default), waits +indefinitely, otherwise times out after the given number of seconds.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (self) + + + +
  • + +
+ +
+ + + + +
+
+
+
+128
+129
+130
+131
+
+
# File 'lib/functions_framework/server.rb', line 128
+
+def wait_until_stopped timeout: nil
+  @server&.thread&.join timeout
+  self
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Server/Config.html b/v0.10.0/FunctionsFramework/Server/Config.html new file mode 100644 index 00000000..0bb989d0 --- /dev/null +++ b/v0.10.0/FunctionsFramework/Server/Config.html @@ -0,0 +1,1322 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Class: FunctionsFramework::Server::Config + + + +

+
+ +
+
Inherits:
+
+ Object + +
    +
  • Object
  • + + + +
+ show all + +
+
+ + + + + + + + + + + +
+
Defined in:
+
lib/functions_framework/server.rb
+
+ +
+ +

Overview

+
+

The web server configuration. This object is yielded from the +FunctionsFramework::Server constructor and can be modified at that +point. Afterward, it is available from #config +but it is frozen.

+ + +
+
+
+ + +
+ + + + + + + +

+ Instance Method Summary + collapse +

+ + + + +
+

Constructor Details

+ +
+

+ + #initializeConfig + + + + + +

+
+

Create a new config object with the default settings

+ + +
+
+
+ + +
+ + + + +
+
+
+
+200
+201
+202
+203
+204
+205
+206
+207
+208
+
+
# File 'lib/functions_framework/server.rb', line 200
+
+def initialize
+  self.rack_env = nil
+  self.bind_addr = nil
+  self.port = nil
+  self.min_threads = nil
+  self.max_threads = nil
+  self.show_error_details = nil
+  self.logger = nil
+end
+
+
+ +
+ + +
+

Instance Method Details

+ + +
+

+ + #bind_addrString + + + + + +

+
+

Returns the current bind address.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + +
  • + +
+ +
+ + + + +
+
+
+
+284
+285
+286
+
+
# File 'lib/functions_framework/server.rb', line 284
+
+def bind_addr
+  @bind_addr
+end
+
+
+ +
+

+ + #bind_addr=(bind_addr) ⇒ Object + + + + + +

+
+

Set the bind address, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + bind_addr + + + (String, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+223
+224
+225
+
+
# File 'lib/functions_framework/server.rb', line 223
+
+def bind_addr= bind_addr
+  @bind_addr = bind_addr || ::ENV["FUNCTION_BIND_ADDR"] || "0.0.0.0"
+end
+
+
+ +
+

+ + #loggerLogger + + + + + +

+
+

Returns the logger.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Logger) + + + +
  • + +
+ +
+ + + + +
+
+
+
+324
+325
+326
+
+
# File 'lib/functions_framework/server.rb', line 324
+
+def logger
+  @logger
+end
+
+
+ +
+

+ + #logger=(logger) ⇒ Object + + + + + +

+
+

Set the logger for server messages, or nil to use the global default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + logger + + + (Logger) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+268
+269
+270
+
+
# File 'lib/functions_framework/server.rb', line 268
+
+def logger= logger
+  @logger = logger || ::FunctionsFramework.logger
+end
+
+
+ +
+

+ + #max_threadsInteger + + + + + +

+
+

Returns the maximum number of worker threads in the thread pool.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Integer) + + + +
  • + +
+ +
+ + + + +
+
+
+
+308
+309
+310
+
+
# File 'lib/functions_framework/server.rb', line 308
+
+def max_threads
+  @max_threads || 1
+end
+
+
+ +
+

+ + #max_threads=(max_threads) ⇒ Object + + + + + +

+
+

Set the maximum number of worker threads, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + max_threads + + + (Integer, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+247
+248
+249
+
+
# File 'lib/functions_framework/server.rb', line 247
+
+def max_threads= max_threads
+  @max_threads = (max_threads || ::ENV["FUNCTION_MAX_THREADS"])&.to_i
+end
+
+
+ +
+

+ + #min_threadsInteger + + + + + +

+
+

Returns the minimum number of worker threads in the thread pool.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Integer) + + + +
  • + +
+ +
+ + + + +
+
+
+
+300
+301
+302
+
+
# File 'lib/functions_framework/server.rb', line 300
+
+def min_threads
+  @min_threads || 1
+end
+
+
+ +
+

+ + #min_threads=(min_threads) ⇒ Object + + + + + +

+
+

Set the minimum number of worker threads, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + min_threads + + + (Integer, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+239
+240
+241
+
+
# File 'lib/functions_framework/server.rb', line 239
+
+def min_threads= min_threads
+  @min_threads = (min_threads || ::ENV["FUNCTION_MIN_THREADS"])&.to_i
+end
+
+
+ +
+

+ + #portInteger + + + + + +

+
+

Returns the current port number.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Integer) + + + +
  • + +
+ +
+ + + + +
+
+
+
+292
+293
+294
+
+
# File 'lib/functions_framework/server.rb', line 292
+
+def port
+  @port
+end
+
+
+ +
+

+ + #port=(port) ⇒ Object + + + + + +

+
+

Set the port number, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + port + + + (Integer, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+231
+232
+233
+
+
# File 'lib/functions_framework/server.rb', line 231
+
+def port= port
+  @port = (port || ::ENV["PORT"] || 8080).to_i
+end
+
+
+ +
+

+ + #rack_envString + + + + + +

+
+

Returns the current Rack environment.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (String) + + + +
  • + +
+ +
+ + + + +
+
+
+
+276
+277
+278
+
+
# File 'lib/functions_framework/server.rb', line 276
+
+def rack_env
+  @rack_env
+end
+
+
+ +
+

+ + #rack_env=(rack_env) ⇒ Object + + + + + +

+
+

Set the Rack environment, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + rack_env + + + (String, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+214
+215
+216
+217
+
+
# File 'lib/functions_framework/server.rb', line 214
+
+def rack_env= rack_env
+  @rack_env = rack_env || ::ENV["RACK_ENV"] ||
+              (::ENV["K_REVISION"] ? "production" : "development")
+end
+
+
+ +
+

+ + #show_error_details=(show_error_details) ⇒ Object + + + + + +

+
+

Set whether to show detailed error messages, or nil to use the default.

+ + +
+
+
+

Parameters:

+
    + +
  • + + show_error_details + + + (Boolean, nil) + + + +
  • + +
+ + +
+ + + + +
+
+
+
+255
+256
+257
+258
+259
+260
+261
+262
+
+
# File 'lib/functions_framework/server.rb', line 255
+
+def show_error_details= show_error_details
+  @show_error_details =
+    if show_error_details.nil?
+      !::ENV["FUNCTION_DETAILED_ERRORS"].to_s.empty?
+    else
+      show_error_details ? true : false
+    end
+end
+
+
+ +
+

+ + #show_error_details?Boolean + + + + + +

+
+

Returns whether to show detailed error messages.

+ + +
+
+
+ +

Returns:

+
    + +
  • + + + (Boolean) + + + +
  • + +
+ +
+ + + + +
+
+
+
+316
+317
+318
+
+
# File 'lib/functions_framework/server.rb', line 316
+
+def show_error_details?
+  @show_error_details.nil? ? (@rack_env == "development") : @show_error_details
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/FunctionsFramework/Testing.html b/v0.10.0/FunctionsFramework/Testing.html new file mode 100644 index 00000000..cc82f853 --- /dev/null +++ b/v0.10.0/FunctionsFramework/Testing.html @@ -0,0 +1,1465 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Module: FunctionsFramework::Testing + + + +

+
+ + + + +
+
Extended by:
+
Testing
+
+ + + + + + +
+
Included in:
+
Testing
+
+ + + +
+
Defined in:
+
lib/functions_framework/testing.rb
+
+ +
+ +

Overview

+
+

Helpers for writing unit tests.

+ +

Methods on this module can be called as module methods, or this module can +be included in a test class.

+ +

Example

+ +

Suppose we have the following app that uses the functions framework:

+ +
# app.rb
+
+require "functions_framework"
+
+FunctionsFramework.http "my-function" do |request|
+  "Hello, world!"
+end
+
+ +

The following is a test that could be run against that app:

+ +
# test_app.rb
+
+require "minitest/autorun"
+require "functions_framework/testing"
+
+class MyTest < Minitest::Test
+  # Make the testing methods available.
+  include FunctionsFramework::Testing
+
+  def test_my_function
+    # Load app.rb and apply its functions within this block
+    load_temporary "app.rb" do
+      # Create a mock http (rack) request
+      request = make_get_request "http://example.com"
+
+      # Call the function and get a rack response
+      response = call_http "my-function", request
+
+      # Assert against the response
+      assert_equal "Hello, world!", response.body.join
+    end
+  end
+end
+
+ + +
+
+
+ + +
+ + + + + + + +

+ Instance Method Summary + collapse +

+ + + + + + +
+

Instance Method Details

+ + +
+

+ + #call_event(name, event, globals: nil, logger: nil) ⇒ nil + + + + + +

+
+

Call the given event function for testing. The underlying function must +be of type :cloud_event`.

+ +

By default, the startup tasks will be run for the given function if they +have not already been run. You can, however, disable running startup +tasks by providing an explicit globals hash.

+ +

By default, the FunctionsFramework.logger will be used, but you can +override that by providing your own logger. In particular, to disable +logging, you can pass Logger.new(nil).

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The name of the function to call

    +
    + +
  • + +
  • + + event + + + (::CloudEvents::Event) + + + + — +

    The event to send

    +
    + +
  • + +
  • + + globals + + + (Hash) + + + (defaults to: nil) + + + — +

    Do not run startup tasks, and instead provide the +globals directly. Optional.

    +
    + +
  • + +
  • + + logger + + + (Logger) + + + (defaults to: nil) + + + — +

    Use the given logger instead of the Functions +Framework's global logger. Optional.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (nil) + + + +
  • + +
+ +
+ + + + +
+
+
+
+170
+171
+172
+173
+174
+175
+176
+177
+178
+179
+180
+181
+182
+
+
# File 'lib/functions_framework/testing.rb', line 170
+
+def call_event name, event, globals: nil, logger: nil
+  globals ||= run_startup_tasks name, logger: logger, lenient: true
+  function = Testing.current_registry[name]
+  case function&.type
+  when :cloud_event
+    function.call event, globals: globals, logger: logger
+    nil
+  when nil
+    raise "Unknown function name #{name}"
+  else
+    raise "Function #{name} is not a CloudEvent function"
+  end
+end
+
+
+ +
+

+ + #call_http(name, request, globals: nil, logger: nil) ⇒ Rack::Response + + + + + +

+
+

Call the given HTTP function for testing. The underlying function must +be of type :http. Returns the Rack response.

+ +

By default, the startup tasks will be run for the given function if they +have not already been run. You can, however, disable running startup +tasks by providing an explicit globals hash.

+ +

By default, the FunctionsFramework.logger will be used, but you can +override that by providing your own logger. In particular, to disable +logging, you can pass Logger.new(nil).

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The name of the function to call

    +
    + +
  • + +
  • + + request + + + (Rack::Request) + + + + — +

    The Rack request to send

    +
    + +
  • + +
  • + + globals + + + (Hash) + + + (defaults to: nil) + + + — +

    Do not run startup tasks, and instead provide the +globals directly. Optional.

    +
    + +
  • + +
  • + + logger + + + (Logger) + + + (defaults to: nil) + + + — +

    Use the given logger instead of the Functions +Framework's global logger. Optional.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Rack::Response) + + + +
  • + +
+ +
+ + + + +
+
+
+
+135
+136
+137
+138
+139
+140
+141
+142
+143
+144
+145
+146
+147
+148
+
+
# File 'lib/functions_framework/testing.rb', line 135
+
+def call_http name, request, globals: nil, logger: nil
+  globals ||= run_startup_tasks name, logger: logger, lenient: true
+  function = Testing.current_registry[name]
+  case function&.type
+  when :http
+    Testing.interpret_response do
+      function.call request, globals: globals, logger: logger
+    end
+  when nil
+    raise "Unknown function name #{name}"
+  else
+    raise "Function #{name} is not an HTTP function"
+  end
+end
+
+
+ +
+

+ + #load_temporary(path, &block) ⇒ Object + + + + + +

+
+

Load the given functions source for the duration of the given block, +and restore the previous status afterward.

+ + +
+
+
+

Parameters:

+
    + +
  • + + path + + + (String) + + + + — +

    File path to load

    +
    + +
  • + +
+ + +
+ + + + +
+
+
+
+73
+74
+75
+76
+
+
# File 'lib/functions_framework/testing.rb', line 73
+
+def load_temporary path, &block
+  path = ::File.expand_path path
+  Testing.load_for_testing path, &block
+end
+
+
+ +
+

+ + #make_cloud_event(data, id: nil, source: nil, type: nil, spec_version: nil, data_content_type: nil, data_schema: nil, subject: nil, time: nil) ⇒ ::CloudEvents::Event + + + + + +

+
+

Make a simple CloudEvent, for passing to a function test. The event data +is required, but all other parameters are optional (i.e. a reasonable or +random value will be generated if not provided).

+ + +
+
+
+

Parameters:

+
    + +
  • + + data + + + (Object) + + + + — +

    The data

    +
    + +
  • + +
  • + + id + + + (String) + + + (defaults to: nil) + + + — +

    Event ID (optional)

    +
    + +
  • + +
  • + + source + + + (String, URI) + + + (defaults to: nil) + + + — +

    Event source (optional)

    +
    + +
  • + +
  • + + type + + + (String) + + + (defaults to: nil) + + + — +

    Event type (optional)

    +
    + +
  • + +
  • + + spec_version + + + (String) + + + (defaults to: nil) + + + — +

    Spec version (optional)

    +
    + +
  • + +
  • + + data_content_type + + + (String, ::CloudEvents::ContentType) + + + (defaults to: nil) + + + — +

    Content type for the data (optional)

    +
    + +
  • + +
  • + + data_schema + + + (String, URI) + + + (defaults to: nil) + + + — +

    Data schema (optional)

    +
    + +
  • + +
  • + + subject + + + (String) + + + (defaults to: nil) + + + — +

    Subject (optional)

    +
    + +
  • + +
  • + + time + + + (String, DateTime) + + + (defaults to: nil) + + + — +

    Event timestamp (optional)

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (::CloudEvents::Event) + + + +
  • + +
+ +
+ + + + +
+
+
+
+249
+250
+251
+252
+253
+254
+255
+256
+257
+258
+259
+260
+261
+262
+263
+264
+265
+266
+267
+268
+269
+270
+271
+
+
# File 'lib/functions_framework/testing.rb', line 249
+
+def make_cloud_event data,
+                     id: nil,
+                     source: nil,
+                     type: nil,
+                     spec_version: nil,
+                     data_content_type: nil,
+                     data_schema: nil,
+                     subject: nil,
+                     time: nil
+  id ||= "random-id-#{rand 100_000_000}"
+  source ||= "functions-framework-testing"
+  type ||= "com.example.test"
+  spec_version ||= "1.0"
+  ::CloudEvents::Event.new id:                id,
+                           source:            source,
+                           type:              type,
+                           spec_version:      spec_version,
+                           data_content_type: data_content_type,
+                           data_schema:       data_schema,
+                           subject:           subject,
+                           time:              time,
+                           data:              data
+end
+
+
+ +
+

+ + #make_get_request(url, headers = []) ⇒ Rack::Request + + + + + +

+
+

Make a simple GET request, for passing to a function test.

+ + +
+
+
+

Parameters:

+
    + +
  • + + url + + + (URI, String) + + + + — +

    The URL to get.

    +
    + +
  • + +
  • + + headers + + + (Array, Hash) + + + (defaults to: []) + + + — +

    HTTP headers. May be given as a hash (of +header names mapped to values), an array of strings (where each +string is of the form Header-Name: Header value), or an array of +two-element string arrays.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Rack::Request) + + + +
  • + +
+ +
+ + + + +
+
+
+
+213
+214
+215
+
+
# File 'lib/functions_framework/testing.rb', line 213
+
+def make_get_request url, headers = []
+  make_request url, headers: headers
+end
+
+
+ +
+

+ + #make_post_request(url, body, headers = []) ⇒ Rack::Request + + + + + +

+
+

Make a simple POST request, for passing to a function test.

+ + +
+
+
+

Parameters:

+
    + +
  • + + url + + + (URI, String) + + + + — +

    The URL to post to.

    +
    + +
  • + +
  • + + body + + + (String) + + + + — +

    The body to post.

    +
    + +
  • + +
  • + + headers + + + (Array, Hash) + + + (defaults to: []) + + + — +

    HTTP headers. May be given as a hash (of +header names mapped to values), an array of strings (where each +string is of the form Header-Name: Header value), or an array of +two-element string arrays.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Rack::Request) + + + +
  • + +
+ +
+ + + + +
+
+
+
+228
+229
+230
+
+
# File 'lib/functions_framework/testing.rb', line 228
+
+def make_post_request url, body, headers = []
+  make_request url, method: ::Rack::POST, body: body, headers: headers
+end
+
+
+ +
+

+ + #make_request(url, method: ::Rack::GET, body: nil, headers: []) ⇒ Rack::Request + + + + + +

+
+

Make a Rack request, for passing to a function test.

+ + +
+
+
+

Parameters:

+
    + +
  • + + url + + + (URI, String) + + + + — +

    The URL to get, including query params.

    +
    + +
  • + +
  • + + method + + + (String) + + + (defaults to: ::Rack::GET) + + + — +

    The HTTP method (defaults to "GET").

    +
    + +
  • + +
  • + + body + + + (String) + + + (defaults to: nil) + + + — +

    The HTTP body, if any.

    +
    + +
  • + +
  • + + headers + + + (Array, Hash) + + + (defaults to: []) + + + — +

    HTTP headers. May be given as a hash (of +header names mapped to values), an array of strings (where each +string is of the form Header-Name: Header value), or an array of +two-element string arrays.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Rack::Request) + + + +
  • + +
+ +
+ + + + +
+
+
+
+196
+197
+198
+199
+200
+201
+
+
# File 'lib/functions_framework/testing.rb', line 196
+
+def make_request url, method: ::Rack::GET, body: nil, headers: []
+  env = Testing.build_standard_env URI(url), headers
+  env[::Rack::REQUEST_METHOD] = method
+  env[::Rack::RACK_INPUT] = ::StringIO.new body if body
+  ::Rack::Request.new env
+end
+
+
+ +
+

+ + #run_startup_tasks(name, logger: nil, lenient: false) ⇒ Hash + + + + + +

+
+

Run startup tasks for the given function name and return the initialized +globals hash.

+ +

Normally, this will be run automatically prior to the first call to the +function using #call_http or #call_event, if it has not already been +run. However, you can call it explicitly to test its behavior. It cannot +be called more than once for any given function.

+ +

By default, the FunctionsFramework.logger will be used, but you can +override that by providing your own logger. In particular, to disable +logging, you can pass Logger.new(nil).

+ + +
+
+
+

Parameters:

+
    + +
  • + + name + + + (String) + + + + — +

    The name of the function to start up.

    +
    + +
  • + +
  • + + logger + + + (Logger) + + + (defaults to: nil) + + + — +

    Use the given logger instead of the Functions +Framework's global logger. Optional.

    +
    + +
  • + +
  • + + lenient + + + (Boolean) + + + (defaults to: false) + + + — +

    If false (the default), raise an error if the +given function has already had its startup tasks run. If true, +duplicate requests to run startup tasks are ignored.

    +
    + +
  • + +
+ +

Returns:

+
    + +
  • + + + (Hash) + + + + — +

    The initialized globals.

    +
    + +
  • + +
+ +
+ + + + +
+
+
+
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+
+
# File 'lib/functions_framework/testing.rb', line 99
+
+def run_startup_tasks name, logger: nil, lenient: false
+  function = Testing.current_registry[name]
+  raise "Unknown function name #{name}" unless function
+  globals = Testing.current_globals name
+  if globals
+    raise "Function #{name} has already been started up" unless lenient
+  else
+    globals = function.populate_globals
+    Testing.current_registry.startup_tasks.each do |task|
+      task.call function, globals: globals, logger: logger
+    end
+    Testing.current_globals name, globals
+  end
+  globals.freeze
+end
+
+
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/_index.html b/v0.10.0/_index.html new file mode 100644 index 00000000..f995f104 --- /dev/null +++ b/v0.10.0/_index.html @@ -0,0 +1,218 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Functions

+
+

Alphabetic Index

+ +

File Listing

+ + +
+

Namespace Listing A-Z

+ + + + + + + + +
+ + +
    +
  • C
  • +
      + +
    • + CLI + + (FunctionsFramework) + +
    • + +
    • + Callable + + (FunctionsFramework::Function) + +
    • + +
    • + Config + + (FunctionsFramework::Server) + +
    • + +
    +
+ + + + + + + + +
    +
  • R
  • +
      + +
    • + Registry + + (FunctionsFramework) + +
    • + +
    +
+ + +
    +
  • S
  • +
      + +
    • + Server + + (FunctionsFramework) + +
    • + +
    +
+ + +
    +
  • T
  • +
      + +
    • + Testing + + (FunctionsFramework) + +
    • + +
    +
+ +
+ +
+ +
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/class_list.html b/v0.10.0/class_list.html new file mode 100644 index 00000000..3323c478 --- /dev/null +++ b/v0.10.0/class_list.html @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + Codestin Search App + + + +
+
+

Class List

+ + + +
+ + +
+ + diff --git a/v0.10.0/css/common.css b/v0.10.0/css/common.css new file mode 100644 index 00000000..cf25c452 --- /dev/null +++ b/v0.10.0/css/common.css @@ -0,0 +1 @@ +/* Override this file with custom rules */ \ No newline at end of file diff --git a/v0.10.0/css/full_list.css b/v0.10.0/css/full_list.css new file mode 100644 index 00000000..fa359824 --- /dev/null +++ b/v0.10.0/css/full_list.css @@ -0,0 +1,58 @@ +body { + margin: 0; + font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; + font-size: 13px; + height: 101%; + overflow-x: hidden; + background: #fafafa; +} + +h1 { padding: 12px 10px; padding-bottom: 0; margin: 0; font-size: 1.4em; } +.clear { clear: both; } +.fixed_header { position: fixed; background: #fff; width: 100%; padding-bottom: 10px; margin-top: 0; top: 0; z-index: 9999; height: 70px; } +#search { position: absolute; right: 5px; top: 9px; padding-left: 24px; } +#content.insearch #search, #content.insearch #noresults { background: url(data:image/gif;base64,R0lGODlhEAAQAPYAAP///wAAAPr6+pKSkoiIiO7u7sjIyNjY2J6engAAAI6OjsbGxjIyMlJSUuzs7KamppSUlPLy8oKCghwcHLKysqSkpJqamvT09Pj4+KioqM7OzkRERAwMDGBgYN7e3ujo6Ly8vCoqKjY2NkZGRtTU1MTExDw8PE5OTj4+PkhISNDQ0MrKylpaWrS0tOrq6nBwcKysrLi4uLq6ul5eXlxcXGJiYoaGhuDg4H5+fvz8/KKiohgYGCwsLFZWVgQEBFBQUMzMzDg4OFhYWBoaGvDw8NbW1pycnOLi4ubm5kBAQKqqqiQkJCAgIK6urnJyckpKSjQ0NGpqatLS0sDAwCYmJnx8fEJCQlRUVAoKCggICLCwsOTk5ExMTPb29ra2tmZmZmhoaNzc3KCgoBISEiIiIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCAAAACwAAAAAEAAQAAAHaIAAgoMgIiYlg4kACxIaACEJCSiKggYMCRselwkpghGJBJEcFgsjJyoAGBmfggcNEx0flBiKDhQFlIoCCA+5lAORFb4AJIihCRbDxQAFChAXw9HSqb60iREZ1omqrIPdJCTe0SWI09GBACH5BAkIAAAALAAAAAAQABAAAAdrgACCgwc0NTeDiYozCQkvOTo9GTmDKy8aFy+NOBA7CTswgywJDTIuEjYFIY0JNYMtKTEFiRU8Pjwygy4ws4owPyCKwsMAJSTEgiQlgsbIAMrO0dKDGMTViREZ14kYGRGK38nHguHEJcvTyIEAIfkECQgAAAAsAAAAABAAEAAAB2iAAIKDAggPg4iJAAMJCRUAJRIqiRGCBI0WQEEJJkWDERkYAAUKEBc4Po1GiKKJHkJDNEeKig4URLS0ICImJZAkuQAhjSi/wQyNKcGDCyMnk8u5rYrTgqDVghgZlYjcACTA1sslvtHRgQAh+QQJCAAAACwAAAAAEAAQAAAHZ4AAgoOEhYaCJSWHgxGDJCQARAtOUoQRGRiFD0kJUYWZhUhKT1OLhR8wBaaFBzQ1NwAlkIszCQkvsbOHL7Y4q4IuEjaqq0ZQD5+GEEsJTDCMmIUhtgk1lo6QFUwJVDKLiYJNUd6/hoEAIfkECQgAAAAsAAAAABAAEAAAB2iAAIKDhIWGgiUlh4MRgyQkjIURGRiGGBmNhJWHm4uen4ICCA+IkIsDCQkVACWmhwSpFqAABQoQF6ALTkWFnYMrVlhWvIKTlSAiJiVVPqlGhJkhqShHV1lCW4cMqSkAR1ofiwsjJyqGgQAh+QQJCAAAACwAAAAAEAAQAAAHZ4AAgoOEhYaCJSWHgxGDJCSMhREZGIYYGY2ElYebi56fhyWQniSKAKKfpaCLFlAPhl0gXYNGEwkhGYREUywag1wJwSkHNDU3D0kJYIMZQwk8MjPBLx9eXwuETVEyAC/BOKsuEjYFhoEAIfkECQgAAAAsAAAAABAAEAAAB2eAAIKDhIWGgiUlh4MRgyQkjIURGRiGGBmNhJWHm4ueICImip6CIQkJKJ4kigynKaqKCyMnKqSEK05StgAGQRxPYZaENqccFgIID4KXmQBhXFkzDgOnFYLNgltaSAAEpxa7BQoQF4aBACH5BAkIAAAALAAAAAAQABAAAAdogACCg4SFggJiPUqCJSWGgkZjCUwZACQkgxGEXAmdT4UYGZqCGWQ+IjKGGIUwPzGPhAc0NTewhDOdL7Ykji+dOLuOLhI2BbaFETICx4MlQitdqoUsCQ2vhKGjglNfU0SWmILaj43M5oEAOwAAAAAAAAAAAA==) no-repeat center left; } +#full_list { padding: 0; list-style: none; margin-left: 0; margin-top: 80px; font-size: 1.1em; } +#full_list ul { padding: 0; } +#full_list li { padding: 0; margin: 0; list-style: none; } +#full_list li .item { padding: 5px 5px 5px 12px; } +#noresults { padding: 7px 12px; background: #fff; } +#content.insearch #noresults { margin-left: 7px; } +li.collapsed ul { display: none; } +li a.toggle { cursor: default; position: relative; left: -5px; top: 4px; text-indent: -999px; width: 10px; height: 9px; margin-left: -10px; display: block; float: left; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAK8AAACvABQqw0mAAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTM5jWRgMAAAAVdEVYdENyZWF0aW9uIFRpbWUAMy8xNC8wOeNZPpQAAAE2SURBVDiNrZTBccIwEEXfelIAHUA6CZ24BGaWO+FuzZAK4k6gg5QAdGAq+Bxs2Yqx7BzyL7Llp/VfzZeQhCTc/ezuGzKKnKSzpCxXJM8fwNXda3df5RZETlIt6YUzSQDs93sl8w3wBZxCCE10GM1OcWbWjB2mWgEH4Mfdyxm3PSepBHibgQE2wLe7r4HjEidpnXMYdQPKEMJcsZ4zs2POYQOcaPfwMVOo58zsAdMt18BuoVDPxUJRacELbXv3hUIX2vYmOUvi8C8ydz/ThjXrqKqqLbDIAdsCKBd+Wo7GWa7o9qzOQHVVVXeAbs+yHHCH4aTsaCOQqunmUy1yBUAXkdMIfMlgF5EXLo2OpV/c/Up7jG4hhHcYLgWzAZXUc2b2ixsfvc/RmNNfOXD3Q/oeL9axJE1yT9IOoUu6MGUkAAAAAElFTkSuQmCC) no-repeat bottom left; } +li.collapsed a.toggle { opacity: 0.5; cursor: default; background-position: top left; } +li { color: #888; cursor: pointer; } +li.deprecated { text-decoration: line-through; font-style: italic; } +li.odd { background: #f0f0f0; } +li.even { background: #fafafa; } +.item:hover { background: #ddd; } +li small:before { content: "("; } +li small:after { content: ")"; } +li small.search_info { display: none; } +a, a:visited { text-decoration: none; color: #05a; } +li.clicked > .item { background: #05a; color: #ccc; } +li.clicked > .item a, li.clicked > .item a:visited { color: #eee; } +li.clicked > .item a.toggle { opacity: 0.5; background-position: bottom right; } +li.collapsed.clicked a.toggle { background-position: top right; } +#search input { border: 1px solid #bbb; border-radius: 3px; } +#full_list_nav { margin-left: 10px; font-size: 0.9em; display: block; color: #aaa; } +#full_list_nav a, #nav a:visited { color: #358; } +#full_list_nav a:hover { background: transparent; color: #5af; } +#full_list_nav span:after { content: ' | '; } +#full_list_nav span:last-child:after { content: ''; } + +#content h1 { margin-top: 0; } +li { white-space: nowrap; cursor: normal; } +li small { display: block; font-size: 0.8em; } +li small:before { content: ""; } +li small:after { content: ""; } +li small.search_info { display: none; } +#search { width: 170px; position: static; margin: 3px; margin-left: 10px; font-size: 0.9em; color: #888; padding-left: 0; padding-right: 24px; } +#content.insearch #search { background-position: center right; } +#search input { width: 110px; } + +#full_list.insearch ul { display: block; } +#full_list.insearch .item { display: none; } +#full_list.insearch .found { display: block; padding-left: 11px !important; } +#full_list.insearch li a.toggle { display: none; } +#full_list.insearch li small.search_info { display: block; } diff --git a/v0.10.0/css/style.css b/v0.10.0/css/style.css new file mode 100644 index 00000000..eb0dbc86 --- /dev/null +++ b/v0.10.0/css/style.css @@ -0,0 +1,497 @@ +html { + width: 100%; + height: 100%; +} +body { + font-family: "Lucida Sans", "Lucida Grande", Verdana, Arial, sans-serif; + font-size: 13px; + width: 100%; + margin: 0; + padding: 0; + display: flex; + display: -webkit-flex; + display: -ms-flexbox; +} + +#nav { + position: relative; + width: 100%; + height: 100%; + border: 0; + border-right: 1px dotted #eee; + overflow: auto; +} +.nav_wrap { + margin: 0; + padding: 0; + width: 20%; + height: 100%; + position: relative; + display: flex; + display: -webkit-flex; + display: -ms-flexbox; + flex-shrink: 0; + -webkit-flex-shrink: 0; + -ms-flex: 1 0; +} +#resizer { + position: absolute; + right: -5px; + top: 0; + width: 10px; + height: 100%; + cursor: col-resize; + z-index: 9999; +} +#main { + flex: 5 1; + -webkit-flex: 5 1; + -ms-flex: 5 1; + outline: none; + position: relative; + background: #fff; + padding: 1.2em; + padding-top: 0.2em; + box-sizing: border-box; +} + +@media (max-width: 920px) { + .nav_wrap { width: 100%; top: 0; right: 0; overflow: visible; position: absolute; } + #resizer { display: none; } + #nav { + z-index: 9999; + background: #fff; + display: none; + position: absolute; + top: 40px; + right: 12px; + width: 500px; + max-width: 80%; + height: 80%; + overflow-y: scroll; + border: 1px solid #999; + border-collapse: collapse; + box-shadow: -7px 5px 25px #aaa; + border-radius: 2px; + } +} + +@media (min-width: 920px) { + body { height: 100%; overflow: hidden; } + #main { height: 100%; overflow: auto; } + #search { display: none; } +} + +#main img { max-width: 100%; } +h1 { font-size: 25px; margin: 1em 0 0.5em; padding-top: 4px; border-top: 1px dotted #d5d5d5; } +h1.noborder { border-top: 0px; margin-top: 0; padding-top: 4px; } +h1.title { margin-bottom: 10px; } +h1.alphaindex { margin-top: 0; font-size: 22px; } +h2 { + padding: 0; + padding-bottom: 3px; + border-bottom: 1px #aaa solid; + font-size: 1.4em; + margin: 1.8em 0 0.5em; + position: relative; +} +h2 small { font-weight: normal; font-size: 0.7em; display: inline; position: absolute; right: 0; } +h2 small a { + display: block; + height: 20px; + border: 1px solid #aaa; + border-bottom: 0; + border-top-left-radius: 5px; + background: #f8f8f8; + position: relative; + padding: 2px 7px; +} +.clear { clear: both; } +.inline { display: inline; } +.inline p:first-child { display: inline; } +.docstring, .tags, #filecontents { font-size: 15px; line-height: 1.5145em; } +.docstring p > code, .docstring p > tt, .tags p > code, .tags p > tt { + color: #c7254e; background: #f9f2f4; padding: 2px 4px; font-size: 1em; + border-radius: 4px; +} +.docstring h1, .docstring h2, .docstring h3, .docstring h4 { padding: 0; border: 0; border-bottom: 1px dotted #bbb; } +.docstring h1 { font-size: 1.2em; } +.docstring h2 { font-size: 1.1em; } +.docstring h3, .docstring h4 { font-size: 1em; border-bottom: 0; padding-top: 10px; } +.summary_desc .object_link a, .docstring .object_link a { + font-family: monospace; font-size: 1.05em; + color: #05a; background: #EDF4FA; padding: 2px 4px; font-size: 1em; + border-radius: 4px; +} +.rdoc-term { padding-right: 25px; font-weight: bold; } +.rdoc-list p { margin: 0; padding: 0; margin-bottom: 4px; } +.summary_desc pre.code .object_link a, .docstring pre.code .object_link a { + padding: 0px; background: inherit; color: inherit; border-radius: inherit; +} + +/* style for */ +#filecontents table, .docstring table { border-collapse: collapse; } +#filecontents table th, #filecontents table td, +.docstring table th, .docstring table td { border: 1px solid #ccc; padding: 8px; padding-right: 17px; } +#filecontents table tr:nth-child(odd), +.docstring table tr:nth-child(odd) { background: #eee; } +#filecontents table tr:nth-child(even), +.docstring table tr:nth-child(even) { background: #fff; } +#filecontents table th, .docstring table th { background: #fff; } + +/* style for
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Command-line flagEnvironment variableDescription
--portPORTThe port on which the Functions Framework listens for requests. Default: 8080.
--targetFUNCTION_TARGETThe name of the exported function to be invoked in response to requests. Default: function.
--sourceFUNCTION_SOURCEThe path to the file containing your function. Default: app.rb (in the current working directory).
--signature-typeFUNCTION_SIGNATURE_TYPEVerifies that the function has the expected signature. Allowed values: http, event, or cloudevent.
--environmentRACK_ENVSets the Rack environment.
--bindFUNCTION_BIND_ADDRBinds to the given address. Default: 0.0.0.0.
--min-threadsFUNCTION_MIN_THREADSSets the minimum thread pool size, overriding Puma's default.
--max-threadsFUNCTION_MAX_THREADSSets the maximum thread pool size, overriding Puma's default.
--detailed-errorsFUNCTION_DETAILED_ERRORSNo value. If present, shows exception details in exception responses. Defaults to false.
--verboseFUNCTION_LOGGING_LEVELNo value. Increases log verbosity (e.g. from INFO to DEBUG). Can be given more than once.
--quietFUNCTION_LOGGING_LEVELNo value. Decreases log verbosity (e.g. from INFO to WARN). Can be given more than once.
+ +

Detailed errors are enabled by default if the FUNCTION_DETAILED_ERRORS +environment variable is set to a non-empty string. The exact value does not +matter. Detailed errors are disabled if the variable is unset or empty.

+ +

The logging level defaults to the value of the FUNCTION_LOGGING_LEVEL +environment variable, which can be one of the following values: DEBUG, INFO, +WARN, ERROR, FATAL, or UNKNOWN, corresponding to Ruby's +Logger::Severity +constants. If FUNCTION_LOGGING_LEVEL is not set to one of those values, it +defaults to INFO.

+ + + + + + + \ No newline at end of file diff --git a/v0.10.0/file.testing-functions.html b/v0.10.0/file.testing-functions.html new file mode 100644 index 00000000..62a141f2 --- /dev/null +++ b/v0.10.0/file.testing-functions.html @@ -0,0 +1,276 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Testing Functions

+ +

This guide covers writing unit tests for functions using the Functions Framework +for Ruby. For more information about the Framework, see the +Overview Guide.

+ +

Overview of function testing

+ +

One of the benefits of the functions-as-a-service paradigm is that functions are +easy to test. In many cases, you can simply call a function with input, and test +the output. You do not need to set up (or mock) an actual server.

+ +

The Functions Framework provides utility methods that streamline the process of +setting up functions and the environment for testing, constructing input +parameters, and interpreting results. These are available in the +FunctionsFramework::Testing module. Generally, you can include this module in +your Minitest test class or RSpec describe block.

+ +
require "minitest/autorun"
+require "functions_framework/testing"
+
+class MyTest < Minitest::Test
+  include FunctionsFramework::Testing
+  # define tests...
+end
+
+ +
require "rspec"
+require "functions_framework/testing"
+
+describe "My functions" do
+  include FunctionsFramework::Testing
+  # define examples...
+end
+
+ +

Loading functions for testing

+ +

To test a function, you'll need to load the Ruby file that defines the function, +and run the function to test its results. The Testing module provides a method +FunctionsFramework::Testing#load_temporary, which loads a Ruby file, defining +functions but only for the scope of your test. This allows your test to coexist +with tests for other functions, even functions with the same name from a +different Ruby file.

+ +
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
+
+ +

When running a test suite, you'll typically need to load all the Ruby files +that define your functions. While load_temporary can ensure that the function +definitions do not conflict, it cannot do the same for classes, methods, and +other Ruby constructs. So, for testability, it is generally good practice to +include only functions in one of these files. If you need to write supporting +helper methods, classes, constants, or other code, include them in separate +ruby files that you require.

+ +

Testing HTTP functions

+ +

Testing an HTTP function is generally as simple as generating a request, calling +the function, and asserting against the response.

+ +

The input to an HTTP function is a +Rack::Request object. It is +usually not hard to construct one of these objects, but the Testing module +includes helper methods that you can use to create simple requests for many +basic cases.

+ +

When you have constructed an input request, use +FunctionsFramework::Testing#call_http to call a named function, passing the +request object. This method returns a +Rack::Response that you can +assert against.

+ +
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
+
+ +

If the function raises an exception, the exception will be converted to a 500 +response object. So if you are testing an error case, you should still check the +response object rather than looking for a raised exception.

+ +
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
+
+ +

Testing CloudEvent functions

+ +

Testing a CloudEvent function works similarly. The Testing module provides +methods to help construct example CloudEvent objects, which can then be passed +to the method FunctionsFramework::Testing#call_event.

+ +

Unlike HTTP functions, event functions do not have a return value. Instead, you +will need to test side effects. A common approach is to test logs by capturing +the standard error output.

+ +
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
+
+ +

Testing startup tasks

+ +

When a functions server is starting up, it calls startup tasks automatically. +In the testing environment, when you call a function using the +FunctionsFramework::Testing#call_http or +FunctionsFramework::Testing#call_event methods, the testing environment will +also automatically execute any startup tasks for you.

+ +

You can also call startup tasks explicitly to test them in isolation, using the +FunctionsFramework::Testing#run_startup_tasks method. Pass the name of a +function, and the testing module will execute all defined startup blocks, in +order, as if the server were preparing that function for execution. +FunctionsFramework::Testing#run_startup_tasks returns the resulting globals +as a hash, so you can assert against its contents.

+ +

If you use FunctionsFramework::Testing#run_startup_tasks to run the startup +tasks explicitly, they will not be run again when you call the function itself +using FunctionsFramework::Testing#call_http or +FunctionsFramework::Testing#call_event. However, if startup tasks have +already been run implicitly by FunctionsFramework::Testing#call_http or +FunctionsFramework::Testing#call_event, then attempting to run them again +explicitly by calling FunctionsFramework::Testing#run_startup_tasks will +result in an exception.

+ +

There is currently no way to run a single startup block in isolation. If you +have multiple startup blocks defined, they are always executed together.

+ +

Following is an example test that runs startup tasks explicitly and asserts +against the effect on the globals.

+ +
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
+
+
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/file.writing-functions.html b/v0.10.0/file.writing-functions.html new file mode 100644 index 00000000..7dceabd1 --- /dev/null +++ b/v0.10.0/file.writing-functions.html @@ -0,0 +1,566 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Writing Functions

+ +

This guide covers writing functions using the Functions Framework for Ruby. For +more information about the Framework, see the +Overview Guide.

+ +

About functions

+ +

Functions are Ruby blocks that are run when an input is received. Those inputs +can be HTTP requests or events in a recognized format. Functions that receive +HTTP requests return an HTTP response, but event functions have no return value.

+ +

When you define a function, you must provide an identifying name. The Functions +Framework allows you to use any string as a function name; however, many +deployment environments restrict the characters that can be used in a name. For +maximum portability, it is recommended that you use names that are allowed for +Ruby methods, i.e. beginning with a letter, and containing only letters, +numbers, and underscores.

+ +

Defining an HTTP function

+ +

An HTTP function is a simple web service that takes an HTTP request and returns +an HTTP response. The following example defines an HTTP function named "hello" +that returns a simple message in the HTTP response body:

+ +
require "functions_framework"
+
+FunctionsFramework.http "hello" do |request|
+  # Return the response body.
+  "Hello, world!\n"
+end
+
+ +

HTTP functions take a Rack Request object and return an HTTP response. We'll +now cover these in a bit more detail.

+ +

Using the Request object

+ +

An HTTP function is passed a request, which is an object of type +Rack::Request. This object +provides methods for obtaining request information such as the method, +path, query parameters, body content, and headers. You can also obtain the raw +Rack environment using the env method. The following example includes some +request information in the response:

+ +
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
+
+ +

The Functions Framework sets up a logger in the Rack environment, so you can +use the logger method on the request object if you want to emit logs. These +logs will be written to the standard error stream, and will appear in the +Google Cloud Logs if your function is running on a Google Cloud serverless +hosting environment.

+ +
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
+
+ +

Response types

+ +

The above examples return simple strings as the response body. Often, however, +you will need to return more complex responses such as JSON, binary data, or +even rendered HTML. The Functions Framework recognizes a variety of return +types from an HTTP function:

+ +
    +
  • String : If you return a string, the framework will use it as the +response body in with a 200 (success) HTTP status code. It will set the +Content-Type header to text/plain.
  • +
  • Array : If you return an array, the framework will assume it is a +standard three-element Rack response array, as defined in the +Rack spec.
  • +
  • Rack::Response : You can return a +Rack::Response object. The +Framework will call #finish on this object and retrieve the contents.
  • +
  • Hash : If you return a Hash, the Framework will attempt to encode it as +JSON, and return it in the response body with a 200 (success) HTTP status +code. The Content-Type will be set to application/json.
  • +
  • StandardError : If you return an exception object, the Framework will +return a 500 (server error) response. See the section below on +Error Handling.
  • +
+ +

Using Sinatra

+ +

The Functions Framework, and the functions-as-a-service (FaaS) solutions it +targets, are optimized for relatively simple HTTP requests such as webhooks and +simple APIs. If you want to deploy a large application or use a monolithic +framework such as Ruby on Rails, you may want to consider a solution such as +Google Cloud Run that is tailored to larger applications. However, a lightweight +framework such as Sinatra is sometimes useful when writing HTTP functions.

+ +

It is easy to connect an HTTP function to a Sinatra app. First, declare the +dependency on Sinatra in your Gemfile:

+ +
source "https://rubygems.org"
+gem "functions_framework", "~> 0.10"
+gem "sinatra", "~> 2.0"
+
+ +

Write the Sinatra app using the "modular" Sinatra interface (i.e. subclass +Sinatra::Base), and then run the Sinatra app directly as a Rack handler from +the function. Here is a basic example:

+ +
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
+
+ +

This technique gives you access to pretty much any feature of the Sinatra web +framework, including routes, templates, and even custom middleware.

+ +

Defining an Event function

+ +

An event function is a handler for a standard cloud event. It can receive +industry-standard CloudEvents, as well as events sent +by Google Cloud services such as Pub/Sub and +Storage. Event functions do not have a +return value.

+ +

The following is a simple event handler that receives an event and logs some +information about it:

+ +
require "functions_framework"
+
+FunctionsFramework.cloud_event "hello" do |event|
+  FunctionsFramework.logger.info "I received an event of type #{event.type}!"
+end
+
+ +

The event parameter will be either a +CloudEvents V0.3 Event +object (see spec) or a +CloudEvents V1.0 Event +object (see spec).

+ +

Some Google Cloud services send events in a legacy event format that was defined +prior to CloudEvents. The Functions Framework will convert these legacy events +to an equivalent CloudEvents V1 type, so your function will always receive a +CloudEvent object when it is sent an event from Google Cloud. The precise +mapping between legacy events and CloudEvents is not specified in detail here, +but in general, the data from the legacy event will be mapped to the data +field in the CloudEvent, and the context from the legacy event will be mapped +to equivalent CloudEvent attributes.

+ +

Error handling

+ +

If your function encounters an error, it can raise an exception. The Functions +Framework will catch StandardError exceptions and handle them appropriately.

+ +

If you raise an exception in an HTTP function, the Functions Framework will +return a 500 (server error) response. You can control whether the exception +details (e.g. exception type, message, and backtrace) are sent with the +response by setting the detailed-errors configuration in the server. The +Framework will also log the error for you.

+ +

If you need more control over the error response, you can also construct the +HTTP response yourself. For example:

+ +
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
+
+ +

The runtime environment

+ +

A serverless environment may be somewhat different from server-based runtime +environments you might be used to. Serverless runtimes often provide a simpler +programming model, transparent scaling, and cost savings, but they do so by +controlling how your code is managed and executed. The Functions Framework is +designed around a "functions-as-a-service" (FaaS) paradigm, which runs +self-contained stateless functions that have an input and a return value. It's +important to understand what that means for your Ruby code in order to get the +most out of a cloud serverless product.

+ +

For example, multithreading is a core element of the Functions Framework. When +you write functions, you should assume that multiple executions may be taking +place concurrently in different threads, and thus you should avoid operations +that can cause concurrency issues or race conditions. The easiest way to do +this is to make your functions self-contained and stateless. Avoid global +variables and don't share mutable data between different function executions.

+ +

Additionally, a serverless runtime may throttle the CPU whenever no actual +function executions are taking place. This lets it reduce the CPU resources +used (and therefore the cost to you), while keeping your application warmed up +and ready to respond to new requests quickly. An important implication, though, +is that you should avoid starting up background threads or processes. They may +not get any CPU time during periods when your Ruby application is not actually +executing a function.

+ +

In the sections below, we'll discuss a few techniques and features of the +Functions Framework to help you write Ruby code that fits well into a +serverless paradigm.

+ +

Startup tasks

+ +

It is sometimes useful to perform one-time initialization that applies to many +function executions, for example to warm up caches, perform precomputation, or +establish shared remote connections. To run code during initialization, use +FunctionsFramework.on_startup to define a startup task.

+ +
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
+
+ +

Startup tasks are run once per Ruby instance during cold start -- that is, +after the Ruby VM boots up but before the framework starts receiving requests +and executing functions. You can define multiple startup tasks, and they will +run in order, and are guaranteed to complete before any function is executed.

+ +

The block is optionally passed the FunctionsFramework::Function representing +the function that will be run. You code can, for example, perform different +initialization depending on the FunctionsFramework::Function#name or +FunctionsFramework::Function#type.

+ +

In most cases, initialization code should live in an on_startup block +instead of at the "top level" of your Ruby file. This is because some +serverless runtimes may load your Ruby code at build or deployment time (for +example, to verify that it properly defines the requested function), and this +will execute any code present at the top level of the Ruby file. If top-level +code is long-running or depends on runtime resources or environment variables, +this could cause the deployment to fail. By performing initialization in an +on_startup block instead, you ensure it will run only when an actual runtime +server is starting up, not at build/deployment time.

+ +
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
+
+# ...
+
+ +

Because startup tasks run during cold start, they could have an impact on your +function's startup latency. To mitigate this issue, it is possible to run parts +of your initialization lazily, as described below in the section below on +lazy initialization.

+ +

The execution context and global data

+ +

When your function block executes, the object context (i.e. self) is set to +an instance of FunctionsFramework::Function::Callable. Each function +invocation (including functions that might be running concurrently in separate +threads) runs within a different instance, to help you avoid having functions +interfere with each other.

+ +

The object context also defines a few methods that may be useful when writing +your function.

+ +

First, you can obtain the logger by calling the +FunctionsFramework::Function::Callable#logger convenience method. This is +the same logger that is provided by the HTTP request object or by the +FunctionsFramework.logger global method.

+ +

Second, you can access global shared data by passing a key to +FunctionsFramework::Function::Callable#global. Global shared data is a set +of key-value pairs that are available to every function invocation. By default, +two keys are available to all functions:

+ +
    +
  • :function_name whose String value is the name of the running function.
  • +
  • :function_type whose value is either :http or :cloud_event depending +on the type of the running function.
  • +
+ +

Following is a simple example using the logger and global methods of the +context object:

+ +
require "functions_framework"
+
+FunctionsFramework.cloud_event "hello" do |event|
+  logger.info "Now running the function called #{global(:function_name)}"
+end
+
+ +

To avoid concurrency issues, global shared data is immutable when executing a +function. You cannot add or delete keys or change the value of existing keys. +However, the global data is settable during startup tasks, because startup +tasks never run concurrently. You can use this feature to initialize shared +resources, as described below.

+ +

Using the global data mechanism is generally preferred over actual Ruby global +variables, because the Functions Framework can help you avoid concurrent edits. +Additionally, the framework will isolate the sets of global data associated +with different sets of functions, which lets you run functions in isolation +during unit tests. If you are testing multiple functions, they will not +interfere with each other as they might if they used global variables.

+ +

Sharing resources

+ +

Although functions should generally be self-contained and stateless, it is +sometimes useful to share certain kinds of resources across multiple function +invocations that run on the same Ruby instance. For example, you might +establish a single connection to a remote database or other service, and share +it across function invocations to avoid incurring the overhead of +re-establishing it for every function invocation.

+ +

The best practice for sharing a resource across function invocations is to +initialize it in a FunctionsFramework.on_startup block, and reference it from +global shared data. (As discussed above, the best practice is to initialize +shared resources in a startup task rather than at the top level of a Ruby file, +and to use the Functions Framework's global data mechanism rather than Ruby's +global variables.)

+ +

Here is a simple example:

+ +
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
+
+ +

Importantly, if you do share a resource across function invocations, make sure +the resource is thread-safe, so that separate functions running concurrently in +different threads can access them safely. The API clients provided by Google, +for example, are thread-safe and can be used concurrently.

+ +

Also of note: There is no guaranteed cleanup hook. The Functions Framework does +not provide a way to register a cleanup task, and we recommend against using +resources that require explicit "cleanup". This is because serverless runtimes +may perform CPU throttling, and therefore there may not be an opportunity for +cleanup tasks to run. (For example, you could register a Kernel.at_exit task, +but the Ruby VM may still terminate without calling it.)

+ +

Lazy initialization

+ +

Because startup tasks run during cold start, they could have an impact on your +function's startup latency. You can mitigate this by initializing some globals +lazily. When setting a global, instead of computing and setting the value +directly (e.g. constructing a shared API client object directly), you can +provide a block that describes how to construct it on demand.

+ +

Here is an example using the storage client we saw above.

+ +
require "functions_framework"
+
+# This startup block describes _how_ to initialize a shared client, but
+# does not construct it immediately.
+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
+
+ +

The block will not be called until a function actually attempts to access the +global. From that point, subsequent accesses of the global will return that +same shared value; the block will be called at most once. This is true even if +multiple functions are run concurrently in different threads.

+ +

Lazy initialization is particularly useful if you define several different +functions that may use different sets of shared resources. Instead of +initializing all resources eagerly up front, you could initialize them lazily +and run only the code needed by the function that is actually invoked.

+ +

Structuring a project

+ +

A Functions Framework based "project" or "application" is a typical Ruby +application. It should include a Gemfile that specifies the gem dependencies +(including the functions_framework gem itself), and any other dependencies +needed by the function. It must include at least one Ruby source file that +defines functions, and can also include additional Ruby files defining classes +and methods that assist in the function implementation.

+ +

By convention, the "main" Ruby file that defines functions should be called +app.rb and be located at the root of the project. The path to this file is +sometimes known as the function source. The Functions Framework allows you +to specify an arbitrary source, but some hosting environments (such as Google +Cloud Functions) require it to be ./app.rb.

+ +

A source file can define any number of functions (with distinct names). Each of +the names is known as a function target.

+ +

Following is a typical layout for a Functions Framework based project.

+ +
(project directory)
+|
++- Gemfile
+|
++- app.rb
+|
++- lib/
+|  |
+|  +- hello.rb
+|
++- test/
+   |
+   ...
+
+ +
# Gemfile
+source "https://rubygems.org"
+gem "functions_framework", "~> 0.10"
+
+ +
# app.rb
+require "functions_framework"
+
+FunctionsFramework.on_startup do
+  require_relative "lib/hello"
+end
+
+FunctionsFramework.http "hello" do |request|
+  Hello.new(request).build_response
+end
+
+ +
# lib/hello.rb
+class Hello
+  def initialize request
+    @request = request
+  end
+
+  def build_response
+    "Received request: #{@request.request_method} #{@request.url}\n"
+  end
+end
+
+ +

Next steps

+ +

To learn about writing unit tests for functions, see +Testing Functions.

+ +

To learn how to run your functions in a server, see +Running a Functions Server.

+ +

To learn how to deploy your functions to Google Cloud Functions or Google Cloud +Run, see +Deploying Functions.

+
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/file_list.html b/v0.10.0/file_list.html new file mode 100644 index 00000000..b9d59994 --- /dev/null +++ b/v0.10.0/file_list.html @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + Codestin Search App + + + +
+
+

File List

+ + + +
+ + +
+ + diff --git a/v0.10.0/frames.html b/v0.10.0/frames.html new file mode 100644 index 00000000..2ac156f5 --- /dev/null +++ b/v0.10.0/frames.html @@ -0,0 +1,17 @@ + + + + + Codestin Search App + + + + diff --git a/v0.10.0/index.html b/v0.10.0/index.html new file mode 100644 index 00000000..07da31d9 --- /dev/null +++ b/v0.10.0/index.html @@ -0,0 +1,208 @@ + + + + + +Codestin Search App + + + + + + + + + + + + + + + + + +
+ + +

Functions Framework for Ruby

+ +

The Functions Framework is an open source framework for writing lightweight, +portable Ruby functions that run in a serverless environment. Functions written +to this Framework will run in many different environments, including:

+ + + +

The framework allows you to go from:

+ +
FunctionsFramework.http("hello") do |request|
+  "Hello, world!\n"
+end
+
+ +

To:

+ +
curl http://my-url
+# Output: Hello, world!
+
+ +

Running on a fully-managed or self-managed serverless environment, without +requiring an HTTP server or complicated request handling logic.

+ +

Features

+ +
    +
  • Define named functions using normal Ruby constructs.
  • +
  • Invoke functions in response to requests.
  • +
  • Automatically unmarshal events conforming to the +CloudEvents spec.
  • +
  • Automatically convert most legacy events from Google Cloud services such +as Cloud Pub/Sub and Cloud Storage, to CloudEvents.
  • +
  • Spin up a local development server for quick testing.
  • +
  • Integrate with standard Ruby libraries such as Rack and Minitest.
  • +
  • Portable between serverless platforms.
  • +
  • Supports all non-end-of-life versions of Ruby.
  • +
+ +

Supported Ruby versions

+ +

This library is supported on Ruby 2.5+.

+ +

Google provides official support for Ruby versions that are actively supported +by Ruby Core—that is, Ruby versions that are either in normal maintenance or +in security maintenance, and not end of life. Currently, this means Ruby 2.5 +and later. Older versions of Ruby may still work, but are unsupported and not +recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details +about the Ruby support schedule.

+ +

Quickstart

+ +

Here is how to run a Hello World function on your local machine.

+ +

Create a Gemfile listing the Functions Framework as a dependency:

+ +
# Gemfile
+source "https://rubygems.org"
+gem "functions_framework", "~> 0.10"
+
+ +

Create a file called app.rb and include the following code. This defines a +simple function called "hello".

+ +
# app.rb
+require "functions_framework"
+
+FunctionsFramework.http("hello") do |request|
+  "Hello, world!\n"
+end
+
+ +

Install the bundle, and start the framework. This spins up a local web server +running your "hello" function:

+ +
bundle install
+# ...installs the functions_framework gem and other dependencies
+bundle exec functions-framework-ruby --target hello
+# ...starts the functions server in the foreground
+
+ +

In a separate shell, you can send requests to this function using curl:

+ +
curl http://localhost:8080
+# Output: Hello, world!
+
+ +

Stop the server with CTRL+C.

+ +

Documentation

+ +

These guides provide additional getting-started information.

+ + + +

The library reference documentation can be found at: +https://googlecloudplatform.github.io/functions-framework-ruby

+ +

Additional examples are available in the GitHub repository: +https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/examples/

+ +

Development

+ +

The source for the Ruby Functions Framework is available on GitHub at +https://github.com/GoogleCloudPlatform/functions-framework-ruby. For more +information on the Functions Framework contract implemented by this framework, +as well as links to Functions Frameworks for other languages, see +https://github.com/GoogleCloudPlatform/functions-framework.

+ +

The Functions Framework is open source under the Apache 2.0 license. +Contributions are welcome. Please see the contributing guide at +https://github.com/GoogleCloudPlatform/functions-framework-ruby/blob/master/.github/CONTRIBUTING.md.

+ +

Report issues at +https://github.com/GoogleCloudPlatform/functions-framework-ruby/issues.

+
+ + + +
+ + \ No newline at end of file diff --git a/v0.10.0/js/app.js b/v0.10.0/js/app.js new file mode 100644 index 00000000..8d067fe3 --- /dev/null +++ b/v0.10.0/js/app.js @@ -0,0 +1,314 @@ +(function() { + +var localStorage = {}, sessionStorage = {}; +try { localStorage = window.localStorage; } catch (e) { } +try { sessionStorage = window.sessionStorage; } catch (e) { } + +function createSourceLinks() { + $('.method_details_list .source_code'). + before("[View source]"); + $('.toggleSource').toggle(function() { + $(this).parent().nextAll('.source_code').slideDown(100); + $(this).text("Hide source"); + }, + function() { + $(this).parent().nextAll('.source_code').slideUp(100); + $(this).text("View source"); + }); +} + +function createDefineLinks() { + var tHeight = 0; + $('.defines').after(" more..."); + $('.toggleDefines').toggle(function() { + tHeight = $(this).parent().prev().height(); + $(this).prev().css('display', 'inline'); + $(this).parent().prev().height($(this).parent().height()); + $(this).text("(less)"); + }, + function() { + $(this).prev().hide(); + $(this).parent().prev().height(tHeight); + $(this).text("more..."); + }); +} + +function createFullTreeLinks() { + var tHeight = 0; + $('.inheritanceTree').toggle(function() { + tHeight = $(this).parent().prev().height(); + $(this).parent().toggleClass('showAll'); + $(this).text("(hide)"); + $(this).parent().prev().height($(this).parent().height()); + }, + function() { + $(this).parent().toggleClass('showAll'); + $(this).parent().prev().height(tHeight); + $(this).text("show all"); + }); +} + +function searchFrameButtons() { + $('.full_list_link').click(function() { + toggleSearchFrame(this, $(this).attr('href')); + return false; + }); + window.addEventListener('message', function(e) { + if (e.data === 'navEscape') { + $('#nav').slideUp(100); + $('#search a').removeClass('active inactive'); + $(window).focus(); + } + }); + + $(window).resize(function() { + if ($('#search:visible').length === 0) { + $('#nav').removeAttr('style'); + $('#search a').removeClass('active inactive'); + $(window).focus(); + } + }); +} + +function toggleSearchFrame(id, link) { + var frame = $('#nav'); + $('#search a').removeClass('active').addClass('inactive'); + if (frame.attr('src') === link && frame.css('display') !== "none") { + frame.slideUp(100); + $('#search a').removeClass('active inactive'); + } + else { + $(id).addClass('active').removeClass('inactive'); + if (frame.attr('src') !== link) frame.attr('src', link); + frame.slideDown(100); + } +} + +function linkSummaries() { + $('.summary_signature').click(function() { + document.location = $(this).find('a').attr('href'); + }); +} + +function summaryToggle() { + $('.summary_toggle').click(function(e) { + e.preventDefault(); + localStorage.summaryCollapsed = $(this).text(); + $('.summary_toggle').each(function() { + $(this).text($(this).text() == "collapse" ? "expand" : "collapse"); + var next = $(this).parent().parent().nextAll('ul.summary').first(); + if (next.hasClass('compact')) { + next.toggle(); + next.nextAll('ul.summary').first().toggle(); + } + else if (next.hasClass('summary')) { + var list = $('