aspose_ocr_cloud 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/aspose_ocr_cloud.gemspec +26 -0
- data/lib/aspose_ocr_cloud.rb +36 -0
- data/lib/aspose_ocr_cloud/api/ocr_api.rb +137 -0
- data/lib/aspose_ocr_cloud/api_client.rb +335 -0
- data/lib/aspose_ocr_cloud/api_error.rb +24 -0
- data/lib/aspose_ocr_cloud/configuration.rb +166 -0
- data/lib/aspose_ocr_cloud/models/base_object.rb +86 -0
- data/lib/aspose_ocr_cloud/models/ocr_response.rb +77 -0
- data/lib/aspose_ocr_cloud/models/page.rb +47 -0
- data/lib/aspose_ocr_cloud/models/pages_info.rb +39 -0
- data/lib/aspose_ocr_cloud/models/part_info.rb +37 -0
- data/lib/aspose_ocr_cloud/models/parts_info.rb +39 -0
- data/lib/aspose_ocr_cloud/version.rb +3 -0
- data/test/data/Sampleocr.bmp +0 -0
- data/test/ocr_tests.rb +41 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37de5f2a1a4442e062698043eb125c1490c85fa9
|
4
|
+
data.tar.gz: bfd021e2fedf22f660c45279c04d33f9436c51ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbfa94f09ef9ee52346991ae891422094805db864b26bb8282b1bf00f9e63366c7cbc3d7a7d413d7ff782860d8a1ccc0de86cfae3d05e3abdb6fc06a15d14d24
|
7
|
+
data.tar.gz: 62c80c8d526181bfcfc9f5652130cc7888e9db8196e4a1666abea9975d8c068e71b53b86b1d8a69f989119f38304b6c1e12bcc1f16894ffc67254d06b7e7eb85
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2001-2016 Aspose Pty Ltd
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Aspose.OCR Cloud SDK For Ruby
|
2
|
+
This SDK lets you use [Aspose Cloud OCR APIs](http://www.aspose.com/cloud/ocr-api.aspx) in your Ruby web apps.
|
3
|
+
|
4
|
+
## Installing
|
5
|
+
You can simply install Aspose OCR Cloud SDK with gem:
|
6
|
+
|
7
|
+
`gem install aspose_ocr_cloud`
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
APIs of this SDK can be called as follows:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'aspose_ocr_cloud'
|
14
|
+
|
15
|
+
class OCRUsage
|
16
|
+
|
17
|
+
include AsposeOCRCloud
|
18
|
+
include AsposeStorageCloud
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
#Get App key and App SID from https://cloud.aspose.com
|
22
|
+
AsposeApp.app_key_and_sid("APP_KEY", "APP_SID")
|
23
|
+
@ocr_api = OcrApi.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def upload_file(file_name)
|
27
|
+
@storage_api = StorageApi.new
|
28
|
+
response = @storage_api.put_create(file_name, File.open("data/" << file_name,"r") { |io| io.read } )
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_recognize_document
|
32
|
+
#Recognize image text, language and text region can be selected, default dictionaries can be used for correction.
|
33
|
+
file_name = "Sampleocr.bmp"
|
34
|
+
upload_file(file_name)
|
35
|
+
|
36
|
+
response = @ocr_api.get_recognize_document(file_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
```
|
41
|
+
## Unit Tests
|
42
|
+
Aspose OCR SDK includes a suite of unit tests within the [test](https://github.com/asposeocr/Aspose_OCR_Cloud/blob/master/SDKs/Aspose.OCR_Cloud_SDK_for_Ruby/test/ocr_tests.rb) subdirectory. These Unit Tests also serves as examples of how to use the Aspose OCR SDK.
|
43
|
+
|
44
|
+
## Contact
|
45
|
+
Your feedback is very important to us. Please email us all your queries and feedback at marketplace@aspose.com.
|
46
|
+
|
47
|
+
## License
|
48
|
+
Aspose OCR SDK is available under the MIT license. See the [LICENSE](https://github.com/asposeocr/Aspose_OCR_Cloud/blob/master/SDKs/Aspose.OCR_Cloud_SDK_for_Ruby/LICENSE) file for more info.
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "aspose_ocr_cloud/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "aspose_ocr_cloud"
|
7
|
+
s.version = AsposeOCRCloud::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["M. Sohail Ismail"]
|
10
|
+
s.email = ["muhammad.sohail@aspose.com"]
|
11
|
+
s.homepage = "http://www.aspose.com/cloud/ocr-api.aspx"
|
12
|
+
s.summary = %q{Aspose.OCR for Cloud}
|
13
|
+
s.description = %q{Aspose.OCR for Cloud is a cloud-based REST API for optical character recognition and document scanning. It allows you to scan documents and recognize characters. Recognize text in English and other languages, and recognize text in only part of an image. Aspose.OCR for Cloud supports a variety of fonts in different styles, like regular, bold, and italic, and different image formats. You can use Aspose.OCR for Cloud in many scenarios, for example, extracting text and saving to a database.}
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.add_runtime_dependency 'typhoeus', '~> 0.8'
|
17
|
+
s.add_runtime_dependency 'json', '~> 1.7'
|
18
|
+
s.add_runtime_dependency 'aspose_storage_cloud', '~> 1.0', '>= 1.0.0'
|
19
|
+
|
20
|
+
s.add_development_dependency 'minitest', '~> 5.8'
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n")
|
23
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
24
|
+
s.executables = []
|
25
|
+
s.require_paths = ["lib"]
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'aspose_storage_cloud'
|
2
|
+
|
3
|
+
# Common files
|
4
|
+
require_relative 'aspose_ocr_cloud/api_client'
|
5
|
+
require_relative 'aspose_ocr_cloud/api_error'
|
6
|
+
require_relative 'aspose_ocr_cloud/version'
|
7
|
+
require_relative 'aspose_ocr_cloud/configuration'
|
8
|
+
|
9
|
+
# Models
|
10
|
+
require_relative 'aspose_ocr_cloud/models/base_object'
|
11
|
+
require_relative 'aspose_ocr_cloud/models/ocr_response'
|
12
|
+
require_relative 'aspose_ocr_cloud/models/parts_info'
|
13
|
+
require_relative 'aspose_ocr_cloud/models/pages_info'
|
14
|
+
require_relative 'aspose_ocr_cloud/models/part_info'
|
15
|
+
require_relative 'aspose_ocr_cloud/models/page'
|
16
|
+
|
17
|
+
# APIs
|
18
|
+
require_relative 'aspose_ocr_cloud/api/ocr_api'
|
19
|
+
|
20
|
+
module AsposeOCRCloud
|
21
|
+
class << self
|
22
|
+
# Configure sdk using block.
|
23
|
+
# AsposeOCRCloud.configure do |config|
|
24
|
+
# config.username = "xxx"
|
25
|
+
# config.password = "xxx"
|
26
|
+
# end
|
27
|
+
# If no block given, return the configuration singleton instance.
|
28
|
+
def configure
|
29
|
+
if block_given?
|
30
|
+
yield Configuration.instance
|
31
|
+
else
|
32
|
+
Configuration.instance
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module AsposeOCRCloud
|
4
|
+
class OcrApi
|
5
|
+
attr_accessor :api_client
|
6
|
+
|
7
|
+
def initialize(api_client = nil)
|
8
|
+
@api_client = api_client || Configuration.api_client
|
9
|
+
end
|
10
|
+
|
11
|
+
# Recognize image text from some url if provided or from the request body content, language can be selected, default dictionaries can be used for correction.
|
12
|
+
#
|
13
|
+
# @param [Hash] opts the optional parameters
|
14
|
+
# @option opts [String] :url The image file url.
|
15
|
+
# @option opts [String] :language Language of the document.
|
16
|
+
# @option opts [BOOLEAN] :use_default_dictionaries Use default dictionaries for result correction.
|
17
|
+
# @option opts [File] :file
|
18
|
+
# @return [OCRResponse]
|
19
|
+
def post_ocr_from_url_or_content(opts = {})
|
20
|
+
if Configuration.debugging
|
21
|
+
Configuration.logger.debug "Calling API: OcrApi#post_ocr_from_url_or_content ..."
|
22
|
+
end
|
23
|
+
|
24
|
+
# resource path
|
25
|
+
path = "/ocr/recognize".sub('{format}','json')
|
26
|
+
|
27
|
+
# query parameters
|
28
|
+
query_params = {}
|
29
|
+
query_params[:'url'] = opts[:'url'] if opts[:'url']
|
30
|
+
query_params[:'language'] = opts[:'language'] if opts[:'language']
|
31
|
+
query_params[:'useDefaultDictionaries'] = opts[:'use_default_dictionaries'] if opts[:'use_default_dictionaries']
|
32
|
+
|
33
|
+
# header parameters
|
34
|
+
header_params = {}
|
35
|
+
|
36
|
+
# HTTP header 'Accept' (if needed)
|
37
|
+
_header_accept = ['application/json']
|
38
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
39
|
+
|
40
|
+
# HTTP header 'Content-Type'
|
41
|
+
_header_content_type = ['multipart/form-data']
|
42
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
43
|
+
|
44
|
+
# form parameters
|
45
|
+
form_params = {}
|
46
|
+
form_params["file"] = opts[:'file'] if opts[:'file']
|
47
|
+
|
48
|
+
# http body (model)
|
49
|
+
post_body = nil
|
50
|
+
|
51
|
+
|
52
|
+
auth_names = []
|
53
|
+
result = @api_client.call_api(:POST, path,
|
54
|
+
:header_params => header_params,
|
55
|
+
:query_params => query_params,
|
56
|
+
:form_params => form_params,
|
57
|
+
:body => post_body,
|
58
|
+
:auth_names => auth_names,
|
59
|
+
:return_type => 'OCRResponse')
|
60
|
+
if Configuration.debugging
|
61
|
+
Configuration.logger.debug "API called: OcrApi#post_ocr_from_url_or_content. Result: #{result.inspect}"
|
62
|
+
end
|
63
|
+
return result
|
64
|
+
end
|
65
|
+
|
66
|
+
# Recognize image text, language and text region can be selected, default dictionaries can be used for correction.
|
67
|
+
#
|
68
|
+
# @param name Name of the file to recognize.
|
69
|
+
# @param [Hash] opts the optional parameters
|
70
|
+
# @option opts [String] :language Language of the document.
|
71
|
+
# @option opts [Integer] :rect_x Top left point X coordinate of Rectangle to recognize text inside.
|
72
|
+
# @option opts [Integer] :rect_y Top left point Y coordinate of Rectangle to recognize text inside.
|
73
|
+
# @option opts [Integer] :rect_width Width of Rectangle to recognize text inside.
|
74
|
+
# @option opts [Integer] :rect_height Height of Rectangle to recognize text inside.
|
75
|
+
# @option opts [BOOLEAN] :use_default_dictionaries Use default dictionaries for result correction.
|
76
|
+
# @option opts [String] :storage Image's storage.
|
77
|
+
# @option opts [String] :folder Image's folder.
|
78
|
+
# @return [OCRResponse]
|
79
|
+
def get_recognize_document(name, opts = {})
|
80
|
+
if Configuration.debugging
|
81
|
+
Configuration.logger.debug "Calling API: OcrApi#get_recognize_document ..."
|
82
|
+
end
|
83
|
+
|
84
|
+
# verify the required parameter 'name' is set
|
85
|
+
fail "Missing the required parameter 'name' when calling get_recognize_document" if name.nil?
|
86
|
+
|
87
|
+
# resource path
|
88
|
+
path = "/ocr/{name}/recognize".sub('{format}','json').sub('{' + 'name' + '}', name.to_s)
|
89
|
+
|
90
|
+
# query parameters
|
91
|
+
query_params = {}
|
92
|
+
query_params[:'language'] = opts[:'language'] if opts[:'language']
|
93
|
+
query_params[:'rectX'] = opts[:'rect_x'] if opts[:'rect_x']
|
94
|
+
query_params[:'rectY'] = opts[:'rect_y'] if opts[:'rect_y']
|
95
|
+
query_params[:'rectWidth'] = opts[:'rect_width'] if opts[:'rect_width']
|
96
|
+
query_params[:'rectHeight'] = opts[:'rect_height'] if opts[:'rect_height']
|
97
|
+
query_params[:'useDefaultDictionaries'] = opts[:'use_default_dictionaries'] if opts[:'use_default_dictionaries']
|
98
|
+
query_params[:'storage'] = opts[:'storage'] if opts[:'storage']
|
99
|
+
query_params[:'folder'] = opts[:'folder'] if opts[:'folder']
|
100
|
+
|
101
|
+
# header parameters
|
102
|
+
header_params = {}
|
103
|
+
|
104
|
+
# HTTP header 'Accept' (if needed)
|
105
|
+
_header_accept = ['application/json', 'text/json', 'application/xml', 'text/xml', 'text/javascript']
|
106
|
+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
|
107
|
+
|
108
|
+
# HTTP header 'Content-Type'
|
109
|
+
_header_content_type = []
|
110
|
+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
|
111
|
+
|
112
|
+
# form parameters
|
113
|
+
form_params = {}
|
114
|
+
|
115
|
+
# http body (model)
|
116
|
+
post_body = nil
|
117
|
+
|
118
|
+
|
119
|
+
auth_names = []
|
120
|
+
result = @api_client.call_api(:GET, path,
|
121
|
+
:header_params => header_params,
|
122
|
+
:query_params => query_params,
|
123
|
+
:form_params => form_params,
|
124
|
+
:body => post_body,
|
125
|
+
:auth_names => auth_names,
|
126
|
+
:return_type => 'OCRResponse')
|
127
|
+
if Configuration.debugging
|
128
|
+
Configuration.logger.debug "API called: OcrApi#get_recognize_document. Result: #{result.inspect}"
|
129
|
+
end
|
130
|
+
return result
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
@@ -0,0 +1,335 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'json'
|
3
|
+
require 'logger'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'typhoeus'
|
6
|
+
require 'uri'
|
7
|
+
require 'openssl'
|
8
|
+
require 'base64'
|
9
|
+
require 'rexml/document'
|
10
|
+
|
11
|
+
module AsposeOCRCloud
|
12
|
+
class ApiClient
|
13
|
+
|
14
|
+
include AsposeStorageCloud
|
15
|
+
|
16
|
+
attr_accessor :host
|
17
|
+
|
18
|
+
# Defines the headers to be used in HTTP requests of all API calls by default.
|
19
|
+
#
|
20
|
+
# @return [Hash]
|
21
|
+
attr_accessor :default_headers
|
22
|
+
|
23
|
+
# Stores the HTTP response from the last API call using this API client.
|
24
|
+
attr_accessor :last_response
|
25
|
+
|
26
|
+
def initialize(host = nil)
|
27
|
+
@host = host || Configuration.base_url
|
28
|
+
@format = 'json'
|
29
|
+
@user_agent = "ruby-swagger-#{VERSION}"
|
30
|
+
@default_headers = {
|
31
|
+
'Content-Type' => "application/#{@format.downcase}",
|
32
|
+
'User-Agent' => @user_agent
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def call_api(http_method, path, opts = {})
|
37
|
+
request = build_request(http_method, path, opts)
|
38
|
+
response = request.run
|
39
|
+
|
40
|
+
# record as last response
|
41
|
+
@last_response = response
|
42
|
+
|
43
|
+
if Configuration.debugging
|
44
|
+
Configuration.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
|
45
|
+
end
|
46
|
+
|
47
|
+
unless response.success?
|
48
|
+
fail ApiError.new(:code => response.code,
|
49
|
+
:response_headers => response.headers,
|
50
|
+
:response_body => response.body),
|
51
|
+
response.status_message
|
52
|
+
end
|
53
|
+
|
54
|
+
if opts[:return_type]
|
55
|
+
deserialize(response, opts[:return_type])
|
56
|
+
else
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def build_request(http_method, path, opts = {})
|
62
|
+
url = build_request_url(path)
|
63
|
+
http_method = http_method.to_sym.downcase
|
64
|
+
|
65
|
+
header_params = @default_headers.merge(opts[:header_params] || {})
|
66
|
+
query_params = {}
|
67
|
+
form_params = opts[:form_params] || {}
|
68
|
+
|
69
|
+
req_opts = {
|
70
|
+
:method => http_method,
|
71
|
+
:headers => header_params,
|
72
|
+
:params => query_params,
|
73
|
+
:ssl_verifypeer => Configuration.verify_ssl,
|
74
|
+
:sslcert => Configuration.cert_file,
|
75
|
+
:sslkey => Configuration.key_file,
|
76
|
+
:cainfo => Configuration.ssl_ca_cert,
|
77
|
+
:verbose => Configuration.debugging
|
78
|
+
}
|
79
|
+
|
80
|
+
if [:post, :patch, :put, :delete].include?(http_method)
|
81
|
+
req_body = build_request_body(header_params, form_params, opts[:body])
|
82
|
+
req_opts.update :body => req_body
|
83
|
+
if Configuration.debugging
|
84
|
+
Configuration.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
url = sign(url, opts[:query_params])
|
89
|
+
Typhoeus::Request.new(url, req_opts)
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
# Signs a URI with your appSID and Key.
|
94
|
+
# * :url describes the URL to sign
|
95
|
+
|
96
|
+
def sign(url, query_params)
|
97
|
+
|
98
|
+
fail "Please set Aspose App key and SID first. You can get App key and App SID from https://cloud.aspose.com" if AsposeApp.app_key.nil? || AsposeApp.app_sid.nil?
|
99
|
+
|
100
|
+
url = url[0..-2] if url[-1].eql? '/'
|
101
|
+
|
102
|
+
unless query_params.empty?
|
103
|
+
url = "#{url}?"
|
104
|
+
query_params.each { |key, value|
|
105
|
+
url = "#{url}#{key}=#{value}&"
|
106
|
+
}
|
107
|
+
url = url[0..-2]
|
108
|
+
end
|
109
|
+
|
110
|
+
url = URI.escape(url)
|
111
|
+
parsed_url = URI.parse(url)
|
112
|
+
|
113
|
+
url_to_sign = "#{parsed_url.scheme}://#{parsed_url.host}#{parsed_url.path}"
|
114
|
+
url_to_sign += "?#{parsed_url.query}" if parsed_url.query
|
115
|
+
if parsed_url.query
|
116
|
+
url_to_sign += "&appSID=#{AsposeApp.app_sid}"
|
117
|
+
else
|
118
|
+
url_to_sign += "?appSID=#{AsposeApp.app_sid}"
|
119
|
+
end
|
120
|
+
|
121
|
+
# create a signature using the private key and the URL
|
122
|
+
raw_signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), AsposeApp.app_key, url_to_sign)
|
123
|
+
|
124
|
+
#Convert raw to encoded string
|
125
|
+
signature = Base64.strict_encode64(raw_signature).tr('+/', '-_')
|
126
|
+
|
127
|
+
#remove invalid character
|
128
|
+
signature = signature.gsub(/[=_-]/, '=' => '', '_' => '%2f', '-' => '%2b')
|
129
|
+
|
130
|
+
#Define expression
|
131
|
+
pat = Regexp.new('%[0-9a-f]{2}')
|
132
|
+
|
133
|
+
#Replace the portion matched to the above pattern to upper case
|
134
|
+
6.times do
|
135
|
+
signature = signature.sub(pat, pat.match(signature).to_s.upcase)
|
136
|
+
end
|
137
|
+
|
138
|
+
# prepend the server and append the signature.
|
139
|
+
url_to_sign + "&signature=#{signature}"
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
# Deserialize the response to the given return type.
|
144
|
+
#
|
145
|
+
# @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
|
146
|
+
def deserialize(response, return_type)
|
147
|
+
body = response.body
|
148
|
+
return nil if body.nil? || body.empty?
|
149
|
+
|
150
|
+
# handle file downloading - save response body into a tmp file and return the File instance
|
151
|
+
return download_file(response) if return_type == 'File'
|
152
|
+
|
153
|
+
# ensuring a default content type
|
154
|
+
content_type = response.headers['Content-Type'] || 'application/json'
|
155
|
+
|
156
|
+
unless content_type.start_with?('application/json')
|
157
|
+
fail "Content-Type is not supported: #{content_type}"
|
158
|
+
end
|
159
|
+
|
160
|
+
begin
|
161
|
+
data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
|
162
|
+
rescue JSON::ParserError => e
|
163
|
+
if %w(String Date DateTime).include?(return_type)
|
164
|
+
data = body
|
165
|
+
else
|
166
|
+
raise e
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
convert_to_type data, return_type
|
171
|
+
end
|
172
|
+
|
173
|
+
# Convert data to the given return type.
|
174
|
+
def convert_to_type(data, return_type)
|
175
|
+
return nil if data.nil?
|
176
|
+
case return_type
|
177
|
+
when 'String'
|
178
|
+
data.to_s
|
179
|
+
when 'Integer'
|
180
|
+
data.to_i
|
181
|
+
when 'Float'
|
182
|
+
data.to_f
|
183
|
+
when 'BOOLEAN'
|
184
|
+
data == true
|
185
|
+
when 'DateTime'
|
186
|
+
# parse date time (expecting ISO 8601 format)
|
187
|
+
DateTime.parse data
|
188
|
+
when 'Date'
|
189
|
+
# parse date time (expecting ISO 8601 format)
|
190
|
+
Date.parse data
|
191
|
+
when 'Object'
|
192
|
+
# generic object, return directly
|
193
|
+
data
|
194
|
+
when /\AArray<(.+)>\z/
|
195
|
+
# e.g. Array<Pet>
|
196
|
+
sub_type = $1
|
197
|
+
data.map {|item| convert_to_type(item, sub_type) }
|
198
|
+
when /\AHash\<String, (.+)\>\z/
|
199
|
+
# e.g. Hash<String, Integer>
|
200
|
+
sub_type = $1
|
201
|
+
{}.tap do |hash|
|
202
|
+
data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
|
203
|
+
end
|
204
|
+
else
|
205
|
+
# models, e.g. Pet
|
206
|
+
AsposeOCRCloud.const_get(return_type).new.tap do |model|
|
207
|
+
model.build_from_hash data
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# Save response body into a file in (the defined) temporary folder, using the filename
|
213
|
+
# from the "Content-Disposition" header if provided, otherwise a random filename.
|
214
|
+
#
|
215
|
+
# @see Configuration#temp_folder_path
|
216
|
+
# @return [File] the file downloaded
|
217
|
+
def download_file(response)
|
218
|
+
tmp_file = Tempfile.new '', Configuration.temp_folder_path
|
219
|
+
content_disposition = response.headers['Content-Disposition']
|
220
|
+
|
221
|
+
if content_disposition
|
222
|
+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
|
223
|
+
path = File.join File.dirname(tmp_file), filename
|
224
|
+
else
|
225
|
+
path = tmp_file.path
|
226
|
+
end
|
227
|
+
# close and delete temp file
|
228
|
+
tmp_file.close!
|
229
|
+
|
230
|
+
File.open(path, 'w') { |file| file.write(response.body) }
|
231
|
+
Configuration.logger.info "File written to #{path}. Please move the file to a proper "\
|
232
|
+
"folder for further processing and delete the temp afterwards"
|
233
|
+
File.new(path)
|
234
|
+
end
|
235
|
+
|
236
|
+
def build_request_url(path)
|
237
|
+
# Add leading and trailing slashes to path
|
238
|
+
path = "/#{path}".gsub(/\/+/, '/')
|
239
|
+
URI.encode(host + path)
|
240
|
+
end
|
241
|
+
|
242
|
+
def build_request_body(header_params, form_params, body)
|
243
|
+
# http form
|
244
|
+
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
|
245
|
+
header_params['Content-Type'] == 'multipart/form-data'
|
246
|
+
|
247
|
+
data = form_params.dup
|
248
|
+
data.each do |key, value|
|
249
|
+
if key.eql? "file"
|
250
|
+
data = value
|
251
|
+
break
|
252
|
+
else
|
253
|
+
data[key] = value.to_s if value && !value.is_a?(File)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
elsif body
|
257
|
+
data = body.is_a?(String) ? body : body.to_json
|
258
|
+
else
|
259
|
+
data = nil
|
260
|
+
end
|
261
|
+
data
|
262
|
+
end
|
263
|
+
|
264
|
+
# Update hearder and query params based on authentication settings.
|
265
|
+
def update_params_for_auth!(header_params, query_params, auth_names)
|
266
|
+
Array(auth_names).each do |auth_name|
|
267
|
+
auth_setting = Configuration.auth_settings[auth_name]
|
268
|
+
next unless auth_setting
|
269
|
+
case auth_setting[:in]
|
270
|
+
when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
|
271
|
+
when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
|
272
|
+
else fail ArgumentError, 'Authentication token must be in `query` of `header`'
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def user_agent=(user_agent)
|
278
|
+
@user_agent = user_agent
|
279
|
+
@default_headers['User-Agent'] = @user_agent
|
280
|
+
end
|
281
|
+
|
282
|
+
# Return Accept header based on an array of accepts provided.
|
283
|
+
# @param [Array] accepts array for Accept
|
284
|
+
# @return [String] the Accept header (e.g. application/json)
|
285
|
+
def select_header_accept(accepts)
|
286
|
+
if accepts.empty?
|
287
|
+
return
|
288
|
+
elsif accepts.any?{ |s| s.casecmp('application/json') == 0 }
|
289
|
+
'application/json' # look for json data by default
|
290
|
+
else
|
291
|
+
accepts.join(',')
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# Return Content-Type header based on an array of content types provided.
|
296
|
+
# @param [Array] content_types array for Content-Type
|
297
|
+
# @return [String] the Content-Type header (e.g. application/json)
|
298
|
+
def select_header_content_type(content_types)
|
299
|
+
if content_types.empty?
|
300
|
+
'application/json' # use application/json by default
|
301
|
+
elsif content_types.any?{ |s| s.casecmp('application/json')==0 }
|
302
|
+
'application/json' # use application/json if it's included
|
303
|
+
else
|
304
|
+
content_types[0] # otherwise, use the first one
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# Convert object (array, hash, object, etc) to JSON string.
|
309
|
+
# @param [Object] model object to be converted into JSON string
|
310
|
+
# @return [String] JSON string representation of the object
|
311
|
+
def object_to_http_body(model)
|
312
|
+
return if model.nil?
|
313
|
+
_body = nil
|
314
|
+
if model.is_a?(Array)
|
315
|
+
_body = model.map{|m| object_to_hash(m) }
|
316
|
+
else
|
317
|
+
_body = object_to_hash(model)
|
318
|
+
end
|
319
|
+
_body.to_json
|
320
|
+
end
|
321
|
+
|
322
|
+
# Convert object(non-array) to hash.
|
323
|
+
# @param [Object] obj object to be converted into JSON string
|
324
|
+
# @return [String] JSON string representation of the object
|
325
|
+
def object_to_hash(obj)
|
326
|
+
if obj.respond_to?(:to_hash)
|
327
|
+
obj.to_hash
|
328
|
+
else
|
329
|
+
obj
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
|
334
|
+
end
|
335
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
class ApiError < StandardError
|
3
|
+
attr_reader :code, :response_headers, :response_body
|
4
|
+
|
5
|
+
# Usage examples:
|
6
|
+
# ApiError.new
|
7
|
+
# ApiError.new("message")
|
8
|
+
# ApiError.new(:code => 500, :response_headers => {}, :response_body => "")
|
9
|
+
# ApiError.new(:code => 404, :message => "Not Found")
|
10
|
+
def initialize(arg = nil)
|
11
|
+
if arg.is_a? Hash
|
12
|
+
arg.each do |k, v|
|
13
|
+
if k.to_s == 'message'
|
14
|
+
super v
|
15
|
+
else
|
16
|
+
instance_variable_set "@#{k}", v
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
super arg
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module AsposeOCRCloud
|
5
|
+
class Configuration
|
6
|
+
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
# Default api client
|
10
|
+
attr_accessor :api_client
|
11
|
+
|
12
|
+
# Defines url scheme
|
13
|
+
attr_accessor :scheme
|
14
|
+
|
15
|
+
# Defines url host
|
16
|
+
attr_accessor :host
|
17
|
+
|
18
|
+
# Defines url base path
|
19
|
+
attr_accessor :base_path
|
20
|
+
|
21
|
+
# Defines API keys used with API Key authentications.
|
22
|
+
#
|
23
|
+
# @return [Hash] key: parameter name, value: parameter value (API key)
|
24
|
+
#
|
25
|
+
# @example parameter name is "api_key", API key is "xxx" (e.g. "api_key=xxx" in query string)
|
26
|
+
# config.api_key['api_key'] = 'xxx'
|
27
|
+
attr_accessor :api_key
|
28
|
+
|
29
|
+
# Defines API key prefixes used with API Key authentications.
|
30
|
+
#
|
31
|
+
# @return [Hash] key: parameter name, value: API key prefix
|
32
|
+
#
|
33
|
+
# @example parameter name is "Authorization", API key prefix is "Token" (e.g. "Authorization: Token xxx" in headers)
|
34
|
+
# config.api_key_prefix['api_key'] = 'Token'
|
35
|
+
attr_accessor :api_key_prefix
|
36
|
+
|
37
|
+
# Defines the username used with HTTP basic authentication.
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
attr_accessor :username
|
41
|
+
|
42
|
+
# Defines the password used with HTTP basic authentication.
|
43
|
+
#
|
44
|
+
# @return [String]
|
45
|
+
attr_accessor :password
|
46
|
+
|
47
|
+
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
|
48
|
+
# details will be logged with `logger.debug` (see the `logger` attribute).
|
49
|
+
# Default to false.
|
50
|
+
#
|
51
|
+
# @return [true, false]
|
52
|
+
attr_accessor :debugging
|
53
|
+
|
54
|
+
# Defines the logger used for debugging.
|
55
|
+
# Default to `Rails.logger` (when in Rails) or logging to STDOUT.
|
56
|
+
#
|
57
|
+
# @return [#debug]
|
58
|
+
attr_accessor :logger
|
59
|
+
|
60
|
+
# Defines the temporary folder to store downloaded files
|
61
|
+
# (for API endpoints that have file response).
|
62
|
+
# Default to use `Tempfile`.
|
63
|
+
#
|
64
|
+
# @return [String]
|
65
|
+
attr_accessor :temp_folder_path
|
66
|
+
|
67
|
+
### TLS/SSL
|
68
|
+
# Set this to false to skip verifying SSL certificate when calling API from https server.
|
69
|
+
# Default to true.
|
70
|
+
#
|
71
|
+
# @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
|
72
|
+
#
|
73
|
+
# @return [true, false]
|
74
|
+
attr_accessor :verify_ssl
|
75
|
+
|
76
|
+
# Set this to customize the certificate file to verify the peer.
|
77
|
+
#
|
78
|
+
# @return [String] the path to the certificate file
|
79
|
+
#
|
80
|
+
# @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
|
81
|
+
# https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
|
82
|
+
attr_accessor :ssl_ca_cert
|
83
|
+
|
84
|
+
# Client certificate file (for client certificate)
|
85
|
+
attr_accessor :cert_file
|
86
|
+
|
87
|
+
# Client private key file (for client certificate)
|
88
|
+
attr_accessor :key_file
|
89
|
+
|
90
|
+
attr_accessor :inject_format
|
91
|
+
|
92
|
+
attr_accessor :force_ending_format
|
93
|
+
|
94
|
+
class << self
|
95
|
+
def method_missing(method_name, *args, &block)
|
96
|
+
config = Configuration.instance
|
97
|
+
if config.respond_to?(method_name)
|
98
|
+
config.send(method_name, *args, &block)
|
99
|
+
else
|
100
|
+
super
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def initialize
|
106
|
+
@scheme = 'http'
|
107
|
+
@host = 'api.aspose.com'
|
108
|
+
@base_path = '/v1.1'
|
109
|
+
@api_key = {}
|
110
|
+
@api_key_prefix = {}
|
111
|
+
@verify_ssl = true
|
112
|
+
@cert_file = nil
|
113
|
+
@key_file = nil
|
114
|
+
@debugging = false
|
115
|
+
@inject_format = false
|
116
|
+
@force_ending_format = false
|
117
|
+
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
118
|
+
end
|
119
|
+
|
120
|
+
def api_client
|
121
|
+
@api_client ||= ApiClient.new
|
122
|
+
end
|
123
|
+
|
124
|
+
def scheme=(scheme)
|
125
|
+
# remove :// from scheme
|
126
|
+
@scheme = scheme.sub(/:\/\//, '')
|
127
|
+
end
|
128
|
+
|
129
|
+
def host=(host)
|
130
|
+
# remove http(s):// and anything after a slash
|
131
|
+
@host = host.sub(/https?:\/\//, '').split('/').first
|
132
|
+
end
|
133
|
+
|
134
|
+
def base_path=(base_path)
|
135
|
+
# Add leading and trailing slashes to base_path
|
136
|
+
@base_path = "/#{base_path}".gsub(/\/+/, '/')
|
137
|
+
@base_path = "" if @base_path == "/"
|
138
|
+
end
|
139
|
+
|
140
|
+
def base_url
|
141
|
+
url = "#{scheme}://#{[host, base_path].join('/').gsub(/\/+/, '/')}".sub(/\/+\z/, '')
|
142
|
+
URI.encode(url)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Gets API key (with prefix if set).
|
146
|
+
# @param [String] param_name the parameter name of API key auth
|
147
|
+
def api_key_with_prefix(param_name)
|
148
|
+
if @api_key_prefix[param_name]
|
149
|
+
"#{@api_key_prefix[param_name]} #{@api_key[param_name]}"
|
150
|
+
else
|
151
|
+
@api_key[param_name]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# Gets Basic Auth token string
|
156
|
+
def basic_auth_token
|
157
|
+
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns Auth Settings hash for api client.
|
161
|
+
def auth_settings
|
162
|
+
{
|
163
|
+
}
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module AsposeOCRCloud
|
4
|
+
# base class containing fundamental method such as to_hash, build_from_hash and more
|
5
|
+
class BaseObject
|
6
|
+
|
7
|
+
# build the object from hash
|
8
|
+
def build_from_hash(attributes)
|
9
|
+
return nil unless attributes.is_a?(Hash)
|
10
|
+
self.class.swagger_types.each_pair do |key, type|
|
11
|
+
if type =~ /^Array<(.*)>/i
|
12
|
+
if attributes[self.class.attribute_map[key]].is_a?(Array)
|
13
|
+
self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
|
14
|
+
else
|
15
|
+
#TODO show warning in debug mode
|
16
|
+
end
|
17
|
+
elsif !attributes[self.class.attribute_map[key]].nil?
|
18
|
+
self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
|
19
|
+
else
|
20
|
+
# data not found in attributes(hash), not an issue as the data can be optional
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def _deserialize(type, value)
|
28
|
+
case type.to_sym
|
29
|
+
when :DateTime
|
30
|
+
DateTime.parse(value)
|
31
|
+
when :Date
|
32
|
+
Date.parse(value)
|
33
|
+
when :String
|
34
|
+
value.to_s
|
35
|
+
when :Integer
|
36
|
+
value.to_i
|
37
|
+
when :Float
|
38
|
+
value.to_f
|
39
|
+
when :BOOLEAN
|
40
|
+
if value =~ /^(true|t|yes|y|1)$/i
|
41
|
+
true
|
42
|
+
else
|
43
|
+
false
|
44
|
+
end
|
45
|
+
else # model
|
46
|
+
_model = AsposeOCRCloud.const_get(type).new
|
47
|
+
_model.build_from_hash(value)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
to_hash.to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
# to_body is an alias to to_body (backward compatibility))
|
56
|
+
def to_body
|
57
|
+
to_hash
|
58
|
+
end
|
59
|
+
|
60
|
+
# return the object in the form of hash
|
61
|
+
def to_hash
|
62
|
+
hash = {}
|
63
|
+
self.class.attribute_map.each_pair do |attr, param|
|
64
|
+
value = self.send(attr)
|
65
|
+
next if value.nil?
|
66
|
+
if value.is_a?(Array)
|
67
|
+
hash[param] = value.compact.map{ |v| _to_hash(v) }
|
68
|
+
else
|
69
|
+
hash[param] = _to_hash(value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
hash
|
73
|
+
end
|
74
|
+
|
75
|
+
# Method to output non-array value in the form of hash
|
76
|
+
# For object, use to_hash. Otherwise, just return the value
|
77
|
+
def _to_hash(value)
|
78
|
+
if value.respond_to? :to_hash
|
79
|
+
value.to_hash
|
80
|
+
else
|
81
|
+
value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
#
|
3
|
+
class OCRResponse < BaseObject
|
4
|
+
attr_accessor :text, :parts_info, :pages_info, :status, :code
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'text' => :'Text',
|
11
|
+
|
12
|
+
#
|
13
|
+
:'parts_info' => :'PartsInfo',
|
14
|
+
|
15
|
+
#
|
16
|
+
:'pages_info' => :'PagesInfo',
|
17
|
+
|
18
|
+
#
|
19
|
+
:'status' => :'Status',
|
20
|
+
|
21
|
+
#
|
22
|
+
:'code' => :'Code'
|
23
|
+
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# attribute type
|
28
|
+
def self.swagger_types
|
29
|
+
{
|
30
|
+
:'text' => :'String',
|
31
|
+
:'parts_info' => :'PartsInfo',
|
32
|
+
:'pages_info' => :'PagesInfo',
|
33
|
+
:'status' => :'String',
|
34
|
+
:'code' => :'String'
|
35
|
+
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(attributes = {})
|
40
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
41
|
+
|
42
|
+
# convert string to symbol for hash key
|
43
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
44
|
+
|
45
|
+
|
46
|
+
if attributes[:'Text']
|
47
|
+
self.text = attributes[:'Text']
|
48
|
+
end
|
49
|
+
|
50
|
+
if attributes[:'PartsInfo']
|
51
|
+
self.parts_info = attributes[:'PartsInfo']
|
52
|
+
end
|
53
|
+
|
54
|
+
if attributes[:'PagesInfo']
|
55
|
+
self.pages_info = attributes[:'PagesInfo']
|
56
|
+
end
|
57
|
+
|
58
|
+
if attributes[:'Status']
|
59
|
+
self.status = attributes[:'Status']
|
60
|
+
end
|
61
|
+
|
62
|
+
if attributes[:'Code']
|
63
|
+
self.code = attributes[:'Code']
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
def status=(status)
|
69
|
+
allowed_values = ["Continue", "SwitchingProtocols", "OK", "Created", "Accepted", "NonAuthoritativeInformation", "NoContent", "ResetContent", "PartialContent", "MultipleChoices", "Ambiguous", "MovedPermanently", "Moved", "Found", "Redirect", "SeeOther", "RedirectMethod", "NotModified", "UseProxy", "Unused", "TemporaryRedirect", "RedirectKeepVerb", "BadRequest", "Unauthorized", "PaymentRequired", "Forbidden", "NotFound", "MethodNotAllowed", "NotAcceptable", "ProxyAuthenticationRequired", "RequestTimeout", "Conflict", "Gone", "LengthRequired", "PreconditionFailed", "RequestEntityTooLarge", "RequestUriTooLong", "UnsupportedMediaType", "RequestedRangeNotSatisfiable", "ExpectationFailed", "UpgradeRequired", "InternalServerError", "NotImplemented", "BadGateway", "ServiceUnavailable", "GatewayTimeout", "HttpVersionNotSupported"]
|
70
|
+
if status && !allowed_values.include?(status)
|
71
|
+
fail "invalid value for 'status', must be one of #{allowed_values}"
|
72
|
+
end
|
73
|
+
@status = status
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
#
|
3
|
+
class Page < BaseObject
|
4
|
+
attr_accessor :page_text, :parts
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'page_text' => :'PageText',
|
11
|
+
|
12
|
+
#
|
13
|
+
:'parts' => :'Parts'
|
14
|
+
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# attribute type
|
19
|
+
def self.swagger_types
|
20
|
+
{
|
21
|
+
:'page_text' => :'String',
|
22
|
+
:'parts' => :'Array<PartInfo>'
|
23
|
+
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(attributes = {})
|
28
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
29
|
+
|
30
|
+
# convert string to symbol for hash key
|
31
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
32
|
+
|
33
|
+
|
34
|
+
if attributes[:'PageText']
|
35
|
+
self.page_text = attributes[:'PageText']
|
36
|
+
end
|
37
|
+
|
38
|
+
if attributes[:'Parts']
|
39
|
+
if (value = attributes[:'Parts']).is_a?(Array)
|
40
|
+
self.parts = value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
#
|
3
|
+
class PagesInfo < BaseObject
|
4
|
+
attr_accessor :pages
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'pages' => :'Pages'
|
11
|
+
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
# attribute type
|
16
|
+
def self.swagger_types
|
17
|
+
{
|
18
|
+
:'pages' => :'Array<Page>'
|
19
|
+
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(attributes = {})
|
24
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
25
|
+
|
26
|
+
# convert string to symbol for hash key
|
27
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
28
|
+
|
29
|
+
|
30
|
+
if attributes[:'Pages']
|
31
|
+
if (value = attributes[:'Pages']).is_a?(Array)
|
32
|
+
self.pages = value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
#
|
3
|
+
class PartInfo < BaseObject
|
4
|
+
attr_accessor :text
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'text' => :'Text'
|
11
|
+
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
# attribute type
|
16
|
+
def self.swagger_types
|
17
|
+
{
|
18
|
+
:'text' => :'String'
|
19
|
+
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(attributes = {})
|
24
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
25
|
+
|
26
|
+
# convert string to symbol for hash key
|
27
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
28
|
+
|
29
|
+
|
30
|
+
if attributes[:'Text']
|
31
|
+
self.text = attributes[:'Text']
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module AsposeOCRCloud
|
2
|
+
#
|
3
|
+
class PartsInfo < BaseObject
|
4
|
+
attr_accessor :parts
|
5
|
+
# attribute mapping from ruby-style variable name to JSON key
|
6
|
+
def self.attribute_map
|
7
|
+
{
|
8
|
+
|
9
|
+
#
|
10
|
+
:'parts' => :'Parts'
|
11
|
+
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
# attribute type
|
16
|
+
def self.swagger_types
|
17
|
+
{
|
18
|
+
:'parts' => :'Array<PartInfo>'
|
19
|
+
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(attributes = {})
|
24
|
+
return if !attributes.is_a?(Hash) || attributes.empty?
|
25
|
+
|
26
|
+
# convert string to symbol for hash key
|
27
|
+
attributes = attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
28
|
+
|
29
|
+
|
30
|
+
if attributes[:'Parts']
|
31
|
+
if (value = attributes[:'Parts']).is_a?(Array)
|
32
|
+
self.parts = value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
Binary file
|
data/test/ocr_tests.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "minitest/unit"
|
3
|
+
|
4
|
+
require_relative '../lib/aspose_ocr_cloud'
|
5
|
+
|
6
|
+
class OCRTests < Minitest::Test
|
7
|
+
include MiniTest::Assertions
|
8
|
+
include AsposeOCRCloud
|
9
|
+
include AsposeStorageCloud
|
10
|
+
|
11
|
+
def setup
|
12
|
+
#Get App key and App SID from https://cloud.aspose.com
|
13
|
+
AsposeApp.app_key_and_sid("", "")
|
14
|
+
@ocr_api = OcrApi.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def teardown
|
18
|
+
end
|
19
|
+
|
20
|
+
def upload_file(file_name)
|
21
|
+
@storage_api = StorageApi.new
|
22
|
+
response = @storage_api.put_create(file_name, File.open("data/" << file_name,"r") { |io| io.read } )
|
23
|
+
assert(response, message="Failed to upload {file_name} file.")
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_post_ocr_from_url_or_content
|
27
|
+
opts = {url: "http://s017.radikal.ru/i406/1202/7b/70183bef7a09.jpg", use_default_dictionaries: true}
|
28
|
+
|
29
|
+
response = @ocr_api.post_ocr_from_url_or_content(opts)
|
30
|
+
assert(response, message="Failed to recognize image text from some url if provided or from the request body content.")
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_get_recognize_document
|
34
|
+
file_name = "Sampleocr.bmp"
|
35
|
+
upload_file(file_name)
|
36
|
+
|
37
|
+
response = @ocr_api.get_recognize_document(file_name)
|
38
|
+
assert(response, message="Failed to recognize image text.")
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aspose_ocr_cloud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- M. Sohail Ismail
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: typhoeus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: aspose_storage_cloud
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 1.0.0
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '1.0'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.0.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: minitest
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '5.8'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '5.8'
|
75
|
+
description: Aspose.OCR for Cloud is a cloud-based REST API for optical character
|
76
|
+
recognition and document scanning. It allows you to scan documents and recognize
|
77
|
+
characters. Recognize text in English and other languages, and recognize text in
|
78
|
+
only part of an image. Aspose.OCR for Cloud supports a variety of fonts in different
|
79
|
+
styles, like regular, bold, and italic, and different image formats. You can use
|
80
|
+
Aspose.OCR for Cloud in many scenarios, for example, extracting text and saving
|
81
|
+
to a database.
|
82
|
+
email:
|
83
|
+
- muhammad.sohail@aspose.com
|
84
|
+
executables: []
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files: []
|
87
|
+
files:
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- aspose_ocr_cloud.gemspec
|
91
|
+
- lib/aspose_ocr_cloud.rb
|
92
|
+
- lib/aspose_ocr_cloud/api/ocr_api.rb
|
93
|
+
- lib/aspose_ocr_cloud/api_client.rb
|
94
|
+
- lib/aspose_ocr_cloud/api_error.rb
|
95
|
+
- lib/aspose_ocr_cloud/configuration.rb
|
96
|
+
- lib/aspose_ocr_cloud/models/base_object.rb
|
97
|
+
- lib/aspose_ocr_cloud/models/ocr_response.rb
|
98
|
+
- lib/aspose_ocr_cloud/models/page.rb
|
99
|
+
- lib/aspose_ocr_cloud/models/pages_info.rb
|
100
|
+
- lib/aspose_ocr_cloud/models/part_info.rb
|
101
|
+
- lib/aspose_ocr_cloud/models/parts_info.rb
|
102
|
+
- lib/aspose_ocr_cloud/version.rb
|
103
|
+
- test/data/Sampleocr.bmp
|
104
|
+
- test/ocr_tests.rb
|
105
|
+
homepage: http://www.aspose.com/cloud/ocr-api.aspx
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.5.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Aspose.OCR for Cloud
|
129
|
+
test_files:
|
130
|
+
- test/data/Sampleocr.bmp
|
131
|
+
- test/ocr_tests.rb
|