Class: SupabaseApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/supabase_api/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



22
23
24
25
26
27
# File 'lib/supabase_api/client.rb', line 22

def initialize
  @headers = {
    'apikey':         self.class.api_key,
    'Authorization':  "Bearer #{self.class.api_key}"
  }
end

Class Method Details

.api_keyObject



18
19
20
# File 'lib/supabase_api/client.rb', line 18

def self.api_key
  SupabaseApi::Config.api_key
end

.api_versionObject



14
15
16
# File 'lib/supabase_api/client.rb', line 14

def self.api_version
  SupabaseApi::Config.api_version || 'v1'
end

.base_urlObject



10
11
12
# File 'lib/supabase_api/client.rb', line 10

def self.base_url
  SupabaseApi::Config.base_url
end

Instance Method Details

#create(table_name, body) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/supabase_api/client.rb', line 42

def create(table_name, body)
  self.class.post(
    self.class.collection_endpoint(table_name),
    body:     body.to_json,
    headers:  @headers.merge({
      'Content-Type': 'application/json',
      'Prefer':       'return=representation'
    })
  )
end

#destroy(table_name, id) ⇒ Object



64
65
66
67
68
# File 'lib/supabase_api/client.rb', line 64

def destroy(table_name, id)
  self.class.delete(
    self.class.list_endpoint(table_name, { id: id })
  )      
end

#find(table_name, id) ⇒ Object



38
39
40
# File 'lib/supabase_api/client.rb', line 38

def find(table_name, id)
  list(table_name, { id: id })
end

#list(table_name, params = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/supabase_api/client.rb', line 29

def list(table_name, params = {})
  self.class.get(
    self.class.list_endpoint(table_name, params),
    headers: @headers.merge({
      'Range': '0-9'
    })
  )
end

#update(table_name, id, body) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/supabase_api/client.rb', line 53

def update(table_name, id, body)
  self.class.patch(
    self.class.list_endpoint(table_name, { id: id }),
    body:     body.to_json,
    headers:  @headers.merge({
      'Content-Type': 'application/json',
      'Prefer':       'return=representation'
    })
  )
end