supabase_api 0.2.0 → 0.3.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: 84f7e5cb75c6780f9e5d92c3754ba8bc47a4025435138c15a3eca2277bf94c0b
4
- data.tar.gz: 3e8dd8a4d6660f151ad4e710a6b2deddc00ffa053246a97936473f13dd373b2a
3
+ metadata.gz: 19e109b4ba10480413356a7a64e56e72f9e3d3efe6e4c8723e56bb0daad000b3
4
+ data.tar.gz: 28caa806525353af39a39b07a06d1e1675dbe1e5fa7f3fcdf9c2b30c967ade89
5
5
  SHA512:
6
- metadata.gz: 29bada7921d062dd522ceb7859c846649859c9032e571914af230275c1f109cdd712f2121a920d82cb8ccc56131f10340486b83c2d97e5a0387a982634555796
7
- data.tar.gz: c265459ec9dae8192f9637a6c80711fc5966aa6a55880b9a1132598b266fa5dd6ae35d1209ad933bf26498f8ace7419a964af5157ed707e83d90bd6cdaa878c0
6
+ metadata.gz: 688301121727380971f25ce30a238e4a0cabb5c9ccaa9d38f3974557b517a847a519537b9243709ca94c0c95a1272f5817859fcebf086d92355a2b9940f8f4c1
7
+ data.tar.gz: 7546f6fca428c0af286f8c5c24ea7795b7348f2beb77b22b73416a78fd33a3bfd3dba1173de58991fab3ab2018c78054ecbab99280f27c7ab34af54724d612bc
data/Gemfile CHANGED
@@ -8,6 +8,9 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
+ gem 'simplecov', require: false, group: :test
12
+ gem 'simplecov-shields-badge', require: false
13
+
11
14
  gem "webmock"
12
15
  gem "rubocop", "~> 1.21"
13
16
 
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- supabase_api (0.1.0)
4
+ supabase_api (0.3.0)
5
5
  httparty
6
+ rack
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
@@ -14,6 +15,7 @@ GEM
14
15
  crack (0.4.5)
15
16
  rexml
16
17
  diff-lcs (1.5.0)
18
+ docile (1.4.0)
17
19
  dotenv (2.7.6)
18
20
  hashdiff (1.0.1)
19
21
  httparty (0.20.0)
@@ -28,6 +30,7 @@ GEM
28
30
  parser (3.1.2.0)
29
31
  ast (~> 2.4.1)
30
32
  public_suffix (4.0.7)
33
+ rack (2.2.4)
31
34
  rainbow (3.1.1)
32
35
  rake (13.0.6)
33
36
  regexp_parser (2.5.0)
@@ -58,6 +61,14 @@ GEM
58
61
  rubocop-ast (1.19.1)
59
62
  parser (>= 3.1.1.0)
60
63
  ruby-progressbar (1.11.0)
64
+ simplecov (0.21.2)
65
+ docile (~> 1.1)
66
+ simplecov-html (~> 0.11)
67
+ simplecov_json_formatter (~> 0.1)
68
+ simplecov-html (0.12.3)
69
+ simplecov-shields-badge (0.1.0)
70
+ simplecov (~> 0.15)
71
+ simplecov_json_formatter (0.1.4)
61
72
  unicode-display_width (2.2.0)
62
73
  webmock (3.14.0)
63
74
  addressable (>= 2.8.0)
@@ -73,6 +84,8 @@ DEPENDENCIES
73
84
  rake (~> 13.0)
74
85
  rspec (~> 3.0)
75
86
  rubocop (~> 1.21)
87
+ simplecov
88
+ simplecov-shields-badge
76
89
  supabase_api!
77
90
  webmock
