Class: WebFunction::Client
- Inherits:
- BasicObject
- Defined in:
- lib/web_function/client.rb
Overview
# Client
A Client is a wrapper around a Web Function package that provides a convenient interface for invoking endpoints.
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Class Method Summary collapse
-
.from_package_endpoint(url, bearer_auth: nil, pipeline_url: nil, pipeline: nil) ⇒ Client
## Instantiates a new Client from a package endpoint.
Instance Method Summary collapse
-
#initialize(package, bearer_auth: nil, pipeline: nil) ⇒ Client
constructor
A new instance of Client.
-
#method_missing(method_name, *args) ⇒ Object
:nodoc:.
-
#methods ⇒ Object
:nodoc:.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(package, bearer_auth: nil, pipeline: nil) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 |
# File 'lib/web_function/client.rb', line 14 def initialize(package, bearer_auth: nil, pipeline: nil) @package = package @endpoints = package.endpoints.to_h { |e| [e.name.gsub("-", "_").to_sym, e] } @bearer_auth = bearer_auth @pipeline = pipeline end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
:nodoc:
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/web_function/client.rb', line 53 def method_missing(method_name, *args) #:nodoc: endpoint = @endpoints[method_name] unless endpoint super end url = ::URI.join(@package.base_url, endpoint.name).to_s args = args.first if @pipeline step = ::WebFunction::Endpoint.step(url, bearer_auth: @bearer_auth, args: args) promise = @pipeline.add_step(step) return promise end ::WebFunction::Endpoint.invoke(url, bearer_auth: @bearer_auth, args: args) end |
Instance Attribute Details
#package ⇒ Object (readonly)
Returns the value of attribute package.
21 22 23 |
# File 'lib/web_function/client.rb', line 21 def package @package end |
Class Method Details
.from_package_endpoint(url, bearer_auth: nil, pipeline_url: nil, pipeline: nil) ⇒ Client
## Instantiates a new Client from a package endpoint
Creates a new Client from a package endpoint.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/web_function/client.rb', line 34 def self.from_package_endpoint(url, bearer_auth: nil, pipeline_url: nil, pipeline: nil) response = ::WebFunction::Endpoint.invoke(url, bearer_auth: bearer_auth) package = ::WebFunction::Package.new(response) if pipeline_url.is_a?(::String) pipeline = ::WebFunction::Pipeline.new(pipeline_url) end new(package, bearer_auth: bearer_auth, pipeline: pipeline) end |
Instance Method Details
#methods ⇒ Object
:nodoc:
45 46 47 |
# File 'lib/web_function/client.rb', line 45 def methods #:nodoc: super + @endpoints.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:nodoc:
49 50 51 |
# File 'lib/web_function/client.rb', line 49 def respond_to_missing?(method_name, include_private = false) #:nodoc: @endpoints[method_name] || super end |