abbyy 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ace1c914a60d684baeafc6b07b42845c6667c8f
4
+ data.tar.gz: de357e43d7e14a8dab0d9c2f1a3eba3c43fd74c5
5
+ SHA512:
6
+ metadata.gz: db15fdb3e899501ec0b662d2491555e72ea5a36387e4230446c9e608e9a5df37b1ecee845439dadffde7a1bc45d28e02e50a4a11bdccdc94f44f18fce7cb9953
7
+ data.tar.gz: af015b8457a2635ff1229932fdc0b04ebd6be753fce06a82744e9eca507756ab7d28544fa9c504e1d65d3cdf26ca184aff58f238577b80b8e52976a7a42ca999
@@ -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.2.2
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/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Vincent Durand
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,97 @@
1
+ # abbyy
2
+
3
+ Simple Ruby wrapper for Abbyy Cloud OCR SDK
4
+
5
+ [![Build Status](https://img.shields.io/travis/madwork/abbyy.svg)](https://travis-ci.org/madwork/abbyy)
6
+ [![Gem](https://img.shields.io/gem/v/abbyy.svg)](https://rubygems.org/gems/abbyy)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'abbyy'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install abbyy
23
+
24
+ ### Example
25
+
26
+ ```ruby
27
+ client = Abbyy::Client.new(APPLICATION_ID, PASSWORD)
28
+ client.process_business_card "business_card.png", exportFormat: 'xml', imageSource: 'photo'
29
+ client.get_task_status
30
+ client.get
31
+ ```
32
+
33
+ ### Example with Ruby on Rails
34
+
35
+ ```shell
36
+ touch config/initializers/abbyy.rb
37
+ ```
38
+
39
+ ```ruby
40
+ require 'abbyy'
41
+
42
+ Abbyy.configure do |config|
43
+ config.application_id = APPLICATION_ID
44
+ config.password = PASSWORD
45
+ end
46
+ ```
47
+
48
+ Everywhere in your rails application, you can access Abbyy without crendentials:
49
+
50
+ ```ruby
51
+ client = Abbyy::Client.new
52
+ client.process_image "image.png"
53
+ ```
54
+
55
+ ### Example with Resque
56
+
57
+ Create new worker ImageScanner:
58
+
59
+ ```shell
60
+ touch app/workers/image_scanner.rb
61
+ ```
62
+
63
+ ```ruby
64
+ class ImageScanner
65
+ @queue = :scanner
66
+
67
+ def self.perform(attachment_id)
68
+ attachment = Attachment.find_by_id(attachment_id)
69
+ client = Abbyy::Client.new
70
+ client.process_business_card attachment.full_filename, exportFormat: 'xml', imageSource: 'photo'
71
+ while %w(Queued InProgress).include?(client.task[:status])
72
+ sleep(client.task[:estimatedProcessingTime].to_i)
73
+ client.get_task_status
74
+ end
75
+ if client.task[:status] == 'Completed'
76
+ xml_data = REXML::Document.new(client.get)
77
+ ...
78
+ else
79
+ ...
80
+ end
81
+ end
82
+ end
83
+ ```
84
+
85
+ ## Development
86
+
87
+ 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.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`.
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it ( https://github.com/madwork/abbyy/fork )
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
96
+ 4. Push to the branch (`git push origin my-new-feature`)
97
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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"
8
+ spec.version = Abbyy::VERSION
9
+ spec.authors = ["Vincent Durand"]
10
+ spec.email = ["vincent.durand@madwork.org"]
11
+ spec.summary = %q{A Ruby wrapper for the Abbyy Cloud OCR SDK API.}
12
+ spec.homepage = "https://github.com/madwork/abbyy"
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.9"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ spec.add_development_dependency "guard-rspec", "~> 4.0"
23
+ spec.add_development_dependency "pry", "~> 0"
24
+ spec.add_development_dependency "pry-doc", "~> 0"
25
+
26
+ spec.add_dependency "rest-client", ">= 1.8.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,46 @@
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
+ # http://ocrsdk.com/documentation/apireference/processImage/
12
+ def process_image(image_path, options = {})
13
+ RestClient.post("#{url}/processImage", options.merge(:upload => { :file => File.new(image_path, 'r') }))
14
+ end
15
+
16
+ # http://ocrsdk.com/documentation/apireference/processBusinessCard/
17
+ def process_business_card(image_path, options = {})
18
+ RestClient.post("#{url}/processBusinessCard", options.merge(:upload => { :file => File.new(image_path, 'r') }))
19
+ end
20
+
21
+ # http://ocrsdk.com/documentation/apireference/submitImage/
22
+ def submit_image(image_path, options = {})
23
+ RestClient.post("#{url}/submitImage", options.merge(:upload => { :file => File.new(image_path, 'r') }))
24
+ end
25
+
26
+ # http://ocrsdk.com/documentation/apireference/processTextField/
27
+ def process_text_field(image_path, options = {})
28
+ RestClient.post("#{url}/processTextField", options.merge(:upload => { :file => File.new(image_path, 'r') }))
29
+ end
30
+
31
+ # http://ocrsdk.com/documentation/apireference/getTaskStatus/
32
+ def get_task_status(task_id = task[:id])
33
+ RestClient.get("#{url}/getTaskStatus?taskId=#{task_id}")
34
+ end
35
+
36
+ # http://ocrsdk.com/documentation/apireference/processDocument/
37
+ def process_document(task_id = task[:id])
38
+ RestClient.get("#{url}/processDocument?taskId=#{task_id}")
39
+ end
40
+
41
+ # http://ocrsdk.com/documentation/apireference/processFields/
42
+ def process_fields(file_path, task_id = task[:id], options = {})
43
+ RestClient.post("#{url}/processFields?taskId=#{task_id}", options.merge(:upload => { :file => File.new(file_path, 'r') }))
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,47 @@
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
+ attr_reader :task, :url
11
+
12
+ def initialize(application_id = Abbyy.application_id, password = Abbyy.password)
13
+ @appliction_id = application_id
14
+ @password = password
15
+ @url = "http://#{CGI.escape(@appliction_id)}:#{CGI.escape(@password)}@cloud.ocrsdk.com"
16
+ @task = {}
17
+ end
18
+
19
+ alias_method :current_task, :task
20
+
21
+ # Retrieve the result of processing task
22
+ #
23
+ # @param [String] the url of the task result
24
+ # @return [String] the xml representation
25
+ def get(url = task[:resultUrl])
26
+ RestClient.get(url)
27
+ end
28
+
29
+ # http://ocrsdk.com/documentation/apireference/listTasks/
30
+ # @return [Array] the list of tasks created
31
+ def list_tasks
32
+ parse_task get("#{url}/listTasks")
33
+ end
34
+
35
+ def method_missing(meth, *args, &block)
36
+ api = API.new self
37
+ self.resource = api.execute meth, *args, &block
38
+ current_task
39
+ end
40
+
41
+ private
42
+
43
+ def resource=(resource)
44
+ @task = parse_task(resource)
45
+ end
46
+ end
47
+ 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,13 @@
1
+ require 'abbyy/inflector'
2
+
3
+ module Abbyy
4
+ module Task
5
+ @@attributes = %w(id status result_url registration_time error description estimated_processing_time credits files_count status_change_time)
6
+
7
+ @@attributes.each do |attribute|
8
+ define_method("current_task_#{attribute}") do
9
+ current_task[camelize(attribute).to_sym]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Abbyy
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,41 @@
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[:estimatedProcessingTime] = xml_data.attributes["estimatedProcessingTime"]
32
+ task[:description] = xml_data.attributes["description"]
33
+ task[:error] = xml_data.attributes["error"]
34
+ task[:credits] = xml_data.attributes["credits"]
35
+ task[:filesCount] = xml_data.attributes["filesCount"]
36
+ task[:statusChangeTime] = xml_data.attributes["statusChangeTime"]
37
+ task[:registrationTime] = xml_data.attributes["registrationTime"]
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abbyy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Durand
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-10 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
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.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
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: 1.8.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.8.0
111
+ description:
112
+ email:
113
+ - vincent.durand@madwork.org
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - abbyy.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/abbyy.rb
130
+ - lib/abbyy/api.rb
131
+ - lib/abbyy/client.rb
132
+ - lib/abbyy/exceptions.rb
133
+ - lib/abbyy/inflector.rb
134
+ - lib/abbyy/task.rb
135
+ - lib/abbyy/version.rb
136
+ - lib/abbyy/xml.rb
137
+ homepage: https://github.com/madwork/abbyy
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: A Ruby wrapper for the Abbyy Cloud OCR SDK API.
161
+ test_files: []
162
+ has_rdoc: