wovnrb 3.0.1 → 3.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b52fa4c2f9eb449a733283bbe12420dcf4f60dd606ade7f919ffa43274659d4
4
- data.tar.gz: e5b4113a7e9e09aa352efc2a53cbafa26930f3aca5f4ade9e2fe0b46e7754686
3
+ metadata.gz: e5c458ea4be1b5bc1325a0c54a9672f7fd744b864975699ae48e70acde6d5265
4
+ data.tar.gz: 318102aa29ae9bbb03d5cf0b1ccffa3f39ada7a8fc963daad856b058f46e46ec
5
5
  SHA512:
6
- metadata.gz: 9010845d1a66b75de3093a5446571081598092183db5c02b8299492bc09319ab9372782467c694c2c86f62d4dd21238c31770267871542c819c87748ac4f4c20
7
- data.tar.gz: 712d54ef68e854dec1017ef57bb147924fcbc4ecdb456af2d797aeb497a8d469045c35b9e5dd41d6dda0c74c6659f32c7560c236c0b1a3c4c1baa9961c479df9
6
+ metadata.gz: fd81bc766461ce8212a32afafc0a673790985213304c55765322e42591e34107a649d70e1133a597b88c6cda3ee0813cd1b4efbac1ff45d2d794f52d78c80814
7
+ data.tar.gz: fd4304143e2226ab1b9a887d45e2326da8b8b452e0a60bcbb94f3b680fc4280459e47fb3b025d6660c2d338a1d3150617fc279b279e68693b0566a41050bd3de
data/.circleci/config.yml CHANGED
@@ -1,18 +1,29 @@
1
- version: 2
1
+ version: 2.1
2
+
3
+ orbs:
4
+ # https://github.com/CircleCI-Public/ruby-orb
5
+ ruby: circleci/ruby@1.1
2
6
 
3
7
  jobs:
4
- build:
8
+ test:
9
+ parameters:
10
+ ruby-version:
11
+ type: string
5
12
  docker:
6
- - image: circleci/ruby:2.3-jessie
13
+ - image: cimg/ruby:<< parameters.ruby-version >>
7
14
  steps:
8
15
  - checkout
9
- - restore_cache:
10
- keys:
11
- - bundler-
12
16
  - run: bundle install --path vendor/bundle --jobs=4
13
- - save_cache:
14
- key: bundler-{{ checksum "Gemfile.lock" }}
15
- paths:
16
- - vendor/bundle
17
17
  - run: bundle exec rubocop
18
18
  - run: bundle exec rake test
19
+
20
+ # https://circleci.com/blog/circleci-matrix-jobs/
21
+ workflows:
22
+ build_and_test:
23
+ jobs:
24
+ - test:
25
+ matrix:
26
+ parameters:
27
+ # https://github.com/CircleCI-Public/cimg-ruby
28
+ # only supports the last three ruby versions
29
+ ruby-version: ["2.5", "2.6", "2.7"]
data/.rubocop_todo.yml CHANGED
@@ -6,17 +6,8 @@
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 1
10
- # Cop supports --auto-correct.
11
- Layout/EmptyLinesAroundAccessModifier:
12
- Exclude:
13
- - 'lib/wovnrb/text_caches/memory_cache.rb'
14
-
15
- # Offense count: 1
16
- # Cop supports --auto-correct.
17
- Layout/EmptyLinesAroundMethodBody:
18
- Exclude:
19
- - 'test/lib/text_caches/memory_cache_test.rb'
9
+ Gemspec/RequiredRubyVersion:
10
+ Enabled: false
20
11
 
21
12
  # Offense count: 11
22
13
  # Cop supports --auto-correct.
@@ -25,13 +16,9 @@ Layout/ExtraSpacing:
25
16
  Exclude:
26
17
  - 'lib/wovnrb/lang.rb'
27
18
 
28
- # Offense count: 1
29
- # Cop supports --auto-correct.
30
- # Configuration parameters: EnforcedStyle, IndentationWidth.
31
- # SupportedStyles: special_inside_parentheses, consistent, align_braces
32
- Layout/FirstHashElementIndentation:
19
+ Style/HashEachMethods:
33
20
  Exclude:
