PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vanity/playground.rb

http://github.com/assaf/vanity
Ruby | 262 lines | 127 code | 42 blank | 93 comment | 2 complexity | 9f0d6acbbb88b3f9bfb6506c1ccd3c21 MD5 | raw file
  1. require "uri"
  2. module Vanity
  3. # Playground catalogs all your experiments. For configuration please see
  4. # Vanity::Configuration, for connection management, please see
  5. # Vanity::Connection.
  6. class Playground
  7. # Returns hash of metrics (key is metric id).
  8. #
  9. # @see Vanity::Metric
  10. # @since 1.1.0
  11. # @deprecated
  12. attr_reader :metrics
  13. # Returns hash of experiments (key is experiment id). This creates the
  14. # Experiment and persists it to the datastore.
  15. #
  16. # @see Vanity::Experiment
  17. attr_reader :experiments
  18. # Created new Playground. Unless you need to, use the global
  19. # Vanity.playground.
  20. def initialize
  21. @loading = []
  22. set_metrics
  23. set_experiments
  24. end
  25. # @deprecated
  26. # @see Configuration#experiments_path
  27. def load_path
  28. Vanity.configuration.experiments_path
  29. end
  30. # @deprecated
  31. # @see Configuration#experiments_path
  32. def load_path=(path)
  33. Vanity.configuration.experiments_path = path
  34. end
  35. # @deprecated
  36. # @see Configuration#logger
  37. def logger
  38. Vanity.configuration.logger
  39. end
  40. # @deprecated
  41. # @see Configuration#logger
  42. def logger=(logger)
  43. Vanity.configuration.logger = logger
  44. end
  45. # @deprecated
  46. # @see Configuration#templates_path
  47. def custom_templates_path
  48. Vanity.configuration.templates_path
  49. end
  50. def custom_templates_path=(path)
  51. Vanity.configuration.templates_path = path
  52. end
  53. # @deprecated
  54. # @see Configuration#use_js
  55. def use_js!
  56. Vanity.configuration.use_js = true
  57. end
  58. # @deprecated
  59. # @see Configuration#use_js
  60. def using_js?
  61. Vanity.configuration.use_js
  62. end
  63. # @deprecated
  64. # @see Configuration#add_participant_route
  65. def add_participant_path
  66. Vanity.configuration.add_participant_route
  67. end
  68. # @deprecated
  69. # @see Configuration#add_participant_route=
  70. def add_participant_path=(path)
  71. Vanity.configuration.add_participant_route=path
  72. end
  73. # @since 1.9.0
  74. # @deprecated
  75. # @see Configuration#failover_on_datastore_error
  76. def failover_on_datastore_error!
  77. Vanity.configuration.failover_on_datastore_error = true
  78. end
  79. # @since 1.9.0
  80. # @deprecated
  81. # @see Configuration#failover_on_datastore_error
  82. def failover_on_datastore_error?
  83. Vanity.configuration.failover_on_datastore_error
  84. end
  85. # @since 1.9.0
  86. # @deprecated
  87. # @see Configuration#on_datastore_error
  88. def on_datastore_error
  89. Vanity.configuration.on_datastore_error
  90. end
  91. # @deprecated
  92. # @see Configuration#on_datastore_error
  93. def on_datastore_error=(closure)
  94. Vanity.configuration.on_datastore_error = closure
  95. end
  96. # @since 1.9.0
  97. # @deprecated
  98. # @see Configuration#request_filter
  99. def request_filter
  100. Vanity.configuration.request_filter
  101. end
  102. # @deprecated
  103. # @see Configuration#request_filter=
  104. def request_filter=(filter)
  105. Vanity.configuration.request_filter = filter
  106. end
  107. # @since 1.4.0
  108. # @deprecated
  109. # @see Configuration#collecting
  110. def collecting?
  111. Vanity.configuration.collecting
  112. end
  113. # @since 1.4.0
  114. # @deprecated
  115. # @see Configuration#collecting
  116. def collecting=(enabled)
  117. Vanity.configuration.collecting = enabled
  118. end
  119. # @deprecated
  120. # @see Vanity#reload!
  121. def reload!
  122. Vanity.reload!
  123. end
  124. # @deprecated
  125. # @see Vanity#load!
  126. def load!
  127. Vanity.load!
  128. end
  129. def experiments_persisted?
  130. experiments.keys.all? { |id| connection.experiment_persisted?(id) }
  131. end
  132. # Returns a metric (raises NameError if no metric with that identifier).
  133. #
  134. # @see Vanity::Metric
  135. # @since 1.1.0
  136. def metric(id)
  137. metrics[id.to_sym] or raise NameError, "No metric #{id}"
  138. end
  139. # Tracks an action associated with a metric.
  140. #
  141. # @example
  142. # Vanity.playground.track! :uploaded_video
  143. #
  144. # @since 1.1.0
  145. def track!(id, count = 1)
  146. metric(id).track!(count)
  147. end
  148. # Returns the experiment. You may not have guessed, but this method raises
  149. # an exception if it cannot load the experiment's definition.
  150. #
  151. # @see Vanity::Experiment
  152. # @deprecated
  153. def experiment(name)
  154. id = name.to_s.downcase.gsub(/\W/, "_").to_sym
  155. Vanity.logger.warn("Deprecated: Please call experiment method with experiment identifier (a Ruby symbol)") unless id == name
  156. experiments[id.to_sym] or raise NoExperimentError, "No experiment #{id}"
  157. end
  158. # -- Participant Information --
  159. # Returns an array of all experiments this participant is involved in, with their assignment.
  160. # This is done as an array of arrays [[<experiment_1>, <assignment_1>], [<experiment_2>, <assignment_2>]], sorted by experiment name, so that it will give a consistent string
  161. # when converted to_s (so could be used for caching, for example)
  162. def participant_info(participant_id)
  163. participant_array = []
  164. experiments.values.sort_by(&:name).each do |e|
  165. index = connection.ab_assigned(e.id, participant_id)
  166. if index
  167. participant_array << [e, e.alternatives[index.to_i]]
  168. end
  169. end
  170. participant_array
  171. end
  172. # @since 1.4.0
  173. # @deprecated
  174. # @see Vanity::Connection
  175. def establish_connection(spec=nil)
  176. disconnect!
  177. Vanity.connect!(spec)
  178. end
  179. # @since 1.4.0
  180. # @deprecated
  181. # @see Vanity.connection
  182. def connection
  183. Vanity.connection.adapter
  184. end
  185. # @since 1.4.0
  186. # @deprecated
  187. # @see Vanity.connection
  188. def connected?
  189. Vanity.connection.connected?
  190. end
  191. # @since 1.4.0
  192. # @deprecated
  193. # @see Vanity.disconnect!
  194. def disconnect!
  195. Vanity.disconnect!
  196. end
  197. # Closes the current connection and establishes a new one.
  198. #
  199. # @since 1.3.0
  200. # @deprecated
  201. def reconnect!
  202. Vanity.reconnect!
  203. end
  204. private
  205. def set_experiments
  206. @experiments = {}
  207. Vanity.logger.info("Vanity: loading experiments from #{Vanity.configuration.experiments_path}")
  208. Dir[File.join(Vanity.configuration.experiments_path, "*.rb")].each do |file|
  209. Experiment::Base.load(self, @loading, file)
  210. end
  211. end
  212. def set_metrics
  213. @metrics = {}
  214. Vanity.logger.info("Vanity: loading metrics from #{Vanity.configuration.experiments_path}/metrics")
  215. Dir[File.join(Vanity.configuration.experiments_path, "metrics/*.rb")].each do |file|
  216. Metric.load(self, @loading, file)
  217. end
  218. end
  219. end
  220. end