deta 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35687498010f2d7c0724fcf993a800269d6c2f61aa4f4716371018f46774d373
4
- data.tar.gz: e9b04aadc3bbc443df406a61c070188de2029d62b0cb191713785043ddf9e9f0
3
+ metadata.gz: 76a8441ba038bdfc44abe26a8568bd445e8bf943420ba82c8572708b514158ba
4
+ data.tar.gz: 29e13de22bdb9900cf0510363c313dd0314a75be02fd3d3942130bc923ce0028
5
5
  SHA512:
6
- metadata.gz: 708c43716d995f5a5edc8399e39a68a16ff4b738442c25ffd1ddb370dd00013027c0bb78dd3ef849761700ae4a5fa6edae799faf99d94f62287e52e390df3aa1
7
- data.tar.gz: 3a6a9f168d1d3c0758adeea5354128ce369932e0b0aa716c7350edcaae1dc26a4c1bf1e556e826f3dda7921271410a6de3d2154b9b2813eebe1c7661bc90d45b
6
+ metadata.gz: e7f7d384edbfad45567aacaf52bb53a5db5fbba052b51d3699b607bffc3a7eed31048f3846c75dfcab0b2b3b606b59dec57e1ee0bd4ad5636aad9d3be3e34187
7
+ data.tar.gz: d76a5846a163b896f58c17eec943207a064275e551d7715dfa89b551e5ae6698c31acc694f688d898c7daa3a7ff21120bc9ead937baf7406e88db8727648f04d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- deta (0.1.0)
4
+ deta (0.1.1)
5
5
  faraday (~> 2.1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -10,6 +10,11 @@ Add this line to your application's Gemfile:
10
10
  gem 'deta'
11
11
  ```
12
12
 
13
+ or grab it from Github
14
+ ```ruby
15
+ gem 'deta', github: "arubinofaux/deta-ruby"
16
+ ```
17
+
13
18
  And then execute:
14
19
 
15
20
  $ bundle install
@@ -44,7 +49,7 @@ put = detabase.put({name: "Mike Jones", age: 31, hometown: "Denver"})
44
49
  put.processed.items.first.key
45
50
 
46
51
  # multi items
47
- puts = detabase.put({name: "Mike Jones", age: 31, hometown: "Denver"}, {name: "Mike Dow", age: 25, hometown: "Miami"})
52
+ puts = detabase.put([{name: "Mike Jones", age: 31, hometown: "Denver"}, {name: "Mike Dow", age: 25, hometown: "Miami"}])
48
53
  puts.processed
49
54
  ```
50
55
 
@@ -63,7 +68,10 @@ detabase.delete(key)
63
68
  ### Insert Item
64
69
 
65
70
  ```ruby
66
- TODO:
71
+ detabase.insert(name: "Mike Jones", age: 31, hometown: "Denver")
72
+
73
+ # specify the key
74
+ detabase.insert(key: "ab5tsstc9bh8", name: "Mike Jones", age: 31, hometown: "Denver")
67
75
  ```
68
76
 
69
77
  ### Update Item
@@ -75,7 +83,10 @@ detabase.update(key, set: {hometown: "Miami"})
75
83
  ### Query Items
76
84
 
77
85
  ```ruby
78
- TODO:
86
+ detabase.fetch(query: {hometown: "Denver"})
87
+ detabase.fetch(query: [{hometown: "Boulder"}, {"age?lt": 33}])
88
+
89
+ detabase.fetch(query: [{hometown: "Boulder"}], limit: 1, last: "8523bdyxlqww")
79
90
  ```
80
91
 
81
92
  ## Drive
data/lib/deta/client.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  module Deta
2
2
  class Client
3
- BASE_URL = "https://database.deta.sh/v1/"
4
-
5
3
  attr_reader :project_id, :project_key, :adapter
6
4
 
7
5
  def initialize(project_key:, adapter: Faraday.default_adapter)
@@ -10,13 +8,16 @@ module Deta
10
8
  @adapter = adapter
11
9
  end
12
10
 
13
- def base(base_db = nil)
14
- BaseResource.new(self, base_db)
11
+ def base(base_name = nil)
12
+ BaseResource.new(self, base_name)
13
+ end
14
+
15
+ def drive(drive_name = nil)
16
+ DriveResource.new(self, drive_name)
15
17
  end
16
18
 
17
19
  def connection
18
20
  @connection ||= Faraday.new do |conn|
19
- conn.url_prefix = BASE_URL
20
21
  conn.headers['X-API-Key'] = project_key
21
22
  conn.request :json
22
23
  conn.response :json, content_type: "application/json"
@@ -0,0 +1,4 @@
1
+ module Deta
2
+ class DriveObject < Object
3
+ end
4
+ end
data/lib/deta/resource.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  module Deta
2
2
  class Resource
3
- attr_reader :client, :base_db
3
+ attr_reader :client, :resource_name
4
4
 
5
- def initialize(client, base_db)
5
+ def initialize(client, resource_name)
6
6
  @client = client
7
- @base_db = base_db
7
+ @resource_name = resource_name
8
8
  end
9
9
 
10
10
  private
@@ -21,6 +21,10 @@ module Deta
21
21
  handle_response client.connection.patch(url, body, headers)
22
22
  end
23
23
 
24
+ def post_request(url, body:, headers: {})
25
+ handle_response client.connection.post(url, body, headers)
26
+ end
27
+
24
28
  def delete_request(url, params: {}, headers: {})
25
29
  handle_response client.connection.delete(url, params, headers)
26
30
  end
@@ -40,7 +44,6 @@ module Deta
40
44
  when 500
41
45
  raise Error, "We were unable to perform the request due to server-side problems. #{response.body["errors"]}"
42
46
  end
43
-
44
47
  response
45
48
  end
46
49
  end
@@ -1,9 +1,29 @@
1
1
  module Deta
2
2
  class BaseResource < Resource
3
- def put(*attributes)
4
- items = {items: attributes}
5
-
6
- BaseObject.new put_request("#{client.project_id}/#{base_db}/items", body: items).body
3
+ @@api_url = "https://database.deta.sh/v1"
4
+
5
+ def put(attributes)
6
+ items = {}
7
+ if attributes.is_a?(Array)
8
+ items["items"] = attributes
9
+ else
10
+ items["items"] = [attributes]
11
+ end
12
+
13
+ BaseObject.new put_request([@@api_url, client.project_id, resource_name, "items"].join("/"), body: items).body
14
+ end
15
+
16
+ def get(key = nil)
17
+ BaseObject.new get_request([@@api_url, client.project_id, resource_name, "items", key].join("/")).body
18
+ end
19
+
20
+ def delete(key = nil)
21
+ BaseObject.new delete_request([@@api_url, client.project_id, resource_name, "items", key].join("/")).body
22
+ end
23
+
24
+ def insert(**attributes)
25
+ item = {item: attributes}
26
+ BaseObject.new post_request([@@api_url, client.project_id, resource_name, "items"].join("/"), body: item).body
7
27
  end
8
28
 
9
29
  def update(key = nil, set: nil, increment: nil, append: nil, prepend: nil, delete: nil)
@@ -13,16 +33,21 @@ module Deta
13
33
  request[:append] = append if append
14
34
  request[:prepend] = prepend if prepend
15
35
  request[:delete] = delete if delete
16
-
17
- BaseObject.new patch_request("#{client.project_id}/#{base_db}/items/#{key}", body: request).body
36
+
37
+ BaseObject.new patch_request([@@api_url, client.project_id, resource_name, "items", key].join("/"), body: request).body
18
38
  end
19
39
 
20
- def get(key = nil)
21
- BaseObject.new get_request("#{client.project_id}/#{base_db}/items/#{key}").body
22
- end
40
+ def fetch(query: nil, limit: nil, last: nil)
41
+ request = {}
42
+ request[:limit] = limit
43
+ request[:last] = last
44
+ if query.is_a?(Array)
45
+ request[:query] = query
46
+ else
47
+ request[:query] = [query]
48
+ end
23
49
 
24
- def delete(key = nil)
25
- BaseObject.new delete_request("#{client.project_id}/#{base_db}/items/#{key}").body
50
+ BaseObject.new post_request([@@api_url, client.project_id, resource_name, "query"].join("/"), body: request).body
26
51
  end
27
52
  end
28
53
  end
@@ -0,0 +1,6 @@
1
+ module Deta
2
+ class DriveResource < Resource
3
+ @@api_url = "https://drive.deta.sh/v1"
4
+
5
+ end
6
+ end
data/lib/deta/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Deta
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/deta.rb CHANGED
@@ -11,6 +11,8 @@ module Deta
11
11
  autoload :Object, "deta/object"
12
12
 
13
13
  autoload :BaseResource, "deta/resources/base"
14
+ autoload :DriveResource, "deta/resources/drive"
14
15
 
15
16
  autoload :BaseObject, "deta/objects/base"
17
+ autoload :DriveObject, "deta/objects/drive"
16
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dainel Vera
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-29 00:00:00.000000000 Z
11
+ date: 2022-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -46,8 +46,10 @@ files:
46
46
  - lib/deta/error.rb
47
47
  - lib/deta/object.rb
48
48
  - lib/deta/objects/base.rb
49
+ - lib/deta/objects/drive.rb
49
50
  - lib/deta/resource.rb
50
51
  - lib/deta/resources/base.rb
52
+ - lib/deta/resources/drive.rb
51
53
  - lib/deta/version.rb
52
54
  homepage: https://github.com/arubinofaux/deta-ruby
53
55
  licenses: