Class: SupabaseFunc::Client
- Inherits:
-
Object
- Object
- SupabaseFunc::Client
- Includes:
- HTTParty
- Defined in:
- lib/supabase_func/client.rb
Instance Method Summary collapse
-
#initialize(url, headers = {}) ⇒ Client
constructor
A new instance of Client.
- #invoke(function_name, options) ⇒ Object
- #setauth(token) ⇒ Object
Constructor Details
#initialize(url, headers = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 |
# File 'lib/supabase_func/client.rb', line 9 def initialize(url, headers = {}) @base_url = url self.class.headers(headers) end |
Instance Method Details
#invoke(function_name, options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/supabase_func/client.rb', line 23 def invoke(function_name, ) # Invokes a function # Parameters # ---------- # function_name : the name of the function to invoke # options : hash with the following properties # `headers`: hash representing the headers to send with the request # `body`: the body of the request # `response_type`: how the response should be parsed. The default is `json` # Returns # ------- # Hash with data and/or error message headers = .fetch(:headers, {}) body = [:body] response_type = .fetch(:response_type, "json") url = "#{@base_url}/#{function_name}" headers["Content-Type"] ||= "application/json" begin response = HTTParty.post(url, headers: headers, body: body) if response.success? data = response_type == "json" ? response.parsed_response : response.body { data: data, error: nil } elsif response.headers["x-relay-header"] == "true" { data: nil, error: response.body } else { data: nil, error: "HTTP error #{response.code}" } end rescue StandardError => e { data: nil, error: e. } end end |
#setauth(token) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/supabase_func/client.rb', line 14 def setauth(token) # Updates the authorization header # Parameters # ---------- # token : str # he new jwt token sent in the authorization header self.class.headers("Authorization" => "Bearer #{token}") end |