abbyy-ruby 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fc6450ad5597fc12860b98eb3d6cbe931700f156178f2253c1689a492bd5d79a
4
+ data.tar.gz: bed1bfaf806bd81e34bb5220d5c1365a6d1b48b9a2e2b0f4de09eabfe4444650
5
+ SHA512:
6
+ metadata.gz: 41f2a33b5a258e8ee91002e5249b1198e04906c7cc15b6c363842458f1618a2920dc71624f26f9aadc0c0aaa6e4ecc771b55cbe6b8156982dbf09194d8577150
7
+ data.tar.gz: 71543ac82d519ecc44b05fef53819396220db6c6d00a0e4dc1e7dab506be872e5052ebefbec58bc72c563758c8291d55a160a09e02dd9cc3a741470d41e118dd
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.6.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in abbyy.gemspec
4
+ gemspec
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, cmd: 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1,94 @@
1
+ # abbyy-ruby
2
+
3
+ Simple Ruby wrapper for Abbyy Cloud OCR SDK
4
+ The initial code was borrowed from https://github.com/silex-pro/abbyy
5
+
6
+ [![Build Status](https://img.shields.io/travis/silex-pro/abbyy-ruby.svg)](https://travis-ci.org/silex-pro/abbyy-ruby)
7
+ [![Gem](https://img.shields.io/gem/v/abbyy-ruby.svg)](https://rubygems.org/gems/abbyy-ruby)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'abbyy-ruby'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install abbyy-ruby
24
+
25
+ ### Example
26
+
27
+ ```ruby
28
+ client = Abbyy::Client.new(APPLICATION_ID, PASSWORD)
29
+ client.process_business_card "business_card.png", exportFormat: 'xml', imageSource: 'photo'
30
+ client.get_task_status
31
+ client.get
32
+ ```
33
+
34
+ ### Example with Ruby on Rails
35
+
36
+ $ touch config/initializers/abbyy.rb
37
+
38
+ ```ruby
39
+ require 'abbyy'
40
+
41
+ Abbyy.configure do |config|
42
+ config.application_id = APPLICATION_ID
43
+ config.password = PASSWORD
44
+ end
45
+ ```
46
+
47
+ Everywhere in your rails application, you can access Abbyy without crendentials:
48
+
49
+ ```ruby
50
+ client = Abbyy::Client.new
51
+ client.process_image "image.png"
52
+ ```
53
+
54
+ ### Example with Resque
55
+
56
+ Create new worker ImageScanner:
57
+
58
+ $ touch app/workers/image_scanner.rb
59
+
60
+ ```ruby
61
+ class ImageScanner
62
+ @queue = :scanner
63
+
64
+ def self.perform(attachment_id)
65
+ attachment = Attachment.find_by_id(attachment_id)
66
+ client = Abbyy::Client.new
67
+ client.process_business_card attachment.full_filename, exportFormat: 'xml', imageSource: 'photo'
68
+ while %w(Queued InProgress).include?(client.task[:status])
69
+ sleep(client.task[:estimatedProcessingTime].to_i)
70
+ client.get_task_status
71
+ end
72
+ if client.task[:status] == 'Completed'
73
+ xml_data = REXML::Document.new(client.get)
74
+ ...
75
+ else
76
+ ...
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ To install this gem onto your local machine, run `bundle exec rake install`.
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/silex-pro/abbyy-ruby/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'progress']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'abbyy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "abbyy-ruby"
8
+ spec.version = Abbyy::VERSION
9
+ spec.authors = ["Adrien Rambert"]
10
+ spec.email = ["contact@silex.pro"]
11
+ spec.summary = %q{A Ruby wrapper for the Abbyy Cloud OCR SDK API.}
12
+ spec.homepage = "https://github.com/silex-pro/abbyy-ruby"
13
+ spec.license = "MIT"
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", ">= 1.3"
20
+ spec.add_development_dependency "rake", ">= 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.8"
22
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
23
+ spec.add_development_dependency "pry", "~> 0"
24
+ spec.add_development_dependency "pry-doc", "~> 0"
25
+
26
+ spec.add_dependency "rest-client", ">= 2.0.0"
27
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "abbyy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ require 'rest-client'
2
+ require 'rexml/document'
3
+ require 'cgi'
4
+
5
+ require 'abbyy/version'
6
+ require 'abbyy/client'
7
+ require 'abbyy/exceptions'
8
+
9
+ module Abbyy
10
+ extend self
11
+
12
+ attr_accessor :application_id, :password
13
+
14
+ # Abbyy.configure do |config|
15
+ # config.application_id = 'application_id'
16
+ # config.password = 'password'
17
+ # end
18
+ def configure
19
+ yield self
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ module Abbyy
2
+ class API < SimpleDelegator
3
+ def execute(meth, *args, &block)
4
+ send meth, *args, &block
5
+ rescue RestClient::BlockedByWindowsParentalControls => ex
6
+ raise Abbyy::IncorrectParameters.new(parse_error(ex).message)
7
+ rescue RestClient::RequestFailed => ex
8
+ raise Abbyy::ProcessingFailed.new(parse_error(ex).message)
9
+ end
10
+
11
+ # https://www.ocrsdk.com/documentation/api-reference/get-application-info-method/
12
+ def application_info
13
+ RestClient.get("#{url}/getApplicationInfo")
14
+ end
15
+
16
+ # http://ocrsdk.com/documentation/apireference/getTaskStatus/
17
+ def get_task_status(task_id = task[:id])
18
+ RestClient.get("#{url}/getTaskStatus?taskId=#{task_id}")
19
+ end
20
+
21
+ # http://ocrsdk.com/documentation/apireference/processBusinessCard/
22
+ def process_business_card(image_path, options = {})
23
+ RestClient.post("#{url}/processBusinessCard", options.merge(:upload => { :file => File.new(image_path, 'r') }))
24
+ end
25
+
26
+ # http://ocrsdk.com/documentation/apireference/processDocument/
27
+ def process_document(task_id = task[:id], options={})
28
+ RestClient.get("#{url}/processDocument?taskId=#{task_id}", params: options)
29
+ end
30
+
31
+ # http://ocrsdk.com/documentation/apireference/processFields/
32
+ def process_fields(file_path, task_id = task[:id], options = {})
33
+ RestClient.post("#{url}/processFields?taskId=#{task_id}", options.merge(:upload => { :file => File.new(file_path, 'r') }))
34
+ end
35
+
36
+ # https://www.ocrsdk.com/documentation/api-reference/process-image-method/
37
+ def process_image(image_path, options = {})
38
+ RestClient.post("#{url}/processImage", options.merge(:upload => { :file => File.new(image_path, 'r') }))
39
+ end
40
+
41
+ # https://www.ocrsdk.com/documentation/api-reference/process-mrz-method/
42
+ def process_mrz(image_path, options = {})
43
+ RestClient.post("#{url}/processMRZ", options.merge(:upload => { :file => File.new(image_path, 'r') }))
44
+ end
45
+
46
+ # https://www.ocrsdk.com/documentation/api-reference/process-receipt-method/
47
+ def process_receipt(receipt_path, options = {})
48
+ RestClient.post("#{url}/processReceipt", options.merge(:upload => { :file => File.new(receipt_path, 'r') }))
49
+ end
50
+
51
+ # http://ocrsdk.com/documentation/apireference/processTextField/
52
+ def process_text_field(image_path, options = {})
53
+ RestClient.post("#{url}/processTextField", options.merge(:upload => { :file => File.new(image_path, 'r') }))
54
+ end
55
+
56
+ # http://ocrsdk.com/documentation/apireference/submitImage/
57
+ def submit_image(image_path, options = {})
58
+ RestClient.post("#{url}/submitImage", options.merge(:upload => { :file => File.new(image_path, 'r') }))
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,58 @@
1
+ require 'abbyy/api'
2
+ require 'abbyy/xml'
3
+ require 'abbyy/task'
4
+
5
+ module Abbyy
6
+ class Client
7
+ include Abbyy::XML
8
+ include Abbyy::Task
9
+
10
+ API_ENDPOINT = "cloud.ocrsdk.com"
11
+
12
+ attr_reader :task
13
+
14
+ def initialize(application_id = Abbyy.application_id, password = Abbyy.password)
15
+ @appliction_id = application_id
16
+ @password = password
17
+ @task = {}
18
+ end
19
+
20
+ # @return [String] the api url with credentials
21
+ def url
22
+ @url ||= begin
23
+ uri = URI::HTTP.build host: API_ENDPOINT
24
+ uri.user = CGI.escape @appliction_id.to_s
25
+ uri.password = CGI.escape @password.to_s if uri.user
26
+ uri.to_s
27
+ end
28
+ end
29
+
30
+ alias_method :current_task, :task
31
+
32
+ # Retrieve the result of processing task
33
+ #
34
+ # @param [String] the url of the task result
35
+ # @return [String] the xml representation
36
+ def get(url = task[:resultUrl])
37
+ RestClient.get(url)
38
+ end
39
+
40
+ # http://ocrsdk.com/documentation/apireference/listTasks/
41
+ # @return [Array] the list of tasks created
42
+ def list_tasks
43
+ parse_task get("#{url}/listTasks")
44
+ end
45
+
46
+ def method_missing(meth, *args, &block)
47
+ api = API.new self
48
+ self.resource = api.execute meth, *args, &block
49
+ current_task
50
+ end
51
+
52
+ private
53
+
54
+ def resource=(resource)
55
+ @task = parse_task(resource)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ module Abbyy
2
+ IncorrectParameters = Class.new(StandardError)
3
+ ProcessingFailed = Class.new(StandardError)
4
+ end
@@ -0,0 +1,5 @@
1
+ module Abbyy::Inflector
2
+ def camelize(str)
3
+ str[0,1] << str.split('_').map{|w| w.capitalize}.join[1..-1]
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'abbyy/inflector'
2
+
3
+ module Abbyy
4
+ module Task
5
+ include Inflector
6
+
7
+ @@attributes = %w(id status result_url registration_time error description estimated_processing_time credits files_count status_change_time)
8
+
9
+ @@attributes.each do |attribute|
10
+ define_method("current_task_#{attribute}") do
11
+ current_task[camelize(attribute).to_sym]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Abbyy
2
+ VERSION = "0.2.2"
3
+ end
@@ -0,0 +1,43 @@
1
+ module Abbyy
2
+ module XML
3
+ def parse_task(resource)
4
+ xml_data = REXML::Document.new(resource)
5
+ if xml_data.elements["response"].size > 1
6
+ Array.new.tap do |tasks|
7
+ xml_data.elements["response"].each { |response| tasks << build_task(response) }
8
+ end
9
+ else
10
+ build_task(xml_data.elements["response/task"])
11
+ end
12
+ end
13
+
14
+ AbbyyXmlError = Struct.new(:code, :message)
15
+
16
+ def parse_error(resource)
17
+ AbbyyXmlError.new.tap do |error|
18
+ xml_data = REXML::Document.new(resource.http_body)
19
+ error.code = resource.http_code
20
+ error.message = xml_data.elements["error/message"].text
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def build_task(xml_data)
27
+ Hash.new.tap do |task|
28
+ task[:id] = xml_data.attributes["id"]
29
+ task[:status] = xml_data.attributes["status"]
30
+ task[:resultUrl] = xml_data.attributes["resultUrl"]
31
+ task[:resultUrl2] = xml_data.attributes["resultUrl2"] rescue nil
32
+ task[:resultUrl3] = xml_data.attributes["resultUrl3"] rescue nil
33
+ task[:estimatedProcessingTime] = xml_data.attributes["estimatedProcessingTime"]
34
+ task[:description] = xml_data.attributes["description"]
35
+ task[:error] = xml_data.attributes["error"]
36
+ task[:credits] = xml_data.attributes["credits"]
37
+ task[:filesCount] = xml_data.attributes["filesCount"]
38
+ task[:statusChangeTime] = xml_data.attributes["statusChangeTime"]
39
+ task[:registrationTime] = xml_data.attributes["registrationTime"]
40
+ end
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abbyy-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Adrien Rambert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-doc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rest-client
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.0
111
+ description:
112
+ email:
113
+ - contact@silex.pro
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - README.md
124
+ - Rakefile
125
+ - abbyy-ruby.gemspec
126
+ - bin/console
127
+ - bin/setup
128
+ - lib/abbyy.rb
129
+ - lib/abbyy/api.rb
130
+ - lib/abbyy/client.rb
131
+ - lib/abbyy/exceptions.rb
132
+ - lib/abbyy/inflector.rb
133
+ - lib/abbyy/task.rb
134
+ - lib/abbyy/version.rb
135
+ - lib/abbyy/xml.rb
136
+ homepage: https://github.com/silex-pro/abbyy-ruby
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubygems_version: 3.0.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: A Ruby wrapper for the Abbyy Cloud OCR SDK API.
159
+ test_files: []