cognito-client 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/Gemfile +37 -0
- data/Guardfile +4 -0
- data/README.md +55 -18
- data/Rakefile +3 -5
- data/bin/console +1 -1
- data/circle.yml +7 -0
- data/cognito.gemspec +4 -6
- data/config/devtools.yml +2 -0
- data/config/errors.yml +2 -0
- data/config/flay.yml +5 -0
- data/config/flog.yml +5 -0
- data/config/mutant.yml +6 -0
- data/config/reek.yml +111 -0
- data/config/rubocop.yml +91 -0
- data/config/yardstick.yml +2 -0
- data/lib/cognito/client.rb +59 -96
- data/lib/cognito/client/command.rb +34 -0
- data/lib/cognito/client/commands/create_identity.rb +22 -0
- data/lib/cognito/client/commands/create_identity_assessment.rb +26 -0
- data/lib/cognito/client/commands/create_identity_search.rb +37 -0
- data/lib/cognito/client/commands/create_profile.rb +24 -0
- data/lib/cognito/client/commands/mixins/create_behavior.rb +20 -0
- data/lib/cognito/client/commands/retrieve_identity_location.rb +28 -0
- data/lib/cognito/client/commands/retrieve_identity_search_job.rb +40 -0
- data/lib/cognito/client/connection.rb +36 -0
- data/lib/cognito/client/document.rb +32 -0
- data/lib/cognito/client/params.rb +13 -0
- data/lib/cognito/client/params/identity.rb +48 -0
- data/lib/cognito/client/params/identity_assessment.rb +23 -0
- data/lib/cognito/client/params/identity_search.rb +25 -0
- data/lib/cognito/client/params/omitted.rb +9 -0
- data/lib/cognito/client/request.rb +98 -0
- data/lib/cognito/client/resource.rb +124 -0
- data/lib/cognito/client/resource/identity_assessment.rb +53 -0
- data/lib/cognito/client/resource/identity_search.rb +64 -0
- data/lib/cognito/client/resource/identity_search_job.rb +12 -0
- data/lib/cognito/client/resource/profile.rb +10 -0
- data/lib/cognito/client/resource_identifier.rb +17 -0
- data/lib/cognito/client/response.rb +17 -0
- data/lib/cognito/client/response/builder.rb +48 -0
- data/lib/cognito/client/response/identity_assessment.rb +10 -0
- data/lib/cognito/client/response/identity_search.rb +32 -0
- data/lib/cognito/client/response/identity_search_job.rb +20 -0
- data/lib/cognito/client/response/profile.rb +10 -0
- data/lib/cognito/version.rb +3 -3
- metadata +77 -83
- data/.rubocop.yml +0 -57
- data/.travis.yml +0 -4
- data/lib/cognito.rb +0 -32
- data/lib/cognito/cleaner.rb +0 -30
- data/lib/cognito/constants.rb +0 -8
- data/lib/cognito/document.rb +0 -33
- data/lib/cognito/error.rb +0 -8
- data/lib/cognito/notary.rb +0 -56
- data/lib/cognito/resource.rb +0 -129
- data/lib/cognito/resource/identity_assessment.rb +0 -40
- data/lib/cognito/resource/identity_search.rb +0 -63
- data/lib/cognito/resource/identity_search_job.rb +0 -11
- data/lib/cognito/resource/profile.rb +0 -9
- data/lib/cognito/responder.rb +0 -61
data/lib/cognito/client.rb
CHANGED
@@ -1,104 +1,67 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'concord'
|
5
|
+
require 'anima'
|
6
|
+
require 'procto'
|
7
|
+
require 'adamantium'
|
8
|
+
require 'abstract_type'
|
9
|
+
require 'http'
|
10
|
+
require 'addressable'
|
11
|
+
require 'openssl'
|
12
|
+
require 'base64'
|
13
|
+
|
14
|
+
require 'cognito/client/connection'
|
15
|
+
require 'cognito/client/request'
|
16
|
+
require 'cognito/client/document'
|
17
|
+
require 'cognito/client/resource_identifier'
|
18
|
+
|
19
|
+
# resources
|
20
|
+
require 'cognito/client/resource'
|
21
|
+
require 'cognito/client/resource/profile'
|
22
|
+
require 'cognito/client/resource/identity_search'
|
23
|
+
require 'cognito/client/resource/identity_search_job'
|
24
|
+
require 'cognito/client/resource/identity_assessment'
|
25
|
+
|
26
|
+
# responses
|
27
|
+
require 'cognito/client/response'
|
28
|
+
require 'cognito/client/response/builder'
|
29
|
+
require 'cognito/client/response/profile'
|
30
|
+
require 'cognito/client/response/identity_assessment'
|
31
|
+
require 'cognito/client/response/identity_search'
|
32
|
+
require 'cognito/client/response/identity_search_job'
|
33
|
+
|
34
|
+
# params
|
35
|
+
require 'cognito/client/params'
|
36
|
+
require 'cognito/client/params/omitted'
|
37
|
+
require 'cognito/client/params/identity'
|
38
|
+
require 'cognito/client/params/identity_assessment'
|
39
|
+
require 'cognito/client/params/identity_search'
|
40
|
+
|
41
|
+
# commands
|
42
|
+
require 'cognito/client/command'
|
43
|
+
require 'cognito/client/commands/mixins/create_behavior'
|
44
|
+
require 'cognito/client/commands/create_profile'
|
45
|
+
require 'cognito/client/commands/create_identity'
|
46
|
+
require 'cognito/client/commands/create_identity_assessment'
|
47
|
+
require 'cognito/client/commands/create_identity_search'
|
48
|
+
require 'cognito/client/commands/retrieve_identity_search_job'
|
49
|
+
require 'cognito/client/commands/retrieve_identity_location'
|
50
|
+
|
51
|
+
class Cognito
|
4
52
|
class Client
|
5
|
-
include
|
6
|
-
Responder
|
7
|
-
|
8
|
-
URI = 'https://sandbox.cognitohq.com'
|
9
|
-
|
10
|
-
# Default URI, can be override with Client#base_uri
|
11
|
-
base_uri URI
|
12
|
-
|
13
|
-
# Don't follow redirects.
|
14
|
-
no_follow true
|
15
|
-
follow_redirects false
|
16
|
-
|
17
|
-
def initialize(options = {})
|
18
|
-
@errors = []
|
19
|
-
@api_key = options.fetch(:api_key) { @errors << :api_key }
|
20
|
-
@api_secret = options.fetch(:api_secret) { @errors << :api_secret }
|
21
|
-
self.base_uri = options.fetch(:base_uri) { URI }
|
22
|
-
|
23
|
-
if @errors.any?
|
24
|
-
raise ArgumentError, "missing keyword: #{@errors.join(',')}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def base_uri=(uri)
|
29
|
-
self.class.base_uri(uri)
|
30
|
-
end
|
31
|
-
|
32
|
-
def create_profile!
|
33
|
-
post('/profiles', profile_params.to_json)
|
34
|
-
end
|
35
|
-
|
36
|
-
def search!(profile_id, phone_number, options = {})
|
37
|
-
payload = search_params(profile_id, phone_number).to_json
|
38
|
-
post('/identity_searches', payload, options)
|
39
|
-
end
|
40
|
-
|
41
|
-
def search_status!(search_job_id, options = {})
|
42
|
-
get("/identity_searches/jobs/#{search_job_id}", options)
|
43
|
-
end
|
44
|
-
|
45
|
-
def retrieve_search(search_id, options = {})
|
46
|
-
get("/identity_searches/#{search_id}", options)
|
47
|
-
end
|
48
|
-
|
49
|
-
protected
|
50
|
-
|
51
|
-
def get(path, options = {})
|
52
|
-
headers = notarize_request('get', path, '').headers
|
53
|
-
response_from(self.class.get(path, headers: headers), options)
|
54
|
-
end
|
55
|
-
|
56
|
-
def post(path, payload, options = {})
|
57
|
-
headers = notarize_request('post', path, payload).headers
|
58
|
-
response_from(self.class.post(path, headers: headers, body: payload), options)
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
def notarize_request(verb, path, body)
|
64
|
-
target = "#{verb} #{path}"
|
53
|
+
include Concord.new(:server)
|
65
54
|
|
66
|
-
|
67
|
-
|
68
|
-
api_secret: @api_secret,
|
69
|
-
target: target,
|
70
|
-
body: body
|
71
|
-
)
|
55
|
+
def self.create(**connection_params)
|
56
|
+
new(Connection.parse(**connection_params))
|
72
57
|
end
|
73
58
|
|
74
|
-
def
|
75
|
-
|
76
|
-
data: {
|
77
|
-
type: PROFILE
|
78
|
-
}
|
79
|
-
}
|
59
|
+
def create_profile
|
60
|
+
Command::CreateProfile.call(server)
|
80
61
|
end
|
81
62
|
|
82
|
-
|
83
|
-
|
84
|
-
{
|
85
|
-
data: {
|
86
|
-
type: IDENTITY_SEARCH,
|
87
|
-
attributes: {
|
88
|
-
phone: {
|
89
|
-
number: phone_number
|
90
|
-
}
|
91
|
-
},
|
92
|
-
relationships: {
|
93
|
-
profile: {
|
94
|
-
data: {
|
95
|
-
type: PROFILE,
|
96
|
-
id: profile_id
|
97
|
-
}
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}
|
101
|
-
}
|
63
|
+
def create_identity_search(**params)
|
64
|
+
Command::CreateIdentitySearch.call(params.merge(connection: server))
|
102
65
|
end
|
103
|
-
end
|
104
|
-
end
|
66
|
+
end # Client
|
67
|
+
end # Cognito
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
include Procto.call(:run), AbstractType, Adamantium
|
6
|
+
|
7
|
+
Failed = Class.new(StandardError)
|
8
|
+
|
9
|
+
def run
|
10
|
+
fail Failed, failure_reason unless success?
|
11
|
+
|
12
|
+
present_response
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def failure_reason
|
18
|
+
return response.reason if response.to_str.empty?
|
19
|
+
|
20
|
+
response
|
21
|
+
end
|
22
|
+
|
23
|
+
abstract_method :present_response
|
24
|
+
abstract_method :success?
|
25
|
+
abstract_method :request
|
26
|
+
abstract_method :connection
|
27
|
+
|
28
|
+
def response
|
29
|
+
connection.run(request)
|
30
|
+
end
|
31
|
+
memoize :response, freezer: :flat
|
32
|
+
end # Command
|
33
|
+
end # Client
|
34
|
+
end # Cognito
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
class Cognito
|
4
|
+
class Client
|
5
|
+
class Command
|
6
|
+
class CreateIdentity < self
|
7
|
+
include Mixins::CreateBehavior,
|
8
|
+
AbstractType,
|
9
|
+
Anima.new(:connection, :name, :phone_number)
|
10
|
+
|
11
|
+
private :name, :phone_number
|
12
|
+
|
13
|
+
OMITTED = Params::Omitted.new
|
14
|
+
|
15
|
+
# ignores :reek:FeatureEnvy:
|
16
|
+
def initialize(name: OMITTED, phone_number: OMITTED, **params)
|
17
|
+
super(params.merge(name: name, phone_number: phone_number))
|
18
|
+
end
|
19
|
+
end # CreateIdentity
|
20
|
+
end # Command
|
21
|
+
end # Client
|
22
|
+
end # Cognito
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
class CreateIdentityAssessment < CreateIdentity
|
6
|
+
include Mixins::CreateBehavior, anima.add(:identity_search_identifier)
|
7
|
+
|
8
|
+
ENDPOINT = '/identity_assessments'
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def params
|
13
|
+
Params::IdentityAssessment.call(
|
14
|
+
identity_search_identifier: identity_search_identifier,
|
15
|
+
name: name,
|
16
|
+
phone_number: phone_number
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def present_response
|
21
|
+
Response::IdentityAssessment.build(response, connection)
|
22
|
+
end
|
23
|
+
end # CreateIdentityAssessment
|
24
|
+
end # Command
|
25
|
+
end # Client
|
26
|
+
end # Cognito
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
class CreateIdentitySearch < CreateIdentity
|
6
|
+
include Mixins::CreateBehavior, anima.add(:profile_id)
|
7
|
+
|
8
|
+
private :profile_id, :name, :phone_number
|
9
|
+
|
10
|
+
ENDPOINT = '/identity_searches'
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def params
|
15
|
+
Params::IdentitySearch.call(
|
16
|
+
profile_id: profile_id,
|
17
|
+
name: name,
|
18
|
+
phone_number: phone_number
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
def present_response
|
23
|
+
case response.code
|
24
|
+
when 202
|
25
|
+
Response::IdentitySearchJob.build(response, connection)
|
26
|
+
when 201
|
27
|
+
Response::IdentitySearch.build(response, connection)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def success?
|
32
|
+
response.status.accepted? || response.status.created?
|
33
|
+
end
|
34
|
+
end # CreateIdentitySearch
|
35
|
+
end # Command
|
36
|
+
end # Client
|
37
|
+
end # Cognito
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
class CreateProfile < self
|
6
|
+
include Mixins::CreateBehavior, Concord.new(:connection)
|
7
|
+
|
8
|
+
ENDPOINT = '/profiles'
|
9
|
+
PARAMS = IceNine.deep_freeze(data: { type: 'profile' })
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def present_response
|
14
|
+
Response::Profile.build(response, connection)
|
15
|
+
end
|
16
|
+
|
17
|
+
# ignores :reek:UtilityFunction:
|
18
|
+
def params
|
19
|
+
PARAMS
|
20
|
+
end
|
21
|
+
end # CreateProfile
|
22
|
+
end # Command
|
23
|
+
end # Client
|
24
|
+
end # Cognito
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
module Mixins
|
6
|
+
module CreateBehavior
|
7
|
+
private
|
8
|
+
|
9
|
+
def success?
|
10
|
+
response.status.created?
|
11
|
+
end
|
12
|
+
|
13
|
+
def request
|
14
|
+
Request.post(self.class::ENDPOINT, params)
|
15
|
+
end
|
16
|
+
end # CreateBehavior
|
17
|
+
end # Mixins
|
18
|
+
end # Command
|
19
|
+
end # Client
|
20
|
+
end # Cognito
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Command
|
5
|
+
class RetrieveIdentityLocation < self
|
6
|
+
include Concord.new(:connection, :endpoint)
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def present_response
|
11
|
+
identity_resource
|
12
|
+
end
|
13
|
+
|
14
|
+
def identity_resource
|
15
|
+
Response::IdentitySearch.build(response, connection)
|
16
|
+
end
|
17
|
+
|
18
|
+
def success?
|
19
|
+
response.code.equal?(200)
|
20
|
+
end
|
21
|
+
|
22
|
+
def request
|
23
|
+
Request.get(endpoint)
|
24
|
+
end
|
25
|
+
end # RetrieveIdentityLocation
|
26
|
+
end # Command
|
27
|
+
end # Client
|
28
|
+
end # Cognito
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Cognito
|
4
|
+
class Client
|
5
|
+
class Command
|
6
|
+
class RetrieveIdentitySearchJob < self
|
7
|
+
include Concord.new(:connection, :endpoint)
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def present_response
|
12
|
+
case response.code
|
13
|
+
when 303
|
14
|
+
identity_resource
|
15
|
+
when 200
|
16
|
+
processing_response
|
17
|
+
else
|
18
|
+
fail Failed, response.reason
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def identity_resource
|
23
|
+
RetrieveIdentityLocation.call(connection, response['Location'])
|
24
|
+
end
|
25
|
+
|
26
|
+
def processing_response
|
27
|
+
Response::IdentitySearchJob.build(response, connection)
|
28
|
+
end
|
29
|
+
|
30
|
+
def success?
|
31
|
+
response.status.success? || response.status.redirect?
|
32
|
+
end
|
33
|
+
|
34
|
+
def request
|
35
|
+
Request.get(endpoint)
|
36
|
+
end
|
37
|
+
end # RetrieveIdentitySearchJob
|
38
|
+
end # Command
|
39
|
+
end # Client
|
40
|
+
end # Cognito
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Cognito
|
3
|
+
class Client
|
4
|
+
class Connection
|
5
|
+
include Anima.new(:uri, :api_key, :api_secret, :api_version)
|
6
|
+
|
7
|
+
def self.parse(uri:, **api_params)
|
8
|
+
new(uri: Addressable::URI.parse(uri), **api_params)
|
9
|
+
end
|
10
|
+
|
11
|
+
# ignores :reek:FeatureEnvy:
|
12
|
+
def run(request)
|
13
|
+
signed_request =
|
14
|
+
request.sign(date: Time.now.httpdate, api_key: api_key, api_secret: api_secret)
|
15
|
+
|
16
|
+
HTTP
|
17
|
+
.headers(signed_request.headers.merge(version_headers))
|
18
|
+
.request(
|
19
|
+
signed_request.verb,
|
20
|
+
endpoint(signed_request.endpoint),
|
21
|
+
json: signed_request.data
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def endpoint(route)
|
26
|
+
uri.join(route).to_str
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def version_headers
|
32
|
+
{ 'Cognito-Version' => api_version }
|
33
|
+
end
|
34
|
+
end # Connection
|
35
|
+
end # Client
|
36
|
+
end # Cognito
|