Class: OnlyofficeDocumentserverTestingFramework::IntegrationExampleApi

Inherits:
Object
  • Object
show all
Defined in:
lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb

Overview

Class for working with integration example api

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, api_endpoint: "#{instance.doc_server_base_url}/example") ⇒ IntegrationExampleApi

Returns a new instance of IntegrationExampleApi.



12
13
14
15
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 12

def initialize(instance, api_endpoint: "#{instance.doc_server_base_url}/example")
  @instance = instance
  @api_endpoint = api_endpoint
end

Instance Attribute Details

#api_endpointString (readonly)

Returns url of api endpoint.

Returns:

  • (String)

    url of api endpoint



10
11
12
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 10

def api_endpoint
  @api_endpoint
end

Instance Method Details

#delete_file(file_name, sleep_after_delete: 3) ⇒ nil

Delete file on server by name

Parameters:

  • file_name (String)

    name of file to delete

  • sleep_after_delete (Integer) (defaults to: 3)

    how much time to sleep after file delete sometimes it took time to delete this file and it cannot be checked by ‘file_data` method, since file will be not created but you get inside old file

Returns:

  • (nil)


44
45
46
47
48
49
50
51
52
53
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 44

def delete_file(file_name, sleep_after_delete: 3)
  raise 'File name is not found on server' unless file_data(file_name)

  url = URI::DEFAULT_PARSER.escape("#{@api_endpoint}/file?filename=#{file_name}")
  request = Net::HTTP::Delete.new(url)
  response = http_from_url(url).request(request)
  result = JSON.parse(response.read_body)
  sleep(sleep_after_delete)
  result
end

#file_data(name) ⇒ String?

Get file data by it’s name

Parameters:

  • name (String)

Returns:

  • (String, nil)

    id



58
59
60
61
62
63
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 58

def file_data(name)
  files.each do |file|
    return file if file['title'] == name
  end
  nil
end

#filesArray<Hash>

Returns list of file on example.

Returns:

  • (Array<Hash>)

    list of file on example



18
19
20
21
22
23
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 18

def files
  url = "#{@api_endpoint}/files/"
  request = Net::HTTP::Get.new(url)
  response = http_from_url(url).request(request)
  JSON.parse(response.read_body)
end

#upload_file(file_path) ⇒ String

Upload file by it’s path

Parameters:

  • file_path (String)

Returns:

  • (String)

    name of uploaded file



28
29
30
31
32
33
34
35
36
# File 'lib/onlyoffice_documentserver_testing_framework/test_instance_docs/integration_example_api.rb', line 28

def upload_file(file_path)
  url = URI("#{@api_endpoint}/upload/")

  request = Net::HTTP::Post.new(url)
  form_data = [['uploadedFile', File.open(file_path)]]
  request.set_form(form_data, 'multipart/form-data')
  response = http_from_url(url).request(request)
  JSON.parse(response.read_body)['filename']
end