Class: Supabase::Postgrest::APIResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/supabase/postgrest/request_builder.rb

Overview

Result of QueryRequestBuilder#execute. ‘data` is an Array of rows (or whatever PostgREST returned). `count` is populated when the request used a count Prefer header.

Direct Known Subclasses

SingleAPIResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, count: nil) ⇒ APIResponse

Returns a new instance of APIResponse.



124
125
126
127
# File 'lib/supabase/postgrest/request_builder.rb', line 124

def initialize(data:, count: nil)
  @data = data
  @count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



122
123
124
# File 'lib/supabase/postgrest/request_builder.rb', line 122

def count
  @count
end

#dataObject (readonly)

Returns the value of attribute data.



122
123
124
# File 'lib/supabase/postgrest/request_builder.rb', line 122

def data
  @data
end

Class Method Details

.extract_count(response, request_prefer) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/supabase/postgrest/request_builder.rb', line 141

def self.extract_count(response, request_prefer)
  return nil unless request_prefer
  return nil unless request_prefer.match?(/count=(?:exact|planned|estimated)/)

  content_range = response.headers["content-range"] || response.headers["Content-Range"]
  return nil unless content_range

  total = content_range.split("/").last
  total == "*" ? nil : total.to_i
end

.from_response(response, request_prefer: nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/supabase/postgrest/request_builder.rb', line 129

def self.from_response(response, request_prefer: nil)
  count = extract_count(response, request_prefer)
  data =
    begin
      body = response.body
      body && !body.empty? ? JSON.parse(body) : []
    rescue JSON::ParserError
      body.to_s.empty? ? [] : body.to_s
    end
  new(data: data, count: count)
end