Class: Proc::Client

Inherits:
BasicObject
Includes:
Is::Async, Is::Global, Is::Inspectable
Defined in:
lib/proc/client.rb

Overview

public

Connection to proc, configured with an authorization.

Constant Summary collapse

DEFAULT_HEADERS =
{
  "accept" => "application/vnd.proc+msgpack",
  "content-type" => "application/vnd.proc+msgpack"
}.freeze
IGNORE_MISSING =
%i[
].freeze
KERNEL_DELEGATE =
%i[
  class
  instance_variables
  instance_variable_get
  instance_variable_set
  object_id
  public_send
  respond_to?
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization = ::Proc::Client.authorization, scheme: "https", host: "proc.run") ⇒ Client

Returns a new instance of Client.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/proc/client.rb', line 106

def initialize(authorization = ::Proc::Client.authorization, scheme: "https", host: "proc.run")
  @authorization = authorization
  @scheme = scheme
  @host = host
  @count = 0

  @__base_url = "#{@scheme}://#{host}"
  @__headers = {
    "authorization" => "bearer #{@authorization}"
  }.merge(DEFAULT_HEADERS)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, input = input_omitted = true, *parameters, **arguments, &block) ⇒ Object

public

Allows callable contexts to be built through method lookups.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/proc/client.rb', line 238

def method_missing(name, input = input_omitted = true, *parameters, **arguments, &block)
  if IGNORE_MISSING.include?(name)
    super
  elsif KERNEL_DELEGATE.include?(name)
    if input_omitted
      ::Kernel.instance_method(name).bind_call(self, *parameters, **arguments, &block)
    else
      ::Kernel.instance_method(name).bind_call(self, input, *parameters, **arguments, &block)
    end
  elsif input_omitted
    ::Proc::Callable.new(name, client: self, arguments: arguments)
  else
    ::Proc::Callable.new(name, client: self, input: input, arguments: arguments)
  end
end

Instance Attribute Details

#authorizationObject (readonly)

public

The configured authorization.



85
86
87
# File 'lib/proc/client.rb', line 85

def authorization
  @authorization
end

#countObject (readonly)

public

The number of requests this client has performed.



97
98
99
# File 'lib/proc/client.rb', line 97

def count
  @count
end

#hostObject (readonly)

public

The configured host.



93
94
95
# File 'lib/proc/client.rb', line 93

def host
  @host
end

#responseObject (readonly)

Returns the value of attribute response.



99
100
101
# File 'lib/proc/client.rb', line 99

def response
  @response
end

#schemeObject (readonly)

public

The configured scheme.



89
90
91
# File 'lib/proc/client.rb', line 89

def scheme
  @scheme
end

Class Method Details

.authorizationObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/proc/client.rb', line 65

def authorization
  ::ENV.fetch("PROC_AUTH") {
    auth_file_path = ::Pathname.new("~/.proc/auth").expand_path

    if auth_file_path.exist?
      auth_file_path.read
    else
      ""
    end
  }.strip
end

Instance Method Details

#[](proc) ⇒ Object

public

Returns a callable context for ‘proc`.



120
121
122
123
124
125
126
# File 'lib/proc/client.rb', line 120

def [](proc)
  if ::Kernel.block_given?
    ::Proc::Callable.new(proc, client: self, arguments: {proc: yield})
  else
    ::Proc::Callable.new(proc, client: self)
  end
end

#argument(name, **options) ⇒ Object Also known as: arg

public

Builds a named argument with options.



260
261
262
# File 'lib/proc/client.rb', line 260

def argument(name, **options)
  ::Proc::Composer::Argument.new(name, **options)
end

#call(proc = nil, input = ::Proc::Composer.undefined, **arguments, &block) ⇒ Object

public

Calls a proc with the given input and arguments.

If a block is passed and the proc returns an enumerable, the block will be called with each value.



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/proc/client.rb', line 159

def call(proc = nil, input = ::Proc::Composer.undefined, **arguments, &block)
  body = []

  unless ::Proc::Composer.undefined?(input)
    body << [">>", serialize_value(input)]
  end

  arguments.each_pair do |key, value|
    body << ["$$", key.to_s, serialize_value(value)]
  end

  process(proc: proc, body: body, input: input, arguments: arguments, &block)
end

#rate_limitObject

public

Returns the current rate limit.



130
131
132
133
# File 'lib/proc/client.rb', line 130

def rate_limit
  refresh_rate_limit
  @rate_limit
end

#rate_limit_resetObject

public

Returns the time at which the current rate limit will reset.



144
145
146
147
# File 'lib/proc/client.rb', line 144

def rate_limit_reset
  refresh_rate_limit
  @rate_limit_reset
end

#rate_limit_windowObject

public

Returns the current rate limit window.



137
138
139
140
# File 'lib/proc/client.rb', line 137

def rate_limit_window
  refresh_rate_limit
  @rate_limit_window
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/proc/client.rb', line 254

def respond_to_missing?(name, *)
  true
end

#safe_authorizationObject

public

Returns a partial representation of the authorization that is safe to include in logs.



267
268
269
270
# File 'lib/proc/client.rb', line 267

def safe_authorization
  return unless @authorization
  "#{@authorization[0..7]}...#{@authorization[-5..]}"
end