Class: BetterAuth::Endpoint::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/better_auth/endpoint.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, status: 200, headers: {}, raw_response: nil) ⇒ Result

Returns a new instance of Result.



172
173
174
175
176
177
# File 'lib/better_auth/endpoint.rb', line 172

def initialize(response:, status: 200, headers: {}, raw_response: nil)
  @response = response
  @status = status
  @headers = normalize_headers(headers)
  @raw_response = raw_response
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



170
171
172
# File 'lib/better_auth/endpoint.rb', line 170

def headers
  @headers
end

#responseObject

Returns the value of attribute response.



170
171
172
# File 'lib/better_auth/endpoint.rb', line 170

def response
  @response
end

#statusObject

Returns the value of attribute status.



170
171
172
# File 'lib/better_auth/endpoint.rb', line 170

def status
  @status
end

Class Method Details

.from_value(value, context) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/better_auth/endpoint.rb', line 179

def self.from_value(value, context)
  return value if value.is_a?(self)

  if value.is_a?(APIError)
    return new(response: value, status: value.status_code, headers: merge_headers(context.response_headers, value.headers))
  end

  if rack_response?(value)
    return new(response: nil, status: value[0], headers: value[1], raw_response: value)
  end

  headers = context.response_headers.dup
  if value.is_a?(self)
    headers = merge_headers(headers, value.headers)
    return new(response: value.response, status: value.status, headers: headers)
  end

  new(response: value, status: context.status, headers: headers)
end

.merge_headers(base, extra) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/better_auth/endpoint.rb', line 203

def self.merge_headers(base, extra)
  extra.each_with_object(base.dup) do |(key, value), result|
    normalized = key.to_s.downcase
    result[normalized] = if normalized == "set-cookie" && result[normalized]
      [result[normalized], value].join("\n")
    else
      value
    end
  end
end

.rack_response?(value) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/better_auth/endpoint.rb', line 199

def self.rack_response?(value)
  value.is_a?(Array) && value.length == 3 && value[0].is_a?(Integer) && value[1].is_a?(Hash)
end

Instance Method Details

#raw_response?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/better_auth/endpoint.rb', line 214

def raw_response?
  !@raw_response.nil?
end

#to_rack_responseObject



218
219
220
# File 'lib/better_auth/endpoint.rb', line 218

def to_rack_response
  to_response.to_a
end

#to_responseObject



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/better_auth/endpoint.rb', line 222

def to_response
  return Response.from_rack(@raw_response) if raw_response?

  body = if response.nil?
    [JSON.generate(nil)]
  elsif response.is_a?(String)
    [response]
  else
    [JSON.generate(response)]
  end
  response_headers = {"content-type" => "application/json"}.merge(headers)
  Response.new(status: status, headers: response_headers, body: body)
end