databasus_ruby 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9223ce08d06f8a4f29e700d9046bab99ee5aaec01131af37a12620cdec27b7ed
4
- data.tar.gz: 1cd2c4226183d6106dd8a351e61533c2bdf513f818bb0e43f4c3dd077fe548b0
3
+ metadata.gz: bad2c73042db7ba0a3f3ee861359422ee14dc8a842250138e7320fe0c1f40b57
4
+ data.tar.gz: 963517d89578177d4f48e06e3be5c146ee973c0ca2a9163cda80bd2262d0d630
5
5
  SHA512:
6
- metadata.gz: f4c7dda68f74e63dc5833e41b2aff40c730f5462920bc8aaee57549a0b435516d187d07bd6e62591d7ef10b03f863d2492d5754da422021089bcaa4226d26fe2
7
- data.tar.gz: 5c59d0c7e223f8904a54de677f5083c9cd94466e16e60d082b02ef42124784e52f28c09922fbe33542bf5e7b5f5c07c3b2e7a21a5d675e96e46cda29715ba7e7
6
+ metadata.gz: 990c3c288bce649ac4f8ca2db43b90197bdaeb0e0e3ad34b71aadfc71db58099d1c534fcdd82c2167b1f5b6d6f3b44f1077378447f8611e37faa0d22ddcbfd17
7
+ data.tar.gz: e405fe4b643aceec693cdc9b6a1a8f3261a94022271428b3aaa660ae8cb5bcf1a9aa3e4fc726570edd7fd489d64c1671bd7256c198cd6060f400dbccddf6fbdb
data/README.md CHANGED
@@ -109,10 +109,18 @@ A database can reach its own backups, which delegates to the `backups` endpoint
109
109
  with the same filters as `client.backups.list`:
110
110
 
111
111
  ```ruby
112
+ database.backups(status: "COMPLETED", limit: 25)
113
+ database.backup # queue a run
114
+
115
+ # or by id, from the endpoint
112
116
  client.databases.backups(database.id, status: "COMPLETED", limit: 25)
113
- client.databases.backup(database.id) # queue a run
117
+ client.databases.backup(database.id)
114
118
  ```
115
119
 
120
+ `list`, `get`, `create`, `update` and `copy` return `DatabasusRuby::Database`
121
+ records: payload objects that read exactly like any other response but also
122
+ carry the two methods above.
123
+
116
124
  `update` posts to `/databases/update` with the id in the body, matching the API.
117
125
  There is also `test_connection_direct`, `create_readonly_user`,
118
126
  `create_replication_only_user` and `readonly?` for a payload you have not saved
@@ -13,8 +13,10 @@ module DatabasusRuby
13
13
 
14
14
  attr_reader :data, :total, :limit, :offset
15
15
 
16
- def initialize(data: [], total: nil, limit: nil, offset: nil)
17
- @data = data.map { |item| item.is_a?(Object) ? item : Object.new(item) }
16
+ # `wrap` builds one entry from its raw hash; it defaults to a plain Object
17
+ # and endpoints pass a Record builder instead.
18
+ def initialize(data: [], total: nil, limit: nil, offset: nil, wrap: nil)
19
+ @data = data.map { |item| item.is_a?(Object) ? item : (wrap || Object.method(:new)).call(item) }
18
20
  @total = total || @data.size
19
21
  @limit = limit
20
22
  @offset = offset
@@ -22,12 +24,12 @@ module DatabasusRuby
22
24
 
23
25
  # `key` names the array inside an envelope response and is ignored for
24
26
  # bare-array responses.
25
- def self.from(body, key:)
27
+ def self.from(body, key:, wrap: nil)
26
28
  case body
27
29
  when ::Array
28
- new(data: body)
30
+ new(data: body, wrap: wrap)
29
31
  when Hash
30
- new(data: body[key] || [], total: body["total"], limit: body["limit"], offset: body["offset"])
32
+ new(data: body[key] || [], total: body["total"], limit: body["limit"], offset: body["offset"], wrap: wrap)
31
33
  when nil
32
34
  new
33
35
  else
@@ -43,14 +43,21 @@ module DatabasusRuby
43
43
  client.request(:delete, path, params: params)
44
44
  end
45
45
 
46
- # Wraps a single-object response.
47
- def object(response)
48
- Object.new(response.body.is_a?(Hash) ? response.body : {})
46
+ # Wraps a single-object response. `as` names a Record subclass for the
47
+ # domains whose payloads carry methods of their own.
48
+ def object(response, as: Object)
49
+ build(as, response.body.is_a?(Hash) ? response.body : {})
49
50
  end
