Module: Keiyaku

Defined in:
lib/keiyaku/runtime.rb,
lib/keiyaku/names.rb,
lib/keiyaku/emitter.rb,
lib/keiyaku/version.rb,
lib/keiyaku/emitter/rbs.rb,
lib/keiyaku/adapters/http.rb,
lib/keiyaku/adapters/faraday.rb,
lib/keiyaku/emitter/security.rb,
lib/keiyaku/adapters/net_http.rb,
sig/keiyaku.rbs

Overview

A runtime for generated OpenAPI clients.

Everything that is identical across every API lives here; the generated code carries only what is specific to one API. The contract between the two is a single method, Client#__invoke, plus Keiyaku.model.

Defined Under Namespace

Modules: Names, Serialize, _Adapter Classes: ByStatus, CastError, Client, ClientError, ConnectionError, Emitter, Error, FaradayAdapter, HTTPAdapter, HTTPError, Impossible, Model, NetHTTPAdapter, OneOf, ServerError, Unsupported, Upload

Constant Summary collapse

UNSET =

A parameter that was not passed, distinct from one passed as nil.

Returns:

  • (Object)
Object.new
VERSION =

Returns:

  • (String)
"0.1.0"
KEYWORDS =

The words Ruby will not read as a name. Lives here rather than with the generator's other name tables because the generated method body has to know too: a parameter may be named for one of these, and reading it is not a matter of writing it down.

%w[
  BEGIN END alias and begin break case class def defined do else elsif end ensure false for if in module
  next nil not or redo rescue retry return self super then true undef unless until when while yield __FILE__
  __LINE__ __ENCODING__
].freeze
TRANSPORT_ERRORS =

What a transport failure looks like from the stdlib, which an adapter an application wrote itself is likely to let through as-is. Net::OpenTimeout and Net::ReadTimeout are Timeout::Error; Errno::ECONNREFUSED and the rest of Errno are SystemCallError. socket and timeout are required above for these two names, which is all the runtime itself wants from a transport.

[IOError, SystemCallError, SocketError, Timeout::Error].freeze

Class Method Summary collapse

Class Method Details

.camelize(name) ⇒ String

Parameters:

  • (String, Symbol)

Returns:

  • (String)


56
# File 'sig/keiyaku.rbs', line 56

def self.camelize: (String | Symbol) -> String

.coerce(type, value, path) ⇒ Object

Coerce a decoded JSON value into a declared type.

Types are written the way they read: Integer, String, :bool, Time, [Pet] for an array, { String => Pet } for a map, :any to pass through.



61
# File 'sig/keiyaku.rbs', line 61

def self.coerce: (untyped type, untyped value, String path) -> untyped

.dump(value) ⇒ Object

Inverse of #coerce: a Ruby value on its way into a request body.



135
# File 'lib/keiyaku/runtime.rb', line 135

def self.dump: (untyped) -> untyped

.for_status(table, status) ⇒ Object

What a table of responses says about one status. A document keys an entry by the code itself, by one of the ranges it is allowed to write in place of one — "4XX" for every client error — or by default for whatever it did not describe. The narrowest of them answers: a 404 is answered by its own entry before the range's, and by the range's before the catch-all, which is the order the document wrote them in for.



66
# File 'sig/keiyaku.rbs', line 66

def self.for_status: (Hash[untyped, untyped] table, Integer status) -> untyped

.model(fields, required: [], from: {}, open: false) ⇒ Object

Build a value type for one schema.

Pet = Keiyaku.model({ id: Integer, name: String }, required: %i[name])

Returns a Keiyaku::Model subclass, so callers get immutability, #with and pattern matching. Missing optional fields arrive as nil rather than raising, and unknown fields in a response are ignored so that a server adding a field does not break an old client.

The fields are a positional Hash rather than keywords because they are the API's names, not ours: a DIDComm message has a property called from, and as keywords it would have quietly taken the place of the option below it.

open: is what the document's additionalProperties said: false for a schema that named all its properties, true for one that allows any others, or the type it declared for their values.



71
# File 'sig/keiyaku.rbs', line 71

def self.model: (

.snake(name) ⇒ String

Parameters:

  • (String, Symbol)

Returns:

  • (String)


57
# File 'sig/keiyaku.rbs', line 57

def self.snake: (String | Symbol) -> String

.timeouts(timeout) ⇒ Object

timeout: as every adapter takes it: one number for both phases, or { open: 2, read: 10 } for two. They are usually two different patiences — how long to wait to find out a host is not there is not how long to wait for a slow answer — and a client sitting inside somebody else's request has to bound the first one much more tightly than the second.

Raises:

  • (ArgumentError)


90
# File 'lib/keiyaku/runtime.rb', line 90

def self.timeouts: (timeout) -> [Integer | Float, Integer | Float]