Class: Rack::PactBroker::ConvertFileExtensionToAcceptHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/pact_broker/convert_file_extension_to_accept_header.rb

Overview

If the HTML and the CSV group resources are both requested by the browser, Chrome gets confused by the content types, and when you click back, it tries to load the CSV instead of the HTML page. So we have to give the CSV resource a different URL (.csv)

Constant Summary collapse

EXTENSION_REGEXP =
/\.\w+$/.freeze
EXTENSIONS =
{
  ".csv" => "text/csv",
  ".svg" => "image/svg+xml",
  ".json" => "application/hal+json",
  ".yaml" => "application/yaml"
}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ConvertFileExtensionToAcceptHeader

Returns a new instance of ConvertFileExtensionToAcceptHeader.



17
18
19
# File 'lib/rack/pact_broker/convert_file_extension_to_accept_header.rb', line 17

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/rack/pact_broker/convert_file_extension_to_accept_header.rb', line 21

def call env
  file_extension = extension(env)
  if convert_to_accept_header? file_extension
    @app.call(set_accept_header_and_path_info(env, file_extension))
  else
    @app.call(env)
  end
end

#convert_to_accept_header?(file_extension) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rack/pact_broker/convert_file_extension_to_accept_header.rb', line 30

def convert_to_accept_header? file_extension
  EXTENSIONS[file_extension]
end

#extension(env) ⇒ Object



34
35
36
# File 'lib/rack/pact_broker/convert_file_extension_to_accept_header.rb', line 34

def extension env
  env["PATH_INFO"] =~ EXTENSION_REGEXP && $~[0]
end

#set_accept_header_and_path_info(env, file_extension) ⇒ Object



38
39
40
41
42
43
# File 'lib/rack/pact_broker/convert_file_extension_to_accept_header.rb', line 38

def set_accept_header_and_path_info env, file_extension
  env.merge(
    "PATH_INFO" => env["PATH_INFO"].gsub(EXTENSION_REGEXP, ''),
    "HTTP_ACCEPT" => EXTENSIONS[file_extension]
  )
end