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.

Examples:

client = WebFunction::Client.from_package_endpoint("https://api.webfunction.com/package")
client.list_items(a: "b") # => { "c" => "d" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#packageObject (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.

Parameters:

  • url (String)

    The URL of the package endpoint

  • bearer_auth (String) (defaults to: nil)

    The bearer authentication token

  • pipeline_url (String) (defaults to: nil)

    The URL of the pipeline endpoint

  • pipeline (Pipeline) (defaults to: nil)

    The pipeline to use

Returns:

  • (Client)

    A new Client instance



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

#methodsObject

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

Returns:

  • (Boolean)


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