wovnrb 0.1.35 → 0.1.36
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wovnrb.rb +53 -33
- data/lib/wovnrb/headers.rb +5 -5
- data/lib/wovnrb/store.rb +44 -7
- data/lib/wovnrb/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb6a923f625d953649b1de0242b1f477947d58c7
|
4
|
+
data.tar.gz: 1e75041212f5da667742dde22913779a125b042c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cae95611865c9f2b5e8aaa856c5644b36e6204bd5a4cdf2ab7063764c25f0739160372b3175cc66249f46f5a3a75f7f790b7a142ee6086bc4bd8d350f490c33d
|
7
|
+
data.tar.gz: 61eea27872c4b496d83a45cafc6edbeb6291f1711deba5798a83baf1bf7bae6852682ad949e5a189f97895e8055a3dd3d31cc12697c1eca5e4d65b9ce90b332c
|
data/lib/wovnrb.rb
CHANGED
@@ -17,40 +17,30 @@ module Wovnrb
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def call(env)
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
unless STORE.valid_settings?
|
21
|
+
return @app.call(env)
|
22
|
+
end
|
23
23
|
@env = env
|
24
|
-
STORE.refresh_settings
|
25
24
|
headers = Headers.new(env, STORE.settings)
|
26
25
|
# redirect if the path is set to the default language (for SEO purposes)
|
27
26
|
if (headers.path_lang == STORE.settings['default_lang'])
|
28
27
|
redirect_headers = headers.redirect(STORE.settings['default_lang'])
|
29
28
|
return [307, redirect_headers, ['']]
|
30
29
|
end
|
31
|
-
#if ((headers.path_lang != '' && !STORE.settings['supported_langs'].include?(headers.path_lang)) || headers.path_lang == STORE.settings['default_lang'])
|
32
|
-
# redirect_headers = headers.redirect(STORE.settings['default_lang'])
|
33
|
-
# redirect_headers['set-cookie'] = "wovn_selected_lang=#{STORE.settings['default_lang']};"
|
34
|
-
# return [307, redirect_headers, ['']]
|
35
|
-
#elsif headers.path_lang == '' && (headers.browser_lang != STORE.settings['default_lang'] && STORE.settings['supported_langs'].include?(headers.browser_lang))
|
36
|
-
# redirect_headers = headers.redirect(headers.browser_lang)
|
37
|
-
# return [307, redirect_headers, ['']]
|
38
|
-
#end
|
39
30
|
lang = headers.lang
|
40
31
|
|
41
32
|
# pass to application
|
42
33
|
status, res_headers, body = @app.call(headers.request_out)
|
43
|
-
#binding.pry if res_headers["Content-Type"] =~ /html/ && !body[0].nil?
|
44
34
|
|
45
35
|
if res_headers["Content-Type"] =~ /html/ && !body[0].nil?
|
46
|
-
|
36
|
+
|
47
37
|
values = STORE.get_values(headers.redis_url)
|
48
38
|
url = {
|
49
39
|
:protocol => headers.protocol,
|
50
40
|
:host => headers.host,
|
51
41
|
:pathname => headers.pathname
|
52
42
|
}
|
53
|
-
body = switch_lang(body, values, url, lang) unless status.to_s =~ /^1|302/
|
43
|
+
body = switch_lang(body, values, url, lang, headers) unless status.to_s =~ /^1|302/
|
54
44
|
|
55
45
|
content_length = 0
|
56
46
|
body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
|
@@ -60,15 +50,12 @@ module Wovnrb
|
|
60
50
|
end
|
61
51
|
|
62
52
|
headers.out(res_headers)
|
63
|
-
# res_headers['Content-Length'] = body.each {|b| break b.length.to_s }
|
64
|
-
#binding.pry if res_headers["Content-Type"] =~ /html/ && !body[0].nil?
|
65
53
|
[status, res_headers, body]
|
66
54
|
#[status, res_headers, d.transform()]
|
67
55
|
end
|
68
56
|
|
69
57
|
|
70
|
-
def switch_lang(body, values, url, lang=STORE.settings['default_lang'])
|
71
|
-
def_lang = 'en'
|
58
|
+
def switch_lang(body, values, url, lang=STORE.settings['default_lang'], headers)
|
72
59
|
text_index = values['text_vals'] || {}
|
73
60
|
src_index = values['img_vals'] || {}
|
74
61
|
img_src_prefix = values['img_src_prefix'] || ''
|
@@ -76,6 +63,7 @@ module Wovnrb
|
|
76
63
|
body.map! do |b|
|
77
64
|
d = Nokogiri::HTML5(b)
|
78
65
|
d.encoding = "UTF-8"
|
66
|
+
# swap text
|
79
67
|
d.xpath('//text()').each do |node|
|
80
68
|
node_text = node.content.strip
|
81
69
|
# shouldn't need size check, but for now...
|
@@ -83,6 +71,17 @@ module Wovnrb
|
|
83
71
|
node.content = node.content.gsub(/^(\s*)[\S\s]*(\s*)$/, '\1' + text_index[node_text][lang][0]['data'] + '\2')
|
84
72
|
end
|
85
73
|
end
|
74
|
+
# swap meta tag values
|
75
|
+
d.xpath('//meta').select{ |t|
|
76
|
+
(t.get_attribute('name') || t.get_attribute('property') || '') =~ /^(description|keywords|og:title|og:description)$/
|
77
|
+
}.each do |node|
|
78
|
+
node_content = node.get_attribute('content').strip
|
79
|
+
# shouldn't need size check, but for now...
|
80
|
+
if text_index[node_content] && text_index[node_content][lang] && text_index[node_content][lang].size > 0
|
81
|
+
node.set_attribute('content', node_content.gsub(/^(\s*)[\S\s]*(\s*)$/, '\1' + text_index[node_content][lang][0]['data'] + '\2'))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
# swap img srcs
|
86
85
|
d.xpath('//img').each do |node|
|
87
86
|
# use regular expressions to support case insensitivity (right?)
|
88
87
|
if node.to_html =~ /src=['"][^'"]*['"]/i
|
@@ -105,41 +104,62 @@ module Wovnrb
|
|
105
104
|
end
|
106
105
|
end
|
107
106
|
end
|
108
|
-
|
109
|
-
#
|
110
|
-
#insert_node['type'] = 'text/javascript'
|
111
|
-
#insert_node.content = "window.wovn_backend = function() { return {'currentLang': '#{lang}'}; };"
|
112
|
-
#parent_node = d.at_css('head') || d.at_css('body') || d.at_css('html')
|
113
|
-
#parent_node.add_child(insert_node)
|
114
|
-
|
115
|
-
# If dev can't be used on production gem
|
107
|
+
|
108
|
+
# REMOVE WIDGET
|
116
109
|
d.xpath('//script').each do |script_node|
|
117
110
|
if script_node['src'] && script_node['src'].include?('//j.wovn.io/')
|
118
111
|
script_node.remove
|
119
112
|
end
|
120
113
|
end
|
114
|
+
|
115
|
+
# PARENT NODE FOR INSERTS
|
116
|
+
parent_node = d.at_css('head') || d.at_css('body') || d.at_css('html')
|
117
|
+
|
118
|
+
# INSERT BACKEND WIDGET
|
121
119
|
insert_node = Nokogiri::XML::Node.new('script', d)
|
122
120
|
insert_node['src'] = '//j.wovn.io/0'
|
123
|
-
insert_node['data-wovnio'] = "key=#{STORE.settings['user_token']}&backend=true¤tLang=#{lang}&defaultLang=#{STORE.settings['default_lang']}&urlPattern=#{STORE.settings['
|
121
|
+
insert_node['data-wovnio'] = "key=#{STORE.settings['user_token']}&backend=true¤tLang=#{lang}&defaultLang=#{STORE.settings['default_lang']}&urlPattern=#{STORE.settings['url_pattern']}"
|
124
122
|
# do this so that there will be a closing tag (better compatibility with browsers)
|
125
123
|
insert_node.content = ' '
|
126
|
-
parent_node = d.at_css('head') || d.at_css('body') || d.at_css('html')
|
127
124
|
if parent_node.children.size > 0
|
128
125
|
parent_node.children.first.add_previous_sibling(insert_node)
|
129
126
|
else
|
130
127
|
parent_node.add_child(insert_node)
|
131
128
|
end
|
132
129
|
|
130
|
+
|
131
|
+
# INSERT LANGUAGE METALINKS
|
132
|
+
published_langs = get_langs(values)
|
133
|
+
published_langs.each do |l|
|
134
|
+
insert_node = Nokogiri::XML::Node.new('link', d)
|
135
|
+
insert_node['rel'] = 'alternate'
|
136
|
+
insert_node['hreflang'] = l
|
137
|
+
insert_node['href'] = headers.redirect_location(l)
|
138
|
+
parent_node.add_child(insert_node)
|
139
|
+
end
|
140
|
+
|
141
|
+
# set lang property on HTML tag
|
142
|
+
if d.at_css('html') || d.at_css('HTML')
|
143
|
+
(d.at_css('html') || d.at_css('HTML')).set_attribute('lang', lang)
|
144
|
+
end
|
145
|
+
|
133
146
|
output = d.to_html.gsub(/href="([^"]*)"/) {|m| "href=\"#{URI.decode($1)}\""}
|
134
|
-
# RAILS
|
135
|
-
#if Object.const_defined?('ActionView') && ActionView.const_defined?('OutputBuffer')
|
136
|
-
# output = ActionView::OutputBuffer.new(output)
|
137
|
-
#end
|
138
147
|
output
|
139
148
|
end
|
140
149
|
body
|
141
150
|
end
|
142
151
|
|
152
|
+
# this clearly needs to be refactored. I'm thinking maybe a Value service? (STORE.values.get_langs)
|
153
|
+
def get_langs(values)
|
154
|
+
langs = Set.new
|
155
|
+
(values['text_vals'] || {}).merge(values['img_vals'] || {}).each do |key, index|
|
156
|
+
index.each do |l, val|
|
157
|
+
langs.add(l)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
langs
|
161
|
+
end
|
162
|
+
|
143
163
|
end
|
144
164
|
|
145
165
|
end
|
data/lib/wovnrb/headers.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'pry'
|
1
2
|
module Wovnrb
|
2
3
|
|
3
4
|
class Headers
|
@@ -49,7 +50,6 @@ module Wovnrb
|
|
49
50
|
@query = remove_lang(@query, self.lang)
|
50
51
|
@pathname = @pathname.gsub(/\/$/, '')
|
51
52
|
@redis_url = "#{@host}#{@pathname}#{@query}"
|
52
|
-
#binding.pry
|
53
53
|
end
|
54
54
|
|
55
55
|
def lang
|
@@ -103,7 +103,7 @@ module Wovnrb
|
|
103
103
|
#return remove_lang("#{@env['HTTP_HOST']}#{@env['REQUEST_URI']}", lang)
|
104
104
|
else
|
105
105
|
location = self.url
|
106
|
-
case STORE.settings['
|
106
|
+
case STORE.settings['url_pattern']
|
107
107
|
when 'query'
|
108
108
|
if location !~ /\?/
|
109
109
|
location = "#{location}?wovn=#{lang}"
|
@@ -121,7 +121,7 @@ module Wovnrb
|
|
121
121
|
end
|
122
122
|
|
123
123
|
def request_out(def_lang=STORE.settings['default_lang'])
|
124
|
-
case STORE.settings['
|
124
|
+
case STORE.settings['url_pattern']
|
125
125
|
when 'query'
|
126
126
|
@env['REQUEST_URI'] = remove_lang(@env['REQUEST_URI']) if @env.has_key?('REQUEST_URI')
|
127
127
|
@env['QUERY_STRING'] = remove_lang(@env['QUERY_STRING']) if @env.has_key?('QUERY_STRING')
|
@@ -147,7 +147,7 @@ module Wovnrb
|
|
147
147
|
end
|
148
148
|
|
149
149
|
def remove_lang(uri, lang=self.path_lang)
|
150
|
-
case STORE.settings['
|
150
|
+
case STORE.settings['url_pattern']
|
151
151
|
when 'query'
|
152
152
|
return uri.sub(/(^|\?|&)wovn=#{lang}(&|$)/, '\1').gsub(/(\?|&)$/, '')
|
153
153
|
when 'subdomain'
|
@@ -161,7 +161,7 @@ module Wovnrb
|
|
161
161
|
|
162
162
|
def out(headers)
|
163
163
|
if headers.has_key?("Location")
|
164
|
-
case STORE.settings['
|
164
|
+
case STORE.settings['url_pattern']
|
165
165
|
when 'query'
|
166
166
|
if headers["Location"] =~ /\?/
|
167
167
|
headers["Location"] += "&"
|
data/lib/wovnrb/store.rb
CHANGED
@@ -11,11 +11,12 @@ module Wovnrb
|
|
11
11
|
@settings =
|
12
12
|
{
|
13
13
|
'user_token' => '',
|
14
|
-
|
14
|
+
'secret_key' => '',
|
15
|
+
# 'url_pattern' => 'query'
|
15
16
|
# 'url_pattern_reg' => "?.*wovn=(?<lang>[^&]+)(&|$)",
|
16
|
-
'
|
17
|
+
'url_pattern' => 'path',
|
17
18
|
'url_pattern_reg' => "/(?<lang>[^/.?]+)",
|
18
|
-
#'
|
19
|
+
#'url_pattern' => 'subdomain',
|
19
20
|
#'url_pattern_reg' => "^(?<lang>[^.]+)\.",
|
20
21
|
'query' => [],
|
21
22
|
'backend_host' => 'rs1.wovn.io',
|
@@ -27,19 +28,55 @@ module Wovnrb
|
|
27
28
|
@config_loaded = false
|
28
29
|
end
|
29
30
|
|
31
|
+
def valid_settings?
|
32
|
+
if !settings.has_key?('user_token') || settings['user_token'].length < 5 || settings['user_token'].length > 6
|
33
|
+
return false
|
34
|
+
elsif !settings.has_key?('secret_key') || settings['secret_key'].length == 0 #|| settings['secret_key'].length < 5 || settings['secret_key'].length > 6
|
35
|
+
return false
|
36
|
+
elsif !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
|
37
|
+
return false
|
38
|
+
elsif !settings.has_key?('query') || !settings['query'].kind_of?(Array)
|
39
|
+
return false
|
40
|
+
elsif !settings.has_key?('backend_host') || settings['backend_host'].length == 0
|
41
|
+
return false
|
42
|
+
elsif !settings.has_key?('backend_port') || settings['backend_port'].length == 0
|
43
|
+
return false
|
44
|
+
elsif !settings.has_key?('default_lang') || settings['default_lang'].length == 0
|
45
|
+
return false
|
46
|
+
elsif !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
|
47
|
+
return false
|
48
|
+
else
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
30
53
|
def settings
|
31
54
|
if !@config_loaded
|
55
|
+
# get Rails config.wovnrb
|
32
56
|
if Object.const_defined?('Rails') && Rails.configuration.respond_to?(:wovnrb)
|
33
57
|
config_settings = Rails.configuration.wovnrb.stringify_keys
|
34
58
|
if config_settings.has_key?('url_pattern')
|
35
59
|
if config_settings['url_pattern'] == 'query' || config_settings['url_pattern'] == 'subdomain' || config_settings['url_pattern'] == 'path'
|
36
|
-
config_settings['
|
60
|
+
config_settings['url_pattern'] = config_settings['url_pattern']
|
37
61
|
config_settings.delete('url_pattern')
|
38
62
|
end
|
39
63
|
end
|
40
64
|
@settings.merge!(Rails.configuration.wovnrb.stringify_keys)
|
41
65
|
end
|
42
|
-
|
66
|
+
|
67
|
+
# fix settings object
|
68
|
+
@settings['backend_port'] = @settings['backend_port'].to_s
|
69
|
+
@settings['default_lang'] = Lang.get_code(@settings['default_lang'])
|
70
|
+
if !@settings.has_key?('supported_langs')
|
71
|
+
@settings['supported_langs'] = [@settings['default_lang']]
|
72
|
+
end
|
73
|
+
if @settings['url_pattern'] == 'path'
|
74
|
+
@settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
|
75
|
+
elsif @settings['url_pattern'] == 'query'
|
76
|
+
@settings['url_pattern_reg'] = "((\\?.*&)|\\?)wovn=(?<lang>[^&]+)(&|$)"
|
77
|
+
elsif @settings['url_pattern'] == 'subdomain'
|
78
|
+
@settings['url_pattern_reg'] = "^(?<lang>[^.]+)\."
|
79
|
+
end
|
43
80
|
@config_loaded = true
|
44
81
|
end
|
45
82
|
@settings
|
@@ -68,9 +105,9 @@ module Wovnrb
|
|
68
105
|
if !vals.has_key?('supported_langs')
|
69
106
|
@settings['supported_langs'] = [@settings['default_lang']]
|
70
107
|
end
|
71
|
-
if @settings['
|
108
|
+
if @settings['url_pattern'] == 'path'
|
72
109
|
@settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
|
73
|
-
elsif @settings['
|
110
|
+
elsif @settings['url_pattern'] == 'query'
|
74
111
|
@settings['url_pattern_reg'] = "((\\?.*&)|\\?)wovn=(?<lang>[^&]+)(&|$)"
|
75
112
|
end
|
76
113
|
@settings
|
data/lib/wovnrb/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wovnrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.36
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Sandford
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-07-
|
12
|
+
date: 2015-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogumbo
|