active_call-doc_fox 0.1.1 → 0.2.0

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +12 -0
  3. data/CHANGELOG.md +17 -2
  4. data/README.md +445 -17
  5. data/lib/doc_fox/access_token/get_service.rb +5 -1
  6. data/lib/doc_fox/authentication/get_service.rb +7 -1
  7. data/lib/doc_fox/base_service.rb +25 -8
  8. data/lib/doc_fox/concerns/enumerable.rb +3 -1
  9. data/lib/doc_fox/data_requirement/facade.rb +25 -0
  10. data/lib/doc_fox/data_requirement/get_service.rb +53 -0
  11. data/lib/doc_fox/data_requirement/list_service.rb +67 -0
  12. data/lib/doc_fox/document/download_service.rb +81 -0
  13. data/lib/doc_fox/document/facade.rb +22 -0
  14. data/lib/doc_fox/document/get_service.rb +54 -0
  15. data/lib/doc_fox/document/list_service.rb +36 -0
  16. data/lib/doc_fox/document/upload_service.rb +62 -0
  17. data/lib/doc_fox/document_token/create_service.rb +57 -0
  18. data/lib/doc_fox/document_token/facade.rb +15 -0
  19. data/lib/doc_fox/document_token/get_service.rb +46 -0
  20. data/lib/doc_fox/document_token/list_service.rb +36 -0
  21. data/lib/doc_fox/evidence_submission/approve_service.rb +54 -0
  22. data/lib/doc_fox/evidence_submission/facade.rb +20 -0
  23. data/lib/doc_fox/evidence_submission/get_service.rb +54 -0
  24. data/lib/doc_fox/evidence_submission/list_service.rb +36 -0
  25. data/lib/doc_fox/evidence_submission/reject_service.rb +58 -0
  26. data/lib/doc_fox/evidence_submission/replace_service.rb +54 -0
  27. data/lib/doc_fox/evidence_submission/update_service.rb +95 -0
  28. data/lib/doc_fox/kyc_application/approve_service.rb +2 -0
  29. data/lib/doc_fox/kyc_application/archive_service.rb +2 -0
  30. data/lib/doc_fox/kyc_application/create_service.rb +2 -0
  31. data/lib/doc_fox/kyc_application/delete_service.rb +2 -0
  32. data/lib/doc_fox/kyc_application/get_service.rb +5 -3
  33. data/lib/doc_fox/kyc_application/list_service.rb +2 -0
  34. data/lib/doc_fox/kyc_application/transfer_service.rb +2 -0
  35. data/lib/doc_fox/kyc_application/unapprove_service.rb +2 -0
  36. data/lib/doc_fox/kyc_application/unarchive_service.rb +2 -0
  37. data/lib/doc_fox/kyc_entity_template/get_service.rb +4 -2
  38. data/lib/doc_fox/kyc_entity_template/list_service.rb +2 -0
  39. data/lib/doc_fox/profile/facade.rb +15 -0
  40. data/lib/doc_fox/profile/get_service.rb +53 -0
  41. data/lib/doc_fox/profile/list_service.rb +36 -0
  42. data/lib/doc_fox/status_summary/facade.rb +14 -0
  43. data/lib/doc_fox/status_summary/get_service.rb +47 -0
  44. data/lib/doc_fox/user/get_service.rb +4 -2
  45. data/lib/doc_fox/user/list_service.rb +2 -0
  46. data/lib/doc_fox/version.rb +1 -1
  47. data/lib/doc_fox.rb +1 -0
  48. metadata +40 -2
@@ -1,12 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class DocFox::Authentication::GetService < DocFox::BaseService
4
+ validates :api_key, presence: true
5
+
6
+ skip_callback :call, :before, :set_access_token
7
+
4
8
  after_call :set_facade
5
9
 
6
10
  delegate_missing_to :@facade
7
11
 
8
12
  # Get access token.
9
13
  #
