Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.4
TargetRubyVersion: 2.7

Style/StringLiterals:
Enabled: true
Expand Down
6 changes: 1 addition & 5 deletions lib/uid2.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# frozen_string_literal: true

require_relative "uid2/version"

module Uid2
class Error < StandardError; end
# Your code goes here...
end
require_relative "uid2/client"
104 changes: 104 additions & 0 deletions lib/uid2/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# frozen_string_literal: true

require "net/http/persistent"
require "faraday"
require "faraday_middleware"
require "time"

module Uid2
class Client
attr_accessor :bearer_token, :base_url

def initialize(_options = {})
yield(self) if block_given?

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

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

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

def refresh_token(refresh_token:)
http.get("token/refresh", { refresh_token: refresh_token }).body
end

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

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

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

private

def credentials
{
"Authorization" => "Bearer #{bearer_token}"
}
end

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
end
end
27 changes: 11 additions & 16 deletions uid2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@ Gem::Specification.new do |spec|
spec.authors = ["Richard Lee"]
spec.email = ["[email protected]"]

spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = "Ruby API client for Unified ID 2.0"
spec.description = spec.summary
spec.homepage = "https://github.com/polydice/uid2"
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")

spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "https://github.com/polydice/uid2/blob/master/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:spec|features)/}) }
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"

# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
spec.add_dependency "faraday", ">= 1"
spec.add_dependency "faraday_middleware", ">= 1"
spec.add_dependency "hashie", ">= 4"
spec.add_dependency "net-http-persistent", ">= 4"
end