123456789_123456789_123456789_123456789_123456789_

Class: Gem::GemcutterUtilities::WebauthnListener::Response

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(host) ⇒ Response

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 35

def initialize(host)
  @host = host

  build_http_response
end

Class Method Details

.for(host)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 31

def self.for(host)
  new(host)
end

Instance Attribute Details

#http_response (readonly)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 29

attr_reader :http_response

Instance Method Details

#add_access_control_headers (private)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 76

def add_access_control_headers
  @http_response["access-control-allow-origin"] = @host
  @http_response["access-control-allow-methods"] = "POST"
  @http_response["access-control-allow-headers"] = %w[Content-Type Authorization x-csrf-token]
end

#add_body (private)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 82

def add_body
  return unless body
  @http_response["content-type"] = "text/plain; charset=utf-8"
  @http_response["content-length"] = body.bytesize
  @http_response.instance_variable_set(:@body, body)
end

#add_connection_header (private)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 72

def add_connection_header
  @http_response["connection"] = "close"
end

#body (private)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 60

def body; end

#build_http_response (private)

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 62

def build_http_response
  response_class = Gem::Net::HTTPResponse::CODE_TO_OBJ[code.to_s]
  @http_response = response_class.new("1.1", code, reason_phrase)
  @http_response.instance_variable_set(:@read, true)

  add_connection_header
  add_access_control_headers
  add_body
end

#code (private)

Must be implemented in subclasses

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 52

def code
  raise NotImplementedError
end

#reason_phrase (private)

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 56

def reason_phrase
  raise NotImplementedError
end

#to_s

[ GitHub ]

  
# File 'lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb', line 41

def to_s
  status_line = "HTTP/#{@http_response.http_version} #{@http_response.code} #{@http_response.message}\r\n"
  headers = @http_response.to_hash.map {|header, value| "#{header}: #{value.join(", ")}\r\n" }.join + "\r\n"
  body = @http_response.body ? "#{@http_response.body}\n" : ""

  status_line + headers + body
end