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
@@ -0,0 +1,45 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ class CopyleaksTextModerationRequestModel
5
+ # The server accepts at most this many labels per request.
6
+ MAX_LABELS = 16
7
+
8
+ attr_accessor :text, :sandbox, :language, :labels
9
+
10
+ # @param sandbox [Boolean, nil] Test mode. nil omits the field (server default: false).
11
+ # @param language [String, nil] Language code; auto-detected when nil.
12
+ # @param labels [Array<CopyleaksTextModerationLabel>] Required, 1 to MAX_LABELS labels to check.
13
+ def initialize(text: '', sandbox: nil, language: nil, labels: [])
14
+
15
+ @text = text
16
+ @sandbox = sandbox
17
+ @language = language
18
+ @labels = labels
19
+
20
+ raise ArgumentError, "String cannot be blank" if @text.nil?
21
+ raise ArgumentError, "Labels must be a non-empty array." unless labels.is_a?(Array) && !labels.empty?
22
+ raise ArgumentError, "Labels cannot have more than #{MAX_LABELS} elements." if labels.length > MAX_LABELS
23
+
24
+ end
25
+
26
+ def to_json(options = {})
27
+ {
28
+ text: @text,
29
+ sandbox: @sandbox,
30
+ language: @language,
31
+ labels: @labels
32
+ }.reject { |_k, v| v.nil? }.to_json(options)
33
+ end
34
+
35
+ def self.from_json(json_string)
36
+ data = JSON.parse(json_string, symbolize_names: true)
37
+ new(
38
+ text: data[:text],
39
+ sandbox: data[:sandbox],
40
+ language: data[:language],
41
+ labels: data[:labels]
42
+ )
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ # Builds the text moderation models from a parsed Hash (String or Symbol keys) or a JSON String.
5
+ module TextModerationJson
6
+ def self.to_hash(json)
7
+ data = json.is_a?(String) ? JSON.parse(json) : json
8
+ data.is_a?(Hash) ? data : nil
9
+ end
10
+
11
+ def self.get(data, key)
12
+ data.key?(key) ? data[key] : data[key.to_sym]
13
+ end
14
+ end
15
+
16
+ class CopyleaksTextModerationResponseModel
17
+ attr_accessor :moderations, :legend, :scanned_document, :model_version
18
+
19
+ # @param moderations [ModerationsModel] The flagged character ranges.
20
+ # @param legend [Array<TextModerationsLegend>] Maps label indexes to label ids.
21
+ def initialize(moderations: ModerationsModel.new, legend: [], scanned_document: TextModerationScannedDocument.new,
22
+ model_version: nil, **_ignored)
23
+ @moderations = moderations
24
+ @legend = legend
25
+ @scanned_document = scanned_document
26
+ @model_version = model_version
27
+ end
28
+
29
+ def to_json(options = {})
30
+ hash = {
31
+ moderations: @moderations ? JSON.parse(@moderations.to_json) : nil,
32
+ legend: (@legend || []).map { |l| JSON.parse(l.to_json) },
33
+ scannedDocument: @scanned_document ? JSON.parse(@scanned_document.to_json) : nil
34
+ }
35
+ hash[:modelVersion] = @model_version unless @model_version.nil?
36
+ hash.to_json(options)
37
+ end
38
+
39
+ # @param json [Hash, String] The client result (a Hash) or the raw JSON String.
40
+ def self.from_json(json)
41
+ data = TextModerationJson.to_hash(json)
42
+ return nil if data.nil?
43
+
44
+ moderations = TextModerationJson.get(data, 'moderations')
45
+ legend = TextModerationJson.get(data, 'legend')
46
+ scanned_document = TextModerationJson.get(data, 'scannedDocument')
47
+ new(
48
+ moderations: moderations ? ModerationsModel.from_json(moderations) : nil,
49
+ legend: (legend || []).map { |l| TextModerationsLegend.from_json(l) },
50
+ scanned_document: scanned_document ? TextModerationScannedDocument.from_json(scanned_document) : nil,
51
+ model_version: TextModerationJson.get(data, 'modelVersion')
52
+ )
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ class ModerationsModel
5
+ attr_accessor :text
6
+
7
+ def initialize(text: Text.new)
8
+ @text = text
9
+ end
10
+
11
+ def to_json(options = {})
12
+ {
13
+ text: @text ? JSON.parse(@text.to_json) : nil
14
+ }.to_json(options)
15
+ end
16
+
17
+ def self.from_json(json)
18
+ data = TextModerationJson.to_hash(json)
19
+ return nil if data.nil?
20
+
21
+ text = TextModerationJson.get(data, 'text')
22
+ new(
23
+ text: text ? Text.from_json(text) : nil
24
+ )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ class Text
5
+ attr_accessor :chars
6
+
7
+ def initialize(chars: TextModerationChars.new)
8
+ @chars = chars
9
+ end
10
+
11
+ def to_json(options = {})
12
+ {
13
+ chars: @chars ? JSON.parse(@chars.to_json) : nil
14
+ }.to_json(options)
15
+ end
16
+
17
+ def self.from_json(json)
18
+ data = TextModerationJson.to_hash(json)
19
+ return nil if data.nil?
20
+
21
+ chars = TextModerationJson.get(data, 'chars')
22
+ new(
23
+ chars: chars ? TextModerationChars.from_json(chars) : nil
24
+ )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ class TextModerationChars
5
+ attr_accessor :labels, :starts, :lengths
6
+
7
+ def initialize(starts: [], labels: [], lengths: [])
8
+ @starts = starts
9
+ @labels = labels
10
+ @lengths = lengths
11
+ end
12
+
13
+ def to_json(options = {})
14
+ {
15
+ labels: @labels,
16
+ starts: @starts,
17
+ lengths: @lengths
18
+ }.to_json(options)
19
+ end
20
+
21
+ def self.from_json(json)
22
+ data = TextModerationJson.to_hash(json)
23
+ return nil if data.nil?
24
+
25
+ new(
26
+ labels: TextModerationJson.get(data, 'labels'),
27
+ starts: TextModerationJson.get(data, 'starts'),
28
+ lengths: TextModerationJson.get(data, 'lengths')
29
+ )
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ require 'json'
2
+ require 'time'
3
+
4
+ module Copyleaks
5
+ # Metadata about a text scan. Base of CopyleaksAiTextDetectionScannedDocumentModel.
6
+ class TextModerationScannedDocument
7
+ attr_accessor :scan_id, :total_words, :total_excluded, :actual_credits, :expected_credits, :creation_time
8
+
9
+ # @param creation_time [Time, String, nil]
10
+ def initialize(scan_id: '', total_words: 0, total_excluded: 0, actual_credits: 0, expected_credits: 0, creation_time: nil)
11
+ @scan_id = scan_id
12
+ @total_words = total_words
13
+ @total_excluded = total_excluded
14
+ @actual_credits = actual_credits
15
+ @expected_credits = expected_credits
16
+ @creation_time = creation_time
17
+ end
18
+
19
+ def as_json(*_args)
20
+ {
21
+ 'scanId' => @scan_id,
22
+ 'totalWords' => @total_words,
23
+ 'totalExcluded' => @total_excluded,
24
+ 'actualCredits' => @actual_credits,
25
+ 'expectedCredits' => @expected_credits,
26
+ 'creationTime' => @creation_time.respond_to?(:iso8601) ? @creation_time.iso8601 : @creation_time
27
+ }
28
+ end
29
+
30
+ def to_json(*args)
31
+ as_json.to_json(*args)
32
+ end
33
+
34
+ # @param json [Hash, String] A parsed Hash (String or Symbol keys) or a JSON String; invalid JSON raises JSON::ParserError.
35
+ def self.from_json(json)
36
+ data = TextModerationJson.to_hash(json)
37
+ return nil if data.nil?
38
+
39
+ new(
40
+ scan_id: TextModerationJson.get(data, 'scanId'),
41
+ total_words: TextModerationJson.get(data, 'totalWords'),
42
+ total_excluded: TextModerationJson.get(data, 'totalExcluded'),
43
+ actual_credits: TextModerationJson.get(data, 'actualCredits'),
44
+ expected_credits: TextModerationJson.get(data, 'expectedCredits'),
45
+ creation_time: parse_creation_time(TextModerationJson.get(data, 'creationTime'))
46
+ )
47
+ end
48
+
49
+ # Returns creationTime as a Time.
50
+ def self.parse_creation_time(value)
51
+ value.is_a?(String) ? Time.parse(value) : value
52
+ end
53
+ private_class_method :parse_creation_time
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+
3
+ module Copyleaks
4
+ class TextModerationsLegend
5
+ attr_accessor :index, :id
6
+
7
+ def initialize(index: 0, id: '')
8
+ @index = index
9
+ @id = id
10
+ end
11
+
12
+ def to_json(options = {})
13
+ {
14
+ index: @index,
15
+ id: @id
16
+ }.to_json(options)
17
+ end
18
+
19
+ def self.from_json(json)
20
+ data = TextModerationJson.to_hash(json)
21
+ return nil if data.nil?
22
+
23
+ new(
24
+ index: TextModerationJson.get(data, 'index'),
25
+ id: TextModerationJson.get(data, 'id')
26
+ )
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,61 @@
1
+ module Copyleaks
2
+ # Async AI video detection request: Copyleaks downloads the video from url and posts the result to webhooks.url.
3
+ class CopyleaksAiVideoDetectionRequestModel
4
+ VERBS = %w[GET POST PUT].freeze
5
+
6
+ attr_reader :url, :filename, :model, :webhooks, :headers, :verb, :sandbox, :developer_payload
7
+
8
+ # @param url [String] Publicly accessible video URL.
9
+ # @param filename [String] With extension, at most 255 characters.
10
+ # Supported: .mp4, .avi, .mov, .mkv, .webm, .flv, .wmv, .mpg, .m4v, .3gp, .mxf
11
+ # @param model [String] See CopyleaksAiVideoDetectionModels.
12
+ # @param webhooks [CopyleaksAsyncWebhooksModel, String] A String is used as webhooks.url.
13
+ # @param headers [Array<Array<String>>, nil] Sent with the video download, e.g. [['X-Custom-Auth', 'my-token']].
14
+ # @param verb [String, nil] HTTP method used to download the video: 'GET', 'POST' or 'PUT'.
15
+ # @param sandbox [Boolean, nil] Free mock results. Server default: false.
16
+ # @param developer_payload [String, nil] Echoed back in the webhook.
17
+ def initialize(url:, filename:, model:, webhooks:, headers: nil, verb: nil, sandbox: nil, developer_payload: nil)
18
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - url must be a String' unless url.is_a?(String)
19
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - filename must be a String' unless filename.is_a?(String)
20
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - model must be a String' unless model.is_a?(String)
21
+ unless Utils.headers_valid?(headers)
22
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - headers must be an Array of [name, value] String pairs'
23
+ end
24
+ if !verb.nil? && !VERBS.include?(verb)
25
+ raise ArgumentError, "Copyleaks::CopyleaksAiVideoDetectionRequestModel - verb must be one of #{VERBS.join(', ')}"
26
+ end
27
+ if !sandbox.nil? && ![true, false].include?(sandbox)
28
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - sandbox must be a Boolean'
29
+ end
30
+ if !developer_payload.nil? && !developer_payload.is_a?(String)
31
+ raise ArgumentError, 'Copyleaks::CopyleaksAiVideoDetectionRequestModel - developer_payload must be a String'
32
+ end
33
+
34
+ @url = url
35
+ @filename = filename
36
+ @model = model
37
+ @webhooks = CopyleaksAsyncWebhooksModel.coerce(webhooks)
38
+ @headers = headers
39
+ @verb = verb
40
+ @sandbox = sandbox
41
+ @developer_payload = developer_payload
42
+ end
43
+
44
+ def as_json(*_args)
45
+ {
46
+ url: @url,
47
+ headers: @headers,
48
+ verb: @verb,
49
+ filename: @filename,
50
+ model: @model,
51
+ webhooks: @webhooks.as_json,
52
+ sandbox: @sandbox,
53
+ developerPayload: @developer_payload
54
+ }.reject { |_k, v| v.nil? }
55
+ end
56
+
57
+ def to_json(*options)
58
+ as_json(*options).to_json(*options)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,177 @@
1
+ require_relative '../../imageDetection/responses/CopyleaksAiImageDetectionScannedDocumentModel.rb'
2
+
3
+ module Copyleaks
4
+ # Time ranges of one track (positions and lengths are in milliseconds).
5
+ class CopyleaksAiVideoDetectionRangesModel
6
+ attr_accessor :starts, :lengths
7
+
8
+ def initialize(starts: nil, lengths: nil)
9
+ @starts = starts
10
+ @lengths = lengths
11
+ end
12
+
13
+ def self.from_json(json_hash)
14
+ return nil unless json_hash.is_a?(Hash)
15
+
16
+ new(starts: json_hash['starts'], lengths: json_hash['lengths'])
17
+ end
18
+
19
+ def as_json(*_args)
20
+ { 'starts' => @starts, 'lengths' => @lengths }.reject { |_k, v| v.nil? }
21
+ end
22
+
23
+ def to_json(*args)
24
+ as_json.to_json(*args)
25
+ end
26
+ end
27
+
28
+ # AI-detected segments of one track, plus the ranges that were not scored (exclude).
29
+ class CopyleaksAiVideoDetectionTrackResultModel < CopyleaksAiVideoDetectionRangesModel
30
+ attr_accessor :exclude
31
+
32
+ def initialize(starts: nil, lengths: nil, exclude: nil)
33
+ super(starts: starts, lengths: lengths)
34
+ @exclude = exclude
35
+ end
36
+
37
+ def self.from_json(json_hash)
38
+ return nil unless json_hash.is_a?(Hash)
39
+
40
+ new(
41
+ starts: json_hash['starts'],
42
+ lengths: json_hash['lengths'],
43
+ exclude: CopyleaksAiVideoDetectionRangesModel.from_json(json_hash['exclude'])
44
+ )
45
+ end
46
+
47
+ def as_json(*_args)
48
+ super.merge('exclude' => @exclude&.as_json).reject { |_k, v| v.nil? }
49
+ end
50
+ end
51
+
52
+ # Overall AI ratios (0.0 to 1.0), computed from the scored segments only.
53
+ class CopyleaksAiVideoDetectionSummaryModel
54
+ attr_accessor :audio_ai_ratio, :visual_ai_ratio, :overall_ai_ratio
55
+
56
+ def initialize(audio_ai_ratio: nil, visual_ai_ratio: nil, overall_ai_ratio: nil)
57
+ @audio_ai_ratio = audio_ai_ratio
58
+ @visual_ai_ratio = visual_ai_ratio
59
+ @overall_ai_ratio = overall_ai_ratio
60
+ end
61
+
62
+ def self.from_json(json_hash)
63
+ return nil unless json_hash.is_a?(Hash)
64
+
65
+ new(
66
+ audio_ai_ratio: json_hash['audioAIRatio'],
67
+ visual_ai_ratio: json_hash['visualAIRatio'],
68
+ overall_ai_ratio: json_hash['overallAIRatio']
69
+ )
70
+ end
71
+
72
+ def as_json(*_args)
73
+ {
74
+ 'audioAIRatio' => @audio_ai_ratio,
75
+ 'visualAIRatio' => @visual_ai_ratio,
76
+ 'overallAIRatio' => @overall_ai_ratio
77
+ }.reject { |_k, v| v.nil? }
78
+ end
79
+
80
+ def to_json(*args)
81
+ as_json.to_json(*args)
82
+ end
83
+ end
84
+
85
+ # Information about the analyzed video.
86
+ class CopyleaksAiVideoDetectionVideoInfoModel
87
+ attr_accessor :duration, :metadata
88
+
89
+ # @param duration [Float] In seconds.
90
+ # @param metadata [CopyleaksImageMetadataModel] Optional provenance metadata.
91
+ def initialize(duration: nil, metadata: nil)
92
+ @duration = duration
93
+ @metadata = metadata
94
+ end
95
+
96
+ def self.from_json(json_hash)
97
+ return nil unless json_hash.is_a?(Hash)
98
+
99
+ new(
100
+ duration: json_hash['duration'],
101
+ metadata: CopyleaksImageMetadataModel.from_json(json_hash['metadata'])
102
+ )
103
+ end
104
+
105
+ def as_json(*_args)
106
+ {
107
+ 'duration' => @duration,
108
+ 'metadata' => @metadata ? JSON.parse(@metadata.to_json) : nil
109
+ }.reject { |_k, v| v.nil? }
110
+ end
111
+
112
+ def to_json(*args)
113
+ as_json.to_json(*args)
114
+ end
115
+ end
116
+
117
+ # Metadata about the video scan. Same fields as the image model; unset fields are left out of the JSON.
118
+ class CopyleaksAiVideoDetectionScannedVideoModel < CopyleaksAiImageDetectionScannedDocumentModel
119
+ def as_json(*_args)
120
+ super.reject { |_k, v| v.nil? }
121
+ end
122
+ end
123
+
124
+ # AI video detection result delivered to your webhook; build it with from_json(JSON.parse(body)).
125
+ # Check error first: when the scan failed, error (a Copyleaks::Error) is set and the result fields are nil.
126
+ class CopyleaksAiVideoDetectionResponseModel
127
+ attr_accessor :model, :audio_result, :visual_result, :summary, :is_ai_detected, :video_info, :scanned_video,
128
+ :error, :developer_payload
129
+
130
+ def initialize(model: nil, audio_result: nil, visual_result: nil, summary: nil, is_ai_detected: nil,
131
+ video_info: nil, scanned_video: nil, error: nil, developer_payload: nil)
132
+ @model = model
133
+ @audio_result = audio_result
134
+ @visual_result = visual_result
135
+ @summary = summary
136
+ @is_ai_detected = is_ai_detected
137
+ @video_info = video_info
138
+ @scanned_video = scanned_video
139
+ @error = error
140
+ @developer_payload = developer_payload
141
+ end
142
+
143
+ def self.from_json(json_hash)
144
+ return nil unless json_hash.is_a?(Hash)
145
+
146
+ new(
147
+ model: json_hash['model'],
148
+ audio_result: CopyleaksAiVideoDetectionTrackResultModel.from_json(json_hash['audioResult']),
149
+ visual_result: CopyleaksAiVideoDetectionTrackResultModel.from_json(json_hash['visualResult']),
150
+ summary: CopyleaksAiVideoDetectionSummaryModel.from_json(json_hash['summary']),
151
+ is_ai_detected: json_hash['isAiDetected'],
152
+ video_info: CopyleaksAiVideoDetectionVideoInfoModel.from_json(json_hash['videoInfo']),
153
+ scanned_video: CopyleaksAiVideoDetectionScannedVideoModel.from_json(json_hash['scannedVideo']),
154
+ error: Error.from_json(json_hash['error']),
155
+ developer_payload: json_hash['developerPayload']
156
+ )
157
+ end
158
+
159
+ def as_json(*_args)
160
+ {
161
+ 'model' => @model,
162
+ 'audioResult' => @audio_result&.as_json,
163
+ 'visualResult' => @visual_result&.as_json,
164
+ 'summary' => @summary&.as_json,
165
+ 'isAiDetected' => @is_ai_detected,
166
+ 'videoInfo' => @video_info&.as_json,
167
+ 'scannedVideo' => @scanned_video&.as_json,
168
+ 'error' => @error&.as_json,
169
+ 'developerPayload' => @developer_payload
170
+ }.reject { |_k, v| v.nil? }
171
+ end
172
+
173
+ def to_json(*args)
174
+ as_json.to_json(*args)
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,18 @@
1
+ module Copyleaks
2
+ class TextModerationClient
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
+ # Scans text for unsafe or policy-violating content.
9
+ # For more info: https://docs.copyleaks.com/reference/actions/text-moderation/check
10
+ # @param [CopyleaksTextModerationRequestModel] submission
11
+ def submit_text(authToken, scanId, submission)
12
+ ClientUtils.send_authorized(@api_client, :post, '/v1/text-moderation/%s/check', 'submit_text_moderation',
13
+ authToken, ids: { 'scanId' => scanId },
14
+ model: [submission, CopyleaksTextModerationRequestModel,
15
+ 'submission is invalid, must be an instance of CopyleaksTextModerationRequestModel'])
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Copyleaks
6
+ class ClientUtils
7
+ REQUEST_CLASSES = {
8
+ get: Net::HTTP::Get,
9
+ post: Net::HTTP::Post,
10
+ put: Net::HTTP::Put,
11
+ patch: Net::HTTP::Patch
12
+ }.freeze
13
+
14
+ def self.handle_response(response, used_by)
15
+ if Utils.is_success_status_code(response.code)
16
+ if response.body.nil? || response.body == ''
17
+ nil
18
+ else
19
+ JSON.parse(response.body)
20
+ end
21
+ elsif Utils.is_under_maintenance_response(response.code)
22
+ raise UnderMaintenanceException.new(response)
23
+ elsif Utils.is_rate_limit_response(response.code)
24
+ raise RateLimitException.new(response)
25
+ else
26
+ raise CommandException.new(response: response, used_by: used_by)
27
+ end
28
+ end
29
+
30
+ # json is serialized with to_json; multipart (a MultipartForm) is sent as multipart/form-data.
31
+ # Requests without a body still declare JSON, as earlier versions did.
32
+ def self.build_request(method, path, auth_token: nil, json: nil, multipart: nil)
33
+ klass = REQUEST_CLASSES.fetch(method)
34
+ headers = { 'User-Agent' => Config.user_agent }
35
+ headers['Authorization'] = "Bearer #{auth_token.accessToken}" unless auth_token.nil?
36
+ if multipart.nil?
37
+ headers['Content-Type'] = 'application/json'
38
+ else
39
+ headers['Content-Type'] = multipart.content_type
40
+ end
41
+
42
+ request = klass.new(path, headers)
43
+ if !multipart.nil?
44
+ request.body = multipart.body
45
+ elsif !json.nil?
46
+ request.body = json.is_a?(String) ? json : json.to_json
47
+ end
48
+ request
49
+ end
50
+
51
+ # Returns the parsed JSON response.
52
+ def self.send_request(http_client, method, path, used_by, auth_token: nil, json: nil, multipart: nil)
53
+ request = build_request(method, path, auth_token: auth_token, json: json, multipart: multipart)
54
+ handle_response(http_client.request(request), used_by)
55
+ end
56
+
57
+ # Checks the arguments and the token, sends the request and returns the parsed response.
58
+ # path: each %s is replaced, in order, by a percent-encoded ids value.
59
+ # ids: path arguments by name, e.g. { 'scanId' => scanId }; each must be a String.
60
+ # model: [body, Class, error message]. The body is sent as JSON, or as multipart/form-data when body.multipart?.
61
+ # verify: the token check, called with the token (Copyleaks::API passes its own #verify_auth_token).
62
+ def self.send_authorized(http_client, method, path, used_by, auth_token, ids: {}, model: nil,
63
+ verify: method(:verify_auth_token))
64
+ verify_arguments(auth_token, ids: ids, model: model, verify: verify)
65
+
66
+ path = format(path, *ids.values.map { |id| escape_path(id) })
67
+ body = model&.first
68
+ if body.respond_to?(:multipart?) && body.multipart?
69
+ send_request(http_client, method, path, used_by, auth_token: auth_token, multipart: body.to_multipart)
70
+ else
71
+ send_request(http_client, method, path, used_by, auth_token: auth_token, json: body)
72
+ end
73
+ end
74
+
75
+ # Raises RuntimeError for an id that is not a String or a body of the wrong class, then checks the token.
76
+ def self.verify_arguments(auth_token, ids: {}, model: nil, verify: method(:verify_auth_token))
77
+ ids.each do |name, value|
78
+ raise "#{name} is Invalid, must be instance of String" unless value.instance_of?(String)
79
+ end
80
+ unless model.nil?
81
+ body, type, message = model
82
+ raise message unless body.instance_of?(type)
83
+ end
84
+ verify.call(auth_token)
85
+ end
86
+
87
+ # Percent-encodes one path segment (scan id, export id, ...) so it cannot change the request path.
88
+ # Only RFC 3986 unreserved characters are kept; every other byte, UTF-8 included, becomes %XX.
89
+ def self.escape_path(segment)
90
+ segment.to_s.b.gsub(/[^A-Za-z0-9\-._~]/n) { |byte| format('%%%02X', byte.ord) }.force_encoding(Encoding::UTF_8)
91
+ end
92
+
93
+ def self.verify_auth_token(authToken)
94
+ if authToken.nil? || !authToken.instance_of?(CopyleaksAuthToken)
95
+ raise 'authToken is Invalid, must be instance of CopyleaksAuthToken'
96
+ end
97
+
98
+ raise AuthExpiredException if authToken.expired?
99
+ end
100
+ end
101
+ end