14
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Authentication/paths/~1api~1v2~1authentications~1new/get
15
+ #
10
16
  # ==== Examples
11
17
  #
12
18
  # service = DocFox::Authentication::GetService.call
@@ -22,7 +28,6 @@ class DocFox::Authentication::GetService < DocFox::BaseService
22
28
 
23
29
  def connection
24
30
  @_connection ||= Faraday.new do |conn|
25
- conn.adapter Faraday.default_adapter
26
31
  conn.url_prefix = base_url
27
32
  conn.headers['X-Client-Api-Key'] = api_key
28
33
  conn.request :json
@@ -32,6 +37,7 @@ class DocFox::Authentication::GetService < DocFox::BaseService
32
37
  logger.filter(/(X-Client-Api-Key:).*"(.+)."/i, '\1 [FILTERED]')
33
38
  logger.filter(/"nonce":"([^"]+)"/i, '"nonce":"[FILTERED]"')
34
39
  end
40
+ conn.adapter Faraday.default_adapter
35
41
  end
36
42
  end
37
43
 
@@ -15,7 +15,15 @@ class DocFox::BaseService < ActiveCall::Base
15
15
  config_accessor :log_bodies, default: false, instance_writer: false
16
16
  config_accessor :api_key, :secret, instance_writer: false
17
17
 
18
- attr_reader :facade
18
+ attr_reader :access_token, :facade
19
+
20
+ before_call :set_access_token
21
+
22
+ validate on: :request do
23
+ next if is_a?(DocFox::AccessToken::GetService) || is_a?(DocFox::Authentication::GetService)
24
+
25
+ errors.merge!(access_token_service.errors) if access_token.nil? && !access_token_service.success?
26
+ end
19
27
 
20
28
  class << self
21
29
  def exception_mapping
@@ -48,15 +56,16 @@ class DocFox::BaseService < ActiveCall::Base
48
56
 
49
57
  def connection
50
58
  @_connection ||= Faraday.new do |conn|
51
- conn.adapter Faraday.default_adapter
52
59
  conn.url_prefix = base_url
53
- conn.request :authorization, 'Bearer', -> { access_token }
60
+ conn.request :authorization, 'Bearer', access_token
54
61
  conn.request :json
55
62
  conn.request :retry
56
63
  conn.response :json
64
+ conn.response :follow_redirects
57
65
  conn.response :logger, logger, **logger_options do |logger|
58
66
  logger.filter(/(Authorization:).*"(.+)."/i, '\1 [FILTERED]')
59
67
  end
68
+ conn.adapter Faraday.default_adapter
60
69
  end
61
70
  end
62
71
 
@@ -69,11 +78,19 @@ class DocFox::BaseService < ActiveCall::Base
69
78
  }
70
79
  end
71
80
 
72
- def access_token
73
- access_token = cache.read(CACHE_KEY[:access_token])
74
- return access_token if access_token.present?
81
+ def set_access_token
82
+ @access_token = cache.read(CACHE_KEY[:access_token])
83
+ return if @access_token.present?
84
+ return unless access_token_service.success?
85
+
86
+ expires_at = Time.parse(access_token_service.expires_at)
87
+
88
+ @access_token = cache.fetch(CACHE_KEY[:access_token], expires_at: expires_at - 10) do
89
+ access_token_service.token
90
+ end
91
+ end
75
92
 
76
- service = DocFox::AccessToken::GetService.call
77
- cache.fetch(CACHE_KEY[:access_token], expires_at: Time.parse(service.expires_at) - 10) { service.token }
93
+ def access_token_service
94
+ @_access_token_service ||= DocFox::AccessToken::GetService.call
78
95
  end
79
96
  end
@@ -31,7 +31,7 @@ module DocFox::Enumerable
31
31
 
32
32
  catch :list_end do
33
33
  loop do
34
- @response = connection.get(path, params)
34
+ @response = connection.get(path, **params)
35
35
  validate(:response)
36
36
 
