Class: DataDome

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 1500, endpoint: "account-api.datadome.co", logger: Logger.new(STDOUT)) ⇒ DataDome

Returns a new instance of DataDome.



16
17
18
19
20
# File 'lib/datadome_fraud_sdk_ruby.rb', line 16

def initialize(timeout: 1500, endpoint: "account-api.datadome.co", logger: Logger.new(STDOUT))
  @timeout = timeout
  @endpoint = endpoint
  @logger = logger
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



14
15
16
# File 'lib/datadome_fraud_sdk_ruby.rb', line 14

def endpoint
  @endpoint
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/datadome_fraud_sdk_ruby.rb', line 14

def logger
  @logger
end

#timeoutObject

Returns the value of attribute timeout.



14
15
16
# File 'lib/datadome_fraud_sdk_ruby.rb', line 14

def timeout
  @timeout
end

Instance Method Details

#collect(request:, event:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/datadome_fraud_sdk_ruby.rb', line 57

def collect(request:, event:)
  if event.status == DataDomeStatusType::UNDEFINED
    event.status = DataDomeStatusType::FAILED
  end

  payload = build_payload(request, event)

  begin
    api_response = send_request(DataDomeOperationType::COLLECT, event.action, payload)

    if api_response.success?
      datadome_response = DataDomeResponseSuccess.new(nil, DataDomeResponseStatus::OK, nil, nil, nil)
    else
      begin
        body = JSON.parse(api_response.body)
        @logger.error("DataDome: error on Fraud API /collect response #{body["errors"]}")
        datadome_response = DataDomeResponseError.new(nil, DataDomeResponseStatus::FAILURE, body["message"], body["errors"].map { |error| DataDomeError.new(error) })
      rescue JSON::ParserError => e
        @logger.error("DataDome: error parsing JSON from Fraud API /collect #{e.message}")
        return DataDomeResponseError.new(nil, DataDomeResponseStatus::FAILURE, "DataDome: error parsing JSON from Fraud API", e.message)
      end
    end
  rescue Faraday::TimeoutError => e
    datadome_response = DataDomeResponseError.new(nil, DataDomeResponseStatus::TIMEOUT, "DataDome Fraud API request timeout", e.message)
  rescue Faraday::ConnectionFailed => e
    datadome_response = DataDomeResponseError.new(nil, DataDomeResponseStatus::FAILURE, "DataDome Fraud API request failed", e.message)
  end

  datadome_response
end

#validate(request:, event:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datadome_fraud_sdk_ruby.rb', line 22

def validate(request:, event:)
  if event.status == DataDomeStatusType::UNDEFINED
    event.status = DataDomeStatusType::SUCCEEDED
  end

  payload = build_payload(request, event)

  begin
    api_response = send_request(DataDomeOperationType::VALIDATE, event.action, payload)

    begin
      body = JSON.parse(api_response.body) unless api_response.body.nil?
    rescue JSON::ParserError => e
      @logger.error("DataDome: error parsing JSON from Fraud API /validate #{e.message}")
      return DataDomeResponseError.new(DataDomeResponseAction::ALLOW, DataDomeResponseStatus::FAILURE, "DataDome: error parsing JSON from Fraud API", e.message)
    end

    if api_response.success?
      datadome_response = DataDomeResponseSuccess.new(DataDomeResponseAction::ALLOW, DataDomeResponseStatus::OK, body["reasons"], body["ip"], DataDomeLocation.new(location: body["location"]))
      if body["action"] === DataDomeResponseAction::DENY
        datadome_response.action = DataDomeResponseAction::DENY
      end
    else
      @logger.error("DataDome: error on Fraud API /validate response #{body["errors"]}")
      return DataDomeResponseError.new(DataDomeResponseAction::ALLOW, DataDomeResponseStatus::FAILURE, body["message"], body["errors"].map { |error| DataDomeError.new(error) })
    end
  rescue Faraday::TimeoutError => e
    return DataDomeResponseError.new(DataDomeResponseAction::ALLOW, DataDomeResponseStatus::TIMEOUT, "DataDome Fraud API request timeout", e.message)
  rescue Faraday::ConnectionFailed => e
    return DataDomeResponseError.new(DataDomeResponseAction::ALLOW, DataDomeResponseStatus::FAILURE, "DataDome Fraud API request failed", e.message)
  end

  datadome_response
end