opener-property-tagger 3.1.1 → 3.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3c33a6b3d4f58d431c9b6eca1c4a4a66239fa651
4
- data.tar.gz: 40f3ca343fe303d8b5bbe69576bafec5e0f555cf
2
+ SHA256:
3
+ metadata.gz: 200701c1c3f256b0fbfa6fb284e321a16d10f399ced1fa750f7f9d4b66ed5a6f
4
+ data.tar.gz: 6da856c2c5beb56d778f12e93db99c6e5109728c1c89a6a8f8cceec488a556d2
5
5
  SHA512:
6
- metadata.gz: 810e45722da0010b75c49add4d9f83cab411c06b72c458402a6e37997c607c49a107bc874b9dd6f06f5ea61f93c7c2e155e03e1c234ba6aa567bc86f9ca2a8c4
7
- data.tar.gz: 01dd805a7355cd5109cc61085ca2a564bb5a81a5a1cdf8f90f2d8b199a18f1c4d69e9f263b8fc9872e918bd10e74e6a2ba9a18103e59287ccd52441cf5885488
6
+ metadata.gz: 5d7056c547b3c7b845b4d3ad7d9f43019629c57e447b7ba5d10ada342809e708f4b62a6ac468daef61617a62e60b35391b541e9f393722d26590b531f6292559
7
+ data.tar.gz: 7a79912c1317fb629c95244c8f0bb9cf73690bf3b194984c8a7c818a6cc3123f8eaa5957d6bbe32db01f000457472a9db91ba7f1c3b8b556fdfcd440ff033c1a
@@ -2,13 +2,18 @@ require 'open3'
2
2
  require 'slop'
3
3
  require 'oga'
4
4
  require 'monitor'
5
+ require 'httpclient'
6
+ require 'hashie'
7
+ require 'json'
8
+ require 'active_support/all'
5
9
 
6
10
  require 'rexml/document'
7
11
  require 'rexml/formatters/pretty'
8
12
 
9
13
  require_relative 'property_tagger/version'
10
14
  require_relative 'property_tagger/cli'
11
- require_relative 'property_tagger/aspects_cache'
15
+ require_relative 'property_tagger/remote_aspects_cache'
16
+ require_relative 'property_tagger/file_aspects_cache'
12
17
  require_relative 'property_tagger/processor'
13
18
 
14
19
  module Opener
@@ -43,14 +48,17 @@ module Opener
43
48
  # @return [String]
44
49
  #
45
50
  def path
46
- path = options[:resource_path] || ENV['RESOURCE_PATH'] ||
51
+ return @path if @path
52
+
53
+ @path = options[:resource_path] || ENV['RESOURCE_PATH'] ||
47
54
  ENV['PROPERTY_TAGGER_LEXICONS_PATH']
55
+ return unless @path
48
56
 
49
- unless path
50
- raise ArgumentError, 'No lexicon path provided'
51
- end
57
+ @path = File.expand_path @path
58
+ end
52
59
 
53
- return File.expand_path(path)
60
+ def remote_url
61
+ @remote_url ||= ENV['PROPERTY_TAGGER_LEXICONS_URL']
54
62
  end
55
63
 
56
64
  ##
@@ -59,11 +67,18 @@ module Opener
59
67
  # @param [String] input
60
68
  # @return [String]
61
69
  #
62
- def run(input)
70
+ def run input, params = {}
63
71
  timestamp = !options[:no_time]
64
72
 
65
- return Processor.new(input, path, timestamp, options[:pretty]).process
73
+ Processor.new(input,
74
+ params: params,
75
+ url: remote_url,
76
+ path: path,
77
+ timestamp: timestamp,
78
+ pretty: options[:pretty],
79
+ ).process
66
80
  end
67
- end # PolarityTagger
68
- end # Opener
81
+
82
+ end
83
+ end
69
84
 
@@ -3,7 +3,8 @@ module Opener
3
3
  ##
4
4
  # Thread-safe cache for storing the contents of aspect files.
5
5
  #
6
- class AspectsCache
6
+ class FileAspectsCache
7
+
7
8
  include MonitorMixin
8
9
 
9
10
  def initialize
@@ -22,8 +23,6 @@ module Opener
22
23
  synchronize do
23
24
  @cache[path] = load_aspects(path) unless @cache.key?(path)
24
25
  end
