lib/simple_websocket_vcr.rb in simple-websocket-vcr-0.0.4 vs lib/simple_websocket_vcr.rb in simple-websocket-vcr-0.0.5
- old
+ new
@@ -1,103 +1,122 @@
+require 'json'
+require 'yaml'
+require 'websocket-client-simple'
require 'simple_websocket_vcr/cassette'
require 'simple_websocket_vcr/configuration'
require 'simple_websocket_vcr/errors'
require 'simple_websocket_vcr/recordable_websocket_client'
require 'simple_websocket_vcr/version'
-require 'json'
-require 'websocket-client-simple'
+require 'simple_websocket_vcr/monkey_patch'
-module VCR
- module WebSocket
- def configure
- yield configuration
- end
+module WebSocketVCR
+ extend self
- def configuration
- @configuration ||= Configuration.new
- end
+ # @return [String] the current version.
+ # @note This string also has singleton methods:
+ #
+ # * `major` [Integer] The major version.
+ # * `minor` [Integer] The minor version.
+ # * `patch` [Integer] The patch version.
+ # * `parts` [Array<Integer>] List of the version parts.
+ def version
+ @version ||= begin
+ string = WebSocketVCR::VERSION
- def cassette
- @cassette
- end
+ def string.parts
+ split('.').map(&:to_i)
+ end
- def cassette=(v)
- @cassette = v
- end
+ def string.major
+ parts[0]
+ end
- def disabled
- @disabled || false
- end
+ def string.minor
+ parts[1]
+ end
- def disabled=(v)
- @disabled = v
- end
+ def string.patch
+ parts[2]
+ end
- def save_session
+ string
end
+ end
- def use_cassette(name, _options = {})
- fail ArgumentError, '`VCR.use_cassette` requires a block.' unless block_given?
- self.cassette = Cassette.new(name)
- yield
- cassette.save
- self.cassette = nil
- end
+ def configure
+ yield configuration
+ end
- def record(example, context, options = {}, &block)
- fail ArgumentError, '`VCR.record` requires a block.' unless block_given?
- name = filename_helper(example, context)
- use_cassette(name, options, &block)
- end
+ def configuration
+ @configuration ||= Configuration.new
+ end
- def turn_off!(_options = {})
- # TODO: impl
- end
+ def cassette
+ @cassette
+ end
- def turned_on?
- !@cassette.nil?
- end
+ def cassette=(v)
+ @cassette = v
+ end
- def turn_on!
- # TODO: impl
- end
+ def disabled
+ @disabled || false
+ end
- def live?
- @cassette && @cassette.recording?
- end
+ def disabled=(v)
+ @disabled = v
+ end
- private
+ def save_session
+ end
- def filename_helper(example, context)
- if context.class.metadata[:parent_example_group].nil?
- example_name = example.description.gsub(/\s+/, '_')
- directory = context.class.metadata[:description].gsub(/\s+/, '_')
- else
- example_name = "#{context.class.metadata[:description]}_#{example.description}".gsub(/\s+/, '_')
- directory = context.class.metadata[:parent_example_group][:description].gsub(/\s+/, '_')
- end
- "#{directory}/#{example_name}.json"
- end
+ # Use the specified cassette for either recording the real communication or replaying it during the tests.
+ # @param name [String] the cassette
+ # @param options [Hash] options for the cassette
+ # @option options [Symbol] :record if set to :none there will be no recording
+ # @option options [Symbol] :erb a sub-hash with variables used for ERB substitution in given cassette
+ def use_cassette(name, options = {})
+ fail ArgumentError, '`VCR.use_cassette` requires a block.' unless block_given?
+ self.cassette = Cassette.new(name, options)
+ yield
+ cassette.save
+ self.cassette = nil
+ end
- module_function :configure, :configuration, :cassette, :cassette=, :disabled, :disabled=, :save_session,
- :use_cassette, :record, :turn_off!, :turned_on?, :turn_on!, :filename_helper
+ def record(example, context, options = {}, &block)
+ fail ArgumentError, '`VCR.record` requires a block.' unless block_given?
+ name = filename_helper(example, context)
+ use_cassette(name, options, &block)
end
-end
-module WebSocket::Client::Simple
- class << self
- alias_method :real_connect, :connect
+ def turn_off!(_options = {})
+ # TODO: impl
+ end
- def connect(url, options = {})
- if VCR::WebSocket.configuration.hook_uris.any? { |u| url.include?(u) }
- cassette = VCR::WebSocket.cassette
- live = cassette.recording?
- real_client = real_connect(url, options) if live
- fake_client = VCR::WebSocket::RecordableWebsocketClient.new(cassette, live ? real_client : nil)
- yield fake_client if block_given?
- fake_client
- else
- real_connect(url, options)
- end
+ def turned_on?
+ !@cassette.nil?
+ end
+
+ def turn_on!
+ # TODO: impl
+ end
+
+ def live?
+ @cassette && @cassette.recording?
+ end
+
+ private
+
+ def filename_helper(example, context)
+ if context.class.metadata[:parent_example_group].nil?
+ example_name = example.description.gsub(/\s+/, '_')
+ directory = context.class.metadata[:description].gsub(/\s+/, '_')
+ else
+ example_name = "#{context.class.metadata[:description]}_#{example.description}".gsub(/\s+/, '_')
+ directory = context.class.metadata[:parent_example_group][:description].gsub(/\s+/, '_')
end
+ "#{directory}/#{example_name}"
end
+
+ module_function :configure, :configuration, :cassette, :cassette=, :disabled, :disabled=, :save_session,
+ :use_cassette, :record, :turn_off!, :turned_on?, :turn_on!, :filename_helper
end