Class: WebFunction::Client
- Inherits:
- BasicObject
- Defined in:
- lib/web_function/client.rb
Overview
Instance Attribute Summary collapse
-
#package ⇒ Package
readonly
The package that this client is wrapping.
Class Method Summary collapse
- .from_package(package, bearer_auth: nil, version: nil, pipelined: nil) ⇒ Client
-
.from_package_endpoint(url, bearer_auth: nil, version: nil, pipelined: false) ⇒ Client
Creates a new Client from an url.
Instance Method Summary collapse
-
#call(endpoint_name, args = {}) ⇒ Object
Call an endpoint by name with the given arguments.
-
#initialize(base_url:, endpoints: [], package: nil, bearer_auth: nil, version: 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(base_url:, endpoints: [], package: nil, bearer_auth: nil, version: nil, pipeline: nil) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 |
# File 'lib/web_function/client.rb', line 11 def initialize(base_url:, endpoints: [], package: nil, bearer_auth: nil, version: nil, pipeline: nil) @package = package @base_url = base_url @endpoints = endpoints.to_h { |e| [e.gsub("-", "_").to_sym, e] } @bearer_auth = bearer_auth @version = version @pipeline = pipeline end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
:nodoc:
106 107 108 109 110 111 112 113 114 |
# File 'lib/web_function/client.rb', line 106 def method_missing(method_name, *args) # :nodoc: endpoint_name = @endpoints[method_name] unless endpoint_name super end call(endpoint_name, args.first) end |
Instance Attribute Details
#package ⇒ Package (readonly)
The package that this client is wrapping.
96 97 98 |
# File 'lib/web_function/client.rb', line 96 def package @package end |
Class Method Details
.from_package(package, bearer_auth: nil, version: nil, pipelined: nil) ⇒ Client
Creates a new WebFunction::Client from a Package.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/web_function/client.rb', line 46 def from_package(package, bearer_auth: nil, version: nil, pipelined: nil) pipeline = nil if pipelined pipeline = package.pipeline end client = new( package: package, base_url: package.base_url, endpoints: package.endpoints.map(&:name), bearer_auth: bearer_auth, version: version, pipeline: pipeline, ) package.endpoints.each do |endpoint| endpoint.client = client end client end |
.from_package_endpoint(url, bearer_auth: nil, version: nil, pipelined: false) ⇒ Client
Creates a new WebFunction::Client from an url.
30 31 32 33 34 35 |
# File 'lib/web_function/client.rb', line 30 def from_package_endpoint(url, bearer_auth: nil, version: nil, pipelined: false) response = ::WebFunction::Request.execute(url, bearer_auth: bearer_auth, version: version) package = ::WebFunction::Package.from_hash(response) from_package(package, bearer_auth: bearer_auth, version: version, pipelined: pipelined) end |
Instance Method Details
#call(endpoint_name, args = {}) ⇒ Object
Call an endpoint by name with the given arguments.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/web_function/client.rb', line 77 def call(endpoint_name, args = {}) url = ::URI.join(@base_url, endpoint_name).to_s request = ::WebFunction::Request.new(url, bearer_auth: @bearer_auth, version: @version, args: args, ) if @pipeline @pipeline.add_step(request.as_pipeline_step) else request.execute end end |
#methods ⇒ Object
:nodoc:
98 99 100 |
# File 'lib/web_function/client.rb', line 98 def methods # :nodoc: @endpoints.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:nodoc:
102 103 104 |
# File 'lib/web_function/client.rb', line 102 def respond_to_missing?(method_name, include_private = false) # :nodoc: @endpoints[method_name] end |