Completed
Pull Request — master (#90)
by Janis
15:10
created

js/temp/http-service.js   A

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 86
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 86
rs 10
c 0
b 0
f 0
cc 0
nc 2
mnd 1
bc 9
fnc 6
bpm 1.5
cpm 1.6666
noi 8

1 Function

Rating   Name   Duplication   Size   Complexity  
A http-service.js ➔ HttpService 0 58 2
1
/**
2
 * nextCloud - ocr
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Janis Koehr <[email protected]>
8
 * @copyright Janis Koehr 2017
9
 */
10
(function (OC, OCA, window, $) {
11
	'use strict';
12
13
	/**
14
     * Communicates with OCR backend API.
15
     * @public
16
	 * @class Ocr.HttpService
17
	 */
18
	var HttpService = function (config) {
19
20
		/** Constructor */
21
		var ocrConfig;
22
		config !== undefined ? ocrConfig = config : console.error('OCR config is not defined.');
0 ignored issues
show
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
23
24
        /**
25
         * 
26
         * @private
27
         * @param {Object} opts (opts.data, opts.method, opts.url)
28
         * @returns {$.Deferred.Promise} resolve with response or reject in case of error
29
         */
30
		var makeRequest = function (opts) {
31
			return $.ajax(opts);
32
		}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
33
34
        /** 
35
         * Load all languages that are supported form the backend.
36
         * @public
37
         * @returns {$.Deferred}
38
         */
39
		this.loadAvailableLanguages = function () {
40
            var languages = ['deu', 'eng'];
41
			// TODO: enable docker behavior
42
			/*OCP.AppConfig.getValue('ocr', 'available-languages', '', function (data) {
43
                languages = data.split(';');
44
            });*/
45
            return languages;
46
		}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
47
48
        /**
49
         * Triggers the OCR process for the given files and languages.
50
         * @public
51
         * @returns {$.Deferred}
52
         */
53
		this.process = function (selectedFiles, selectedLanguages) {
54
			return makeRequest({
55
				method: 'POST',
56
				url: ocrConfig.PROCESSING_ENDPOINT,
0 ignored issues
show
Bug introduced by
The variable ocrConfig seems to not be initialized for all possible execution paths.
Loading history...
57
				data: {
58
					files: selectedFiles,
59
					languages: selectedLanguages
60
				},
61
			});
62
		}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
63
64
        /**
65
         * Check for the status of the OCR process.
66
         * @public
67
         * @returns {$.Deferred}
68
         */
69
		this.checkStatus = function () {
70
			return makeRequest({
71
				method: 'GET',
72
				url: ocrConfig.STATUS_ENDPOINT,
0 ignored issues
show
Bug introduced by
The variable ocrConfig seems to not be initialized for all possible execution paths.
Loading history...
73
			});
74
		}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
75
	}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
76
77
	/**
78
	 * Init OCR HttpService
79
	 */
80
	/** We have to be in the Files App! */
81
	if (!OCA.Files) {
82
		return;
83
	}
84
	/** Escape when the requested file is public.php */
85
	if (/(public)\.php/i.exec(window.location.href) !== null) {
86
		return;
87
	}
88
	/** Create namespace Ocr */
89
	if (!OCA.Ocr) {
90
		OCA.Ocr = {};
91
	}
92
	OCA.Ocr.HttpService = HttpService;
93
94
	/** global: OC, OCA */
95
})(OC, OCA, window, jQuery);
96