Class: BetterAuth::Endpoint::Result

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

Defined Under Namespace

Classes: SetCookieHeader

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.



247
248
249
250
251
252
# File 'lib/better_auth/endpoint.rb', line 247

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.



209
210
211
# File 'lib/better_auth/endpoint.rb', line 209

def headers
  @headers
end

#responseObject

Returns the value of attribute response.



209
210
211
# File 'lib/better_auth/endpoint.rb', line 209

def response
  @response
end

#statusObject

Returns the value of attribute status.



209
210
211
# File 'lib/better_auth/endpoint.rb', line 209

def status
  @status
end

Class Method Details

.from_value(value, context) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/better_auth/endpoint.rb', line 254

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



278
279
280
281
282
283
284
285
286
287
# File 'lib/better_auth/endpoint.rb', line 278

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)


274
275
276
# File 'lib/better_auth/endpoint.rb', line 274

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)


289
290
291
# File 'lib/better_auth/endpoint.rb', line 289

def raw_response?
  !@raw_response.nil?
end

#to_rack_responseObject



293
294
295
# File 'lib/better_auth/endpoint.rb', line 293

def to_rack_response
  to_response.to_a
end

#to_responseObject



297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/better_auth/endpoint.rb', line 297

def to_response
  return Response.from_rack([@raw_response[0], rack_headers(@raw_response[1]), @raw_response[2]]) if raw_response?

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