Class: Proc::Client

Inherits:
Object
  • Object
show all
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

Returns a new instance of Client.



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

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.



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

def method_missing(name, input = input_omitted = true, *, **arguments)
  if input_omitted
    Callable.new(name, client: self, arguments: arguments)
  else
    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

#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.



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

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

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

public

Builds a named argument with options.



210
211
212
# File 'lib/proc/client.rb', line 210

def argument(name, **options)
  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.



136
137
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
# File 'lib/proc/client.rb', line 136

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?
        Enumerator.new(result)
      else
        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
    raise Proc::Invalid, extract_error_message(payload)
  when 401
    raise Proc::Unauthorized, extract_error_message(payload)
  when 403
    raise Proc::Forbidden, extract_error_message(payload)
  when 404
    raise Proc::Undefined, extract_error_message(payload)
  when 408
    raise Proc::Timeout, extract_error_message(payload)
  when 413
    raise Proc::Invalid, extract_error_message(payload)
  when 429
    raise Proc::Limited, extract_error_message(payload)
  when 500
    raise Proc::Error, extract_error_message(payload)
  when 508
    raise Proc::Error, extract_error_message(payload)
  else
    raise Proc::Error, "unhandled"
  end
end

#rate_limitObject

public

Returns the current rate limit.



107
108
109
110
# File 'lib/proc/client.rb', line 107

def rate_limit
  refresh_rate_limit
  @rate_limit
end

#rate_limit_resetObject

public

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



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

def rate_limit_reset
  refresh_rate_limit
  @rate_limit_reset
end

#rate_limit_windowObject

public

Returns the current rate limit window.



114
115
116
117
# File 'lib/proc/client.rb', line 114

def rate_limit_window
  refresh_rate_limit
  @rate_limit_window
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/proc/client.rb', line 204

def respond_to_missing?(name, *)
  true
end