50
51
 
51
52
  # Wraps a list response; `key` names the array inside an envelope payload.
52
- def collection(response, key:)
53
- Collection.from(response.body, key: key)
53
+ def collection(response, key:, as: Object)
54
+ Collection.from(response.body, key: key, wrap: ->(item) { build(as, item) })
55
+ end
56
+
57
+ # Records need the client to reach their related endpoints; plain payload
58
+ # objects do not.
59
+ def build(klass, attributes)
60
+ klass <= Record ? klass.new(client, attributes) : klass.new(attributes)
54
61
  end
55
62
 
56
63
  # For endpoints that answer 200/204 with no meaningful body.
@@ -18,24 +18,25 @@ module DatabasusRuby
18
18
 
19
19
  # GET /databases
20
20
  def list(workspace_id: nil)
21
- collection(http_get("databases", { workspace_id: workspace_id || client.workspace_id! }), key: "databases")
21
+ collection(http_get("databases", { workspace_id: workspace_id || client.workspace_id! }),
22
+ key: "databases", as: Database)
22
23
  end
23
24
  alias all list
24
25
 
25
26
  # GET /databases/{id}
26
27
  def get(id)
27
- object(http_get("databases/#{escape(id)}"))
28
+ object(http_get("databases/#{escape(id)}"), as: Database)
28
29
  end
29
30
  alias find get
30
31
 
31
32
  # POST /databases/create
32
33
  def create(**attributes)
33
- object(http_post("databases/create", with_default_workspace(attributes)))
34
+ object(http_post("databases/create", with_default_workspace(attributes)), as: Database)
34
35
  end
35
36
 
36
37
  # POST /databases/update — the id travels in the body, not the path.
37
38
  def update(**attributes)
38
- object(http_post("databases/update", attributes))
39
+ object(http_post("databases/update", attributes), as: Database)
39
40
  end
40
41
 
41
42
  # DELETE /databases/{id}
@@ -45,7 +46,7 @@ module DatabasusRuby
45
46
 
46
47
  # POST /databases/{id}/copy
47
48
  def copy(id)
48
- object(http_post("databases/#{escape(id)}/copy"))
49
+ object(http_post("databases/#{escape(id)}/copy"), as: Database)
49
50
  end
50
51
 
51
52
  # POST /databases/{id}/test-connection — true when the database answers.
@@ -78,9 +79,8 @@ module DatabasusRuby
78
79
  readonly(**attributes).isReadOnly == true
79
80
  end
80
81
 
81
- # The backups of one database, so a caller holding a database does not have
82
- # to reach back for the other endpoint. Both delegate to Backups, which
83
- # owns the requests.
82
+ # The backups of one database by id. A database returned by this endpoint
83
+ # answers the same two methods without the id see Database.
84
84
  #
85
85
  # client.databases.backups(database.id, status: "COMPLETED")
86
86
  # client.databases.backup(database.id) # queue a run
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabasusRuby
4
+ # A payload that remembers the client it came from, so the record can reach
5
+ # the endpoints related to it:
6
+ #
7
+ # client.databases.get(id).backups(status: "COMPLETED")
8
+ #
9
+ # Plain Object stays the wrapper for nested fields and for responses that
10
+ # have nothing hanging off them; Record exists only for the payloads whose
11
+ # domain has related endpoints.
12
+ class Record < Object
13
+ attr_reader :client
14
+
15
+ def initialize(client, attributes = {})
16
+ @client = client
17
+ super(attributes)
18
+ end
19
+
20
+ private
21
+
22
+ # Related endpoints address the record by id, and the API omits empty
23
+ # fields, so a record built from a partial payload has to say so rather
24
+ # than sending a request with a nil id.
25
+ def id!
26
+ value = self["id"]
27
+ raise Error, "this #{self.class.name} has no id" if value.nil? || value.to_s.empty?
28
+
29
+ value
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabasusRuby
4
+ # One database, as returned by the Databases endpoint.
5
+ #
6
+ # database = client.databases.get(id)
7
+ # database.backups(status: "COMPLETED")
8
+ # database.backup # queue a run
9
+ #
10
+ # Both delegate to the Backups endpoint, which owns the requests.
11
+ class Database < Record
12
+ # Takes the same filters as Backups#list.
13
+ def backups(**filters)
14
+ client.backups.list(database_id: id!, **filters)
15
+ end
16
+
17
+ def backup
18
+ client.backups.create(database_id: id!)
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DatabasusRuby
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
@@ -16,6 +16,7 @@ module DatabasusRuby
16
16
  autoload :Collection, "databasus_ruby/collection"
