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.



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

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.



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

def endpoint
  @endpoint
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Method Details

#collect(request:, event:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadome_fraud_sdk_ruby.rb', line 63

def collect(request:, event:)
  if event.respond_to?(:status) && 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(status: DataDomeResponseStatus::OK)
    else
      begin
        body = JSON.parse(api_response.body)
        @logger.error("DataDome: error on Fraud API /collect response #{body["errors"]}")
        datadome_response = DataDomeResponseError.new(status: DataDomeResponseStatus::FAILURE, message: body["message"], errors: 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(status: DataDomeResponseStatus::FAILURE, message: "DataDome: error parsing JSON from Fraud API", errors: e.message)
      end
    end
  rescue Faraday::TimeoutError => e
    datadome_response = DataDomeResponseError.new(status: DataDomeResponseStatus::TIMEOUT, message: "DataDome Fraud API request timeout", errors: e.message)
  rescue Faraday::ConnectionFailed => e
    datadome_response = DataDomeResponseError.new(status: DataDomeResponseStatus::FAILURE, message: "DataDome Fraud API request failed", errors: e.message)
  end

  datadome_response
end

#validate(request:, event:) ⇒ Object



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
56
57
58
59
60
61
# File 'lib/datadome_fraud_sdk_ruby.rb', line 23

def validate(request:, event:)
  if event.respond_to?(:status) && 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(status: DataDomeResponseStatus::FAILURE, message: "DataDome: error parsing JSON from Fraud API", errors: e.message)
    end

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

  datadome_response
end