plagiarism-checker 2.1.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -12
  3. data/Gemfile +6 -4
  4. data/LICENSE.txt +21 -21
  5. data/README.md +39 -0
  6. data/Rakefile +4 -2
  7. data/bin/console +10 -2
  8. data/lib/copyleaks/api.rb +493 -0
  9. data/lib/copyleaks/app.config.rb +47 -0
  10. data/lib/copyleaks/models/auth_token.rb +54 -0
  11. data/lib/copyleaks/models/delete_request_model.rb +64 -0
  12. data/lib/copyleaks/models/exceptions/auth_exipred_exception.rb +32 -0
  13. data/lib/copyleaks/models/exceptions/command_exception.rb +32 -0
  14. data/lib/copyleaks/models/exceptions/index.rb +32 -0
  15. data/lib/copyleaks/models/exceptions/rate_limit_exception.rb +32 -0
  16. data/lib/copyleaks/models/exceptions/under_maintenance_exception.rb +32 -0
  17. data/lib/copyleaks/models/exports/export_crawled_version.rb +54 -0
  18. data/lib/copyleaks/models/exports/export_model.rb +84 -0
  19. data/lib/copyleaks/models/exports/export_pdf_report.rb +54 -0
  20. data/lib/copyleaks/models/exports/export_results.rb +56 -0
  21. data/lib/copyleaks/models/exports/index.rb +32 -0
  22. data/lib/copyleaks/models/id_object.rb +44 -0
  23. data/lib/copyleaks/models/index.rb +35 -0
  24. data/lib/copyleaks/models/start_request_model.rb +63 -0
  25. data/lib/copyleaks/models/submissions/file_ocr_submission_model.rb +61 -0
  26. data/lib/copyleaks/models/submissions/file_submission_model.rb +57 -0
  27. data/lib/copyleaks/models/submissions/index.rb +33 -0
  28. data/lib/copyleaks/models/submissions/properties/actions.rb +33 -0
  29. data/lib/copyleaks/models/submissions/properties/author.rb +41 -0
  30. data/lib/copyleaks/models/submissions/properties/copyleaks_db.rb +44 -0
  31. data/lib/copyleaks/models/submissions/properties/domains_mode.rb +31 -0
  32. data/lib/copyleaks/models/submissions/properties/exclude.rb +59 -0
  33. data/lib/copyleaks/models/submissions/properties/filter.rb +67 -0
  34. data/lib/copyleaks/models/submissions/properties/index.rb +45 -0
  35. data/lib/copyleaks/models/submissions/properties/indexing.rb +41 -0
  36. data/lib/copyleaks/models/submissions/properties/pdf_properties.rb +55 -0
  37. data/lib/copyleaks/models/submissions/properties/repository.rb +41 -0
  38. data/lib/copyleaks/models/submissions/properties/scanning.rb +55 -0
  39. data/lib/copyleaks/models/submissions/properties/scanning_exclude.rb +44 -0
  40. data/lib/copyleaks/models/submissions/properties/scanning_repository.rb +46 -0
  41. data/lib/copyleaks/models/submissions/properties/sensitive_data_protection.rb +71 -0
  42. data/lib/copyleaks/models/submissions/properties/submission_properties.rb +136 -0
  43. data/lib/copyleaks/models/submissions/properties/webhooks.rb +44 -0
  44. data/lib/copyleaks/models/submissions/submission_model.rb +47 -0
  45. data/lib/copyleaks/models/submissions/url_submission_model.rb +51 -0
  46. data/lib/copyleaks/utils/status-code.utils.rb +38 -0
  47. data/lib/copyleaks/version.rb +3 -0
  48. data/lib/index.rb +35 -0
  49. data/plagiarism-checker.gemspec +29 -0
  50. metadata +50 -108
  51. data/.rubocop.yml +0 -2
  52. data/copyleaks_api.gemspec +0 -29
  53. data/example_asynchronous.rb +0 -51
  54. data/example_synchronous.rb +0 -74
  55. data/lib/copyleaks_api.rb +0 -24
  56. data/lib/copyleaks_api/Models/ResultRecord.rb +0 -58
  57. data/lib/copyleaks_api/access_token.rb +0 -36
  58. data/lib/copyleaks_api/api.rb +0 -216
  59. data/lib/copyleaks_api/config.rb +0 -37
  60. data/lib/copyleaks_api/copyleaks_cloud.rb +0 -116
  61. data/lib/copyleaks_api/copyleaks_process.rb +0 -78
  62. data/lib/copyleaks_api/errors.rb +0 -29
  63. data/lib/copyleaks_api/validators/custom_fields_validator.rb +0 -35
  64. data/lib/copyleaks_api/validators/email_validator.rb +0 -13
  65. data/lib/copyleaks_api/validators/file_validator.rb +0 -56
  66. data/lib/copyleaks_api/validators/language_validator.rb +0 -12
  67. data/lib/copyleaks_api/validators/response_validator.rb +0 -27
  68. data/lib/copyleaks_api/validators/url_validator.rb +0 -12
  69. data/lib/copyleaks_api/version.rb +0 -3
