Module: DatabasusRuby::Endpoint
- Defined in:
- lib/databasus_ruby/endpoint.rb,
sig/databasus_ruby.rbs
Overview
Shared plumbing for the per-domain endpoint classes. Each domain is a single class (DatabasusRuby::Databases, DatabasusRuby::Backups, ...) that inherits Object and includes this module, so it carries both the endpoint methods and the payload-reading behaviour.
The transport helpers are prefixed http_ so domain classes are free to
expose the natural #get / #create / #update / #delete names.
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#acknowledged(_response) ⇒ Boolean
For endpoints that answer 200/204 with no meaningful body.
-
#build(klass, attributes) ⇒ Object
Records need the client to reach their related endpoints; plain payload objects do not.
-
#collection(response, key:, as: Object) ⇒ Collection
Wraps a list response;
keynames the array inside an envelope payload. -
#compact(body) ⇒ Object
Request bodies are built from keyword arguments, and the API distinguishes an absent key from a null one in a few places, so nils are dropped.
- #escape(segment) ⇒ String
- #http_delete(path, params = nil) ⇒ Object
- #http_get(path, params = nil) ⇒ Object
- #http_post(path, body = nil, params: nil) ⇒ Object
- #http_put(path, body = nil, params: nil) ⇒ Object
-
#initialize(client) ⇒ Endpoint
A new instance of Endpoint.
- #inspect ⇒ String
-
#object(response, as: Object) ⇒ Object
Wraps a single-object response.
-
#single_value(response) ⇒ Object
Endpoints answering a single-entry map whose key is not documented in the swagger schema, e.g.
-
#with_default_workspace(attributes) ⇒ payload
Fills in the client's default workspace for create/save payloads that did not name one explicitly.
Instance Attribute Details
#client ⇒ Client (readonly)
Returns the value of attribute client.
14 15 16 |
# File 'lib/databasus_ruby/endpoint.rb', line 14 def client @client end |
Instance Method Details
#acknowledged(_response) ⇒ Boolean
For endpoints that answer 200/204 with no meaningful body.
64 65 66 |
# File 'lib/databasus_ruby/endpoint.rb', line 64 def acknowledged(_response) true end |
#build(klass, attributes) ⇒ Object
Records need the client to reach their related endpoints; plain payload objects do not.
59 60 61 |
# File 'lib/databasus_ruby/endpoint.rb', line 59 def build(klass, attributes) klass <= Record ? klass.new(client, attributes) : klass.new(attributes) end |
#collection(response, key:, as: Object) ⇒ Collection
Wraps a list response; key names the array inside an envelope payload.
53 54 55 |
# File 'lib/databasus_ruby/endpoint.rb', line 53 def collection(response, key:, as: Object) Collection.from(response.body, key: key, wrap: ->(item) { build(as, item) }) end |
#compact(body) ⇒ Object
Request bodies are built from keyword arguments, and the API distinguishes an absent key from a null one in a few places, so nils are dropped.
77 78 79 80 81 82 83 |
# File 'lib/databasus_ruby/endpoint.rb', line 77 def compact(body) return body unless body.is_a?(Hash) body.each_with_object({}) do |(key, value), acc| acc[key.to_s] = value unless value.nil? end end |
#escape(segment) ⇒ String
94 95 96 |
# File 'lib/databasus_ruby/endpoint.rb', line 94 def escape(segment) URI.encode_uri_component(require_id(segment)) end |
#http_delete(path, params = nil) ⇒ Object
42 43 44 |
# File 'lib/databasus_ruby/endpoint.rb', line 42 def http_delete(path, params = nil) client.request(:delete, path, params: params) end |
#http_get(path, params = nil) ⇒ Object
30 31 32 |
# File 'lib/databasus_ruby/endpoint.rb', line 30 def http_get(path, params = nil) client.request(:get, path, params: params) end |
#http_post(path, body = nil, params: nil) ⇒ Object
34 35 36 |
# File 'lib/databasus_ruby/endpoint.rb', line 34 def http_post(path, body = nil, params: nil) client.request(:post, path, body: compact(body), params: params) end |
#http_put(path, body = nil, params: nil) ⇒ Object
38 39 40 |
# File 'lib/databasus_ruby/endpoint.rb', line 38 def http_put(path, body = nil, params: nil) client.request(:put, path, body: compact(body), params: params) end |
#initialize(client) ⇒ Endpoint
Returns a new instance of Endpoint.
16 17 18 19 20 21 22 |
# File 'lib/databasus_ruby/endpoint.rb', line 16 def initialize(client) @client = client # Endpoint objects are callers, not payloads; leaving @attributes nil # makes Object#method_missing raise NoMethodError on a typo instead of # quietly answering nil. @attributes = nil end |
#inspect ⇒ String
24 25 26 |
# File 'lib/databasus_ruby/endpoint.rb', line 24 def inspect "#<#{self.class.name}>" end |
#object(response, as: Object) ⇒ Object
Wraps a single-object response. as names a Record subclass for the
domains whose payloads carry methods of their own.
48 49 50 |
# File 'lib/databasus_ruby/endpoint.rb', line 48 def object(response, as: Object) build(as, response.body.is_a?(Hash) ? response.body : {}) end |
#single_value(response) ⇒ Object
Endpoints answering a single-entry map whose key is not documented in the swagger schema, e.g. GET /databases/notifier/id/is-using.
70 71 72 73 |
# File 'lib/databasus_ruby/endpoint.rb', line 70 def single_value(response) body = response.body body.is_a?(Hash) ? body.values.first : body end |
#with_default_workspace(attributes) ⇒ payload
Fills in the client's default workspace for create/save payloads that did not name one explicitly.
87 88 89 90 91 92 |
# File 'lib/databasus_ruby/endpoint.rb', line 87 def with_default_workspace(attributes) return attributes if attributes.key?(:workspaceId) || attributes.key?("workspaceId") return attributes if client.workspace_id.nil? attributes.merge(workspaceId: client.workspace_id) end |