34
- - 'lib/wovnrb/text_caches/cache_base.rb'
21
+ - 'lib/wovnrb/store.rb'
35
22
 
36
23
  # Offense count: 4
37
24
  # Cop supports --auto-correct.
@@ -42,13 +29,6 @@ Layout/HeredocIndentation:
42
29
  - 'test/lib/lang_test.rb'
43
30
  - 'test/lib/wovnrb_test.rb'
44
31
 
45
- # Offense count: 1
46
- # Cop supports --auto-correct.
47
- # Configuration parameters: AllowForAlignment.
48
- Layout/SpaceAroundOperators:
49
- Exclude:
50
- - 'lib/wovnrb/text_caches/memory_cache.rb'
51
-
52
32
  # Offense count: 3
53
33
  # Configuration parameters: AllowSafeAssignment.
54
34
  Lint/AssignmentInCondition:
@@ -70,13 +50,6 @@ Lint/ImplicitStringConcatenation:
70
50
  Exclude:
71
51
  - 'test/lib/lang_test.rb'
72
52
 
73
- # Offense count: 3
74
- # Cop supports --auto-correct.
75
- # Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods.
76
- Lint/UnusedMethodArgument:
77
- Exclude:
78
- - 'lib/wovnrb/text_caches/cache_base.rb'
79
-
80
53
  # Offense count: 5
81
54
  Lint/UselessAssignment:
82
55
  Exclude:
@@ -116,12 +89,13 @@ Metrics/MethodLength:
116
89
  # Offense count: 12
117
90
  Metrics/PerceivedComplexity:
118
91
  Max: 22
92
+ Exclude:
93
+ - 'lib/wovnrb/headers.rb'
119
94
 
120
95
  # Offense count: 3
121
96
  Naming/AccessorMethodName:
122
97
  Exclude:
123
98
  - 'lib/wovnrb/services/wovn_logger.rb'
124
- - 'lib/wovnrb/text_caches/cache_base.rb'
125
99
 
126
100
  # Offense count: 120
127
101
  # Configuration parameters: .
@@ -153,12 +127,6 @@ Style/ClassCheck:
153
127
  Exclude:
154
128
  - 'lib/wovnrb/store.rb'
155
129
 
156
- # Offense count: 6
157
- Style/ClassVars:
158
- Exclude:
159
- - 'lib/wovnrb/text_caches/cache_base.rb'
160
- - 'lib/wovnrb/text_caches/memory_cache.rb'
161
-
162
130
  # Offense count: 1
163
131
  # Cop supports --auto-correct.
164
132
  # Configuration parameters: Keywords.
@@ -183,8 +151,6 @@ Style/Documentation:
183
151
  - 'lib/wovnrb/services/wovn_logger.rb'
184
152
  - 'lib/wovnrb/settings.rb'
185
153
  - 'lib/wovnrb/store.rb'
186
- - 'lib/wovnrb/text_caches/cache_base.rb'
187
- - 'lib/wovnrb/text_caches/memory_cache.rb'
188
154
 
189
155
  # Offense count: 2
190
156
  Style/DoubleNegation:
@@ -224,7 +190,6 @@ Style/IfUnlessModifier:
224
190
  - 'Rakefile'
225
191
  - 'lib/wovnrb/helpers/nokogumbo_helper.rb'
226
192
  - 'lib/wovnrb/store.rb'
227
- - 'lib/wovnrb/text_caches/memory_cache.rb'
228
193
 
229
194
  # Offense count: 1
230
195
  # Cop supports --auto-correct.
@@ -241,7 +206,6 @@ Style/NegatedIf:
241
206
  Style/PreferredHashMethods:
242
207
  Exclude:
243
208
  - 'lib/wovnrb/store.rb'
244
- - 'lib/wovnrb/text_caches/memory_cache.rb'
245
209
 
246
210
  # Offense count: 2
247
211
  # Cop supports --auto-correct.
@@ -297,7 +261,7 @@ Style/ZeroLengthPredicate:
297
261
  # Offense count: 1436