37
37
  unless success?
@@ -40,6 +40,8 @@ module DocFox::Enumerable
40
40
  throw :list_end
41
41
  end
42
42
 
43
+ throw :list_end unless response.body
44
+
43
45
  response.body['data'].each do |hash|
44
46
  yield facade_klass.new(hash)
45
47
  total += 1
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DataRequirement::Facade
4
+ attr_reader :id, :attributes, :relationships, :included, :created_at, :duplicable, :duplicates_max, :duplicates_min,
5
+ :duplicate_of, :form, :name, :required, :slug, :updated_at, :valid_evidence_types
6
+
7
+ def initialize(hash)
8
+ @id = hash.dig('data', 'id') || hash['id']
9
+ @attributes = hash.dig('data', 'attributes') || hash['attributes']
10
+ @relationships = hash.dig('data', 'relationships') || hash['relationships']
11
+ @included = hash['included']
12
+
13
+ @created_at = attributes['created_at']
14
+ @duplicable = attributes['duplicable']
15
+ @duplicates_max = attributes['duplicates_max']
16
+ @duplicates_min = attributes['duplicates_min']
17
+ @duplicate_of = attributes['duplicate_of']
18
+ @form = attributes['form']
19
+ @name = attributes['name']
20
+ @required = attributes['required']
21
+ @slug = attributes['slug']
22
+ @updated_at = attributes['updated_at']
23
+ @valid_evidence_types = attributes['valid_evidence_types']
24
+ end
25
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DataRequirement::GetService < DocFox::BaseService
4
+ attr_reader :id, :params
5
+
6
+ validates :id, presence: true
7
+
8
+ after_call :set_facade
9
+
10
+ delegate_missing_to :@facade
11
+
12
+ def initialize(id:, params: {})
13
+ @id = id
14
+ @params = params
15
+ end
16
+
17
+ # Get a data requirement.
18
+ #
19
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Data-Requirements/paths/~1api~1v2~1data_requirements~1%7Bdata_requirement_id%7D/get
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # service = DocFox::DataRequirement::GetService.call(id: '')
24
+ #
25
+ # service.success? # => true
26
+ # service.errors # => #<ActiveModel::Errors []>
27
+ #
28
+ # service.response # => #<Faraday::Response ...>
29
+ # service.response.status # => 200
30
+ # service.response.body # => {}
31
+ #
32
+ # service.facade # => #<DocFox::DataRequirement::Facade ...>
33
+ # service.facade.id
34
+ # service.id
35
+ #
36
+ # service.relationships.dig('kyc_application', 'data', 'id')
37
+ # service.relationships.dig('active_evidence_submission', 'links', 'related')
38
+ #
39
+ # Include related resources.
40
+ #
41
+ # service = DocFox::DataRequirement::GetService.call(id: '', params: { include: 'evidence_submissions,active_evidence_submission' })
42
+ #
43
+ # GET /api/v2/data_requirements/:id
44
+ def call
45
+ connection.get("data_requirements/#{id}", **params)
46
+ end
47
+
48
+ private
49
+
50
+ def set_facade
51
+ @facade = DocFox::DataRequirement::Facade.new(response.body)
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DataRequirement::ListService < DocFox::BaseService
4
+ include DocFox::Enumerable
5
+
6
+ attr_reader :kyc_application_id, :account_application_id, :forms
7
+
8
+ validates :kyc_application_id_or_account_application_id, presence: true
9
+
10
+ # List data requirements.
11
+ #
12
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Data-Requirements/paths/~1api~1v2~1kyc_applications~1%7Bkyc_application_id%7D~1data_requirements/get
13
+ #
14
+ # List data requirements associated with an account application.
15
+ #
16
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Data-Requirements/paths/~1api~1v2~1account_applications~1%7Baccount_application_id%7D~1data_requirements/get
17
+ #
18
+ # ==== Examples
19
+ #
20
+ # service = DocFox::DataRequirement::ListService.call(kyc_application_id: '').first
21
+ # service.id
22
+ # service.name
23
+ #
24
+ # service = DocFox::DataRequirement::ListService.call(account_application_id: '').first
25
+ # service.id
26
+ # service.name
27
+ #
28
+ # If you don't provide the `per_page` argument, multiple API requests will be made untill all records have been
29
+ # returned. You could be rate limited, so use wisely.
30
+ #
31
+ # DocFox::DataRequirement::ListService.call(kyc_application_id: '', page: 1, per_page: 10).map { _1 }
32
+ #
33
+ # Filter by forms.
34
+ #
35
+ # DocFox::DataRequirement::ListService.call(kyc_application_id: '', forms: true).map { _1 }
36
+ #
37
+ # GET /api/v2/kyc_applications/:kyc_application_id/data_requirements
38
+ # GET /api/v2/account_applications/:account_application_id/data_requirements
39
+ def initialize(kyc_application_id: nil, account_application_id: nil, page: 1, per_page: Float::INFINITY, forms: false)
40
+ @kyc_application_id = kyc_application_id
41
+ @account_application_id = account_application_id
42
+ @forms = forms
43
+
44
+ entity_path = kyc_application_id.present? ? 'kyc_applications' : 'account_applications'
45
+
46
+ super(
47
+ path: "#{entity_path}/#{kyc_application_id_or_account_application_id}/data_requirements",
48
+ facade_klass: DocFox::DataRequirement::Facade,
49
+ page: page,
50
+ per_page: per_page
51
+ )
52
+ end
53
+
54
+ private
55
+
56
+ def params
57
+ @_params ||= begin
58
+ params = { page: page, per_page: max_per_page_per_request }
59
+ params[:forms] = true if forms == true
60
+ params
61
+ end
62
+ end
63
+
64
+ def kyc_application_id_or_account_application_id
65
+ kyc_application_id.presence || account_application_id.presence
66
+ end
67
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::Document::DownloadService < DocFox::BaseService
4
+ CONTENT_TYPES = ['image/jpeg', 'image/png', 'application/pdf'].freeze
5
+
6
+ attr_reader :document_id, :document_token_id, :content_type, :url
7
+ private attr_reader :document_token_create_service
8
+
9
+ validates :document_token_id_or_document_id, presence: true
10
+ validates :content_type, presence: true, if: -> { document_token_id.present? }
11
+
12
+ validates :content_type, on: :request,
13
+ inclusion: {
14
+ in: CONTENT_TYPES,
15
+ message: "has to be one of #{CONTENT_TYPES.to_sentence(last_word_connector: ' or ')}"
16
+ }
17
+
18
+ validate on: :request do
19
+ if document_token_id.blank? && !document_token_create_service.success?
20
+ errors.merge!(document_token_create_service.errors)
21
+ end
22
+ end
23
+
24
+ before_call :create_document_token, :set_document_token_id_and_content_type, if: -> { document_token_id.blank? }
25
+
26
+ def initialize(document_id: nil, document_token_id: nil, content_type: nil)
27
+ @document_id = document_id
28
+ @document_token_id = document_token_id
29
+ @content_type = content_type
30
+ end
31
+
32
+ # Download a document file.
33
+ #
34
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Download-Documents/paths/~1api~1v2~1document_file_downloads~1%7Bdocument_token_id%7D/get
35
+ #
36
+ # ==== Examples
37
+ #
38
+ # service = DocFox::Document::DownloadService.call(document_token_id: '', content_type: 'image/png')
39
+ #
40
+ # service.success? # => true
41
+ # service.errors # => #<ActiveModel::Errors []>
42
+ #
43
+ # service.response # => #<Faraday::Response ...>
44
+ # service.response.status # => 200
45
+ # service.response.body # => "\xFF\xD8\xFF\xDB\x00\x84\x00\x04\x05\x..."
46
+ #
47
+ # Possible values for `content_type`.
48
+ #
49
+ # - `image/jpeg`
50
+ # - `image/png`
51
+ # - `application/pdf`
52
+ #
53
+ # You can also provide just the `document_id` and the service will automatically request the `document_token_id` and `content_type` for you.
54
+ #
55
+ # service = DocFox::Document::DownloadService.call(document_id: '')
56
+ #
57
+ # GET /api/v2/document_file_downloads/:document_token_id
58
+ def call
59
+ connection.get("document_file_downloads/#{document_token_id}", nil, { 'Accept' => content_type })
60
+ end
61
+
62
+ private
63
+
64
+ def document_token_id_or_document_id
65
+ document_token_id.presence || document_id.presence
66
+ end
67
+
68
+ def create_document_token
69
+ @document_token_create_service = DocFox::DocumentToken::CreateService.call(
70
+ document_id: document_id,
71
+ data: { type: 'document_token' }
72
+ )
73
+ end
74
+
75
+ def set_document_token_id_and_content_type
76
+ return unless document_token_create_service.success?
77
+
78
+ @document_token_id = document_token_create_service.id
79
+ @content_type = document_token_create_service.content_type
80
+ end
81
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::Document::Facade
4
+ attr_reader :id, :attributes, :relationships, :included, :content_type, :created_at, :data_capture_fields, :filename,
5
+ :token, :token_expiry, :uploaded_by, :updated_at
6
+
7
+ def initialize(hash)
8
+ @id = hash.dig('data', 'id') || hash['id']
9
+ @attributes = hash.dig('data', 'attributes') || hash['attributes']
10
+ @relationships = hash.dig('data', 'relationships') || hash['relationships']
11
+ @included = hash['included']
12
+
13
+ @content_type = attributes['content_type']
14
+ @created_at = attributes['created_at']
15
+ @data_capture_fields = attributes['data_capture_fields']
16
+ @filename = attributes['filename']
17
+ @token = attributes['token']
18
+ @token_expiry = attributes['token_expiry']
19
+ @uploaded_by = attributes['uploaded_by']
20
+ @updated_at = attributes['updated_at']
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::Document::GetService < DocFox::BaseService
4
+ attr_reader :id, :params
5
+
6
+ validates :id, presence: true
7
+
8
+ after_call :set_facade
9
+
10
+ delegate_missing_to :@facade
11
+
12
+ def initialize(id:, params: {})
13
+ @id = id
14
+ @params = params
15
+ end
16
+
17
+ # Get a document.
18
+ #
19
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Documents/paths/~1api~1v2~1documents~1%7Bdocument_id%7D/get
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # service = DocFox::Document::GetService.call(id: '')
24
+ #
25
+ # service.success? # => true
26
+ # service.errors # => #<ActiveModel::Errors []>
27
+ #
28
+ # service.response # => #<Faraday::Response ...>
29
+ # service.response.status # => 200
30
+ # service.response.body # => {}
31
+ #
32
+ # service.facade # => #<DocFox::Document::Facade ...>
33
+ # service.facade.id
34
+ # service.id
35
+ #
36
+ # service.relationships.dig('evidence_submissions', 'data')
37
+ # service.relationships.dig('document_tokens', 'links', 'related')
38
+ #
39
+ # Include related resources.
40
+ #
41
+ # service = DocFox::Document::GetService.call(id: '', params: { include: 'evidence_submissions' })
42
+ # service.included
43
+ #
44
+ # GET /api/v2/documents/:id
45
+ def call
46
+ connection.get("documents/#{id}", **params)
47
+ end
48
+
49
+ private
50
+
51
+ def set_facade
52
+ @facade = DocFox::Document::Facade.new(response.body)
53
+ end
54
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::Document::ListService < DocFox::BaseService
4
+ include DocFox::Enumerable
5
+
6
+ attr_reader :evidence_submission_id
7
+
8
+ validates :evidence_submission_id, presence: true
9
+
10
+ # List documents.
11
+ #
12
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Documents/paths/~1api~1v2~1evidence_submissions~1%7Bevidence_submission_id%7D~1documents/get
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # service = DocFox::Document::ListService.call(evidence_submission_id: '').first
17
+ # service.id
18
+ # service.filename
19
+ #
20
+ # If you don't provide the `per_page` argument, multiple API requests will be made untill all records have been
21
+ # returned. You could be rate limited, so use wisely.
22
+ #
23
+ # DocFox::Document::ListService.call(evidence_submission_id: '', page: 1, per_page: 10).map { _1 }
24
+ #
25
+ # GET /api/v2/evidence_submissions/:evidence_submission_id/documents
26
+ def initialize(evidence_submission_id:, page: 1, per_page: Float::INFINITY)
27
+ @evidence_submission_id = evidence_submission_id
28
+
29
+ super(
30
+ path: "evidence_submissions/#{evidence_submission_id}/documents",
31
+ facade_klass: DocFox::Document::Facade,
32
+ page: page,
33
+ per_page: per_page
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::Document::UploadService < DocFox::BaseService
4
+ attr_reader :evidence_submission_id, :data
5
+
6
+ validates :evidence_submission_id, :data, presence: true
7
+
8
+ after_call :set_facade
9
+
10
+ delegate_missing_to :@facade
11
+
12
+ def initialize(evidence_submission_id:, data:)
13
+ @evidence_submission_id = evidence_submission_id
14
+ @data = data
15
+ end
16
+
17
+ # Upload a document file.
18
+ #
19
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Document-Uploads/paths/~1api~1v2~1evidence_submissions~1%7Bevidence_submission_id%7D~1documents/post
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # service = DocFox::Document::UploadService.call(
24
+ # evidence_submission_id: '',
25
+ # data: {
26
+ # type: 'document',
27
+ # attributes: {
28
+ # evidence_type: 'taxes_paid_in_prior_quarter',
29
+ # filename: 'taxes_paid.png',
30
+ # document_data: "data:image/png;base64,#{Base64.encode64(File.read('/path/to/taxes_paid.png'))}"
31
+ # }
32
+ # }
33
+ # )
34
+ #
35
+ # service.success? # => true
36
+ # service.errors # => #<ActiveModel::Errors []>
37
+ #
38
+ # service.response # => #<Faraday::Response ...>
39
+ # service.response.status # => 201
40
+ # service.response.body # => {}
41
+ #
42
+ # service.facade # => #<DocFox::Document::Facade ...>
43
+ # service.facade.id
44
+ # service.id
45
+ #
46
+ # POST /api/v2/evidence_submissions/:evidence_submission_id/documents
47
+ def call
48
+ connection.post("evidence_submissions/#{evidence_submission_id}/documents", **params)
49
+ end
50
+
51
+ private
52
+
53
+ def params
54
+ {
55
+ data: data
56
+ }
57
+ end
58
+
59
+ def set_facade
60
+ @facade = DocFox::Document::Facade.new(response.body)
61
+ end
62
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DocumentToken::CreateService < DocFox::BaseService
4
+ attr_reader :document_id, :data
5
+
6
+ validates :document_id, :data, presence: true
7
+
8
+ after_call :set_facade
9
+
10
+ delegate_missing_to :@facade
11
+
12
+ def initialize(document_id:, data:)
13
+ @document_id = document_id
14
+ @data = data
15
+ end
16
+
17
+ # Create a document token.
18
+ #
19
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Document-Tokens/paths/~1api~1v2~1documents~1%7Bdocument_id%7D~1document_tokens/post
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # service = DocFox::DocumentToken::CreateService.call(
24
+ # document_id: '',
25
+ # data: {
26
+ # type: 'document_token'
27
+ # }
28
+ # )
29
+ #
30
+ # service.success? # => true
31
+ # service.errors # => #<ActiveModel::Errors []>
32
+ #
33
+ # service.response # => #<Faraday::Response ...>
34
+ # service.response.status # => 201
35
+ # service.response.body # => {}
36
+ #
37
+ # service.facade # => #<DocFox::DocumentToken::Facade ...>
38
+ # service.facade.id
39
+ # service.id
40
+ #
41
+ # POST /api/v2/documents/:document_id/document_tokens
42
+ def call
43
+ connection.post("documents/#{document_id}/document_tokens", **params)
44
+ end
45
+
46
+ private
47
+
48
+ def params
49
+ {
50
+ data: data
51
+ }
52
+ end
53
+
54
+ def set_facade
55
+ @facade = DocFox::DocumentToken::Facade.new(response.body)
56
+ end
57
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DocumentToken::Facade
4
+ attr_reader :id, :attributes, :content_type, :created_at, :expiry, :updated_at
5
+
6
+ def initialize(hash)
7
+ @id = hash.dig('data', 'id') || hash['id']
8
+ @attributes = hash.dig('data', 'attributes') || hash['attributes']
9
+
10
+ @content_type = attributes['content_type']
11
+ @created_at = attributes['created_at']
12
+ @expiry = attributes['expiry']
13
+ @updated_at = attributes['updated_at']
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DocumentToken::GetService < DocFox::BaseService
4
+ attr_reader :id, :params
5
+
6
+ validates :id, presence: true
7
+
8
+ after_call :set_facade
9
+
10
+ delegate_missing_to :@facade
11
+
12
+ def initialize(id:, params: {})
13
+ @id = id
14
+ @params = params
15
+ end
16
+
17
+ # Get a document token.
18
+ #
19
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Document-Tokens/paths/~1api~1v2~1document_tokens~1%7Bdocument_token_id%7D/get
20
+ #
21
+ # ==== Examples
22
+ #
23
+ # service = DocFox::DocumentToken::GetService.call(id: '')
24
+ #
25
+ # service.success? # => true
26
+ # service.errors # => #<ActiveModel::Errors []>
27
+ #
28
+ # service.response # => #<Faraday::Response ...>
29
+ # service.response.status # => 200
30
+ # service.response.body # => {}
31
+ #
32
+ # service.facade # => #<DocFox::DocumentToken::Facade ...>
33
+ # service.facade.id
34
+ # service.id
35
+ #
36
+ # GET /api/v2/document_tokens/:id
37
+ def call
38
+ connection.get("document_tokens/#{id}", **params)
39
+ end
40
+
41
+ private
42
+
43
+ def set_facade
44
+ @facade = DocFox::DocumentToken::Facade.new(response.body)
45
+ end
46
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocFox::DocumentToken::ListService < DocFox::BaseService
4
+ include DocFox::Enumerable
5
+
6
+ attr_reader :document_id
7
+
8
+ validates :document_id, presence: true
9
+
10
+ # List document tokens.
11
+ #
12
+ # https://www.docfoxapp.com/api/v2/documentation#tag/Document-Tokens/paths/~1api~1v2~1documents~1%7Bdocument_id%7D~1document_tokens/get
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # service = DocFox::DocumentToken::ListService.call(document_id: '').first
17
+ # service.id
18
+ # service.expiry
19
+ #
20
+ # If you don't provide the `per_page` argument, multiple API requests will be made untill all records have been
21
+ # returned. You could be rate limited, so use wisely.
22
+ #
23
+ # DocFox::DocumentToken::ListService.call(document_id: '', page: 1, per_page: 10).map { _1 }
24
+ #
25
+ # GET /api/v2/documents/:document_id/document_tokens
26
+ def initialize(document_id:, page: 1, per_page: Float::INFINITY)
27
+ @document_id = document_id
28
+
29
+ super(
30
+ path: "documents/#{document_id}/document_tokens",
31
+ facade_klass: DocFox::DocumentToken::Facade,
32
+ page: page,
33
+ per_page: per_page
34
+ )
35
+ end
36
+ end