class Uid2::Client

Attributes

base_url[RW]
bearer_token[RW]

Public Class Methods

new(_options = {}) { |self| ... } click to toggle source
# File lib/uid2/client.rb, line 12
def initialize(_options = {})
  yield(self) if block_given?

  self.base_url ||= "https://integ.uidapi.com/v1/"
end

Public Instance Methods

batch_generate_identifier(email: nil, email_hash: nil) click to toggle source
# File lib/uid2/client.rb, line 67
def batch_generate_identifier(email: nil, email_hash: nil)
  raise ArgumentError, "Either email or email_hash needs to be provided" if email.nil? && email_hash.nil?

  # As stated in doc, if email and email_hash are both supplied in the same request,
  # only the email will return a mapping response.
  params = if email.empty?
             { email_hash: Array(email_hash) }
           else
             { email: Array(email) }
           end

  http.post("identity/map", params).body
end
generate_identifier(email: nil, email_hash: nil) click to toggle source
# File lib/uid2/client.rb, line 53
def generate_identifier(email: nil, email_hash: nil)
  raise ArgumentError, "Either email or email_hash needs to be provided" if email.nil? && email_hash.nil?

  # As stated in doc, if email and email_hash are both supplied in the same request,
  # only the email will return a mapping response.
  params = if email.empty?
             { email_hash: email_hash }
           else
             { email: email }
           end

  http.get("identity/map", params).body
end
generate_token(email: nil, email_hash: nil) click to toggle source
# File lib/uid2/client.rb, line 18
def generate_token(email: nil, email_hash: nil)
  raise ArgumentError, "Either email or email_hash needs to be provided" if email.nil? && email_hash.nil?

  # As stated in doc, if email and email_hash are both supplied in the same request,
  # only the email will return a mapping response.
  params = if email.empty?
             { email_hash: email_hash }
           else
             { email: email }
           end
  http.get("token/generate", params).body
end
get_salt_buckets(since: Time.now) click to toggle source
# File lib/uid2/client.rb, line 47
def get_salt_buckets(since: Time.now)
  # By default, Ruby's iso8601 generates timezone parts (`T`)
  # which needs to be removed for UID2 APIs
  http.get("identity/buckets", since_timestamp: since.utc.iso8601[0..-2]).body
end
refresh_token(refresh_token:) click to toggle source
# File lib/uid2/client.rb, line 43
def refresh_token(refresh_token:)
  http.get("token/refresh", { refresh_token: refresh_token }).body
end
validate_token(token:, email: nil, email_hash: nil) click to toggle source
# File lib/uid2/client.rb, line 31
def validate_token(token:, email: nil, email_hash: nil)
  raise ArgumentError, "Either email or email_hash needs to be provided" if email.nil? && email_hash.nil?

  params = if email.empty?
             { email_hash: email_hash }
           else
             { email: email }
           end

  http.get("token/validate", params.merge(token: token)).body
end

Private Instance Methods

credentials() click to toggle source
# File lib/uid2/client.rb, line 83
def credentials
  {
    "Authorization" => "Bearer #{bearer_token}"
  }
end
http() click to toggle source
# File lib/uid2/client.rb, line 89
def http
  @http ||= Faraday.new(
    url: base_url,
    headers: credentials
  ) do |f|
    f.request :json

    f.response :raise_error
    f.response :mashify
    f.response :json

    f.adapter :net_http_persistent
  end
end