Class: Proc::Client

Inherits:
BasicObject
Includes:
Is::Async, Is::Global
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/proc/client.rb', line 103

def initialize(authorization = ::Proc::Client.authorization, scheme: "https", host: "api.proc.dev")
  @authorization = authorization
  @scheme = scheme
  @host = host
  @request_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, **arguments) ⇒ Object

public

Allows callable contexts to be built through method lookups.



220
221
222
223
224
225
226
# File 'lib/proc/client.rb', line 220

def method_missing(name, input = input_omitted = true, *, **arguments)
  if 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.



82
83
84
# File 'lib/proc/client.rb', line 82

def authorization
  @authorization
end

#hostObject (readonly)

public

The configured host.



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

def host
  @host
end

#request_countObject (readonly)

public

The number of requests this client has performed.



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

def request_count
  @request_count
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#schemeObject (readonly)

public

The configured scheme.



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

def scheme
  @scheme
end

Class Method Details

.authorizationObject



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

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.



117
118
119
120
121
122
123
# File 'lib/proc/client.rb', line 117

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.



234
235
236
# File 'lib/proc/client.rb', line 234

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.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/proc/client.rb', line 156

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.



127
128
129
130
# File 'lib/proc/client.rb', line 127

def rate_limit
  refresh_rate_limit
  @rate_limit
end

#rate_limit_resetObject

public

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



141
142
143
144
# File 'lib/proc/client.rb', line 141

def rate_limit_reset
  refresh_rate_limit
  @rate_limit_reset
end

#rate_limit_windowObject

public

Returns the current rate limit window.



134
135
136
137
# File 'lib/proc/client.rb', line 134

def rate_limit_window
  refresh_rate_limit
  @rate_limit_window
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/proc/client.rb', line 228

def respond_to_missing?(name, *)
  true
end