copyleaks 5.0.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 (157) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/CHANGELOG.md +102 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +487 -0
  7. data/Rakefile +13 -0
  8. data/bin/console +11 -0
  9. data/bin/setup +8 -0
  10. data/lib/copyleaks/ai_detection_client.rb +27 -0
  11. data/lib/copyleaks/ai_image_detection_client.rb +35 -0
  12. data/lib/copyleaks/ai_video_detection_client.rb +20 -0
  13. data/lib/copyleaks/api.rb +247 -0
  14. data/lib/copyleaks/app.config.rb +36 -0
  15. data/lib/copyleaks/image_plagiarism_client.rb +19 -0
  16. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionExplainModel.rb +26 -0
  17. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionMatchModel.rb +33 -0
  18. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionMatchTextModel.rb +31 -0
  19. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionPatternMatchModel.rb +32 -0
  20. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionPatternStatisticsModel.rb +46 -0
  21. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionPatternsModel.rb +38 -0
  22. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionPositionsModel.rb +39 -0
  23. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionResponseModel.rb +68 -0
  24. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionResultModel.rb +41 -0
  25. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionScannedDocumentModel.rb +21 -0
  26. data/lib/copyleaks/models/aiDetection/responses/CopyleaksAiTextDetectionSummaryModel.rb +34 -0
  27. data/lib/copyleaks/models/auth_token.rb +46 -0
  28. data/lib/copyleaks/models/constants/CopyleaksAiImageDetectionModels.rb +5 -0
  29. data/lib/copyleaks/models/constants/CopyleaksAiImageMaskTypes.rb +11 -0
  30. data/lib/copyleaks/models/constants/CopyleaksAiVideoDetectionModels.rb +5 -0
  31. data/lib/copyleaks/models/constants/CopyleaksAlertCodes.rb +7 -0
  32. data/lib/copyleaks/models/constants/CopyleaksTextModerationConstants.rb +15 -0
  33. data/lib/copyleaks/models/constants/CopyleaksTextModerationLanguages.rb +6 -0
  34. data/lib/copyleaks/models/constants/index.rb +10 -0
  35. data/lib/copyleaks/models/delete_request_model.rb +50 -0
  36. data/lib/copyleaks/models/exceptions/auth_expired_exception.rb +7 -0
  37. data/lib/copyleaks/models/exceptions/command_exception.rb +13 -0
  38. data/lib/copyleaks/models/exceptions/copyleaks_api_exception.rb +18 -0
  39. data/lib/copyleaks/models/exceptions/index.rb +8 -0
  40. data/lib/copyleaks/models/exceptions/rate_limit_exception.rb +8 -0
  41. data/lib/copyleaks/models/exceptions/under_maintenance_exception.rb +8 -0
  42. data/lib/copyleaks/models/exports/export_crawled_version.rb +6 -0
  43. data/lib/copyleaks/models/exports/export_model.rb +87 -0
  44. data/lib/copyleaks/models/exports/export_pdf_report.rb +6 -0
  45. data/lib/copyleaks/models/exports/export_results.rb +30 -0
  46. data/lib/copyleaks/models/exports/export_target.rb +42 -0
  47. data/lib/copyleaks/models/exports/index.rb +8 -0
  48. data/lib/copyleaks/models/id_object.rb +22 -0
  49. data/lib/copyleaks/models/imageDetection/requests/CopyleaksAiImageDetectionAsyncRequestModel.rb +63 -0
  50. data/lib/copyleaks/models/imageDetection/requests/CopyleaksAiImageDetectionRequestModel.rb +135 -0
  51. data/lib/copyleaks/models/imageDetection/requests/CopyleaksAsyncWebhooksModel.rb +37 -0
  52. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectedObjectModel.rb +31 -0
  53. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectionImageInfoModel.rb +31 -0
  54. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectionResponseModel.rb +79 -0
  55. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectionResultModel.rb +40 -0
  56. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectionScannedDocumentModel.rb +46 -0
  57. data/lib/copyleaks/models/imageDetection/responses/CopyleaksAiImageDetectionSummaryModel.rb +33 -0
  58. data/lib/copyleaks/models/imageDetection/responses/CopyleaksImageMetadataModel.rb +45 -0
  59. data/lib/copyleaks/models/imageDetection/responses/CopyleaksImageShapeModel.rb +31 -0
  60. data/lib/copyleaks/models/imagePlagiarism/requests/CopyleaksImagePlagiarismRequestModel.rb +51 -0
  61. data/lib/copyleaks/models/imagePlagiarism/responses/CopyleaksImagePlagiarismResponseModel.rb +187 -0
  62. data/lib/copyleaks/models/index.rb +51 -0
  63. data/lib/copyleaks/models/start_request_model.rb +40 -0
  64. data/lib/copyleaks/models/submissions/ai_detection/ai_detection_submission_model.rb +25 -0
  65. data/lib/copyleaks/models/submissions/ai_detection/natural_language_submission_model.rb +41 -0
  66. data/lib/copyleaks/models/submissions/file_ocr_submission_model.rb +36 -0
  67. data/lib/copyleaks/models/submissions/file_submission_model.rb +35 -0
  68. data/lib/copyleaks/models/submissions/index.rb +16 -0
  69. data/lib/copyleaks/models/submissions/properties/actions.rb +10 -0
  70. data/lib/copyleaks/models/submissions/properties/ai_generated_text.rb +40 -0
  71. data/lib/copyleaks/models/submissions/properties/ai_generated_text_explain.rb +6 -0
  72. data/lib/copyleaks/models/submissions/properties/ai_source_match.rb +6 -0
  73. data/lib/copyleaks/models/submissions/properties/assignment.rb +6 -0
  74. data/lib/copyleaks/models/submissions/properties/author.rb +18 -0
  75. data/lib/copyleaks/models/submissions/properties/copyleaks_db.rb +22 -0
  76. data/lib/copyleaks/models/submissions/properties/course.rb +6 -0
  77. data/lib/copyleaks/models/submissions/properties/cross_languages.rb +21 -0
  78. data/lib/copyleaks/models/submissions/properties/custom_metadata.rb +19 -0
  79. data/lib/copyleaks/models/submissions/properties/domains_mode.rb +8 -0
  80. data/lib/copyleaks/models/submissions/properties/enable_flag.rb +26 -0
  81. data/lib/copyleaks/models/submissions/properties/exclude.rb +57 -0
  82. data/lib/copyleaks/models/submissions/properties/exclude_code.rb +19 -0
  83. data/lib/copyleaks/models/submissions/properties/filter.rb +49 -0
  84. data/lib/copyleaks/models/submissions/properties/index.rb +43 -0
  85. data/lib/copyleaks/models/submissions/properties/indexing.rb +25 -0
  86. data/lib/copyleaks/models/submissions/properties/indexing_repository.rb +26 -0
  87. data/lib/copyleaks/models/submissions/properties/institution.rb +6 -0
  88. data/lib/copyleaks/models/submissions/properties/internal_ai_source_match.rb +7 -0
  89. data/lib/copyleaks/models/submissions/properties/language.rb +18 -0
  90. data/lib/copyleaks/models/submissions/properties/masking_policy.rb +11 -0
  91. data/lib/copyleaks/models/submissions/properties/overview.rb +42 -0
  92. data/lib/copyleaks/models/submissions/properties/pdf_colors.rb +48 -0
  93. data/lib/copyleaks/models/submissions/properties/pdf_properties.rb +51 -0
  94. data/lib/copyleaks/models/submissions/properties/pdf_report_version.rb +9 -0
  95. data/lib/copyleaks/models/submissions/properties/pdf_version.rb +10 -0
  96. data/lib/copyleaks/models/submissions/properties/references.rb +26 -0
  97. data/lib/copyleaks/models/submissions/properties/repository.rb +18 -0
  98. data/lib/copyleaks/models/submissions/properties/scan_method_algorithm.rb +8 -0
  99. data/lib/copyleaks/models/submissions/properties/scanning.rb +44 -0
  100. data/lib/copyleaks/models/submissions/properties/scanning_exclude.rb +47 -0
  101. data/lib/copyleaks/models/submissions/properties/scanning_include.rb +25 -0
  102. data/lib/copyleaks/models/submissions/properties/scanning_repository.rb +23 -0
  103. data/lib/copyleaks/models/submissions/properties/sensitive_data_protection.rb +42 -0
  104. data/lib/copyleaks/models/submissions/properties/submission_properties.rb +179 -0
  105. data/lib/copyleaks/models/submissions/properties/webhooks.rb +42 -0
  106. data/lib/copyleaks/models/submissions/properties/writing_feedback.rb +30 -0
  107. data/lib/copyleaks/models/submissions/submission_model.rb +24 -0
  108. data/lib/copyleaks/models/submissions/url_submission_model.rb +41 -0
  109. data/lib/copyleaks/models/submissions/webhooks/CompletedWebhook.rb +67 -0
  110. data/lib/copyleaks/models/submissions/webhooks/CreditsCheckedWebhook.rb +24 -0
  111. data/lib/copyleaks/models/submissions/webhooks/ErrorWebhook.rb +22 -0
  112. data/lib/copyleaks/models/submissions/webhooks/ExportCompletedWebhook.rb +25 -0
  113. data/lib/copyleaks/models/submissions/webhooks/HelperModels/BaseModels/Metadata.rb +61 -0
  114. data/lib/copyleaks/models/submissions/webhooks/HelperModels/BaseModels/StatusWebhook.rb +23 -0
  115. data/lib/copyleaks/models/submissions/webhooks/HelperModels/BaseModels/Webhook.rb +25 -0
  116. data/lib/copyleaks/models/submissions/webhooks/HelperModels/CompletedModels/Notifications.rb +24 -0
  117. data/lib/copyleaks/models/submissions/webhooks/HelperModels/CompletedModels/Results.rb +54 -0
  118. data/lib/copyleaks/models/submissions/webhooks/HelperModels/CompletedModels/ScannedDocument.rb +36 -0
  119. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ErrorModels/Error.rb +49 -0
  120. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ExportModels/Task.rb +29 -0
  121. data/lib/copyleaks/models/submissions/webhooks/HelperModels/NewResultsModels/NewResultScore.rb +22 -0
  122. data/lib/copyleaks/models/submissions/webhooks/HelperModels/NewResultsModels/NewResultsInternet.rb +37 -0
  123. data/lib/copyleaks/models/submissions/webhooks/HelperModels/NewResultsModels/NewResultsRepositories.rb +28 -0
  124. data/lib/copyleaks/models/submissions/webhooks/HelperModels/NotificationsModels/Alerts.rb +91 -0
  125. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Batch.rb +26 -0
  126. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Database.rb +26 -0
  127. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Internet.rb +23 -0
  128. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Repositories.rb +36 -0
  129. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/RepositoryMetadata.rb +11 -0
  130. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Score.rb +20 -0
  131. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/SharedResultsModel.rb +22 -0
  132. data/lib/copyleaks/models/submissions/webhooks/HelperModels/ResultsModels/Tags.rb +12 -0
  133. data/lib/copyleaks/models/submissions/webhooks/IndexedWebhook.rb +9 -0
  134. data/lib/copyleaks/models/submissions/webhooks/NewResultWebhook.rb +32 -0
  135. data/lib/copyleaks/models/submissions/webhooks/index.rb +34 -0
  136. data/lib/copyleaks/models/submissions/writing_assistant/score_weights.rb +26 -0
  137. data/lib/copyleaks/models/submissions/writing_assistant/writing_assistant_submission_model.rb +32 -0
  138. data/lib/copyleaks/models/textModeration/requests/CopyleaksTextModerationLabel.rb +21 -0
  139. data/lib/copyleaks/models/textModeration/requests/CopyleaksTextModerationRequestModel.rb +45 -0
  140. data/lib/copyleaks/models/textModeration/responses/CopyleaksTextModerationResponseModel.rb +55 -0
  141. data/lib/copyleaks/models/textModeration/responses/submodules/ModerationsModel.rb +27 -0
  142. data/lib/copyleaks/models/textModeration/responses/submodules/Text.rb +27 -0
  143. data/lib/copyleaks/models/textModeration/responses/submodules/TextModerationChars.rb +32 -0
  144. data/lib/copyleaks/models/textModeration/responses/submodules/TextModerationScannedDocument.rb +55 -0
  145. data/lib/copyleaks/models/textModeration/responses/submodules/TextModerationsLegend.rb +29 -0
  146. data/lib/copyleaks/models/videoDetection/requests/CopyleaksAiVideoDetectionRequestModel.rb +61 -0
  147. data/lib/copyleaks/models/videoDetection/responses/CopyleaksAiVideoDetectionResponseModel.rb +177 -0
  148. data/lib/copyleaks/text_moderation_client.rb +18 -0
  149. data/lib/copyleaks/utils/copyleaks_client.utils.rb +101 -0
  150. data/lib/copyleaks/utils/http_client.rb +164 -0
  151. data/lib/copyleaks/utils/multipart.rb +86 -0
  152. data/lib/copyleaks/utils/status-code.utils.rb +23 -0
  153. data/lib/copyleaks/version.rb +3 -0
  154. data/lib/copyleaks/writing_assistant_client.rb +26 -0
  155. data/lib/copyleaks.rb +11 -0
  156. data/lib/index.rb +4 -0
  157. metadata +248 -0
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ # Offline unit tests. They run against a local stub server and never call the Copyleaks API.
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test' << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.warning = false
11
+ end
12
+
13
+ task default: %i[test]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'copyleaks'
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
+ require 'irb'
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ module Copyleaks
2
+ class AIDetectionClient
3
+ # @param [Copyleaks::HttpClient] api_client A Net::HTTP is also accepted.
4
+ def initialize(api_client)
5
+ @api_client = HttpClient.wrap(api_client)
6
+ end
7
+
8
+ # Detects whether a text was written by a human or by AI.
9
+ # For more info: https://docs.copyleaks.com/reference/actions/writer-detector/check
10
+ # @param [NaturalLanguageSubmissionModel] submission
11
+ # @return [Hash] For typed access use CopyleaksAiTextDetectionResponseModel.from_json(result).
12
+ def submit_natural_language(authToken, scanId, submission)
13
+ ClientUtils.send_authorized(@api_client, :post, '/v2/writer-detector/%s/check', 'submit_natural_language',
14
+ authToken, ids: { 'scanId' => scanId },
15
+ model: [submission, NaturalLanguageSubmissionModel,
16
+ 'submission is Invalid, must be instance of type Copyleaks::NaturalLanguageSubmissionModel'])
17
+ end
18
+
19
+ # Returns the credit balance available for AI Text Detection.
20
+ # For more info: https://docs.copyleaks.com/reference/actions/writer-detector/credits
21
+ # @return [Hash] For example { 'credits' => 382 }.
22
+ def get_credits_balance(authToken)
23
+ ClientUtils.send_authorized(@api_client, :get, '/v2/writer-detector/credits', 'get_ai_text_credits_balance',
24
+ authToken)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module Copyleaks
2
+ class AIImageDetectionClient
3
+ # @param [Copyleaks::HttpClient] api_client A Net::HTTP is also accepted.
4
+ def initialize(api_client)
5
+ @api_client = HttpClient.wrap(api_client)
6
+ end
7
+
8
+ # Checks whether an image was generated by AI and returns the result (synchronous).
9
+ # Raw image bytes (image:) are sent as multipart/form-data, base64 data as JSON.
10
+ # For more info: https://docs.copyleaks.com/reference/actions/ai-image-detector/check
11
+ # @param [CopyleaksAiImageDetectionRequestModel] submission
12
+ # @return [Hash] For typed access use CopyleaksAiImageDetectionResponseModel.from_json(result).
13
+ def submit_(authToken, scanId, submission)
14
+ ClientUtils.send_authorized(@api_client, :post, '/v1/ai-image-detector/%s/check', 'submit_image_detection',
15
+ authToken, ids: { 'scanId' => scanId },
16
+ model: [submission, CopyleaksAiImageDetectionRequestModel,
17
+ 'submission is Invalid, must be instance of type Copyleaks::CopyleaksAiImageDetectionRequestModel'])
18
+ end
19
+
20
+ # Same as submit_.
21
+ alias check submit_
22
+
23
+ # Submits an image URL for AI detection (asynchronous). The server answers 201 Created and later
24
+ # posts the result, shaped like CopyleaksAiImageDetectionResponseModel, to the request's webhook.
25
+ # For more info: https://docs.copyleaks.com/reference/actions/ai-image-detector/submit
26
+ # @param [CopyleaksAiImageDetectionAsyncRequestModel] submission
27
+ # @return [Hash, nil] Usually empty.
28
+ def submit_async(authToken, scanId, submission)
29
+ ClientUtils.send_authorized(@api_client, :put, '/v1/ai-image-detector-async/%s/submit', 'submit_image_detection_async',
30
+ authToken, ids: { 'scanId' => scanId },
31
+ model: [submission, CopyleaksAiImageDetectionAsyncRequestModel,
32
+ 'submission is Invalid, must be instance of type Copyleaks::CopyleaksAiImageDetectionAsyncRequestModel'])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module Copyleaks
2
+ class AIVideoDetectionClient
3
+ # @param [Copyleaks::HttpClient] api_client A Net::HTTP is also accepted.
4
+ def initialize(api_client)
5
+ @api_client = HttpClient.wrap(api_client)
6
+ end
7
+
8
+ # Submits a video URL for AI detection (asynchronous). The server answers 201 Created and later
9
+ # posts the result, shaped like CopyleaksAiVideoDetectionResponseModel, to the request's webhook.
10
+ # For more info: https://docs.copyleaks.com/reference/actions/ai-video-detector/submit
11
+ # @param [CopyleaksAiVideoDetectionRequestModel] submission
12
+ # @return [Hash, nil] Usually empty.
13
+ def submit(authToken, scanId, submission)
14
+ ClientUtils.send_authorized(@api_client, :post, '/v1/ai-video-detector/%s/submit', 'submit_video_detection',
15
+ authToken, ids: { 'scanId' => scanId },
16
+ model: [submission, CopyleaksAiVideoDetectionRequestModel,
17
+ 'submission is Invalid, must be instance of type Copyleaks::CopyleaksAiVideoDetectionRequestModel'])
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,247 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'date'
4
+ require 'uri'
5
+ require_relative 'utils/http_client.rb'
6
+ require_relative 'utils/multipart.rb'
7
+ require_relative 'utils/copyleaks_client.utils.rb'
8
+ require_relative 'ai_detection_client.rb'
9
+ require_relative 'writing_assistant_client.rb'
10
+ require_relative 'text_moderation_client.rb'
11
+ require_relative 'ai_image_detection_client.rb'
12
+ require_relative 'ai_video_detection_client.rb'
13
+ require_relative 'image_plagiarism_client.rb'
14
+
15
+ module Copyleaks
16
+ # Entry point of the SDK. Thread-safe; keeps a small pool of persistent connections per server.
17
+ #
18
+ # Errors raised by every call:
19
+ # * CommandException: request rejected; exception.response has the status code, headers and body.
20
+ # * UnderMaintenanceException (HTTP 503) and RateLimitException (HTTP 429, see exception.retry_after).
21
+ # * AuthExpiredException: the login token has expired; log in again.
22
+ # * Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, socket errors. Requests are never resent
23
+ # automatically. A timed-out submit may have been accepted: resubmitting its scan id returns 409 Conflict.
24
+ # Retry 429 and 503 with exponential backoff: https://docs.copyleaks.com/concepts/performance/handling-failures
25
+ class API
26
+ attr_reader :ai_detection_client, :writing_assistant_client, :text_moderation_client,
27
+ :ai_image_detection_client, :ai_video_detection_client, :image_plagiarism_client
28
+
29
+ # @param [Numeric] open_timeout Seconds to wait for the connection and the TLS handshake. Default 10.
30
+ # @param [Numeric] read_timeout Seconds to wait for each read of a response. Default 120.
31
+ # @param [Numeric] write_timeout Seconds to wait for each write of a request body. Default 120.
32
+ # @param [Numeric] keep_alive_timeout Seconds an idle connection is kept open for reuse. Default 30.
33
+ # @param [Integer] pool_size Maximum open connections per Copyleaks server. Default 5.
34
+ # @param [String] identity_server_uri Overrides Copyleaks::Config.identity_server_uri for this client.
35
+ # @param [String] api_server_uri Overrides Copyleaks::Config.api_server_uri for this client.
36
+ def initialize(open_timeout: HttpClient::DEFAULT_OPEN_TIMEOUT,
37
+ read_timeout: HttpClient::DEFAULT_READ_TIMEOUT,
38
+ write_timeout: HttpClient::DEFAULT_WRITE_TIMEOUT,
39
+ keep_alive_timeout: HttpClient::DEFAULT_KEEP_ALIVE_TIMEOUT,
40
+ pool_size: HttpClient::DEFAULT_POOL_SIZE,
41
+ identity_server_uri: nil,
42
+ api_server_uri: nil)
43
+ options = {
44
+ open_timeout: open_timeout,
45
+ read_timeout: read_timeout,
46
+ write_timeout: write_timeout,
47
+ keep_alive_timeout: keep_alive_timeout,
48
+ pool_size: pool_size
49
+ }
50
+ @id_client = HttpClient.new(identity_server_uri || Config.identity_server_uri, **options)
51
+ @api_client = HttpClient.new(api_server_uri || Config.api_server_uri, **options)
52
+
53
+ @ai_detection_client = AIDetectionClient.new(@api_client)
54
+ @writing_assistant_client = WritingAssistantClient.new(@api_client)
55
+ @text_moderation_client = TextModerationClient.new(@api_client)
56
+ @ai_image_detection_client = AIImageDetectionClient.new(@api_client)
57
+ @ai_video_detection_client = AIVideoDetectionClient.new(@api_client)
58
+ @image_plagiarism_client = ImagePlagiarismClient.new(@api_client)
59
+ end
60
+
61
+ # Closes the idle pooled connections. The client stays usable and reconnects when needed.
62
+ def close
63
+ @id_client.close
64
+ @api_client.close
65
+ nil
66
+ end
67
+
68
+ # Logs in to the Copyleaks identity server.
69
+ # For more info: https://docs.copyleaks.com/reference/actions/account/login
70
+ # @param [String] key Account secret key.
71
+ # @return [CopyleaksAuthToken] Expires; see #verify_auth_token.
72
+ def login(email, key)
73
+ raise 'email is Invalid, must be instance of String' if email.nil? || !email.instance_of?(String)
74
+ raise 'key is Invalid, must be instance of String' if key.nil? || !key.instance_of?(String)
75
+
76
+ payload = { email: email, key: key }
77
+ res_data = ClientUtils.send_request(@id_client, :post, '/v3/account/login/api', 'login', json: payload)
78
+
79
+ CopyleaksAuthToken.new(res_data['.expires'], res_data['access_token'], res_data['.issued'])
80
+ end
81
+
82
+ # Raises AuthExpiredException when the token has expired or expires within the next 5 minutes.
83
+ def verify_auth_token(authToken)
84
+ ClientUtils.verify_auth_token(authToken)
85
+ end
86
+
87
+ # Starts a scan of a file, given as a CopyleaksFileSubmissionModel.
88
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/submit-file
89
+ def submit_file(authToken, scanId, submission)
90
+ send_authorized(@api_client, :put, '/v3/scans/submit/file/%s', 'submit_file', authToken,
91
+ ids: { 'scanId' => scanId },
92
+ model: [submission, CopyleaksFileSubmissionModel,
93
+ 'submission is Invalid, must be instance of type CopyleaksFileSubmissionModel'])
94
+ end
95
+
96
+ # Starts an OCR scan of an image file, given as a CopyleaksFileOcrSubmissionModel.
97
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/submit-ocr
98
+ def submit_file_ocr(authToken, scanId, submission)
99
+ send_authorized(@api_client, :put, '/v3/scans/submit/ocr/%s', 'submit_file_ocr', authToken,
100
+ ids: { 'scanId' => scanId },
101
+ model: [submission, CopyleaksFileOcrSubmissionModel,
102
+ 'submission is Invalid, must be instance of type CopyleaksFileOcrSubmissionModel'])
103
+ end
104
+
105
+ # Starts a scan of a URL, given as a CopyleaksURLSubmissionModel.
106
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/submit-url
107
+ def submit_url(authToken, scanId, submission)
108
+ send_authorized(@api_client, :put, '/v3/scans/submit/url/%s', 'submit_url', authToken,
109
+ ids: { 'scanId' => scanId },
110
+ model: [submission, CopyleaksURLSubmissionModel,
111
+ 'submission is Invalid, must be instance of CopyleaksURLSubmissionModel'])
112
+ end
113
+
114
+ # Exports a scan's artifacts to your server.
115
+ # For more info: https://docs.copyleaks.com/reference/actions/downloads/export
116
+ # @param [String] exportId A new id for this export.
117
+ # @param [CopyleaksExportModel] model Which artifacts to export, and where.
118
+ def export(authToken, scanId, exportId, model)
119
+ send_authorized(@api_client, :post, '/v3/downloads/%s/export/%s', 'export', authToken,
120
+ ids: { 'scanId' => scanId, 'exportId' => exportId },
121
+ model: [model, CopyleaksExportModel,
122
+ 'model is Invalid, must be instance of type CopyleaksExportModel'])
123
+ end
124
+
125
+ # Starts scans that were submitted for a price check; data is a CopyleaksStartRequestModel.
126
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/start
127
+ def start(authToken, data)
128
+ send_authorized(@api_client, :patch, '/v3/scans/start', 'start', authToken,
129
+ model: [data, CopyleaksStartRequestModel,
130
+ 'data is Invalid, must be instance of type CopyleaksStartRequestModel'])
131
+ end
132
+
133
+ # Deletes scans; data is a CopyleaksDeleteRequestModel.
134
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/delete
135
+ def delete(authToken, data)
136
+ send_authorized(@api_client, :patch, '/v3.1/scans/delete', 'delete', authToken,
137
+ model: [data, CopyleaksDeleteRequestModel,
138
+ 'data is Invalid, must be instance of CopyleaksDeleteRequestModel'])
139
+ end
140
+
141
+ # Resends the status webhooks of an existing scan.
142
+ # For more info: https://docs.copyleaks.com/reference/actions/authenticity/resend-webhook
143
+ def resend_webhook(authToken, scanId)
144
+ send_authorized(@api_client, :post, '/v3/scans/%s/webhooks/resend', 'resend_webhook', authToken,
145
+ ids: { 'scanId' => scanId })
146
+ end
147
+
148
+ # Returns the account's credit balance.
149
+ # For more info: https://docs.copyleaks.com/reference/actions/admin/check-credits
150
+ def get_credits_balance(authToken)
151
+ send_authorized(@api_client, :get, '/v3/scans/credits', 'get_credits_balance', authToken)
152
+ end
153
+
154
+ # Returns the usage history between two dates as CSV.
155
+ # For more info: https://docs.copyleaks.com/reference/actions/admin/usage-history
156
+ # @param [String] startDate `dd-MM-yyyy`.
157
+ # @param [String] endDate `dd-MM-yyyy`.
158
+ # @yield [String] Optional. Streams the CSV in chunks, so a long history is never held in memory.
159
+ # @return [String, nil] The CSV text, or nil when a block is given.
160
+ def get_usages_history_csv(authToken, startDate, endDate, &block)
161
+ verify_arguments(authToken, ids: { 'startDate' => startDate, 'endDate' => endDate })
162
+
163
+ path = "/v3/scans/usages/history?#{URI.encode_www_form(start: startDate, end: endDate)}"
164
+ request = ClientUtils.build_request(:get, path, auth_token: authToken)
165
+
166
+ if block
167
+ response = @api_client.request(request) do |res|
168
+ if Utils.is_success_status_code(res.code)
169
+ res.read_body { |chunk| block.call(chunk.force_encoding(Encoding::UTF_8)) }
170
+ end
171
+ end
172
+ return nil if Utils.is_success_status_code(response.code)
173
+ else
174
+ response = @api_client.request(request)
175
+ # The endpoint returns CSV, so return the body as text.
176
+ return response.body&.force_encoding(Encoding::UTF_8) if Utils.is_success_status_code(response.code)
177
+ end
178
+
179
+ handle_response(response, 'get_usages_history_csv')
180
+ end
181
+
182
+ # Lists the languages supported by OCR file scans (not by the API in general).
183
+ # For more info: https://docs.copyleaks.com/reference/actions/miscellaneous/ocr-supported-languages
184
+ # @return [Array<String>]
185
+ def get_ocr_supported_languages
186
+ ClientUtils.send_request(@api_client, :get, '/v3/miscellaneous/ocr-languages-list', 'get_ocr_supported_languages')
187
+ end
188
+
189
+ # Lists the file types supported by plagiarism scans.
190
+ # For more info: https://docs.copyleaks.com/reference/actions/miscellaneous/supported-plagiarism-file-types
191
+ # @return [Hash] For example { 'textual' => [...], 'ocr' => [...] }.
192
+ def get_supported_file_types
193
+ ClientUtils.send_request(@api_client, :get, '/v3/miscellaneous/supported-plagiarism-file-types',
194
+ 'get_supported_file_types')
195
+ end
196
+
197
+ # Lists the file types supported by AI Text Detection. No login is needed.
198
+ # For more info: https://docs.copyleaks.com/reference/actions/miscellaneous/supported-ai-text-detection-file-types
199
+ # @return [Hash] For example { 'supportedAiFileTypes' => ['pdf', 'docx', ...] }.
200
+ def get_supported_ai_text_detection_file_types
201
+ ClientUtils.send_request(@api_client, :get, '/v3/miscellaneous/supported-ai-text-detection-file-types',
202
+ 'get_supported_ai_text_detection_file_types')
203
+ end
204
+
205
+ # Lists the ISO-639-1 codes for Copyleaks::SubmissionCrossLanguages. No login is needed.
206
+ # For more info: https://docs.copyleaks.com/reference/actions/miscellaneous/supported-cross-languages
207
+ # @return [Hash] Name-to-code maps for scanned documents and for results, for example
208
+ # { 'documentLanguages' => { 'english' => 'en', ... }, 'resultLanguages' => { 'french' => 'fr', ... } }.
209
+ def get_allowed_cross_languages
210
+ ClientUtils.send_request(@api_client, :get, '/v3/miscellaneous/allowed-cross-languages',
211
+ 'get_allowed_cross_languages')
212
+ end
213
+
214
+ # Returns a Private Cloud Hub repository's credits, masking policy and status. Needs the Admin or Super Admin role.
215
+ # For more info: https://docs.copyleaks.com/reference/actions/private-cloud-hub/info
216
+ # @param [String] repositoryId From the Copyleaks Admin Dashboard.
217
+ def get_repository_info(authToken, repositoryId)
218
+ if repositoryId.nil? || !repositoryId.instance_of?(String) || repositoryId.empty?
219
+ raise 'repositoryId is Invalid, must be a non-empty String'
220
+ end
221
+
222
+ send_authorized(@api_client, :get, '/v3/repositories/repository/%s/info', 'get_repository_info',
223
+ authToken, ids: { 'repositoryId' => repositoryId })
224
+ end
225
+
226
+ # Returns the Copyleaks API release notes.
227
+ def get_release_notes
228
+ ClientUtils.send_request(@api_client, :get, '/v3/release-logs.json', 'get_release_notes')
229
+ end
230
+
231
+ # Returns the parsed JSON body (nil when empty), or raises the matching exception for a failed response.
232
+ def handle_response(response, used_by)
233
+ Copyleaks::ClientUtils.handle_response(response, used_by)
234
+ end
235
+
236
+ private
237
+
238
+ # The API calls check the token through #verify_auth_token, so a subclass or a test stub can replace it.
239
+ def send_authorized(*args, **options)
240
+ ClientUtils.send_authorized(*args, verify: method(:verify_auth_token), **options)
241
+ end
242
+
243
+ def verify_arguments(auth_token, **options)
244
+ ClientUtils.verify_arguments(auth_token, verify: method(:verify_auth_token), **options)
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'version.rb'
2
+
3
+ module Copyleaks
4
+ # Process-wide defaults. Copyleaks::API.new also accepts identity_server_uri: and api_server_uri:
5
+ # to override them for a single client.
6
+ class Config
7
+ @@identity_server_uri = 'https://id.copyleaks.com'
8
+ @@api_server_uri = 'https://api.copyleaks.com'
9
+ @@user_agent = "ruby-sdk/#{Copyleaks::VERSION}"
10
+
11
+ def initialize(identity_server_uri, api_server_uri)
12
+ @@identity_server_uri = identity_server_uri
13
+ @@api_server_uri = api_server_uri
14
+ end
15
+
16
+ def self.identity_server_uri
17
+ @@identity_server_uri
18
+ end
19
+
20
+ def self.api_server_uri
21
+ @@api_server_uri
22
+ end
23
+
24
+ def self.identity_server_uri=(uri)
25
+ @@identity_server_uri = uri
26
+ end
27
+
28
+ def self.api_server_uri=(uri)
29
+ @@api_server_uri = uri
30
+ end
31
+
32
+ def self.user_agent
33
+ @@user_agent
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module Copyleaks
2
+ class ImagePlagiarismClient
3
+ # @param [Copyleaks::HttpClient] api_client A Net::HTTP is also accepted.
4
+ def initialize(api_client)
5
+ @api_client = HttpClient.wrap(api_client)
6
+ end
7
+
8
+ # Searches the web for copies of an image and returns the matches (synchronous, multipart upload).
9
+ # For more info: https://docs.copyleaks.com/reference/actions/image-plagiarism-detector/check
10
+ # @param [CopyleaksImagePlagiarismRequestModel] submission
11
+ # @return [Hash] For typed access use CopyleaksImagePlagiarismResponseModel.from_json(result).
12
+ def check(authToken, scanId, submission)
13
+ ClientUtils.send_authorized(@api_client, :post, '/v1/image-plagiarism-detector/%s/check', 'check_image_plagiarism',
14
+ authToken, ids: { 'scanId' => scanId },
15
+ model: [submission, CopyleaksImagePlagiarismRequestModel,
16
+ 'submission is Invalid, must be instance of type Copyleaks::CopyleaksImagePlagiarismRequestModel'])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # AI Logic explanation of the AI text detection result.
5
+ class CopyleaksAiTextDetectionExplainModel
6
+ attr_accessor :patterns
7
+
8
+ def initialize(patterns: nil)
9
+ @patterns = patterns
10
+ end
11
+
12
+ def self.from_json(json_hash)
13
+ return nil unless json_hash.is_a?(Hash)
14
+
15
+ patterns = CopyleaksAiTextDetectionPatternsModel.from_json(json_hash['patterns']) if json_hash['patterns']
16
+
17
+ new(patterns: patterns)
18
+ end
19
+
20
+ def to_json(*args)
21
+ {
22
+ 'patterns' => @patterns
23
+ }.compact.to_json(*args)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # The positions of a classified part of the text.
5
+ class CopyleaksAiTextDetectionMatchModel
6
+ # In the text version of the document.
7
+ attr_accessor :text
8
+
9
+ # In the HTML version of the document. Present only for HTML sources.
10
+ attr_accessor :html
11
+
12
+ def initialize(text: nil, html: nil)
13
+ @text = text
14
+ @html = html
15
+ end
16
+
17
+ def self.from_json(json_hash)
18
+ return nil unless json_hash.is_a?(Hash)
19
+
20
+ text = CopyleaksAiTextDetectionMatchTextModel.from_json(json_hash['text']) if json_hash['text']
21
+ html = CopyleaksAiTextDetectionMatchTextModel.from_json(json_hash['html']) if json_hash['html']
22
+
23
+ new(text: text, html: html)
24
+ end
25
+
26
+ def to_json(*args)
27
+ {
28
+ 'text' => @text,
29
+ 'html' => @html
30
+ }.compact.to_json(*args)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # Character and word positions of a classified part of the text.
5
+ class CopyleaksAiTextDetectionMatchTextModel
6
+ attr_accessor :chars
7
+
8
+ attr_accessor :words
9
+
10
+ def initialize(chars: nil, words: nil)
11
+ @chars = chars
12
+ @words = words
13
+ end
14
+
15
+ def self.from_json(json_hash)
16
+ return nil unless json_hash.is_a?(Hash)
17
+
18
+ chars = CopyleaksAiTextDetectionPositionsModel.from_json(json_hash['chars']) if json_hash['chars']
19
+ words = CopyleaksAiTextDetectionPositionsModel.from_json(json_hash['words']) if json_hash['words']
20
+
21
+ new(chars: chars, words: words)
22
+ end
23
+
24
+ def to_json(*args)
25
+ {
26
+ 'chars' => @chars,
27
+ 'words' => @words
28
+ }.compact.to_json(*args)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # Character and word positions of the AI patterns.
5
+ class CopyleaksAiTextDetectionPatternMatchModel
6
+ # May be nil.
7
+ attr_accessor :chars
8
+
9
+ attr_accessor :words
10
+
11
+ def initialize(chars: nil, words: nil)
12
+ @chars = chars
13
+ @words = words
14
+ end
15
+
16
+ def self.from_json(json_hash)
17
+ return nil unless json_hash.is_a?(Hash)
18
+
19
+ chars = CopyleaksAiTextDetectionPositionsModel.from_json(json_hash['chars']) if json_hash['chars']
20
+ words = CopyleaksAiTextDetectionPositionsModel.from_json(json_hash['words']) if json_hash['words']
21
+
22
+ new(chars: chars, words: words)
23
+ end
24
+
25
+ def to_json(*args)
26
+ {
27
+ 'chars' => @chars,
28
+ 'words' => @words
29
+ }.compact.to_json(*args)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # Statistics of the AI patterns found in the text.
5
+ # Each array has one entry per pattern, in the same order.
6
+ class CopyleaksAiTextDetectionPatternStatisticsModel
7
+ # How often each pattern appears in AI-generated text.
8
+ attr_accessor :ai_count
9
+
10
+ # How often each pattern appears in human-written text.
11
+ attr_accessor :human_count
12
+
13
+ # The ratio between the AI and human frequency of each pattern.
14
+ attr_accessor :proportion
15
+
16
+ # The source of each pattern. 1 = AI, 2 = humanizer.
17
+ attr_accessor :source
18
+
19
+ def initialize(ai_count: nil, human_count: nil, proportion: nil, source: nil)
20
+ @ai_count = ai_count
21
+ @human_count = human_count
22
+ @proportion = proportion
23
+ @source = source
24
+ end
25
+
26
+ def self.from_json(json_hash)
27
+ return nil unless json_hash.is_a?(Hash)
28
+
29
+ new(
30
+ ai_count: json_hash['aiCount'],
31
+ human_count: json_hash['humanCount'],
32
+ proportion: json_hash['proportion'],
33
+ source: json_hash['source']
34
+ )
35
+ end
36
+
37
+ def to_json(*args)
38
+ {
39
+ 'aiCount' => @ai_count,
40
+ 'humanCount' => @human_count,
41
+ 'proportion' => @proportion,
42
+ 'source' => @source
43
+ }.compact.to_json(*args)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Copyleaks
4
+ # The AI patterns found in the text.
5
+ class CopyleaksAiTextDetectionPatternsModel
6
+ attr_accessor :statistics
7
+
8
+ # In the text version of the document.
9
+ attr_accessor :text
10
+
11
+ # In the HTML version of the document. Present only for HTML sources.
12
+ attr_accessor :html
13
+
14
+ def initialize(statistics: nil, text: nil, html: nil)
15
+ @statistics = statistics
16
+ @text = text
17
+ @html = html
18
+ end
19
+
20
+ def self.from_json(json_hash)
21
+ return nil unless json_hash.is_a?(Hash)
22
+
23
+ statistics = CopyleaksAiTextDetectionPatternStatisticsModel.from_json(json_hash['statistics']) if json_hash['statistics']
24
+ text = CopyleaksAiTextDetectionPatternMatchModel.from_json(json_hash['text']) if json_hash['text']
25
+ html = CopyleaksAiTextDetectionPatternMatchModel.from_json(json_hash['html']) if json_hash['html']
26
+
27
+ new(statistics: statistics, text: text, html: html)
28
+ end
29
+
30
+ def to_json(*args)
31
+ {
32
+ 'statistics' => @statistics,
33
+ 'text' => @text,
34
+ 'html' => @html
35
+ }.compact.to_json(*args)
36
+ end
37
+ end
38
+ end