17
17
  autoload :Endpoint, "databasus_ruby/endpoint"
18
18
  autoload :Object, "databasus_ruby/object"
19
+ autoload :Record, "databasus_ruby/record"
19
20
 
20
21
  autoload :Error, "databasus_ruby/error"
21
22
  autoload :ConfigurationError, "databasus_ruby/error"
@@ -35,4 +36,6 @@ module DatabasusRuby
35
36
  autoload :Storages, "databasus_ruby/objects/storages"
36
37
  autoload :Users, "databasus_ruby/objects/users"
37
38
  autoload :Workspaces, "databasus_ruby/objects/workspaces"
39
+
40
+ autoload :Database, "databasus_ruby/records/database"
38
41
  end
@@ -58,6 +58,21 @@ module DatabasusRuby
58
58
  def respond_to_missing?: (Symbol name, ?bool include_private) -> bool
59
59
  end
60
60
 
61
+ class Record < Object
62
+ attr_reader client: Client
63
+
64
+ def initialize: (Client client, ?payload attributes) -> void
65
+
66
+ private
67
+
68
+ def id!: () -> String
69
+ end
70
+
71
+ class Database < Record
72
+ def backups: (**untyped) -> Collection
73
+ def backup: () -> Object
74
+ end
75
+
61
76
  class Collection
62
77
  include Enumerable[Object]
63
78
 
@@ -66,8 +81,14 @@ module DatabasusRuby
66
81
  attr_reader limit: Integer?
67
82
  attr_reader offset: Integer?
68
83
 
69
- def initialize: (?data: Array[payload | Object], ?total: Integer?, ?limit: Integer?, ?offset: Integer?) -> void
70
- def self.from: (untyped body, key: String) -> Collection
84
+ def initialize: (
85
+ ?data: Array[payload | Object],
86
+ ?total: Integer?,
87
+ ?limit: Integer?,
88
+ ?offset: Integer?,
89
+ ?wrap: ^(payload) -> Object | nil
90
+ ) -> void
91
+ def self.from: (untyped body, key: String, ?wrap: (^(payload) -> Object)?) -> Collection
71
92
  def each: () { (Object) -> void } -> Collection
72
93
  | () -> Enumerator[Object, Array[Object]]
73
94
  def size: () -> Integer
@@ -93,8 +114,9 @@ module DatabasusRuby
93
114
  def http_post: (String path, ?payload? body, ?params: payload?) -> untyped
94
115
  def http_put: (String path, ?payload? body, ?params: payload?) -> untyped
95
116
  def http_delete: (String path, ?payload? params) -> untyped
96
- def object: (untyped response) -> Object
97
- def collection: (untyped response, key: String) -> Collection
117
+ def object: (untyped response, ?as: singleton(Object)) -> Object
118
+ def collection: (untyped response, key: String, ?as: singleton(Object)) -> Collection
119
+ def build: (singleton(Object) klass, payload attributes) -> Object
98
120
  def acknowledged: (untyped response) -> bool
99
121
  def single_value: (untyped response) -> untyped
100
122
  def compact: (untyped body) -> untyped
@@ -188,12 +210,12 @@ module DatabasusRuby
188
210
 
189
211
  def list: (?workspace_id: String?) -> Collection
190
212
  def all: (?workspace_id: String?) -> Collection
191
- def get: (String id) -> Object
192
- def find: (String id) -> Object
193
- def create: (**untyped) -> Object
194
- def update: (**untyped) -> Object
213
+ def get: (String id) -> Database
214
+ def find: (String id) -> Database
215
+ def create: (**untyped) -> Database
216
+ def update: (**untyped) -> Database
195
217
  def delete: (String id) -> bool
196
- def copy: (String id) -> Object
218
+ def copy: (String id) -> Database
197
219
  def test_connection: (String id) -> bool
198
220
  def test_connection_direct: (**untyped) -> bool
199
221
  def create_readonly_user: (**untyped) -> Object
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: databasus_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SamuelFCorrea
@@ -51,6 +51,8 @@ files:
51
51
  - lib/databasus_ruby/objects/storages.rb
52
52
  - lib/databasus_ruby/objects/users.rb
53
53
  - lib/databasus_ruby/objects/workspaces.rb
54
+ - lib/databasus_ruby/record.rb
55
+ - lib/databasus_ruby/records/database.rb
54
56
  - lib/databasus_ruby/version.rb
55
57
  - sig/databasus_ruby.rbs
56
58
  homepage: https://github.com/SamuelFCorrea/databasus_ruby