Class: WebFunction::Client

Inherits:
BasicObject
Defined in:
lib/web_function/client.rb

Overview

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

#packagePackage (readonly)

The package that this client is wrapping.

Returns:



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.

Parameters:

  • package (Package)

    A package

  • bearer_auth (String) (defaults to: nil)

    The bearer authentication token

  • version (String) (defaults to: nil)

    The API version to use

  • pipelined (Boolean) (defaults to: nil)

    Whether to have the client use call pipelining

Returns:



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.

Parameters:

  • url (String)

    The URL of the package endpoint

  • bearer_auth (String) (defaults to: nil)

    The bearer authentication token

  • version (String) (defaults to: nil)

    The API version to use

  • pipelined (Boolean) (defaults to: false)

    Whether to have the client use call pipelining

Returns:



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.

Parameters:

  • endpoint_name (String)

    The name of the endpoint to call

  • args (Hash) (defaults to: {})

    The arguments to send to the endpoint

Returns:

  • (Object)

    The decoded response returned by the endpoint



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

#methodsObject

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

Returns:

  • (Boolean)


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