298
262
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
299
263
  # URISchemes: http, https
300
- Metrics/LineLength:
264
+ Layout/LineLength:
301
265
  Max: 1424
302
266
 
303
267
  Style/FrozenStringLiteralComment:
@@ -310,6 +274,7 @@ Style/SafeNavigation:
310
274
  Enabled: false
311
275
 
312
276
  AllCops:
277
+ NewCops: enable
313
278
  Exclude:
314
279
  - 'docker/**/*'
315
- - 'vendor/**/*'
280
+ - 'vendor/**/*'
data/Gemfile CHANGED
@@ -1,7 +1,2 @@
1
1
  source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in wovn.gemspec
4
- if RUBY_VERSION < '2.2.2'
5
- gem 'activesupport', '< 5.0.0'
6
- end
7
2
  gemspec
data/README.en.md CHANGED
@@ -39,8 +39,8 @@ Insert the following into either config/application.rb or config/environments/.
39
39
 
40
40
  config.wovnrb = {
41
41
  :project_token => 'EnS!t3',
42
- :default_lang => 'en',
43
- :supported_langs => ['en'],
42
+ :default_lang => 'ja',
43
+ :supported_langs => ['ja', en'],
44
44
  :url_pattern => 'path'
45
45
  }
46
46
 
@@ -60,8 +60,8 @@ require 'wovnrb'
60
60
 
61
61
  use Wovnrb::Interceptor, {
62
62
  :project_token => 'EnS!t3',
63
- :default_lang => 'en',
64
- :supported_langs => ['en'],
63
+ :default_lang => 'ja',
64
+ :supported_langs => ['ja', 'en'],
65
65
  :url_pattern => 'path'
66
66
  }
67
67
 
@@ -74,18 +74,19 @@ After completing setup, start the Ruby Application, and make sure the WOVN.io li
74
74
 
75
75
  The following is a list of the WOVN.io Ruby Library's valid parameters.
76
76
 
77
- Parameter Name | Required | Default Setting
78
- ------------------ | -------- | ----------------
79
- project_token | yes | ''
80
- default_lang | yes | 'en'
81
- supported_langs | yes | ['en']
82
- url_pattern | yes | 'path'
83
- lang_param_name | | 'wovn'
84
- query | | []
85
- ignore_class | | []
86
- translate_fragment | | true
87
- ignore_paths | | []
88
- install_middleware | | true
77
+ Parameter Name | Required | Default Setting
78
+ ----------------------| -------- | ----------------
79
+ project_token | yes | ''
80
+ default_lang | yes | 'ja'
81
+ supported_langs | yes | ['ja', 'en']
82
+ url_pattern | yes | 'path'
83
+ lang_param_name | | 'wovn'
84
+ query | | []
85
+ ignore_class | | []
86
+ translate_fragment | | true
87
+ ignore_paths | | []
88
+ install_middleware | | true
89
+ compress_api_requests | | true
89
90
 
90
91
  ### 2.1. project_token
91
92
 
@@ -196,3 +197,7 @@ WOVN.rb needs to be added after any compression middleware.
196
197
  :install_middleware => false
197
198
  }