25
-
26
- return @cache[path]
27
26
  end
28
27
 
29
28
  alias_method :get, :[]
@@ -44,6 +43,7 @@ module Opener
44
43
 
45
44
  return mapping
46
45
  end
47
- end # AspectsCache
48
- end # PropertyTagger
49
- end # Opener
46
+
47
+ end
48
+ end
49
+ end
@@ -4,14 +4,18 @@ module Opener
4
4
  # Class that applies property tagging to a given input KAF file.
5
5
  #
6
6
  class Processor
7
- attr_accessor :document, :aspects_path, :timestamp, :pretty
7
+
8
+ attr_accessor :document
9
+ attr_accessor :aspects, :aspects_path, :aspects_url
10
+ attr_accessor :timestamp, :pretty
8
11
 
9
12
  ##
10
13
  # Global cache used for storing loaded aspects.
11
14
  #
12
- # @return [Opener::PropertyTagger::AspectsCache.new]
15
+ # @return [Opener::PropertyTagger::FileAspectsCache.new]
13
16
  #
14
- ASPECTS_CACHE = AspectsCache.new
17
+ FILE_ASPECTS_CACHE = FileAspectsCache.new
18
+ REMOTE_ASPECTS_CACHE = RemoteAspectsCache.new
15
19
 
16
20
  ##
17
21
  # @param [String|IO] file The KAF file/input to process.
@@ -20,13 +24,19 @@ module Opener
20
24
  # @param [TrueClass|FalseClass] pretty Enable pretty formatting, disabled
21
25
  # by default due to the performance overhead.
22
26
  #
23
- def initialize(file, aspects_path, timestamp = true, pretty = false)
24
- @document = Oga.parse_xml(file)
25
- @aspects_path = aspects_path
27
+ def initialize file, params: {}, url: nil, path: nil, timestamp: true, pretty: false
28
+ @document = Oga.parse_xml file
29
+ raise 'Error parsing input. Input is required to be KAF' unless is_kaf?
26
30
  @timestamp = timestamp
27
31
  @pretty = pretty
28
32
 
29
- raise 'Error parsing input. Input is required to be KAF' unless is_kaf?
33
+ @params = params
34
+ @cache_keys = params[:cache_keys] || {lang: language}
35
+ @remote = !url.nil?
36
+ @aspects_path = path
37
+ @aspects_url = url
38
+
39
+ @aspects = if @remote then REMOTE_ASPECTS_CACHE[**@cache_keys].aspects else FILE_ASPECTS_CACHE[aspects_file] end
30
40
  end
31
41
 
32
42
  ##
@@ -50,13 +60,6 @@ module Opener
50
60
  return pretty ? pretty_print(document) : document.to_xml
51
61
  end
52
62
 
53
- ##
54
- # @return [Hash]
55
- #
56
- def aspects
57
- return ASPECTS_CACHE[aspects_file]
58
- end
59
-
60
63
  ##
61
64
  # Get the language of the input file.
62
65
  #
@@ -228,9 +231,9 @@ module Opener
228
231
  # @return [String]
229
232
  #
230
233
  def aspects_file
231
- return @aspects_file ||=
232
- File.expand_path("#{aspects_path}/#{language}.txt", __FILE__)
234
+ @aspects_file ||= File.expand_path "#{aspects_path}/#{language}.txt", __FILE__
233
235
  end
