Class: WebFunction::Endpoint
- Inherits:
-
Object
- Object
- WebFunction::Endpoint
- Includes:
- Flaggable
- Defined in:
- lib/web_function/endpoint.rb
Overview
Represents an endpoint as described in a Web Function package.
An endpoint defines an operation that can be performed via a Web Function API. Endpoints declare their name, documentation, arguments (inputs), attributes (outputs), and the possible errors that may occur when invoking them.
Endpoints are described as objects in each package under the "endpoints" key. For more information, see:
This class provides methods for accessing endpoint metadata (name, docs, arguments, attributes, errors) and supports invocation via HTTP.
Typical tasks include:
- Querying endpoint name or documentation
- Enumerating the arguments or attributes definitions
- Invoking the endpoint through HTTP using required inputs
See: https://webfunction.org/endpoint for more details on endpoint structure and contract.
Instance Attribute Summary collapse
-
#client ⇒ Client?
The Client used to invoke this endpoint.
-
#docs ⇒ String
readonly
Documentation for the endpoint.
-
#group ⇒ String?
readonly
A name used to categorize or group similar endpoints together.
-
#name ⇒ String
readonly
The suffix for the endpoint URL, appended to the package's base URL to form the full endpoint URL.
-
#returns ⇒ Array<String>
readonly
The JSON type(s) returned by the endpoint.
Attributes included from Flaggable
Class Method Summary collapse
-
.from_array(endpoints) ⇒ Array<Endpoint>
Creates a new Endpoint from an array of hashes.
-
.from_hash(endpoint) ⇒ Endpoint
Creates a new Endpoint from a hash.
-
.invoke(url, bearer_auth: nil, version: nil, args: {}) ⇒ Object
Invokes an endpoint through HTTP using the given URL, bearer authentication, version, and arguments.
Instance Method Summary collapse
-
#argument(name) ⇒ Argument?
Looks up a single argument by name.
-
#arguments ⇒ Array<Argument>
The arguments required by the endpoint.
-
#attribute(name) ⇒ Attribute?
Looks up a single returned attribute by name.
-
#attributes ⇒ Array<Attribute>
The attributes of the object returned by the endpoint.
-
#bearer_auth? ⇒ Boolean
Whether the endpoint requires authentication via a bearer token, i.e.
-
#call(args = {}) ⇒ Object
Invokes the endpoint through its assigned #client, passing the given arguments.
-
#capture_bearer? ⇒ Boolean
Whether the endpoint returns a bearer token in its response, i.e.
-
#error(code) ⇒ DocumentedError?
Looks up a single endpoint error by its machine-readable code.
-
#errors ⇒ Array<DocumentedError>
The list of errors specific to this endpoint.
-
#initialize(name:, returns:, flags: [], group: nil, docs: nil, arguments: [], attributes: [], errors: []) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#paginated? ⇒ Boolean
Whether the endpoint supports pagination, i.e.
-
#private? ⇒ Boolean
Whether the endpoint is intended for internal use and is not part of the public API, i.e.
Methods included from Flaggable
Constructor Details
#initialize(name:, returns:, flags: [], group: nil, docs: nil, arguments: [], attributes: [], errors: []) ⇒ Endpoint
Returns a new instance of Endpoint.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/web_function/endpoint.rb', line 28 def initialize(name:, returns:, flags: [], group: nil, docs: nil, arguments: [], attributes: [], errors: []) @name = name @returns = Type.parse(returns) @flags = flags @group = group @docs = docs @arguments = arguments.to_h { |a| [a.name, a] } @attributes = attributes.to_h { |a| [a.name, a] } @errors = errors.to_h { |e| [e.code, e] } end |
Instance Attribute Details
#client ⇒ Client?
102 103 104 |
# File 'lib/web_function/endpoint.rb', line 102 def client @client end |
#docs ⇒ String (readonly)
Documentation for the endpoint. It must be formatted as markdown.
135 136 137 |
# File 'lib/web_function/endpoint.rb', line 135 def docs @docs end |
#group ⇒ String? (readonly)
A name used to categorize or group similar endpoints together. This should be used by documentation tools to organize related endpoints.
129 130 131 |
# File 'lib/web_function/endpoint.rb', line 129 def group @group end |
#name ⇒ String (readonly)
The suffix for the endpoint URL, appended to the package's base URL to form the full endpoint URL. Endpoint names are unique within a package; overloading (two endpoints sharing the same name) is not permitted.
109 110 111 |
# File 'lib/web_function/endpoint.rb', line 109 def name @name end |
#returns ⇒ Array<String> (readonly)
The JSON type(s) returned by the endpoint. A non-empty array whose entries are each one of:
- object
- array
- string
- number
- boolean
- null
122 123 124 |
# File 'lib/web_function/endpoint.rb', line 122 def returns @returns end |
Class Method Details
.from_array(endpoints) ⇒ Array<Endpoint>
Creates a new Endpoint from an array of hashes. Uses from_hash under the hood.
90 91 92 93 94 |
# File 'lib/web_function/endpoint.rb', line 90 def from_array(endpoints) Utils.normalize_array endpoints do |endpoint| from_hash(endpoint) end end |
.from_hash(endpoint) ⇒ Endpoint
Creates a new Endpoint from a hash.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/web_function/endpoint.rb', line 59 def from_hash(endpoint) unless endpoint.is_a?(Hash) return end unless endpoint["name"] return end unless endpoint["returns"] return end new( name: endpoint["name"], returns: endpoint["returns"], flags: Utils.normalize_array_of_strings(endpoint["flags"]), group: endpoint["group"], docs: endpoint["docs"].to_s, arguments: Argument.from_array(endpoint["arguments"]), attributes: Attribute.from_array(endpoint["attributes"]), errors: DocumentedError.from_array(endpoint["errors"]), ) end |
.invoke(url, bearer_auth: nil, version: nil, args: {}) ⇒ Object
Invokes an endpoint through HTTP using the given URL, bearer authentication, version, and arguments.
49 50 51 |
# File 'lib/web_function/endpoint.rb', line 49 def invoke(url, bearer_auth: nil, version: nil, args: {}) Request.execute(url, bearer_auth: bearer_auth, version: version, args: args) end |
Instance Method Details
#argument(name) ⇒ Argument?
Looks up a single argument by name.
204 205 206 |
# File 'lib/web_function/endpoint.rb', line 204 def argument(name) @arguments[name.to_s] end |
#arguments ⇒ Array<Argument>
The arguments required by the endpoint. The array is empty when the endpoint requires no arguments.
194 195 196 |
# File 'lib/web_function/endpoint.rb', line 194 def arguments @arguments.values end |
#attribute(name) ⇒ Attribute?
Looks up a single returned attribute by name.
186 187 188 |
# File 'lib/web_function/endpoint.rb', line 186 def attribute(name) @attributes[name.to_s] end |
#attributes ⇒ Array<Attribute>
The attributes of the object returned by the endpoint. Relevant when the endpoint returns an object.
176 177 178 |
# File 'lib/web_function/endpoint.rb', line 176 def attributes @attributes.values end |
#bearer_auth? ⇒ Boolean
Whether the endpoint requires authentication via a bearer token, i.e. whether it declares the bearer_auth flag.
212 213 214 |
# File 'lib/web_function/endpoint.rb', line 212 def bearer_auth? flag?("bearer_auth") end |
#call(args = {}) ⇒ Object
Invokes the endpoint through its assigned #client, passing the given arguments.
145 146 147 148 149 150 151 |
# File 'lib/web_function/endpoint.rb', line 145 def call(args = {}) unless client raise "Client must be set to invoke an endpoint" end client.call(name, args) end |
#capture_bearer? ⇒ Boolean
Whether the endpoint returns a bearer token in its response, i.e. whether it declares the capture_bearer flag.
See the authentication specification for more information.
221 222 223 |
# File 'lib/web_function/endpoint.rb', line 221 def capture_bearer? flag?("capture_bearer") end |
#error(code) ⇒ DocumentedError?
Looks up a single endpoint error by its machine-readable code.
168 169 170 |
# File 'lib/web_function/endpoint.rb', line 168 def error(code) @errors[code.to_s] end |
#errors ⇒ Array<DocumentedError>
The list of errors specific to this endpoint. Clients SHOULD only refer to this list if the endpoint uses the
error_triple flag. See the error specification for more information.
158 159 160 |
# File 'lib/web_function/endpoint.rb', line 158 def errors @errors.values end |
#paginated? ⇒ Boolean
Whether the endpoint supports pagination, i.e. whether it declares the paginated flag.
229 230 231 |
# File 'lib/web_function/endpoint.rb', line 229 def paginated? flag?("paginated") end |
#private? ⇒ Boolean
Whether the endpoint is intended for internal use and is not part of the public API, i.e. whether it declares the
private flag. Documentation tooling SHOULD omit endpoints with this flag from generated or
published documentation.
239 240 241 |
# File 'lib/web_function/endpoint.rb', line 239 def private? flag?("private") end |