Class: Lurker::Spy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lurker/spy.rb

Defined Under Namespace

Classes: BlindError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Spy

Returns a new instance of Spy.



14
15
16
17
18
19
20
21
22
23
# File 'lib/lurker/spy.rb', line 14

def initialize(options={}, &block)
  @options = options
  @block = block

  @service = if defined?(Rails)
    Service.new(Rails.root.join(DEFAULT_SERVICE_PATH).to_s, Rails.application.class.parent_name)
  else
    Service.default_service
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/lurker/spy.rb', line 7

def block
  @block
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/lurker/spy.rb', line 8

def request
  @request
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/lurker/spy.rb', line 8

def response
  @response
end

#serviceObject (readonly)

Returns the value of attribute service.



7
8
9
# File 'lib/lurker/spy.rb', line 7

def service
  @service
end

Class Method Details

.currentObject



84
85
86
# File 'lib/lurker/spy.rb', line 84

def self.current
  Thread.current[:lurker_spy]
end

.enabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/lurker/spy.rb', line 80

def self.enabled?
  current.present?
end

.on(options = {}, &block) ⇒ Object



73
74
75
76
77
78
# File 'lib/lurker/spy.rb', line 73

def self.on(options={}, &block)
  require 'lurker/spec_helper' unless defined? Lurker::SpecHelper
  (Thread.current[:lurker_spy] ||= new(options, &block)).tap do |spy|
    spy.call
  end
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lurker/spy.rb', line 25

def call
  @request = @response = nil # fill in while test
  @block.call.tap do |result|
    if @request && @response
      @service.verify!(
        verb, endpoint_path, payload,
        extensions, status, body
      )

      @service.persist! if success?(result)
    end
  end
end

#endpoint_pathObject



39
40
41
# File 'lib/lurker/spy.rb', line 39

def endpoint_path
  [request.endpoint_path, suffix].compact.join('-')
end

#extensionsObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lurker/spy.rb', line 43

def extensions
  extensions = {
    path_params: request.path_params,
    path_info: request.path_info,
    method: request.verb,
    suffix: suffix.to_s,
  }
  unless request.query_params.empty?
    extensions[:query_params] = request.query_params
  end
  extensions
end

#suffixObject



56
57
58
59
60
# File 'lib/lurker/spy.rb', line 56

def suffix
  if (suffix = @options[:suffix]).is_a?(String)
    suffix.gsub(/[^[[:alnum:]]]/, '_')
  end
end