Class: Supabase::Postgrest::APIResponse
- Inherits:
-
Object
- Object
- Supabase::Postgrest::APIResponse
- 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
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .extract_count(response, request_prefer) ⇒ Object
- .from_response(response, request_prefer: nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(data:, count: nil) ⇒ APIResponse
constructor
A new instance of APIResponse.
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
122 123 124 |
# File 'lib/supabase/postgrest/request_builder.rb', line 122 def count @count end |
#data ⇒ Object (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 |