Class: Emailage::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret, token, options = {}) ⇒ Client

Note:

HMAC key is created according to Emailage docs.

Returns a new instance of Client.

Parameters:

  • secret (String)

    Consumer secret, e.g. SID or API key.

  • token (String)

    Consumer OAuth token.

  • sandbox (Boolean)

    Whether to use a sandbox instead of a production server. Ensure the according secret and token are supplied.



17
18
19
20
21
22
23
24
# File 'lib/emailage/client.rb', line 17

def initialize(secret, token, options={})
  @secret, @token = secret, token
  @sandbox = options.fetch :sandbox, false
  @raise_errors = options.fetch :raise_errors, false
  
  # @hmac_key = [@secret, @token].map {|e| CGI.escape(e)}.join '&'
  @hmac_key = @token + '&'
end

Instance Attribute Details

#hmac_keyObject (readonly)

Returns the value of attribute hmac_key.



7
8
9
# File 'lib/emailage/client.rb', line 7

def hmac_key
  @hmac_key
end

#raise_errorsObject

Returns the value of attribute raise_errors.



8
9
10
# File 'lib/emailage/client.rb', line 8

def raise_errors
  @raise_errors
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



7
8
9
# File 'lib/emailage/client.rb', line 7

def sandbox
  @sandbox
end

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/emailage/client.rb', line 7

def secret
  @secret
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/emailage/client.rb', line 7

def token
  @token
end

Instance Method Details

#flag(flag, query, fraud_code = nil) ⇒ Object

Mark an email address as fraud, good, or neutral.

Parameters:

  • flag (String)

    Either fraud, neutral, or good.

  • query (String)

    Email to be flagged.

  • fraud_code (Integer | String) (defaults to: nil)

    Reason why the Email or IP is considered fraud. ID or name of the one of FRAUD_CODES options. E.g. 8 or “Syntethic ID” for Syntethic ID Required only if you flag something as fraud.

See Also:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/emailage/client.rb', line 116

def flag(flag, query, fraud_code=nil)
  flags = %w[fraud neutral good]
  unless flags.include? flag.to_s
    raise ArgumentError, "flag must be one of #{flags * ', '}. #{flag} is given."
  end
  
  Validation.validate_email! query
  
  query *= '+' if query.is_a? Array
  params = {:flag => flag, :query => query}
  
  if flag == 'fraud'
    unless (1..9).to_a.include? fraud_code
      raise ArgumentError, "fraud_code must be an integer from 1 to 9 corresponding to #{FRAUD_CODES.values*', '}. #{fraud_code} is given."
    end
    params[:fraudcodeID] = fraud_code
  end
  
  request '/flag', params
end

#flag_as_fraud(query, fraud_code) ⇒ Object

Mark an email address as fraud.

Parameters:

  • query (String)

    Email to be flagged.

  • fraud_code (Integer | String)

    Reason why the Email or IP is considered fraud. ID or name of the one of FRAUD_CODES options. E.g. 8 or “Syntethic ID” for Syntethic ID

See Also:



144
145
146
# File 'lib/emailage/client.rb', line 144

def flag_as_fraud(query, fraud_code)
  flag 'fraud', query, fraud_code
end

#flag_as_good(query) ⇒ Object

Mark an email address as good.

Parameters:

  • query (String)

    Email to be flagged.



152
153
154
# File 'lib/emailage/client.rb', line 152

def flag_as_good(query)
  flag 'good', query
end

#query(query, params = {}) ⇒ Object

Query a risk score information for the provided email address, IP address, or a combination.

Parameters:

  • query (String | Array<String>)

    Email, IP or a tuple of (Email, IP).

  • params (Hash) (defaults to: {})

    Extra request params as in API documentation.

  • urid (Hash)

    a customizable set of options



63
64
65
66
# File 'lib/emailage/client.rb', line 63

def query(query, params={})
  query *= '+' if query.is_a? Array
  request '', params.merge(:query => query)
end

#query_email(email, params = {}) ⇒ Object

Query a risk score information for the provided email address. This method differs from #query in that it ensures that the string supplied is in rfc2822 format.

Parameters:

  • email (String)
  • params (Hash) (defaults to: {})

    Extra request params as in API documentation.

  • urid (Hash)

    a customizable set of options



75
76
77
78
# File 'lib/emailage/client.rb', line 75

def query_email(email, params={})
  Validation.validate_email! email
  query email, params
end

#query_email_and_ip_address(email, ip, params = {}) ⇒ Object

Query a risk score information for the provided combination of an Email and IP address. This method differs from #query in that it ensures that the strings supplied are in rfc2822 and rfc791 formats.

Parameters:

  • email (String)
  • ip (String)
  • params (Hash) (defaults to: {})

    Extra request params as in API documentation.

  • urid (Hash)

    a customizable set of options



100
101
102
103
104
# File 'lib/emailage/client.rb', line 100

def query_email_and_ip_address(email, ip, params={})
  Validation.validate_email! email
  Validation.validate_ip! ip
  query [email, ip], params
end

#query_ip_address(ip, params = {}) ⇒ Object

Query a risk score information for the provided IP address. This method differs from #query in that it ensures that the string supplied is in rfc791 format.

Parameters:

  • ip (String)
  • params (Hash) (defaults to: {})

    Extra request params as in API documentation.

  • urid (Hash)

    a customizable set of options



87
88
89
90
# File 'lib/emailage/client.rb', line 87

def query_ip_address(ip, params={})
  Validation.validate_ip! ip
  query ip, params
end

#remove_flag(query) ⇒ Object

Unflag an email address that was marked as good or fraud previously.

Parameters:

  • query (String)

    Email to be flagged.



160
161
162
# File 'lib/emailage/client.rb', line 160

def remove_flag(query)
  flag 'neutral', query
end