Class: WebFunction::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/web_function/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package, bearer_auth: nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
# File 'lib/web_function/client.rb', line 5

def initialize(package, bearer_auth: nil)
  @package = package
  @endpoints = package.endpoints.to_h { |e| [e.name.gsub("-", "_").to_sym, e] }
  @bearer_auth = bearer_auth
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/web_function/client.rb', line 28

def method_missing(method_name, *args)
  endpoint = @endpoints[method_name]

  unless endpoint
    super
  end

  url = URI.join(@package.base_url, endpoint.name).to_s
  args = args.first

  Endpoint.invoke(url, bearer_auth: @bearer_auth, args: args)
end

Instance Attribute Details

#packageObject (readonly)

Returns the value of attribute package.



11
12
13
# File 'lib/web_function/client.rb', line 11

def package
  @package
end

Class Method Details

.from_package_endpoint(url, bearer_auth: nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/web_function/client.rb', line 13

def self.from_package_endpoint(url, bearer_auth: nil)
  response = Endpoint.invoke(url, bearer_auth: bearer_auth)
  package = Package.new(response)

  new(package, bearer_auth: bearer_auth)
end

Instance Method Details

#methodsObject



20
21
22
# File 'lib/web_function/client.rb', line 20

def methods
  super + @endpoints.keys
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/web_function/client.rb', line 24

def respond_to_missing?(method_name, include_private = false)
  @endpoints[method_name] || super
end