Class: Datadome::ValidationResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/datadome/validation_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ValidationResponse

Returns a new instance of ValidationResponse.



44
45
46
47
48
49
50
51
# File 'lib/datadome/validation_response.rb', line 44

def initialize(attrs = {})
  self.request_headers = {}
  self.response_headers = {}

  attrs.each do |key, value|
    public_send("#{key}=", value)
  end
end

Instance Attribute Details

#passObject

Returns the value of attribute pass.



41
42
43
# File 'lib/datadome/validation_response.rb', line 41

def pass
  @pass
end

#redirectObject

Returns the value of attribute redirect.



41
42
43
# File 'lib/datadome/validation_response.rb', line 41

def redirect
  @redirect
end

#redirection_locationObject

Returns the value of attribute redirection_location.



42
43
44
# File 'lib/datadome/validation_response.rb', line 42

def redirection_location
  @redirection_location
end

#request_headersObject

Returns the value of attribute request_headers.



42
43
44
# File 'lib/datadome/validation_response.rb', line 42

def request_headers
  @request_headers
end

#response_bodyObject

Returns the value of attribute response_body.



42
43
44
# File 'lib/datadome/validation_response.rb', line 42

def response_body
  @response_body
end

#response_headersObject

Returns the value of attribute response_headers.



42
43
44
# File 'lib/datadome/validation_response.rb', line 42

def response_headers
  @response_headers
end

#response_statusObject

Returns the value of attribute response_status.



42
43
44
# File 'lib/datadome/validation_response.rb', line 42

def response_status
  @response_status
end

Class Method Details

.from_faraday_response(response) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datadome/validation_response.rb', line 12

def from_faraday_response(response)
  validation_response =
    if response.status == 403
      new(pass: false, redirect: false, response_status: 403, response_body: response.body)
    elsif response.status == 301 || response.status == 302
      new(pass: false, redirect: true, response_status: response.status, redirection_location: response.headers["Location"])
    else
      pass
    end

  parse_headers_list(response.headers["X-DataDome-request-headers"]).each do |key|
    validation_response.request_headers[key] = response.headers[key]
  end

  parse_headers_list(response.headers["X-DataDome-headers"]).each do |key|
    validation_response.response_headers[key] = response.headers[key]
  end

  validation_response
end

.parse_headers_list(list) ⇒ Object



33
34
35
36
37
# File 'lib/datadome/validation_response.rb', line 33

def parse_headers_list(list)
  return [] if list.nil? || list == ""

  list.split(" ")
end

.passObject



8
9
10
# File 'lib/datadome/validation_response.rb', line 8

def pass
  new(pass: true, redirect: false)
end

Instance Method Details

#to_rack_responseObject



53
54
55
56
57
58
# File 'lib/datadome/validation_response.rb', line 53

def to_rack_response
  response = ::Rack::Response.new(@response_body || [], @response_status, @response_headers)
  response.redirect(@redirection_location, @response_status) if @redirect

  response.finish
end