Class: Brut::FrontEnd::Handlers::LocaleDetectionHandler

Inherits:
Brut::FrontEnd::Handler show all
Defined in:
lib/brut/front_end/handlers/locale_detection_handler.rb

Overview

Receives the Ajax request containing the browser's JavaScript engine's understanding of the user's locale. This is configured in Brut::Framework::MCP, however the requests are initiated from the HTML custom element generated by Components::LocaleDetection. This will set the timezone on the session via Session#timezone_from_browser=, and set the Session#http_accept_language= only if the Accept-Language header did not provide a value that is supported by the app.

Instance Method Summary collapse

Methods inherited from Brut::FrontEnd::Handler

#before_handle, #handle!

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

Methods included from Brut::FrontEnd::HandlingResults

#http_status, #redirect_to

Constructor Details

#initialize(body:, session:) ⇒ LocaleDetectionHandler

Returns a new instance of LocaleDetectionHandler.



8
9
10
11
# File 'lib/brut/front_end/handlers/locale_detection_handler.rb', line 8

def initialize(body:,session:)
  @body    = body
  @session = session
end

Instance Method Details

#handleObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brut/front_end/handlers/locale_detection_handler.rb', line 12

def handle
  begin
    parsed = JSON.parse(@body.read)

    Brut.container.instrumentation.add_attributes(
      prefix: "brut.locale-detection",
      parsed_body: parsed,
      parsed_class: parsed.class
    )

    if parsed.kind_of?(Hash)
      locale   = parsed["locale"]
      timezone = parsed["timeZone"]

      @session.timezone_from_browser = timezone
      if !@session.http_accept_language.known?
        @session.http_accept_language = Brut::I18n::HTTPAcceptLanguage.from_browser(locale)
      end
    end
  rescue => ex
    Brut.container.instrumentation.record_exception(ex)
  end
  http_status(200)
end