234
- end # Processor
235
- end # PropertyTagger
236
- end # Opener
236
+
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,60 @@
1
+ module Opener
2
+ class PropertyTagger
3
+ ##
4
+ # Thread-safe cache for storing the contents of remote aspects.
5
+ #
6
+ class RemoteAspectsCache
7
+
8
+ include MonitorMixin
9
+
10
+ UPDATE_INTERVAL = (ENV['CACHE_EXPIRE_MINS']&.to_i || 5).minutes
11
+
12
+ def initialize
13
+ super
14
+
15
+ @url = ENV['PROPERTY_TAGGER_LEXICONS_URL']
16
+ @cache = {}
17
+ end
18
+
19
+ def [] **params
20
+ synchronize do
21
+ existing = @cache[params]
22
+ break existing if existing and existing.from > UPDATE_INTERVAL.ago
23
+ @cache[params] = cache_update existing, **params
24
+ end
25
+ end
26
+ alias_method :get, :[]
27
+
28
+ def cache_update existing = nil, **params
29
+ from = Time.now
30
+ lexicons = load_aspects cache: existing, **params
31
+
32
+ if existing and lexicons.blank?
33
+ existing.from = from
34
+ return existing
35
+ end
36
+
37
+ Hashie::Mash.new(
38
+ aspects: lexicons,
39
+ from: from,
40
+ )
41
+ end
42
+
43
+ def load_aspects lang:, cache:, **params
44
+ url = "#{@url}&language_code=#{lang}&#{params.to_query}"
45
+ url += "&if_updated_since=#{cache.from.utc.iso8601}" if cache
46
+ puts "#{lang}: loading aspects from #{url}"
47
+
48
+ lexicons = JSON.parse HTTPClient.new.get(url).body
49
+ lexicons = lexicons['data'].map{ |l| Hashie::Mash.new l }
50
+ mapping = Hash.new{ |hash, key| hash[key] = [] }
51
+ lexicons.each do |l|
52
+ mapping[l.lemma.to_sym] << l.aspect
53
+ end
54
+
55
+ mapping
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -1,5 +1,7 @@
1
1
  module Opener
2
2
  class PropertyTagger
3
- VERSION = '3.1.1'
4
- end # PropertyTagger
5
- end # Opener
3
+
4
+ VERSION = '3.3.3'
5
+
6
+ end
7
+ end
@@ -29,6 +29,9 @@ Gem::Specification.new do |gem|
29
29
  gem.add_dependency 'opener-core', '~> 2.2'
30
30
 
31
31
  gem.add_dependency 'oga', ['~> 1.0', '>= 1.3.1']
32
+ gem.add_dependency 'httpclient'
33
+ gem.add_dependency 'hashie'
34
+ gem.add_dependency 'activesupport'
32
35
 
33
36
  gem.add_development_dependency 'rspec', '~> 3.0'
34
37
  gem.add_development_dependency 'cucumber'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opener-property-tagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - development@olery.com
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-07 00:00:00.000000000 Z
11
+ date: 2020-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opener-daemons
@@ -72,6 +72,48 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: 1.3.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: httpclient
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: hashie
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: activesupport
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
75
117
  - !ruby/object:Gem::Dependency
76
118
  name: rspec
77
119
  requirement: !ruby/object:Gem::Requirement
@@ -129,11 +171,11 @@ dependencies:
129
171
  - !ruby/object:Gem::Version
130
172
  version: '2.0'
131
173
  description: Property tagger for hotels in Dutch and English.
132
- email:
174
+ email:
133
175
  executables:
176
+ - property-tagger-server
134
177
  - property-tagger
135
178
  - property-tagger-daemon
136
- - property-tagger-server
137
179
  extensions: []
138
180
  extra_rdoc_files: []
139
181
  files:
@@ -145,10 +187,11 @@ files:
145
187
  - config.ru
146
188
  - exec/property-tagger.rb
147
189
  - lib/opener/property_tagger.rb
148
- - lib/opener/property_tagger/aspects_cache.rb
149
190
  - lib/opener/property_tagger/cli.rb
191
+ - lib/opener/property_tagger/file_aspects_cache.rb
150
192
  - lib/opener/property_tagger/processor.rb
151
193
  - lib/opener/property_tagger/public/markdown.css
194
+ - lib/opener/property_tagger/remote_aspects_cache.rb
152
195
  - lib/opener/property_tagger/server.rb
153
196
  - lib/opener/property_tagger/version.rb
154
197
  - lib/opener/property_tagger/views/index.erb
@@ -160,7 +203,7 @@ homepage: http://opener-project.github.com/
160
203
  licenses:
161
204
  - Apache 2.0
162
205
  metadata: {}
163
- post_install_message:
206
+ post_install_message:
164
207
  rdoc_options: []
165
208
  require_paths:
166
209
  - lib
@@ -175,10 +218,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
218
  - !ruby/object:Gem::Version
176
219
  version: '0'
177
220
  requirements: []
178
- rubyforge_project:
179
- rubygems_version: 2.4.8
180
- signing_key:
221
+ rubyforge_project:
222
+ rubygems_version: 2.7.8
223
+ signing_key:
181
224
  specification_version: 4
182
225
  summary: Property tagger for hotels in Dutch and English.
183
226
  test_files: []
184
- has_rdoc: