omf_ec 6.1.2.pre.5 → 6.1.2.pre.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/omf_ec/dsl.rb +55 -9
  2. metadata +4 -4
data/lib/omf_ec/dsl.rb CHANGED
@@ -7,6 +7,9 @@ require 'active_support'
7
7
  require 'active_support/deprecation'
8
8
  require 'active_support/core_ext'
9
9
  require 'eventmachine'
10
+ require 'uri'
11
+ require 'open-uri'
12
+ require 'tempfile'
10
13
 
11
14
  module OmfEc
12
15
  # DSL methods to be used for OEDL scripts
@@ -211,30 +214,73 @@ module OmfEc
211
214
  end
212
215
  end
213
216
 
214
- # Load an additional OEDL script
217
+ # Load an additional OEDL script referenced by a URI
215
218
  #
216
- # First try to load the script from the paths associated to this running
217
- # Ruby instance. This would allow the loading of scripts shipped with
218
- # the EC gem. If that fails, then look for the script in the local file
219
- # system or at the given web URL.
219
+ # The supported URI schemes are:
220
+ # - file:///foo/bar.rb , which loads the file located at '/foo/bar.rb' on the local filesystem
221
+ # - system:///foo/bar.rb , which loads the file located at 'foo/bar.rb' in the default Ruby path of this EC
222
+ # - http://foo.com/bar.rb , which loads the file located at the URL 'http://foo.com/bar.rb'
220
223
  #
221
224
  # If an optional has of key/value is provided, then define an OMF
222
225
  # Experiment Property for each keys and assigne them the values.
223
226
  #
224
- # @param location name, path or URL for the OEDL script to load
227
+ # @param uri URI for the OEDL script to load
225
228
  # @param opts optional hash of key/values for extra Experiment Property to define
226
229
  #
227
230
  def load_oedl(location, opts = {})
231
+ begin
232
+ u = URI(location.downcase)
233
+ rescue Exception => e
234
+ warn "Unsupported OEDL library location '#{location}'"
235
+ return
236
+ end
237
+
228
238
  # Define the additional properties from opts
229
239
  opts.each { |k,v| def_property(k, v,) }
230
- # Try to load OEDL Library as built-in then external
240
+
241
+ # Keep the old syntax around for a while, warn users to use the new URI syntax
242
+ # TODO: remove this in a couple of EC versions
243
+ if u.scheme.nil? || u.scheme.empty?
244
+ deprecated_load_oedl(location)
245
+ return
246
+ end
247
+
248
+ # Find out which type of location this is and deal with it accordingly
249
+ case u.scheme.downcase.to_sym
250
+ when :system
251
+ begin
252
+ u.path[0]='' # get rid of first '/'
253
+ require u.path
254
+ info "Loaded built-in OEDL library '#{location}'"
255
+ rescue Exception => e
256
+ error "Fail loading built-in OEDL library '#{location}': #{e}"
257
+ end
258
+ when :file, :http
259
+ begin
260
+ file = Tempfile.new("oedl-#{Time.now.to_i}")
261
+ # see: http://stackoverflow.com/questions/7578898
262
+ open(u.to_s.sub(%r{^file:}, '')) { |io| file.write(io.read) }
263
+ file.close
264
+ OmfEc.experiment.archive_oedl(file.path)
265
+ load(file.path)
266
+ file.unlink
267
+ info "Loaded external OEDL library '#{location}'"
268
+ rescue Exception => e
269
+ error "Fail loading external OEDL library '#{location}': #{e}"
270
+ end
271
+ else
272
+ warn "Unsupported scheme for OEDL library location '#{location}'"
273
+ return
274
+ end
275
+ end
276
+
277
+ def deprecated_load_oedl(location)
278
+ warn "Loading OEDL Library using DEPRECATED syntax. Please use proper URI syntax"
231
279
  begin
232
280
  require location
233
281
  info "Loaded built-in OEDL library '#{location}'"
234
282
  rescue LoadError
235
283
  begin
236
- require 'open-uri'
237
- require 'tempfile'
238
284
  file = Tempfile.new("oedl-#{Time.now.to_i}")
239
285
  open(location) { |io| file.write(io.read) }
240
286
  file.close
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omf_ec
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.2.pre.5
4
+ version: 6.1.2.pre.6
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-29 00:00:00.000000000 Z
12
+ date: 2014-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -98,7 +98,7 @@ dependencies:
98
98
  requirements:
99
99
  - - '='
100
100
  - !ruby/object:Gem::Version
101
- version: 6.1.2.pre.5
101
+ version: 6.1.2.pre.6
102
102
  type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  requirements:
107
107
  - - '='
108
108
  - !ruby/object:Gem::Version
109
- version: 6.1.2.pre.5
109
+ version: 6.1.2.pre.6
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: sequel
112
112
  requirement: !ruby/object:Gem::Requirement