cognito-client 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +57 -0
- data/.travis.yml +4 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +7 -0
- data/bin/console +12 -0
- data/bin/setup +8 -0
- data/cognito.gemspec +36 -0
- data/lib/cognito.rb +32 -0
- data/lib/cognito/cleaner.rb +30 -0
- data/lib/cognito/client.rb +104 -0
- data/lib/cognito/constants.rb +8 -0
- data/lib/cognito/document.rb +33 -0
- data/lib/cognito/error.rb +8 -0
- data/lib/cognito/notary.rb +56 -0
- data/lib/cognito/resource.rb +129 -0
- data/lib/cognito/resource/identity_assessment.rb +40 -0
- data/lib/cognito/resource/identity_search.rb +63 -0
- data/lib/cognito/resource/identity_search_job.rb +11 -0
- data/lib/cognito/resource/profile.rb +9 -0
- data/lib/cognito/responder.rb +61 -0
- data/lib/cognito/version.rb +4 -0
- metadata +251 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7f68b740a5ba516167ad1927faf7372906f7a4cd
|
4
|
+
data.tar.gz: fc8c54beb597ceb364c16d8f4474f917c0ddfad2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3bc88bf4ad7896032056f5f6e6c245ef34071953b9e62aa3824dde40e8542e44b904917b3faa4daf1de1f5d8ec9297a8b7bb1af2e076a5cf2e570cf92bdbb81
|
7
|
+
data.tar.gz: e2906b5cfd7bab859723b88368168b047821da3a8b805c902504ae925d54402028b264247791e962f1c28b4f74cfd97a9411fa7f5ed17d64d5b9357dfa24b5e8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
DisplayCopNames: true
|
4
|
+
|
5
|
+
# gemspec is a special snowflake
|
6
|
+
LineLength:
|
7
|
+
Exclude:
|
8
|
+
- cognito.gemspec
|
9
|
+
Max: 80
|
10
|
+
|
11
|
+
ParameterLists:
|
12
|
+
Max: 3
|
13
|
+
CountKeywordArgs: true
|
14
|
+
|
15
|
+
BlockNesting:
|
16
|
+
Max: 3
|
17
|
+
|
18
|
+
BlockDelimiters:
|
19
|
+
EnforcedStyle: 'braces_for_chaining'
|
20
|
+
|
21
|
+
CollectionMethods:
|
22
|
+
Enabled: true
|
23
|
+
PreferredMethods:
|
24
|
+
collect: 'map'
|
25
|
+
inject: 'reduce'
|
26
|
+
find: 'detect'
|
27
|
+
find_all: 'select'
|
28
|
+
|
29
|
+
Documentation:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
# Align if/else blocks with the variable assignment
|
33
|
+
EndAlignment:
|
34
|
+
AlignWith: variable
|
35
|
+
|
36
|
+
# Prefer #kind_of? over #is_a?
|
37
|
+
ClassCheck:
|
38
|
+
EnforcedStyle: kind_of?
|
39
|
+
|
40
|
+
# Prefer
|
41
|
+
#
|
42
|
+
# some_receiver
|
43
|
+
# .foo
|
44
|
+
# .bar
|
45
|
+
# .baz
|
46
|
+
#
|
47
|
+
# Over
|
48
|
+
#
|
49
|
+
# some_receiver.foo
|
50
|
+
# .bar
|
51
|
+
# .baz
|
52
|
+
MultilineMethodCallIndentation:
|
53
|
+
EnforcedStyle: indented
|
54
|
+
|
55
|
+
# Prefer `public_send` and `__send__` over `send`
|
56
|
+
Send:
|
57
|
+
Enabled: true
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Connor Jacobsen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Cognito Client
|
2
|
+
|
3
|
+
Unofficial Ruby client for the BlockScore Cognito API.
|
4
|
+
|
5
|
+
Frankenstein of demo code supplied by BlockScore, and our own stuff. `client.rb` is ours,
|
6
|
+
everything else is theirs. The BlockScore code basically just handles the structuring
|
7
|
+
of their data models.
|
8
|
+
|
9
|
+
Currently in the "make it work" phase of development.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'cognito-client', require: 'cognito'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Create a client
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
client = Cognito::Client.new(api_key: 'API_KEY', api_secret: 'API_SECRET')
|
29
|
+
|
30
|
+
# default base URI is https://sandbox.cognitohq.com
|
31
|
+
#
|
32
|
+
# to set a different API:
|
33
|
+
client.base_uri('SOME_NEW_BASE_URI')
|
34
|
+
```
|
35
|
+
|
36
|
+
Create a Profile:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
profile = client.create_profile!
|
40
|
+
```
|
41
|
+
|
42
|
+
Initiate a search against a phone number:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
profile = client.create_profile!
|
46
|
+
|
47
|
+
search = client.search!(profile.id, '+14151231234')
|
48
|
+
```
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
53
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'cognito'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
require 'pry'
|
12
|
+
Pry.start
|
data/bin/setup
ADDED
data/cognito.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'cognito/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'cognito-client'
|
9
|
+
spec.version = Cognito::VERSION
|
10
|
+
spec.authors = ['Connor Jacobsen']
|
11
|
+
spec.email = ['connor@opendoor.com']
|
12
|
+
|
13
|
+
spec.summary = 'Unofficial Ruby client for the BlockScore Cognito API'
|
14
|
+
spec.homepage = 'https://github.com/opendoor-labs/cognito'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
24
|
+
spec.add_development_dependency 'rake', '~> 11.2'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.5'
|
26
|
+
spec.add_development_dependency 'rubocop', '~> 0.41'
|
27
|
+
spec.add_development_dependency 'timecop', '~> 0.8'
|
28
|
+
spec.add_development_dependency 'rspec-its', '~> 1.2.0'
|
29
|
+
|
30
|
+
spec.add_dependency 'abstract_type', '~> 0.0.7'
|
31
|
+
spec.add_dependency 'adamantium', '~> 0.2.0'
|
32
|
+
spec.add_dependency 'anima', '~> 0.3.0'
|
33
|
+
spec.add_dependency 'concord', '~> 0.1.5'
|
34
|
+
spec.add_dependency 'httparty', '~> 0.13'
|
35
|
+
spec.add_dependency 'procto', '~> 0.0.3'
|
36
|
+
end
|
data/lib/cognito.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
require 'base64'
|
6
|
+
require 'digest'
|
7
|
+
require 'json'
|
8
|
+
require 'openssl'
|
9
|
+
require 'time'
|
10
|
+
|
11
|
+
require 'abstract_type'
|
12
|
+
require 'adamantium'
|
13
|
+
require 'anima'
|
14
|
+
require 'concord'
|
15
|
+
require 'httparty'
|
16
|
+
require 'procto'
|
17
|
+
|
18
|
+
require 'cognito/document'
|
19
|
+
require 'cognito/resource'
|
20
|
+
require 'cognito/resource/profile'
|
21
|
+
require 'cognito/resource/identity_search'
|
22
|
+
require 'cognito/resource/identity_search_job'
|
23
|
+
require 'cognito/resource/identity_assessment'
|
24
|
+
|
25
|
+
require 'cognito/cleaner'
|
26
|
+
require 'cognito/constants'
|
27
|
+
require 'cognito/error'
|
28
|
+
require 'cognito/version'
|
29
|
+
require 'cognito/notary'
|
30
|
+
|
31
|
+
require 'cognito/responder'
|
32
|
+
require 'cognito/client'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Cleaner
|
5
|
+
attr_accessor :response
|
6
|
+
attr_reader :whitelist
|
7
|
+
|
8
|
+
def initialize(response, whitelist)
|
9
|
+
@response = response.dup
|
10
|
+
@whitelist = whitelist
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
# Only do this when we have an identity_search response.
|
15
|
+
return response unless response.key?(:included)
|
16
|
+
|
17
|
+
cleaned_includes = included.select do |record|
|
18
|
+
whitelist.include?(record[:type])
|
19
|
+
end
|
20
|
+
|
21
|
+
response.tap { |r| r[:included] = cleaned_includes }
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def included
|
27
|
+
response[:included]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Client
|
5
|
+
include HTTParty,
|
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}"
|
65
|
+
|
66
|
+
Notary.new(
|
67
|
+
api_key: @api_key,
|
68
|
+
api_secret: @api_secret,
|
69
|
+
target: target,
|
70
|
+
body: body
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def profile_params
|
75
|
+
{
|
76
|
+
data: {
|
77
|
+
type: PROFILE
|
78
|
+
}
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
# rubocop:disable Metrics/MethodLength
|
83
|
+
def search_params(profile_id, phone_number)
|
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
|
+
}
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Document
|
5
|
+
include Anima.new(:data, :included), Adamantium
|
6
|
+
|
7
|
+
def initialize(data:, included: [])
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def resolve(resource_object)
|
12
|
+
lookup.fetch(resource_object).first
|
13
|
+
end
|
14
|
+
|
15
|
+
def include(resource)
|
16
|
+
with(included: included + resource.included)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def lookup
|
22
|
+
resources.group_by do |resource|
|
23
|
+
{ type: resource.fetch(:type), id: resource.fetch(:id) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
memoize :lookup
|
27
|
+
|
28
|
+
def resources
|
29
|
+
included + [data]
|
30
|
+
end
|
31
|
+
memoize :resources
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
# Signs Cognito requests.
|
5
|
+
class Notary
|
6
|
+
include Anima.new(:api_key, :api_secret, :body, :target)
|
7
|
+
|
8
|
+
CONTENT_TYPE = ACCEPT_TYPE = 'application/vnd.api+json'
|
9
|
+
COGNITO_VERSION = '2016-09-01'
|
10
|
+
|
11
|
+
def headers
|
12
|
+
@headers ||= {
|
13
|
+
'Date' => date,
|
14
|
+
'Digest' => digest,
|
15
|
+
'Authorization' => authorization,
|
16
|
+
'Content-Type' => CONTENT_TYPE,
|
17
|
+
'Accept' => ACCEPT_TYPE,
|
18
|
+
'Cognito-Version' => COGNITO_VERSION
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def authorization
|
23
|
+
@authorization ||= [
|
24
|
+
'Signature keyId="' + api_key + '"',
|
25
|
+
'algorithm="hmac-sha256"',
|
26
|
+
'headers="date digest (request-target)"',
|
27
|
+
'signature="' + signature + '"'
|
28
|
+
].join(',')
|
29
|
+
end
|
30
|
+
|
31
|
+
def digest
|
32
|
+
@digest ||=
|
33
|
+
"SHA-256=#{Base64.strict_encode64(Digest::SHA256.digest(body))}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def date
|
37
|
+
@date ||= Time.now.httpdate
|
38
|
+
end
|
39
|
+
|
40
|
+
def signature
|
41
|
+
@signature ||= Base64.strict_encode64(
|
42
|
+
OpenSSL::HMAC.digest(
|
43
|
+
OpenSSL::Digest::SHA256.new, api_secret, signing_string
|
44
|
+
)
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
def signing_string
|
49
|
+
@signing_string ||= [
|
50
|
+
"date: #{date}",
|
51
|
+
"digest: #{digest}",
|
52
|
+
"(request-target): #{target}"
|
53
|
+
].join("\n")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Resource
|
5
|
+
include AbstractType,
|
6
|
+
Memoizable,
|
7
|
+
Anima.new(:id, :type, :attributes, :relationships, :parent)
|
8
|
+
|
9
|
+
class MissingRelation < StandardError
|
10
|
+
MESSAGE = "Registered relation key, '%s', not in relationships"\
|
11
|
+
' for resource: %p'
|
12
|
+
|
13
|
+
def initialize(key, resource)
|
14
|
+
super(format(MESSAGE, *[key, resource]))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.register_type(type)
|
19
|
+
REGISTRY.add(type, self)
|
20
|
+
end
|
21
|
+
private_class_method :register_type
|
22
|
+
|
23
|
+
# ignores :reek:TooManyStatements:
|
24
|
+
def self.one(key)
|
25
|
+
define_relationship(key) do
|
26
|
+
identifier = relationship_data(key)
|
27
|
+
return unless identifier
|
28
|
+
|
29
|
+
find_identifier(identifier)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
private_class_method :one
|
33
|
+
|
34
|
+
def self.many(key)
|
35
|
+
define_relationship(key) do
|
36
|
+
relationship_data(key).map(&method(:find_identifier))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
private_class_method :many
|
40
|
+
|
41
|
+
def self.attribute(key, method_name = key)
|
42
|
+
define_method(method_name) do
|
43
|
+
attributes[key]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.define_relationship(key, &block)
|
48
|
+
memoize(define_method(key, &block))
|
49
|
+
end
|
50
|
+
private_class_method :define_relationship
|
51
|
+
|
52
|
+
def self.create(data)
|
53
|
+
document = Document.new(data)
|
54
|
+
|
55
|
+
build(document.data, document)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.build(data, parent)
|
59
|
+
new(data.merge(parent: parent))
|
60
|
+
end
|
61
|
+
|
62
|
+
def include(resource)
|
63
|
+
with(parent: parent.include(resource))
|
64
|
+
end
|
65
|
+
|
66
|
+
def included
|
67
|
+
parent.included
|
68
|
+
end
|
69
|
+
|
70
|
+
def initialize(relationships: nil, **options)
|
71
|
+
super
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
def resolve(identifier)
|
77
|
+
parent.resolve(identifier)
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def find_identifier(identifier)
|
83
|
+
resource_class = REGISTRY.lookup(identifier.fetch(:type).to_sym)
|
84
|
+
resource_class.build(resolve(identifier), self)
|
85
|
+
end
|
86
|
+
|
87
|
+
def relationship_data(key)
|
88
|
+
fail MissingRelation.new(key, self) unless relationships && relationships.key?(key)
|
89
|
+
|
90
|
+
relationships.fetch(key).fetch(:data)
|
91
|
+
end
|
92
|
+
|
93
|
+
class Registry
|
94
|
+
include Concord.new(:registry)
|
95
|
+
|
96
|
+
class Error < StandardError
|
97
|
+
def initialize(type)
|
98
|
+
super(format(self.class::MESSAGE, type: type))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class MissingError < Error
|
103
|
+
MESSAGE = 'Resource type not registered: %<type>s'
|
104
|
+
end
|
105
|
+
|
106
|
+
class DuplicateTypeError < Error
|
107
|
+
MESSAGE = 'Resource type already registered: %<type>s'
|
108
|
+
end
|
109
|
+
|
110
|
+
def initialize
|
111
|
+
super({})
|
112
|
+
end
|
113
|
+
|
114
|
+
def lookup(type)
|
115
|
+
registry.fetch(type) do
|
116
|
+
fail MissingError, type
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def add(type, resource_class)
|
121
|
+
fail DuplicateTypeError, type if registry.key?(type)
|
122
|
+
|
123
|
+
registry[type] = resource_class
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
REGISTRY = Registry.new
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Resource
|
5
|
+
class IdentityAssessment < self
|
6
|
+
register_type :identity_assessment
|
7
|
+
|
8
|
+
attribute :name
|
9
|
+
attribute :phone
|
10
|
+
|
11
|
+
many :identity_record_comparisons
|
12
|
+
end
|
13
|
+
|
14
|
+
class IdentityRecordComparison < self
|
15
|
+
register_type :identity_record_comparison
|
16
|
+
|
17
|
+
many :name_comparisons
|
18
|
+
many :phone_comparisons
|
19
|
+
one :identity_record
|
20
|
+
|
21
|
+
attribute :score
|
22
|
+
end
|
23
|
+
|
24
|
+
class PhoneComparison < self
|
25
|
+
register_type :phone_comparison
|
26
|
+
|
27
|
+
attribute :score
|
28
|
+
|
29
|
+
one :source_record
|
30
|
+
end
|
31
|
+
|
32
|
+
class NameComparison < self
|
33
|
+
register_type :name_comparison
|
34
|
+
|
35
|
+
attribute :score
|
36
|
+
|
37
|
+
one :source_record
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cognito
|
4
|
+
class Resource
|
5
|
+
class IdentitySearch < self
|
6
|
+
register_type :identity_search
|
7
|
+
|
8
|
+
many :identity_records
|
9
|
+
end
|
10
|
+
|
11
|
+
class IdentityRecord < self
|
12
|
+
register_type :identity_record
|
13
|
+
|
14
|
+
many :addresses
|
15
|
+
many :names
|
16
|
+
many :ssns
|
17
|
+
many :phones
|
18
|
+
many :births
|
19
|
+
many :deaths
|
20
|
+
end
|
21
|
+
|
22
|
+
class USAddress < self
|
23
|
+
register_type :us_address
|
24
|
+
|
25
|
+
attribute :street
|
26
|
+
attribute :city
|
27
|
+
attribute :subdivision
|
28
|
+
attribute :postal_code
|
29
|
+
end
|
30
|
+
|
31
|
+
class PartialName < self
|
32
|
+
register_type :name
|
33
|
+
|
34
|
+
attribute :first
|
35
|
+
attribute :middle
|
36
|
+
attribute :last
|
37
|
+
end
|
38
|
+
|
39
|
+
class SSN < self
|
40
|
+
register_type :ssn
|
41
|
+
|
42
|
+
attribute :number
|
43
|
+
end
|
44
|
+
|
45
|
+
class Phone < self
|
46
|
+
register_type :phone
|
47
|
+
end
|
48
|
+
|
49
|
+
class PartialDate < self
|
50
|
+
attribute :day
|
51
|
+
attribute :month
|
52
|
+
attribute :year
|
53
|
+
end
|
54
|
+
|
55
|
+
class Birth < PartialDate
|
56
|
+
register_type :birth
|
57
|
+
end
|
58
|
+
|
59
|
+
class Death < PartialDate
|
60
|
+
register_type :death
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Cognito
|
6
|
+
module Responder
|
7
|
+
DEFAULT_WHITELIST = %w(
|
8
|
+
identity_record
|
9
|
+
birth
|
10
|
+
death
|
11
|
+
us_address
|
12
|
+
name
|
13
|
+
phone
|
14
|
+
ssn
|
15
|
+
).freeze
|
16
|
+
|
17
|
+
def included(base)
|
18
|
+
base.include InstanceMethods
|
19
|
+
end
|
20
|
+
|
21
|
+
def response_from(response, options = {})
|
22
|
+
build(parse_response(response, options))
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
MAPPER = {
|
28
|
+
IDENTITY_ASSESSMENT => Cognito::Resource::IdentityAssessment,
|
29
|
+
IDENTITY_SEARCH => Cognito::Resource::IdentitySearch,
|
30
|
+
IDENTITY_SEARCH_JOB => Cognito::Resource::IdentitySearchJob,
|
31
|
+
PROFILE => Cognito::Resource::Profile
|
32
|
+
}.freeze
|
33
|
+
|
34
|
+
def build(json)
|
35
|
+
klass_mapper(json.fetch(:data).fetch(:type)).create(json)
|
36
|
+
end
|
37
|
+
|
38
|
+
def klass_mapper(type)
|
39
|
+
MAPPER.fetch(type) { raise ServerError, 'malformed JSON response' }
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_response(response, options = {})
|
43
|
+
json = JSON.parse(response.body, symbolize_names: true)
|
44
|
+
if response.code < 400
|
45
|
+
whitelist = options.fetch(:permit, DEFAULT_WHITELIST).map(&:to_s)
|
46
|
+
Cleaner.new(json, whitelist).call
|
47
|
+
else
|
48
|
+
handle_errors(json, response.code)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def handle_errors(json, code)
|
53
|
+
klass = (code < 500) ? ClientError : ServerError
|
54
|
+
format_error(klass, json[:errors].first)
|
55
|
+
end
|
56
|
+
|
57
|
+
def format_error(klass, error)
|
58
|
+
raise klass, error[:detail]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,251 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cognito-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Connor Jacobsen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '11.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '11.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.5'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.41'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.41'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: timecop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.8'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.8'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-its
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.2.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.2.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: abstract_type
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.0.7
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.0.7
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: adamantium
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.2.0
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.2.0
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: anima
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.3.0
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.3.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: concord
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 0.1.5
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 0.1.5
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: httparty
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0.13'
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0.13'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: procto
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - "~>"
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: 0.0.3
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - "~>"
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: 0.0.3
|
195
|
+
description:
|
196
|
+
email:
|
197
|
+
- connor@opendoor.com
|
198
|
+
executables: []
|
199
|
+
extensions: []
|
200
|
+
extra_rdoc_files: []
|
201
|
+
files:
|
202
|
+
- ".gitignore"
|
203
|
+
- ".rspec"
|
204
|
+
- ".rubocop.yml"
|
205
|
+
- ".travis.yml"
|
206
|
+
- Gemfile
|
207
|
+
- LICENSE.txt
|
208
|
+
- README.md
|
209
|
+
- Rakefile
|
210
|
+
- bin/console
|
211
|
+
- bin/setup
|
212
|
+
- cognito.gemspec
|
213
|
+
- lib/cognito.rb
|
214
|
+
- lib/cognito/cleaner.rb
|
215
|
+
- lib/cognito/client.rb
|
216
|
+
- lib/cognito/constants.rb
|
217
|
+
- lib/cognito/document.rb
|
218
|
+
- lib/cognito/error.rb
|
219
|
+
- lib/cognito/notary.rb
|
220
|
+
- lib/cognito/resource.rb
|
221
|
+
- lib/cognito/resource/identity_assessment.rb
|
222
|
+
- lib/cognito/resource/identity_search.rb
|
223
|
+
- lib/cognito/resource/identity_search_job.rb
|
224
|
+
- lib/cognito/resource/profile.rb
|
225
|
+
- lib/cognito/responder.rb
|
226
|
+
- lib/cognito/version.rb
|
227
|
+
homepage: https://github.com/opendoor-labs/cognito
|
228
|
+
licenses:
|
229
|
+
- MIT
|
230
|
+
metadata: {}
|
231
|
+
post_install_message:
|
232
|
+
rdoc_options: []
|
233
|
+
require_paths:
|
234
|
+
- lib
|
235
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
236
|
+
requirements:
|
237
|
+
- - ">="
|
238
|
+
- !ruby/object:Gem::Version
|
239
|
+
version: '0'
|
240
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
241
|
+
requirements:
|
242
|
+
- - ">="
|
243
|
+
- !ruby/object:Gem::Version
|
244
|
+
version: '0'
|
245
|
+
requirements: []
|
246
|
+
rubyforge_project:
|
247
|
+
rubygems_version: 2.5.1
|
248
|
+
signing_key:
|
249
|
+
specification_version: 4
|
250
|
+
summary: Unofficial Ruby client for the BlockScore Cognito API
|
251
|
+
test_files: []
|