78
91
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # SupabaseApi
1
+ # Supabase Api Ruby Client [![Gem](https://img.shields.io/gem/v/supabase_api?color=blue&label=version)](https://rubygems.org/gems/supabase_api) ![Build Status](https://github.com/galliani/supabase_api/workflows/spec/badge.svg) [![Coverage](https://github.com/galliani/supabase_api/blob/main/badge.svg)](https://github.com/galliani/supabase_api) ![Commit](https://img.shields.io/github/last-commit/galliani/supabase_api)
2
+
2
3
 
3
4
  Hi, this is a Ruby wrapper to access your Supabase tables via REST API in a manner similar to ActiveRecord model.
4
5
 
@@ -27,10 +28,21 @@ Run these commands on the config or initializer file.
27
28
  ```ruby
28
29
  # setups
29
30
  require 'supabase_api' # if not using Rails
30
- SupabaseApi::Sample.base_url = 'https://yourrandomapisubdomain.supabase.co'
31
- SupabaseApi::Sample.api_key = 'veryrandomlongstring'
31
+ SupabaseApi::Config.base_url = 'https://yourrandomapisubdomain.supabase.co'
32
+ SupabaseApi::Config.api_key = 'veryrandomlongstring'
33
+ ```
34
+
35
+ For production usage, of course it is recommended to use ENV variables manager like Figaro or Dotenv, which then will make your setup like this:
36
+ ```ruby
37
+ # setups
38
+ require 'supabase_api' # if not using Rails
39
+ SupabaseApi::Config.base_url = ENV['SUPABASE_API_BASE_URL']
40
+ SupabaseApi::Config.api_key = ENV['SUPABASE_API_KEY']
32
41
  ```
33
42
 
43
+ This setup should be called as early as need be. If using rails, you can put it under `config/initializers` directory inside a file named `supabase_api.rb` as per the usual convention.
44
+
45
+
34
46
  ### With Rails
35
47
  Create a ruby PORO class for your Supabase tables and inherit from the `SupabaseApi::Record` class.
36
48
 
@@ -45,6 +57,7 @@ end
45
57
 
46
58
  Then after that you can access your Supabase table just like a `ActiveRecord`-backed models.
47
59
 
60
+ ### Querying Data
48
61
  ```ruby
49
62
  book_id_in_supabase = 100
50
63
  Book.find(book_id_in_supabase)
@@ -52,6 +65,19 @@ Book.find(book_id_in_supabase)
52
65
  # The line below will yield the same like above but will not raise any exception
53
66
  book = Book.find_by_id(book_id_in_supabase)
54
67
 
68
+ # or you could call .where
69
+ Book.where(id: 100) # would yield the same result as above, also not raising exception
70
+
71
+ # .where works like you expect, passing another key-value pair as arguments
72
+ Book.where(name: 'some name of the book')
73
+
74
+ ```
75
+
76
+ ### Mutating Data
77
+
78
+ ```ruby
79
+ book = Book.find(book_id_in_supabase)
80
+
55
81
  # Assuming the books table has 'status' string column
56
82
  book.status = 'archived'
57
83
  book.save
@@ -77,9 +103,7 @@ Book.find(100) # will raise an exception SupabaseApi::RecordNotFound
77
103
 
78
104
  ## TODO List
79
105
  - add pagination.
80
- - add command `.where` for the Record class to be able to query the table.
81
- - add command `.update` for the Record instance.
82
- - add command `#update` for the Record class to have multiple upserts.
106
+ - adapt command `#create` and `#update` for the Record class to have multiple inserts and upserts respectively.
83
107
  - add more graceful and robust error handler.
84
108
 
85
109
 
data/badge.svg ADDED
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="114" height="20" role="img" aria-label="coverage: 99.57%"><title>coverage: 99.57%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="114" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="61" height="20" fill="#555"/><rect x="61" width="53" height="20" fill="#4c1"/><rect width="114" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text><text aria-hidden="true" x="865" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">99.57%</text><text x="865" y="140" transform="scale(.1)" fill="#fff" textLength="430">99.57%</text></g></svg>
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'httparty'
4
+ require 'rack'
4
5
 
