Class: Proc::Client

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

Instance Method Summary collapse

Constructor Details

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/proc/client.rb', line 85

def initialize(authorization, scheme: "https", host: "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.



198
199
200
201
202
203
204
# File 'lib/proc/client.rb', line 198

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.



64
65
66
# File 'lib/proc/client.rb', line 64

def authorization
  @authorization
end

#hostObject (readonly)

public

The configured host.



72
73
74
# File 'lib/proc/client.rb', line 72

def host
  @host
end

#request_countObject (readonly)

public

The number of requests this client has performed.



76
77
78
# File 'lib/proc/client.rb', line 76

def request_count
  @request_count
end

#responseObject (readonly)

Returns the value of attribute response.



78
79
80
# File 'lib/proc/client.rb', line 78

def response
  @response
end

#schemeObject (readonly)

public

The configured scheme.



68
69
70
# File 'lib/proc/client.rb', line 68

def scheme
  @scheme
end

Instance Method Details

#[](proc) ⇒ Object

public

Returns a callable context for proc.



99
100
101
102
103
104
105
# File 'lib/proc/client.rb', line 99

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.



212
213
214
# File 'lib/proc/client.rb', line 212

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

#call(proc = nil, input = ::Proc.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.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/proc/client.rb', line 138

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

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

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

  status, headers, payload = get_payload(proc: proc, body: body)

  case status
  when 200
    result = extract_output(payload)

    if (cursor = headers["x-cursor"])
      enumerator = if cursor.empty?
        ::Proc::Enumerator.new(result)
      else
        ::Proc::Enumerator.new(result) {
          arguments[:cursor] = cursor.to_s
          call(proc, input, **arguments)
        }
      end

      if block
        enumerator.each(&block)
      else
        enumerator
      end
    else
      result
    end
  when 400
    ::Kernel.raise ::Proc::Invalid, extract_error_message(payload)
  when 401
    ::Kernel.raise ::Proc::Unauthorized, extract_error_message(payload)
  when 403
    ::Kernel.raise ::Proc::Forbidden, extract_error_message(payload)
  when 404
    ::Kernel.raise ::Proc::Undefined, extract_error_message(payload)
  when 408
    ::Kernel.raise ::Proc::Timeout, extract_error_message(payload)
  when 413
    ::Kernel.raise ::Proc::Invalid, extract_error_message(payload)
  when 429
    ::Kernel.raise ::Proc::Limited, extract_error_message(payload)
  when 500
    ::Kernel.raise ::Proc::Error, extract_error_message(payload)
  when 508
    ::Kernel.raise ::Proc::Error, extract_error_message(payload)
  else
    ::Kernel.raise ::Proc::Error, "unhandled"
  end
end

#rate_limitObject

public

Returns the current rate limit.



109
110
111
112
# File 'lib/proc/client.rb', line 109

def rate_limit
  refresh_rate_limit
  @rate_limit
end

#rate_limit_resetObject

public

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



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

def rate_limit_reset
  refresh_rate_limit
  @rate_limit_reset
end

#rate_limit_windowObject

public

Returns the current rate limit window.



116
117
118
119
# File 'lib/proc/client.rb', line 116

def rate_limit_window
  refresh_rate_limit
  @rate_limit_window
end

#respond_to_missing?(name) ⇒ Boolean



206
207
208
# File 'lib/proc/client.rb', line 206

def respond_to_missing?(name, *)
  true
end