Class: BetterAuth::Endpoint::Result
- Inherits:
-
Object
- Object
- BetterAuth::Endpoint::Result
- Defined in:
- lib/better_auth/endpoint.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#response ⇒ Object
Returns the value of attribute response.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
- .from_value(value, context) ⇒ Object
- .merge_headers(base, extra) ⇒ Object
- .rack_response?(value) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(response:, status: 200, headers: {}, raw_response: nil) ⇒ Result
constructor
A new instance of Result.
- #raw_response? ⇒ Boolean
- #to_rack_response ⇒ Object
Constructor Details
#initialize(response:, status: 200, headers: {}, raw_response: nil) ⇒ Result
Returns a new instance of Result.
92 93 94 95 96 97 |
# File 'lib/better_auth/endpoint.rb', line 92 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
#headers ⇒ Object
Returns the value of attribute headers.
90 91 92 |
# File 'lib/better_auth/endpoint.rb', line 90 def headers @headers end |
#response ⇒ Object
Returns the value of attribute response.
90 91 92 |
# File 'lib/better_auth/endpoint.rb', line 90 def response @response end |
#status ⇒ Object
Returns the value of attribute status.
90 91 92 |
# File 'lib/better_auth/endpoint.rb', line 90 def status @status end |
Class Method Details
.from_value(value, context) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/better_auth/endpoint.rb', line 99 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
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/better_auth/endpoint.rb', line 123 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
119 120 121 |
# File 'lib/better_auth/endpoint.rb', line 119 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
134 135 136 |
# File 'lib/better_auth/endpoint.rb', line 134 def raw_response? !@raw_response.nil? end |
#to_rack_response ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/better_auth/endpoint.rb', line 138 def to_rack_response return @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) [status, response_headers, body] end |