5
6
  module SupabaseApi
6
7
  class Client
@@ -25,9 +26,9 @@ module SupabaseApi
25
26
  }
26
27
  end
27
28
 
28
- def list(table_name)
29
+ def list(table_name, params = {})
29
30
  self.class.get(
30
- self.class.list_endpoint(table_name),
31
+ self.class.list_endpoint(table_name, params),
31
32
  headers: @headers.merge({
32
33
  'Range': '0-9'
33
34
  })
@@ -35,12 +36,7 @@ module SupabaseApi
35
36
  end
36
37
 
37
38
  def find(table_name, id)
38
- self.class.get(
39
- self.class.filtered_by_id_endpoint(table_name, id),
40
- headers: @headers.merge({
41
- 'Range': '0-9'
42
- })
43
- )
39
+ list(table_name, { id: id })
44
40
  end
45
41
 
46
42
  def create(table_name, body)
@@ -56,7 +52,7 @@ module SupabaseApi
56
52
 
57
53
  def update(table_name, id, body)
58
54
  self.class.patch(
59
- self.class.filtered_by_id_endpoint(table_name, id),
55
+ self.class.list_endpoint(table_name, { id: id }),
60
56
  body: body.to_json,
61
57
  headers: @headers.merge({
62
58
  'Content-Type': 'application/json',
@@ -67,11 +63,7 @@ module SupabaseApi
67
63
 
68
64
  def destroy(table_name, id)
69
65
  self.class.delete(
70
- self.class.filtered_by_id_endpoint(table_name, id),
71
- headers: @headers.merge({
72
- 'Content-Type': 'application/json',
73
- 'Prefer': 'return=representation'
74
- })
66
+ self.class.list_endpoint(table_name, { id: id })
75
67
  )
76
68
  end
77
69
 
@@ -81,12 +73,16 @@ module SupabaseApi
81
73
  base_url + "/rest/#{api_version}/#{table_name}"
82
74
  end
83
75
 
84
- def self.list_endpoint(table_name)
85
- "#{collection_endpoint(table_name)}?select=*"
86
- end
76
+ def self.list_endpoint(table_name, params = {})
77
+ if params
78
+ query_string = Rack::Utils.build_query(params)
79
+ adjusted_query_string = query_string.gsub('=','=eq.')
80
+ end
81
+
82
+ url = "#{collection_endpoint(table_name)}?select=*"
83
+ return url if adjusted_query_string.nil? || adjusted_query_string.empty?
87
84
 
88
- def self.filtered_by_id_endpoint(table_name, id)
89
- "#{list_endpoint(table_name)}&id=eq.#{id}"
90
- end
85
+ url + '&' + adjusted_query_string
86
+ end
91
87
  end
92
88
  end
@@ -7,14 +7,19 @@ module SupabaseApi
7
7
  end
8
8
 
9
9
  def self.all
10
+ where()
11
+ end
12
+
13
+ def self.where(params = {})
10
14
  output = []
11
15
 
12
- response = Client.new.list(table_name)
16
+ response = Client.new.list(table_name, params)
17
+
13
18
  response.parsed_response.each do |record_hash|
14
19
  output << new(record_hash)
15
20
  end
16
21
 
17
- output
22
+ output
18
23
  end
19
24
 
20
25
  def self.find(id)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SupabaseApi
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/supabase_api.gemspec CHANGED
@@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
33
  spec.add_dependency "httparty"
34
+ spec.add_dependency "rack"
34
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supabase_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Galih Muhammad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-11 00:00:00.000000000 Z
11
+ date: 2022-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  executables: []
@@ -39,6 +53,7 @@ files:
39
53
  - LICENSE.txt
40
54
  - README.md
41
55
  - Rakefile
56
+ - badge.svg
42
57
  - lib/supabase_api.rb
43
58
  - lib/supabase_api/client.rb
44
59
  - lib/supabase_api/config.rb