198
199
  ```
200
+
201
+ ### 2.11 compress_api_requests
202
+
203
+ By default, requests to the translation API will be sent with gzip compression. Set to false to disable compression.
data/README.ja.md CHANGED
@@ -44,8 +44,8 @@ bundle install
44
44
 
45
45
  config.wovnrb = {
46
46
  :project_token => 'EnS!t3',
47
- :default_lang => 'en',
48
- :supported_langs => ['en'],
47
+ :default_lang => 'ja',
48
+ :supported_langs => ['ja', en'],
49
49
  :url_pattern => 'path'
50
50
  }
51
51
 
@@ -63,8 +63,8 @@ require 'wovnrb'
63
63
 
64
64
  use Wovnrb::Interceptor, {
65
65
  :project_token => 'EnS!t3',
66
- :default_lang => 'en',
67
- :supported_langs => ['en'],
66
+ :default_lang => 'ja',
67
+ :supported_langs => ['ja', 'en'],
68
68
  :url_pattern => 'path'
69
69
  }
70
70
 
@@ -80,8 +80,8 @@ use Wovnrb::Interceptor, {
80
80
  パラメータ名 | 必須 | デフォルト設定
81
81
  ------------------ | -------- | ----------------
82
82
  project_token | yes | ''
83
- default_lang | yes | 'en'
84
- supported_langs | yes | ['en']
83
+ default_lang | yes | 'ja'
84
+ supported_langs | yes | ['ja', 'en']
85
85
  url_pattern | yes | 'path'
86
86
  lang_param_name | | 'wovn'
87
87
  query | | []
@@ -9,3 +9,8 @@ services:
9
9
  - 4000:4000
10
10
  volumes:
11
11
  - ../:/usr/src/app/wovnrb
12
+ networks:
13
+ - backend-network
14
+ networks:
15
+ backend-network:
16
+ name: 'backend-network'
@@ -53,4 +53,4 @@ end
53
53
  # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
54
54
  gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
55
55
 
56
- gem 'wovnrb', path: './wovnrb'
56
+ gem 'wovnrb', path: './wovnrb'
@@ -0,0 +1,16 @@
1
+ class CustomResponseController < ApplicationController
2
+ skip_before_action :verify_authenticity_token2
3
+
4
+ def make_response
5
+ response_args_json = params[:response]
6
+ raise ActionController::BadRequest unless response_args_json
7
+
8
+ (response_args_json[:headers] || {}).each do |name, value|
9
+ response.headers[name] = value
10
+ end
11
+
12
+ render inline: response_args_json[:body],
13
+ status: response_args_json[:status],
14
+ content_type: response_args_json[:content_type]
15
+ end
16
+ end
@@ -1,4 +1,5 @@
1
1
  Rails.application.routes.draw do
2
2
  get "/redirecting_page" => "redirects#show"
3
+ get "/custom_response/*all" => "custom_response#make_response"
3
4
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
4
5
  end
@@ -5,6 +5,7 @@ rm /usr/local/bin/bundle
5
5
  rm /usr/local/bin/bundler
6
6
  gem install bundler:2.1.4
7
7
  update --bundler
8
+ bundle update
8
9
  bundle install
9
10
  apt update
10
11
  apt install npm -y
@@ -2592,9 +2592,9 @@ dns-equal@^1.0.0:
2592
2592
  integrity sha1-s55/HabrCnW6nBcySzR1PEfgZU0=
2593
2593
 
2594
2594
  dns-packet@^1.3.1:
2595
- version "1.3.1"
2596
- resolved "https://registry.yarnpkg.com/dns-packet/-/dns-packet-1.3.1.tgz#12aa426981075be500b910eedcd0b47dd7deda5a"
2597
- integrity sha512-0UxfQkMhYAUaZI+xrNZOz/as5KgDU0M/fQ9b6SpkyLbk3GEswDi6PADJVaYJradtRVsRIlF1zLyOodbcTCDzUg==
2595
+ version "1.3.4"
2596
+ resolved "https://registry.yarnpkg.com/dns-packet/-/dns-packet-1.3.4.tgz#e3455065824a2507ba886c55a89963bb107dec6f"
2597
+ integrity sha512-BQ6F4vycLXBvdrJZ6S3gZewt6rcrks9KBgM9vrhW+knGRqc8uEdT7fuCwloc7nny5xNoMJ17HGH0R/6fpo8ECA==
2598
2598
  dependencies:
2599
2599
  ip "^1.1.0"
2600
2600
  safe-buffer "^5.0.1"
@@ -5863,9 +5863,9 @@ postcss-values-parser@^2.0.0, postcss-values-parser@^2.0.1:
5863
5863
  uniq "^1.0.1"
5864
5864
 
5865
5865
  postcss@^7.0.0, postcss@^7.0.1, postcss@^7.0.14, postcss@^7.0.17, postcss@^7.0.2, postcss@^7.0.26, postcss@^7.0.27, postcss@^7.0.32, postcss@^7.0.5, postcss@^7.0.6:
5866
- version "7.0.35"
5867
- resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.35.tgz#d2be00b998f7f211d8a276974079f2e92b970e24"
5868
- integrity sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==
5866
+ version "7.0.36"
5867
+ resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.36.tgz#056f8cffa939662a8f5905950c07d5285644dfcb"
5868
+ integrity sha512-BebJSIUMwJHRH0HAQoxN4u1CN86glsrwsW0q7T+/m44eXOUAxSNdHRkNZPYz5vVUbg17hFgOQDE7fZk7li3pZw==
5869
5869
  dependencies:
5870
5870
  chalk "^2.4.2"
5871
5871
  source-map "^0.6.1"
@@ -7269,9 +7269,9 @@ urix@^0.1.0:
7269
7269
  integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI=
7270
7270
 
7271
7271
  url-parse@^1.4.3, url-parse@^1.5.1:
7272
- version "1.5.1"
7273
- resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.1.tgz#d5fa9890af8a5e1f274a2c98376510f6425f6e3b"
7274
- integrity sha512-HOfCOUJt7iSYzEx/UqgtwKRMC6EU91NFhsCHMv9oM03VJcVo2Qrp8T8kI9D7amFf1cu+/3CEhgb3rF9zL7k85Q==
7272
+ version "1.5.3"
7273
+ resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.3.tgz#71c1303d38fb6639ade183c2992c8cc0686df862"
7274
+ integrity sha512-IIORyIQD9rvj0A4CLWsHkBBJuNqWpFQe224b6j9t/ABmquIS0qDU2pY6kl6AuOrL5OkCXHMCFNe1jBcuAggjvQ==
7275
7275
  dependencies:
7276
7276
  querystringify "^2.1.1"
7277
7277
  requires-port "^1.0.0"
@@ -7581,9 +7581,9 @@ wrappy@1:
7581
7581
  integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
7582
7582
 
7583
7583
  ws@^6.2.1:
7584
- version "6.2.1"
7585
- resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"
7586
- integrity sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==
7584
+ version "6.2.2"
7585
+ resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.2.tgz#dd5cdbd57a9979916097652d78f1cc5faea0c32e"
7586
+ integrity sha512-zmhltoSR8u1cnDsD43TX59mzoMZsLKqUweyYBAIvTngR3shc0W6aOZylZmq/7hqyVxPdi+5Ud2QInblgyE72fw==
7587
7587
  dependencies:
7588
7588
  async-limiter "~1.0.0"
7589
7589
 
@@ -5,9 +5,10 @@ require 'zlib'
5
5
 
6
6
  module Wovnrb
7
7
  class ApiTranslator
8
- def initialize(store, headers)
8
+ def initialize(store, headers, uuid)
9
9
  @store = store
10
10
  @headers = headers
11
+ @uuid = uuid
11
12
  end
12
13
 
13
14
  def translate(body)
@@ -51,24 +52,45 @@ module Wovnrb
51
52
  end
52
53
 
53
54
  def prepare_request(body)
54
- data = compress_request_data(generate_request_data(body))
55
- headers = {
56
- 'Accept-Encoding' => 'gzip',
57
- 'Content-Type' => 'application/octet-stream',
58
- 'Content-Length' => data.bytesize.to_s
59
- }
60
- request = Net::HTTP::Post.new(generate_request_path(body), headers)
55
+ if @store.compress_api_requests?
56
+ gzip_request(body)
57
+ else
58
+ json_request(body)
59
+ end
60
+ end
61
+
62
+ def gzip_request(html_body)
63
+ api_params = build_api_params(html_body)
64
+ compressed_body = compress_request_data(api_params)
65
+ request = Net::HTTP::Post.new(request_path(html_body), {
66
+ 'Accept-Encoding' => 'gzip',
67
+ 'Content-Type' => 'application/octet-stream',
68
+ 'Content-Encoding' => 'gzip',
69
+ 'Content-Length' => compressed_body.bytesize.to_s,
70
+ 'X-Request-Id' => @uuid
71
+ })
72
+ request.body = compressed_body
73
+
74
+ request
75
+ end
61
76
 
62
- request.body = data
77
+ def json_request(html_body)
78
+ api_params = build_api_params(html_body)
79
+ request = Net::HTTP::Post.new(request_path(html_body), {
80
+ 'Accept-Encoding' => 'gzip',
81
+ 'Content-Type' => 'application/json',
82
+ 'X-Request-Id' => @uuid
83
+ })
84
+ request.body = api_params.to_json
63
85
 
64
86
  request
65
87
  end
66
88
 
67
- def generate_request_path(body)
68
- "#{api_uri.path.sub(/\/$/, '')}/translation?cache_key=#{generate_cache_key(body)}"
89
+ def request_path(body)
90
+ "#{api_uri.path}/translation?cache_key=#{cache_key(body)}"
69
91
  end
70
92
 
71
- def generate_cache_key(body)
93
+ def cache_key(body)
72
94
  cache_key_components = {
73
95
  'token' => token,
74
96
  'settings_hash' => settings_hash,
@@ -81,8 +103,8 @@ module Wovnrb
81
103
  CGI.escape("(#{cache_key_components})")
82
104
  end
83
105
 
84
- def generate_request_data(body)
85
- data = {
106
+ def build_api_params(body)
107
+ result = {
86
108
  'url' => page_url,
87
109
  'token' => token,
88
110
  'lang_code' => lang_code,
@@ -93,9 +115,9 @@ module Wovnrb
93
115
  'body' => body
94
116
  }
95
117
 
96
- data['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
118
+ result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
97
119
 
98
- data
120
+ result
99
121
  end
100
122
 
101
123
  def compress_request_data(data_hash)
@@ -109,7 +131,7 @@ module Wovnrb
109
131
  end
110
132
 
111
133
  def api_uri
112
- Addressable::URI.parse(@store.settings['api_url'] + '/v0/')
134
+ Addressable::URI.parse("#{@store.settings['api_url']}/v0")
113
135
  end
114
136
 
115
137
  def api_timeout
@@ -1,17 +1,10 @@
1
1
  module Wovnrb
2
2
  class Headers
3
- attr_reader :unmasked_url
4
- attr_reader :url
5
- attr_reader :protocol
6
- attr_reader :unmasked_host
7
- attr_reader :host
8
- attr_reader :unmasked_pathname
9
- attr_reader :pathname
10
- attr_reader :pathname_with_trailing_slash_if_present
3
+ attr_reader :unmasked_url, :url, :protocol, :unmasked_host, :host, :unmasked_pathname, :pathname, :pathname_with_trailing_slash_if_present
11
4
 
12
5
  # Generates new instance of Wovnrb::Headers.
13
6
  # Its parameters are set by parsing env variable.
14
- #
7
+
15
8
  def initialize(env, settings)
16
9
  request = Rack::Request.new(env)
17
10
 
@@ -42,21 +35,21 @@ module Wovnrb
42
35
  @pathname, @query = @env['REQUEST_URI'].split('?')
43
36
  @pathname = settings['url_pattern'] == 'path' ? remove_lang(@pathname, lang_code) : @pathname
44
37
  @query ||= ''
45
- @url = "#{@host}#{@pathname}#{(!@query.empty? ? '?' : '') + remove_lang(@query, lang_code)}"
46
- if !settings['query'].empty?
38
+ @url = "#{@host}#{@pathname}#{(@query.empty? ? '' : '?') + remove_lang(@query, lang_code)}"
39
+ if settings['query'].empty?
40
+ @query = ''
41
+ else
47
42
  query_vals = []
48
43
  settings['query'].each do |qv|
49
44
  rx = Regexp.new("(^|&)(?<query_val>#{qv}[^&]+)(&|$)")
50
45
  m = @query.match(rx)
51
46
  query_vals.push(m[:query_val]) if m && m[:query_val]
52
47
  end
53
- @query = if !query_vals.empty?
54
- "?#{query_vals.sort.join('&')}"
55
- else
48
+ @query = if query_vals.empty?
56
49
  ''
50
+ else
51
+ "?#{query_vals.sort.join('&')}"
57
52
  end
58
- else
59
- @query = ''
60
53
  end
61
54
  @query = remove_lang(@query, lang_code)
62
55
  @pathname_with_trailing_slash_if_present = @pathname
@@ -115,11 +108,11 @@ module Wovnrb
115
108
  case @settings['url_pattern']
116
109
  when 'query'
117
110
  lang_param_name = @settings['lang_param_name']
118
- if location !~ /\?/
119
- location = "#{location}?#{lang_param_name}=#{lang_code}"
120
- else @env['REQUEST_URI'] !~ /(\?|&)#{lang_param_name}=/
121
- location = "#{location}&#{lang_param_name}=#{lang_code}"
122
- end
111
+ location = if location =~ /\?/
112
+ "#{location}&#{lang_param_name}=#{lang_code}"
113
+ else
114
+ "#{location}?#{lang_param_name}=#{lang_code}"
115
+ end
123
116
  when 'subdomain'
124
117
  location = "#{lang_code.downcase}.#{location}"
125
118
  # when 'path'
@@ -173,7 +166,7 @@ module Wovnrb
173
166
  lang_param_name = @settings['lang_param_name']
174
167
  uri.sub(/(^|\?|&)#{lang_param_name}=#{lang_code}(&|$)/, '\1').gsub(/(\?|&)$/, '')
175
168
  when 'subdomain'
176
- rp = Regexp.new('(^|(//))' + lang_code + '\.', 'i')
169
+ rp = Regexp.new("(^|(//))#{lang_code}\\.", 'i')
177
170
  uri.sub(rp, '\1')
178
171
  # when 'path'
179
172
  else
@@ -182,24 +175,22 @@ module Wovnrb
182
175
  end
183
176
 
184
177
  def out(headers)
185
- r = Regexp.new('//' + @host)
178
+ r = Regexp.new("//#{@host}")
186
179
  lang_code = Store.instance.settings['custom_lang_aliases'][self.lang_code] || self.lang_code
187
- if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r
188
- unless @settings['ignore_globs'].ignore?(headers['Location'])
189
- case @settings['url_pattern']
190
- when 'query'
191
- headers['Location'] += if headers['Location'] =~ /\?/
192
- '&'
193
- else
194
- '?'
195
- end
196
- headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}"
197
- when 'subdomain'
198
- headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, '//' + lang_code + '.\1')
199
- # when 'path'
200
- else
201
- headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, '\1/' + lang_code)
202
- end
180
+ if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r && !@settings['ignore_globs'].ignore?(headers['Location'])
181
+ case @settings['url_pattern']
182
+ when 'query'
183
+ headers['Location'] += if headers['Location'] =~ /\?/
184
+ '&'
185
+ else
186
+ '?'
187
+ end
188
+ headers['Location'] += "#{@settings['lang_param_name']}=#{lang_code}"
189
+ when 'subdomain'
190
+ headers['Location'] = headers['Location'].sub(/\/\/([^.]+)/, "//#{lang_code}.\\1")
191
+ # when 'path'
192
+ else
193
+ headers['Location'] = headers['Location'].sub(/(\/\/[^\/]+)/, "\\1/#{lang_code}")
203
194
  end
204
195
  end
205
196
  headers
@@ -2,15 +2,13 @@ module Wovnrb
2
2
  module Helpers
3
3
  module NokogumboHelper
4
4
  def parse_html(html_string, encoding = 'UTF-8')
5
- dom = if html_string.strip[0..999] =~ /<html/i
6
- d = Nokogiri::HTML5(html_string)
7
- d.encoding = encoding
8
- d
9
- else
10
- parse_fragment(html_string, encoding)
11
- end
12
-
13
- dom
5
+ if html_string.strip[0..999] =~ /<html/i
6
+ d = Nokogiri::HTML5(html_string)
7
+ d.encoding = encoding
8
+ d
9
+ else
10
+ parse_fragment(html_string, encoding)
11
+ end
14
12
  end
15
13
 
16
14
  # https://www.rubydoc.info/gems/nokogumbo/Nokogiri/HTML5#fragment-class_method