Module: DatabasusRuby::Endpoint

Included in:
Auth, Backups, Databases, Notifiers, Storages, Users, Workspaces
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

Instance Method Summary collapse

Instance Attribute Details

#clientClient (readonly)

Returns the value of attribute client.

Returns:



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.

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

  • klass (singleton(Object))
  • attributes (payload)

Returns:



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.

Parameters:

  • response (Object)
  • key: (String)
  • as: (singleton(Object)) (defaults to: Object)

Returns:



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.

Parameters:

Returns:



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

Parameters:

Returns:

  • (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

Parameters:

  • path (String)
  • params (payload, nil) (defaults to: nil)

Returns:



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

Parameters:

  • path (String)
  • params (payload, nil) (defaults to: nil)

Returns:



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

Parameters:

  • path (String)
  • body (payload, nil) (defaults to: nil)
  • params: (payload, nil) (defaults to: nil)

Returns:



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

Parameters:

  • path (String)
  • body (payload, nil) (defaults to: nil)
  • params: (payload, nil) (defaults to: nil)

Returns:



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.

Parameters:

Returns:

  • (Endpoint)

    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

#inspectString

Returns:

  • (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.

Parameters:

  • response (Object)
  • as: (singleton(Object)) (defaults to: Object)

Returns:



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.

Parameters:

Returns:



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.

Parameters:

  • attributes (payload)

Returns:

  • (payload)


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