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.



89
90
91
92
93
94
# File 'lib/better_auth/endpoint.rb', line 89

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.



87
88
89
# File 'lib/better_auth/endpoint.rb', line 87

def headers
  @headers
end

#responseObject

Returns the value of attribute response.



87
88
89
# File 'lib/better_auth/endpoint.rb', line 87

def response
  @response
end

#statusObject

Returns the value of attribute status.



87
88
89
# File 'lib/better_auth/endpoint.rb', line 87

def status
  @status
end

Class Method Details

.from_value(value, context) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/better_auth/endpoint.rb', line 96

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: 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



120
121
122
123
124
125
126
127
128
129
# File 'lib/better_auth/endpoint.rb', line 120

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)


116
117
118
# File 'lib/better_auth/endpoint.rb', line 116

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)


131
132
133
# File 'lib/better_auth/endpoint.rb', line 131

def raw_response?
  !@raw_response.nil?
end

#to_rack_responseObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/better_auth/endpoint.rb', line 135

def to_rack_response
  return @raw_response if raw_response?

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