Class: CopyleaksApi::CopyleaksCloud

Inherits:
Object
  • Object
show all
Defined in:
lib/copyleaks_api/copyleaks_cloud.rb

Constant Summary collapse

ALLOWED_ENDPOINTS =
[:publisher, :academic]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, api_key, type) ⇒ CopyleaksCloud

constructor

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 11

def initialize(email, api_key, type)
  raise ArgumentError, "Endpoint type '#{type}' is invalid" unless ALLOWED_ENDPOINTS.include?(type.to_sym)
  @access_token = AccessToken.new(self, email, api_key)
  @endpoint_type = type
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 7

def access_token
  @access_token
end

#endpoint_typeObject (readonly)

Returns the value of attribute endpoint_type.



8
9
10
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 8

def endpoint_type
  @endpoint_type
end

Instance Method Details

#apiObject



17
18
19
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 17

def api
  @api ||= CopyleaksApi::Api.new
end

#balanceObject

returns account balance from endpoint



22
23
24
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 22

def balance
 api.get(url('count-credits'), token: @access_token.token)['Amount'].to_i
end

#create_by_file(file_path, options = {}) ⇒ Object

uses create-by-file endpoint to create process



33
34
35
36
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 33

def create_by_file(file_path, options = {})
  response = api.post_file(url('create-by-file'), file_path, options.merge(token: @access_token.token))
  CopyleaksProcess.create(self, response)
end

#create_by_ocr(ocr_file_path, options = {}) ⇒ Object

uses create-by-file-ocr endpoint to create process



39
40
41
42
43
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 39

def create_by_ocr(ocr_file_path, options = {})
  response = api.post_file(url_with_language('create-by-file-ocr', options), ocr_file_path,
                           options.merge(token: @access_token.token))
  CopyleaksProcess.create(self, response)
end

#create_by_text(text, options = {}) ⇒ Object

uses create-by-text endpoint to create process



46
47
48
49
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 46

def create_by_text(text, options = {})
  response = api.post(url('create-by-text'), text, options.merge(token: @access_token.token))
  CopyleaksProcess.create(self, response)
end

#create_by_url(url, options = {}) ⇒ Object

uses create-by-url endpoint to create process



27
28
29
30
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 27

def create_by_url(url, options = {})
  response = api.post(url('create-by-url'), { 'Url' => url }.to_json, options.merge(token: @access_token.token))
  CopyleaksProcess.create(self, response)
end

#delete(id) ⇒ Object

deletes process by given id



52
53
54
55
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 52

def delete(id)
  api.delete(url(:delete, id), token: @access_token.token)
  true
end

#listObject

retries all processes



58
59
60
61
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 58

def list
  response = api.get(url(:list), token: @access_token.token)
  response.map { |hash| CopyleaksProcess.create_from_list(self, hash) }
end

#result(id, options = {}) ⇒ Object

retries result information of process with given id



71
72
73
74
75
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 71

def result(id, options = {})
  response = api.get(url(:result, id), no_callbacks: true, token: @access_token.token)
  return response if options[:raw]
  CopyleaksApi::CopyleaksProcess.create_from_result(self, id, response)
end

#status(id, options = {}) ⇒ Object

retries status information of process with given id



64
65
66
67
68
# File 'lib/copyleaks_api/copyleaks_cloud.rb', line 64

def status(id, options = {})
  response = api.get(url(:status, id), no_callbacks: true, token: @access_token.token)
  return response if options[:raw]
  CopyleaksApi::CopyleaksProcess.create_from_status(self, id, response)
end