Class: Aranea::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/aranea/failure_repository.rb

Overview

TODO: Look into moving whitelisting of consumer hostnames to here and allowing it to be configurable via the consuming application

Defined Under Namespace

Classes: Repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Failure

Returns a new instance of Failure.



29
30
31
32
33
34
# File 'lib/aranea/failure_repository.rb', line 29

def initialize(params)
  @pattern = Regexp.new(params[:pattern])
  @response = params[:failure]
  @response_hash = params[:response_hash] || {}
  @response_headers_hash = params[:response_headers_hash] || {}
end

Instance Attribute Details

#expiration_dateObject

Returns the value of attribute expiration_date.



27
28
29
# File 'lib/aranea/failure_repository.rb', line 27

def expiration_date
  @expiration_date
end

Class Method Details

.create(params) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
# File 'lib/aranea/failure_repository.rb', line 13

def create(params)
  failure = retrieve_failure
  raise FailureFailure, "A failure is already in progress (#{failure})." if failure

  failure = new(params)
  Repository.store(failure, params[:minutes].minutes)
  failure
end

.currentObject



9
10
11
# File 'lib/aranea/failure_repository.rb', line 9

def current
  retrieve_failure || NullFailure.new
end

.retrieve_failureObject



22
23
24
# File 'lib/aranea/failure_repository.rb', line 22

def retrieve_failure
  Repository.get
end

Instance Method Details

#respond!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aranea/failure_repository.rb', line 40

def respond!
  case @response
  when "timeout"
    raise ::Faraday::TimeoutError, "Fake failure from Aranea"
  when "ssl_error"
    raise ::Faraday::SSLError, "Fake failure from Aranea"
  else
    ::Faraday::Response.new(
      status: @response.to_i,
      body: @response_hash.to_json,
      response_headers: @response_headers_hash
    )
  end
end

#should_fail?(request_env, _app) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/aranea/failure_repository.rb', line 36

def should_fail?(request_env, _app)
  @pattern.match(request_env[:url].to_s)
end

#to_sObject



55
56
57
# File 'lib/aranea/failure_repository.rb', line 55

def to_s
  "Failure on #{@pattern.inspect} ending at approximately #{@expiration_date}"
end