@@ -1,58 +0,0 @@
1
- module CopyleaksApi
2
- class ResultRecord
3
- def initialize(response)
4
- @URL = response['URL']
5
- @Percents = response['Percents']
6
- @NumberOfCopiedWords = response['NumberOfCopiedWords']
7
- @CachedVersion = response['CachedVersion']
8
- @Title = response['Title']
9
- @Introduction = response['Introduction']
10
- @ComparisonReport = response['ComparisonReport']
11
- @EmbededComparison = response['EmbededComparison']
12
- end
13
-
14
- def get_url
15
- @URL
16
- end
17
-
18
- def get_percents
19
- @Percents
20
- end
21
-
22
- def get_number_of_copied_words
23
- @NumberOfCopiedWords
24
- end
25
-
26
- def get_cached_version
27
- @CachedVersion
28
- end
29
-
30
- def get_title
31
- @Title
32
- end
33
-
34
- def get_introduction
35
- @Introduction
36
- end
37
-
38
- def get_comparison_report
39
- @ComparisonReport
40
- end
41
-
42
- def get_embeded_comparison
43
- @EmbededComparison
44
- end
45
-
46
- def to_s
47
- puts "----------------------------------------------------------------"
48
- puts "Title: #{@Title}"
49
- puts "Introduction: #{@Introduction}"
50
- puts "Url: #{@URL}"
51
- puts "Percents: #{@Percents}%"
52
- puts "NumberOfCopiedWords: #{@NumberOfCopiedWords}"
53
- puts "CachedVersion: #{@CachedVersion}"
54
- puts "ComparisonReport: #{@ComparisonReport}"
55
- puts "EmbededComparison: #{@EmbededComparison}"
56
- end
57
- end
58
- end
@@ -1,36 +0,0 @@
1
- require 'time'
2
- require 'json'
3
-
4
- module CopyleaksApi
5
- class AccessToken
6
- attr_reader :created_at, :expire_at
7
-
8
- # constructor
9
- def initialize(cloud, email, api_key)
10
- @cloud = cloud
11
- @email = email
12
- @api_key = api_key
13
- login
14
- end
15
-
16
- # predicate method to check if token is not expired
17
- def fresh?
18
- DateTime.now.new_offset(0) < @expire_at
19
- end
20
-
21
- # return token string
22
- def token
23
- return @token if fresh?
24
- login
25
- end
26
-
27
- # get token for given email and api_key pair
28
- def login
29
- res = @cloud.api.post('account/login-api', { Email: @email, ApiKey: @api_key }.to_json)
30
- @token = res['access_token']
31
- @created_at = DateTime.parse(res['.issued'])
32
- @expire_at = DateTime.parse(res['.expires'])
33
- @token
34
- end
35
- end
36
- end
@@ -1,216 +0,0 @@
1
- require 'json'
2
- require 'net/http'
3
- require 'mimemagic'
4
- require 'mimemagic/overlay'
5
- require 'openssl'
6
- require 'securerandom'
7
-
8
-
9
- module CopyleaksApi
10
- class Api
11
- BASE_URL = 'https://api.copyleaks.com'.freeze
12
- API_VERSION_V1 = 'v1'.freeze
13
- API_VERSION_V2 = 'v2'.freeze
14
-
15
- # constructor
16
- def initialize
17
- uri = URI(BASE_URL)
18
- @http = Net::HTTP.new(uri.host, uri.port)
19
- @http.use_ssl = true
20
- @http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
21
- end
22
-
23
- # make get request without any callback header
24
- def get(path, options = {})
25
- request = Net::HTTP::Get.new(request_uri(path))
26
- make_request(request, options.merge(no_callbacks: true))
27
- end
28
-
29
- # make get request without any callback header
30
- def get_with_final_path(path, options = {})
31
- request = Net::HTTP::Get.new(path)
32
- make_request(request, options.merge(no_callbacks: true))
33
- end
34
-
35
- # make post request with given options
36
- def post(path, body = nil, options = {})
37
- request = Net::HTTP::Post.new(request_uri(path))
38
- request.body = body
39
- make_request(request, options)
40
- end
41
-
42
- # makes delete request without callbacks
43
- def delete(path, options = {})
44
- request = Net::HTTP::Delete.new(request_uri(path))
45
- make_request(request, options.merge(no_callbacks: true))
46
- end
47
-
48
- # makes post request with file inside
49
- def post_file(path, file_path, options = {})
50
- request = Net::HTTP::Post.new(request_uri(path))
51
- options[:partial_scan] ||= CopyleaksApi::Config.allow_partial_scan
52
- boundary = "copyleaks_sdk_#{SecureRandom.hex(4)}"
53
- request.body = file_body(file_path, boundary)
54
- make_request(request, options.merge(boundary: boundary))
55
- end
56
-
57
- # makes post request with file inside
58
- def post_files(path, files_paths, options = {})
59
- request = Net::HTTP::Post.new(request_uri(path, api_version: API_VERSION_V2))
60
- options[:partial_scan] ||= CopyleaksApi::Config.allow_partial_scan
61
- options[:compare_only] ||= CopyleaksApi::Config.compare_only
62
- boundary = "copyleaks_sdk_#{SecureRandom.hex(4)}"
63
- request.body = files_body(files_paths, boundary)
64
- make_request(request, options.merge(boundary: boundary))
65
- end
66
- private
67
-
68
- # extracts mime type of given file
69
- def extract_mime_type(path)
70
- mime = MimeMagic.by_magic(File.open(path))
71
- mime ? mime.type : 'text/plain'
72
- end
73
-
74
- # prepares post body with file inside
75
- def file_body(path, boundary)
76
- [
77
- "\r\n--#{boundary}\r\n",
78
- "content-disposition: form-data; name=\"file\"",
79
- "; filename=\"#{File.basename(path)}\"\r\n",
80
- "Content-Type: #{extract_mime_type(path)}\r\n",
81
- "Content-Transfer-Encoding: binary\r\n",
82
- "\r\n",
83
- File.open(path, 'rb') { |io| io.read },
84
- "\r\n--#{boundary}--\r\n"
85
- ].join('')
86
- end
87
-
88
- # prepares post body with file inside
89
- def files_body(paths, boundary)
90
- body = "\r\n--#{boundary}\r\n"
91
- counter = 1
92
- paths.each do |path|
93
- body += "content-disposition: form-data; name=\"file_#{counter}\"" +
94
- "; filename=\"#{File.basename(path)}\"\r\n" +
95
- "Content-Type: #{extract_mime_type(path)}\r\n" +
96
- "Content-Transfer-Encoding: binary\r\n" +
97
- "\r\n" +
98
- File.open(path, 'rb') { |io| io.read } +
99
- "\r\n--#{boundary}\r\n"
100
- counter += 1
101
- end
102
- body += "\r\n--#{boundary}--\r\n"
103
- body
104
- end
105
-
106
- # gather all API path
107
- def request_uri(path, api_version: API_VERSION_V1)
108
- "/#{api_version}/#{path}"
109
- end
110
-
111
- # gather headers, makes request and do validation
112
- def make_request(request, options)
113
- gather_headers(request, options)
114
- response = @http.request(request)
115
- Validators::ResponseValidator.validate!(response)
116
- if not options.key?(:parse_json) or options[:parse_json]
117
- JSON.parse(response.body)
118
- else
119
- response.body
120
- end
121
- end
122
-
123
- # gather all headers
124
- def gather_headers(request, options)
125
- [
126
- http_callbacks_header(options),
127
- email_callback_header(options),
128
- authentication_header(options),
129
- sandbox_header,
130
- compare_only_header,
131
- import_to_database_only_header,
132
- in_progress_result(options),
133
- content_type_header(options),
134
- partial_scan_header(options),
135
- 'User-Agent' => "RUBYSDK/#{CopyleaksApi::VERSION}"
136
- ].reduce({}, :merge).each do |header, value|
137
- request[header] = value
138
- end
139
- end
140
-
141
- # prepares header for sandbox mode
142
- def sandbox_header
143
- return {} unless Config.sandbox_mode
144
- { 'copyleaks-sandbox-mode' => '' }
145
- end
146
-
147
- # Compare your files against each other only
148
- def compare_only_header
149
- return {} unless Config.compare_only
150
- { 'copyleaks-compare-documents-for-similarity' => '' }
151
- end
152
-
153
- def import_to_database_only_header
154
- return {} unless Config.import_to_database_only
155
- { 'copyleaks-index-only' => '' }
156
- end
157
-
158
- # Receive callback every time we find a new result without waiting for completion
159
- def in_progress_result(options)
160
- return {} if options[:no_http_callback] || options[:no_callbacks]
161
- value = options[:in_progress_result] || CopyleaksApi::Config.in_progress_result
162
- return {} unless value
163
- Validators::UrlValidator.validate!(value)
164
- { 'copyleaks-in-progress-new-result' => value }
165
- end
166
- # prepares header for content type
167
- def content_type_header(options)
168
- { 'Content-Type' => options[:boundary] ? "multipart/form-data; boundary=\"#{options[:boundary]}\"" :
169
- 'application/json' }
170
- end
171
-
172
- # prepares header for partial scan
173
- def partial_scan_header(options)
174
- return {} unless !options[:allow_partial_scan].nil? && options[:allow_partial_scan] || Config.allow_partial_scan
175
- { 'copyleaks-allow-partial-scan' => '' }
176
- end
177
-
178
- # prepares authentication header
179
- def authentication_header(options)
180
- return {} unless options[:token]
181
- { 'Authorization' => "Bearer #{options[:token]}" }
182
- end
183
-
184
- # prepare header for http callback
185
- def http_callbacks_header(options)
186
- return {} if options[:no_http_callback] || options[:no_callbacks]
187
- value = options[:http_callback] || CopyleaksApi::Config.http_callback
188
- return {} unless value
189
- Validators::UrlValidator.validate!(value)
190
- { 'copyleaks-http-completion-callback' => value }
191
- end
192
-
193
- # prepares header for email callback
194
- def email_callback_header(options)
195
- return {} if options[:no_email_callback] || options[:no_callbacks]
196
- value = options[:email_callback] || CopyleaksApi::Config.email_callback
197
- return {} unless value
198
- Validators::EmailValidator.validate!(value)
199
- { 'copyleaks-email-callback' => value }
200
- end
201
-
202
-
203
- # prepares headers with custom fields
204
- def custom_field_headers(options)
205
- return {} if options[:no_custom_fields]
206
- value = CopyleaksApi::Config.custom_fields.merge(options[:custom_fields] || {})
207
- Validators::CustomFieldsValidator.validate!(value)
208
- prepare_custom_fields(value)
209
- end
210
-
211
- # prepares custom fields before transformation into headers
212
- def prepare_custom_fields(fields)
213
- fields.each_with_object({}) { |e, o| o["copyleaks-client-custom-#{e[0]}"] = e[1] }
214
- end
215
- end
216
- end
@@ -1,37 +0,0 @@
1
- module CopyleaksApi
2
- class Config
3
- DEFAULTS = {
4
- sandbox_mode: false,
5
- allow_partial_scan: false,
6
- http_callback: nil,
7
- email_callback: nil,
8
- custom_fields: {},
9
- compare_only: false,
10
- import_to_database_only: false,
11
- in_progress_result: nil,
12
- }.freeze
13
-
14
- class << self
15
- attr_writer :sandbox_mode, :compare_only, :http_callback, :email_callback, :custom_fields, :allow_partial_scan, :import_to_database_only, :in_progress_result
16
-
17
- DEFAULTS.each do |attr, value|
18
- # getters for all options
19
- define_method(attr) do
20
- var = instance_variable_get("@#{attr}")
21
- return var if var
22
- instance_variable_set("@#{attr}", value)
23
- end
24
- end
25
-
26
- # provide block syntax possibility for setting options
27
- def config
28
- yield(self)
29
- end
30
-
31
- # reset all options to default
32
- def reset
33
- DEFAULTS.each { |attr, value| instance_variable_set("@#{attr}", value) }
34
- end
35
- end
36
- end
37
- end
@@ -1,116 +0,0 @@
1
- require 'copyleaks_api/errors'
2
- require 'copyleaks_api/api'
3
-
4
-
5
- module CopyleaksApi
6
- class CopyleaksCloud
7
- ALLOWED_ENDPOINTS = [:businesses, :education, :websites]
8
- attr_accessor :access_token
9
- attr_reader :endpoint_type
10
-
11
- # constructor
12
- def initialize(email, api_key, type)
13
- raise ArgumentError, "Endpoint type '#{type}' is invalid" unless ALLOWED_ENDPOINTS.include?(type.to_sym)
14
- @access_token = AccessToken.new(self, email, api_key)
15
- @endpoint_type = type
16
- end
17
-
18
- def api
19
- @api ||= CopyleaksApi::Api.new
20
- end
21
-
22
- # returns account balance from endpoint
23
- def get_credits_balance
24
- api.get(url('count-credits'), token: @access_token.token)['Amount'].to_i
25
- end
26
-
27
- # uses create-by-url endpoint to create process
28
- def create_by_url(url, options = {})
29
- response = api.post(url('create-by-url'), { 'Url' => url }.to_json, options.merge(token: @access_token.token))
30
- CopyleaksProcess.create(self, api, response)
31
- end
32
-
33
- # uses create-by-file endpoint to create process
34
- def create_by_file(file_path, options = {})
35
- response = api.post_file(url('create-by-file'), file_path, options.merge(token: @access_token.token))
36
- CopyleaksProcess.create(self, api, response)
37
- end
38
-
39
- # uses create-by-file endpoint to create process
40
- def create_by_files(files_paths, options = {})
41
- response = api.post_files(url('create-by-file'), files_paths, options.merge(token: @access_token.token))
42
- success_uploads = response['Success']
43
- processes = []
44
- success_uploads.each do |upload|
45
- processes.push(CopyleaksProcess.create(self, api, upload))
46
- end
47
- processes
48
- end
49
-
50
- # uses create-by-file-ocr endpoint to create process
51
- def create_by_ocr(ocr_file_path, options = {})
52
- response = api.post_file(url_with_language('create-by-file-ocr', options), ocr_file_path,
53
- options.merge(token: @access_token.token))
54
- CopyleaksProcess.create(self, api, response)
55
- end
56
-
57
- # uses create-by-text endpoint to create process
58
- def create_by_text(text, options = {})
59
- response = api.post(url('create-by-text'), text, options.merge(token: @access_token.token))
60
- CopyleaksProcess.create(self, api, response)
61
- end
62
-
63
- # Returns a list of your past processes
64
- def get_processes
65
- response = api.get(url(:list), token: @access_token.token)
66
- response.map { |hash| CopyleaksProcess.create_from_list(self, @api, hash) }
67
- end
68
-
69
- # Returns the raw text of a given result
70
- def get_raw_text(result)
71
- response = api.get_with_final_path(result.get_cached_version, parse_json: false, token: @access_token.token)
72
- response
73
- end
74
-
75
- # Returns the raw text of a given result
76
- def get_comparison_report(result)
77
- response = api.get_with_final_path(result.get_comparison_report, token: @access_token.token)
78
- response
79
- end
80
-
81
- # Returns a list of currently supported languages for ocr
82
- def get_ocr_languages
83
- response = api.get(url_misc('ocr-languages-list'))
84
- response
85
- end
86
-
87
- # Returns a list of currently supported file types
88
- def get_supported_file_types
89
- response = api.get(url_misc('supported-file-types'))
90
- response
91
- end
92
-
93
- # gather url for endpoints which need language in get parameters
94
- def url_with_language(action, options)
95
- language = options[:language] || Language.english
96
- Validators::LanguageValidator.validate!(language)
97
- url("#{action}?language=#{language}")
98
- end
99
-
100
- # gather path for endpoints
101
- def url(action, id = nil)
102
- return "#{@endpoint_type}/#{action}" if id.nil?
103
- "#{@endpoint_type}/#{id}/#{action}"
104
- end
105
-
106
- # gather path for download endpoint
107
- def url_downloads(action, id = nil)
108
- return "downloads/#{action}"
109
- end
110
-
111
- # gather path for download endpoint
112
- def url_misc(action, id = nil)
113
- return "miscellaneous/#{action}"
114
- end
115
- end
116
- end