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
24
25
# File 'lib/lurker/spy.rb', line 14

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

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

Instance Attribute Details

#block ⇒ Object (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#request ⇒ Object

Returns the value of attribute request.



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

def request
  @request
end

#response ⇒ Object

Returns the value of attribute response.



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

def response
  @response
end

#service ⇒ Object (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Class Method Details

.current ⇒ Object



90
91
92
# File 'lib/lurker/spy.rb', line 90

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

.enabled? ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/lurker/spy.rb', line 86

def self.enabled?
  current.present?
end

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



77
78
79
80
81
82
83
84
# File 'lib/lurker/spy.rb', line 77

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

Instance Method Details

#call ⇒ Object



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

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_path ⇒ Object



41
42
43
# File 'lib/lurker/spy.rb', line 41

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

#extensions ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lurker/spy.rb', line 45

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

#suffix ⇒ Object



60
61
62
63
64
# File 'lib/lurker/spy.rb', line 60

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