Class: InformantCommon::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/informant-common/client.rb

Constant Summary collapse

SUPPORTED_REQUEST_METHODS =
%w[POST PATCH PUT DELETE].freeze

Class Method Summary collapse

Class Method Details

.current_transactionObject



26
27
28
# File 'lib/informant-common/client.rb', line 26

def self.current_transaction
  Thread.current[:informant_transaction] ||= InformantCommon::Event::MockFormSubmission.new
end

.disable!Object



9
10
11
# File 'lib/informant-common/client.rb', line 9

def self.disable!
  @enabled = false
end

.enable!Object



13
14
15
# File 'lib/informant-common/client.rb', line 13

def self.enable!
  @enabled = true
end

.enabled?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/informant-common/client.rb', line 5

def self.enabled?
  @enabled
end

.processObject



34
35
36
37
38
39
40
41
# File 'lib/informant-common/client.rb', line 34

def self.process
  if current_transaction.valid?
    this_transaction = current_transaction
    transmit(this_transaction)
  end
ensure
  reset_transaction!
end

.reset_transaction!Object



30
31
32
# File 'lib/informant-common/client.rb', line 30

def self.reset_transaction!
  Thread.current[:informant_transaction] = nil
end

.start_transaction!(request_method) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/informant-common/client.rb', line 18

def self.start_transaction!(request_method)
  if enabled? && SUPPORTED_REQUEST_METHODS.include?(request_method)
    Thread.current[:informant_transaction] = InformantCommon::Event::FormSubmission.new
  else
    reset_transaction!
  end
end

.transmit(event) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/informant-common/client.rb', line 43

def self.transmit(event)
  Thread.new(Client) do |client_class|
    Net::HTTP.start(*event.net_http_start_arguments) do |http|
      response = http.request(event.post_request)
      client_class.disable! if response.code == '401'
      response
    end
  end
end