raindeer 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sites/locale.rb +23 -9
  3. data/lib/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82fcde177d95de3f5ebd23739f9aa7a57e7577e791500294fb6299780f6417b1
4
- data.tar.gz: 63531aa3bde4df4780e3c3f3a59d39a13998dba5fe9eb0f76b4da5f11869d2f4
3
+ metadata.gz: 2ba6913d288b3b72e6573e9c00a6102c972f623ae6e79a172157295b113242cb
4
+ data.tar.gz: b07dfc5faa90fe83cfd1ede92d66911c902cff1b86e603bd01aef42a1e334fa4
5
5
  SHA512:
6
- metadata.gz: 8eb828025ace56b36618553a251fd8c8768821532580154c6cc7f3424cfa6fe04c77d036c378ba12167d061629b7fbf1db6fe9d80e969908f63dcef30d83d635
7
- data.tar.gz: fc551499692cdaa0e325fcd7f3831c9d848c9d71ec53123bc6eea20ca209cad76eaa619806a83802075a800129204ae22fb7fc5ed0355d8284354867f41efbc0
6
+ metadata.gz: ca803703ebd518640094fb69a73794704a514d595183661269a8b72ba636e891c4f4d476b618b9c1e2a2a3b36f4ff2da032311e520f2884d130ac62b1b84b9e9
7
+ data.tar.gz: 6b4e08ab82d2c192a2f5f288bb4f42854b67cbf0d98da1081c4a9116dcaeeef6b8c48dc724ffd5eb583ca813ee56eae58e62488fc5fe598391fc86e12baf25fd
data/lib/sites/locale.rb CHANGED
@@ -5,21 +5,28 @@ require_relative 'request_header'
5
5
  module Rain
6
6
  # The cookie a visitor's chosen language is kept in, like locale=en-US, as an app's language menu sets it.
7
7
  LOCALE_COOKIE = 'locale'
8
+ # The cookie a visitor's chosen country is kept in, like country=US, as an app's country menu sets it.
9
+ REGION_COOKIE = 'country'
8
10
 
9
11
  # Who a response is for: the language it's written in and the region its visitor is in, which can differ, like an
10
12
  # American reading the Australian site. The language is the one the visitor chose, by their locale cookie, or else the
11
13
  # site's when config.yaml sets it, or else the visitor's Accept-Language among the translations there are, or else the
12
- # site's own. The region is the site's, or else the country a CDN like Cloudflare found them in.
14
+ # site's own. The region is the site's, or else the country the visitor chose by their country cookie, or else the one
15
+ # a CDN like Cloudflare found them in, or else their language's, like US for en-US.
13
16
  Locale = Data.define(:language, :region) do
14
17
  def self.from_request(request, site:)
15
- region = site.region || cloudflare_country(request)
18
+ language = language_of(request, site:)
19
+ region = site.region || chosen_region(request) || cloudflare_country(request) || language[/-([A-Za-z]{2})\z/, 1]&.upcase
20
+ of(language, region)
21
+ end
22
+
23
+ def self.language_of(request, site:)
16
24
  language = chosen_language(request, site:)
17
25
  language ||= site.language if site.fixed_language?
18
- return of(language, region) if language
26
+ return language if language
19
27
 
20
28
  accept_language = RequestHeader.joined(request, 'accept-language')
21
- language = Translations.negotiate(accept_language: (accept_language unless accept_language&.strip&.empty?), default: site.language)
22
- of(language, region)
29
+ Translations.negotiate(accept_language: (accept_language unless accept_language&.strip&.empty?), default: site.language)
23
30
  end
24
31
 
25
32
  # A locale is a value, so each one is made once rather than for every request. There are only so many, as its
@@ -48,12 +55,19 @@ module Rain
48
55
  pair&.delete_prefix("#{name}=")
49
56
  end
50
57
 
51
- # The visitor's country by Cloudflare's CF-IPCountry header, or nil when there's none to go by, like XX for one it
52
- # can't place or T1 for one using Tor.
58
+ # The country a visitor chose, by their country cookie, or else nil.
59
+ def self.chosen_region(request)
60
+ country_code(cookie(request, REGION_COOKIE))
61
+ end
62
+
63
+ # The visitor's country by Cloudflare's CF-IPCountry header, or nil when there's none to go by.
53
64
  def self.cloudflare_country(request)
54
- code = RequestHeader.first(request, 'cf-ipcountry')
55
- return nil if code.nil?
65
+ country_code(RequestHeader.first(request, 'cf-ipcountry'))
66
+ end
56
67
 
68
+ # A two letter country code in capitals, or nil for anything else, like XX for a country Cloudflare can't place or
69
+ # T1 for a visitor using Tor.
70
+ def self.country_code(code)
57
71
  code = code.to_s.upcase
58
72
  code if code.match?(/\A[A-Z]{2}\z/) && !%w[XX T1].include?(code)
59
73
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindeer
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raindeer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi