abbyy-cloud 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +81 -0
- data/.travis.yml +17 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +5 -0
- data/abbyy-cloud.gemspec +27 -0
- data/lib/abbyy/cloud.rb +40 -0
- data/lib/abbyy/cloud/argument_error.rb +14 -0
- data/lib/abbyy/cloud/connection.rb +46 -0
- data/lib/abbyy/cloud/operation.rb +76 -0
- data/lib/abbyy/cloud/operation/translate.rb +21 -0
- data/lib/abbyy/cloud/response_error.rb +26 -0
- data/lib/abbyy/cloud/struct.rb +10 -0
- data/lib/abbyy/cloud/type_error.rb +14 -0
- data/lib/abbyy/cloud/types.rb +15 -0
- data/spec/abbyy/cloud/connection_spec.rb +60 -0
- data/spec/abbyy/cloud/response_error_spec.rb +20 -0
- data/spec/abbyy/cloud_spec.rb +58 -0
- data/spec/feature/abbyy/cloud_translate_spec.rb +130 -0
- data/spec/spec_helper.rb +25 -0
- metadata +199 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cababb6c66e2c17fe4b3b8536f61ca2273695712
|
4
|
+
data.tar.gz: aa4eb72f7805fd507c529ffd8fb79d2ad8f3f8dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 67a49f25576bbece4ee85226116275642bc5e3b3a1bcf86e4bf0182ee318de16687ef206f43edb59048ceb71151f28138be10d4765f2d8bf600ef7a4b1ecb9da
|
7
|
+
data.tar.gz: 394413929ad8198cde627b12056c8119aefafa2a358e5f93dd59024604eb9d2bb3203a8a524b69b67ffe898d77ece5e917371bf5c3d0e12c72570010806d1eb5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Include:
|
5
|
+
- ./Gemfile
|
6
|
+
- ./config.ru
|
7
|
+
Exclude:
|
8
|
+
- db/**/*
|
9
|
+
- config/**/*
|
10
|
+
- script/**/*
|
11
|
+
- bin/*
|
12
|
+
- vendor/**/*
|
13
|
+
TargetRubyVersion: 2.3
|
14
|
+
|
15
|
+
RSpec/NamedSubject:
|
16
|
+
Exclude:
|
17
|
+
- spec/**/*_spec.rb
|
18
|
+
|
19
|
+
Documentation:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Lint/HandleExceptions:
|
23
|
+
Exclude:
|
24
|
+
- spec/**/*_spec.rb
|
25
|
+
|
26
|
+
Lint/RescueException:
|
27
|
+
Exclude:
|
28
|
+
- spec/**/*_spec.rb
|
29
|
+
|
30
|
+
Metrics/LineLength:
|
31
|
+
Max: 80
|
32
|
+
Exclude:
|
33
|
+
- spec/**/*_spec.rb
|
34
|
+
|
35
|
+
Style/AccessorMethodName:
|
36
|
+
Exclude:
|
37
|
+
- spec/**/*_spec.rb
|
38
|
+
|
39
|
+
Style/Alias:
|
40
|
+
Enabled: false # `alias_method` and `alias` have diferent behaviour
|
41
|
+
|
42
|
+
Style/FileName:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/Lambda:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/ClassAndModuleChildren:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/NumericLiterals:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Style/FrozenStringLiteralComment:
|
55
|
+
Enabled: false # not to be used before migration to Ruby 3+
|
56
|
+
|
57
|
+
Style/RaiseArgs:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/Semicolon:
|
61
|
+
Exclude:
|
62
|
+
- spec/**/*_spec.rb
|
63
|
+
|
64
|
+
Style/SingleLineMethods:
|
65
|
+
Exclude:
|
66
|
+
- spec/**/*_spec.rb
|
67
|
+
|
68
|
+
Style/SpecialGlobalVars:
|
69
|
+
Exclude:
|
70
|
+
- Gemfile
|
71
|
+
- abbyy-cloud.gemspec
|
72
|
+
|
73
|
+
Style/StringLiterals:
|
74
|
+
EnforcedStyle: double_quotes
|
75
|
+
|
76
|
+
Style/StringLiteralsInInterpolation:
|
77
|
+
EnforcedStyle: double_quotes
|
78
|
+
|
79
|
+
Style/TrivialAccessors:
|
80
|
+
Exclude:
|
81
|
+
- spec/**/*_spec.rb
|
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
cache: bundler
|
5
|
+
script:
|
6
|
+
- bundle exec rspec
|
7
|
+
- bundle exec rubocop
|
8
|
+
rvm:
|
9
|
+
- '2.3.0'
|
10
|
+
- ruby-head
|
11
|
+
- jruby-9
|
12
|
+
- jruby-head
|
13
|
+
before_install: gem install bundler -v 1.12.5
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: ruby-head
|
17
|
+
- rvm: jruby-head
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Andrew Kozin (andrew.kozin@gmail.com)
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# [WIP] ABBYY::Cloud
|
2
|
+
|
3
|
+
[][gem]
|
4
|
+
[][travis]
|
5
|
+
[][gemnasium]
|
6
|
+
[][codeclimate]
|
7
|
+
[][inch]
|
8
|
+
|
9
|
+
JSON HTTP client to the [ABBYY Cloud API][abbyy-api].
|
10
|
+
|
11
|
+
## Setup
|
12
|
+
|
13
|
+
The library is available as a gem `abbyy-cloud`.
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Initialize the client with a corresponding credentials:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
CLIENT = ABBYY::Cloud.new(id: "foo", token: "bar")
|
21
|
+
```
|
22
|
+
|
23
|
+
The only supported API version is `0`. By default the translation engine is set to "Sandbox".
|
24
|
+
|
25
|
+
You can set these options explicitly:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
CLIENT = ABBYY::Cloud.new id: "foo",
|
29
|
+
token: "bar",
|
30
|
+
engine: "Sandbox", # default engine for translations
|
31
|
+
version: 0 # the only supported version
|
32
|
+
```
|
33
|
+
|
34
|
+
### Instant Translation
|
35
|
+
|
36
|
+
See [the specification](https://api.abbyy.cloud/swagger/ui/index#!/Order/Order_Translate).
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
result = CLIENT.translate("To be or not to be", from: :en, to: :ru)
|
40
|
+
|
41
|
+
result.class # => ABBYY::Cloud::Translation
|
42
|
+
result.translation # => "Быть или не быть"
|
43
|
+
result.order_id # => 2832934
|
44
|
+
```
|
45
|
+
|
46
|
+
You can specify an engine (different from default):
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
CLIENT.translate("To be or not to be", from: :en, to: :ru, engine: "Bing")
|
50
|
+
```
|
51
|
+
|
52
|
+
The method raises an exception in case the cloud responded with error.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
error = \
|
56
|
+
begin
|
57
|
+
CLIENT.translate("To be or not to be")
|
58
|
+
rescue ABBYY::Cloud::Error => error
|
59
|
+
error
|
60
|
+
end
|
61
|
+
|
62
|
+
error.class # => ABBYY::Cloud::ResponceError
|
63
|
+
error.status # => 400
|
64
|
+
error.data.to_h
|
65
|
+
# => {
|
66
|
+
# model_state: {},
|
67
|
+
# requiest_id: "string",
|
68
|
+
# error: "error message",
|
69
|
+
# error_description: "some details"
|
70
|
+
# }
|
71
|
+
|
72
|
+
error.data.error # => "error message"
|
73
|
+
# etc.
|
74
|
+
```
|
75
|
+
|
76
|
+
## Compatibility
|
77
|
+
|
78
|
+
[WIP] Compatible to [ABBYY Cloud API v.0][abbyy-api].
|
79
|
+
|
80
|
+
Tested under [rubies compatible to MRI 2.3+][rubies] using [RSpec][rspec] 3.0+.
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
85
|
+
|
86
|
+
[abbyy-api]: https://api.abbyy.cloud/swagger/ui/index
|
87
|
+
[codeclimate]: https://codeclimate.com/github/nepalez/abbyy-cloud
|
88
|
+
[gem]: https://rubygems.org/gems/abbyy-cloud
|
89
|
+
[gemnasium]: https://gemnasium.com/nepalez/abbyy-cloud
|
90
|
+
[github]: https://github.com/nepalez/abbyy-cloud
|
91
|
+
[inch]: https://inch-ci.org/github/nepalez/abbyy-cloud
|
92
|
+
[rspec]: http://rspec.info/
|
93
|
+
[rubies]: .travis.yml
|
94
|
+
[travis]: https://travis-ci.org/nepalez/abbyy-cloud
|
data/Rakefile
ADDED
data/abbyy-cloud.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "abbyy-cloud"
|
3
|
+
gem.version = "0.0.1"
|
4
|
+
gem.authors = ["Andrew Kozin"]
|
5
|
+
gem.email = ["andrew.kozin@gmail.com"]
|
6
|
+
gem.summary = "HTTP client to ABBYY Cloud API"
|
7
|
+
gem.homepage = "https://github.com/nepalez/abbyy-cloud"
|
8
|
+
gem.license = "MIT"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
11
|
+
gem.test_files = gem.files.grep(/^spec/)
|
12
|
+
gem.extra_rdoc_files = Dir["README.md", "LICENSE", "CHANGELOG.md"]
|
13
|
+
gem.require_paths = %w(lib)
|
14
|
+
|
15
|
+
gem.required_ruby_version = ">= 2.3.0"
|
16
|
+
|
17
|
+
gem.add_runtime_dependency "dry-initializer", "~> 0.4.0"
|
18
|
+
gem.add_runtime_dependency "dry-struct", "~> 0.0.1"
|
19
|
+
gem.add_runtime_dependency "dry-types", "~> 0.8.0"
|
20
|
+
|
21
|
+
gem.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
gem.add_development_dependency "rake", "~> 10.0"
|
23
|
+
gem.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
gem.add_development_dependency "rspec-its", "~> 1.2"
|
25
|
+
gem.add_development_dependency "rubocop-rspec", "~> 1.6"
|
26
|
+
gem.add_development_dependency "webmock", "~> 2.0"
|
27
|
+
end
|
data/lib/abbyy/cloud.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "dry-initializer"
|
2
|
+
require "dry-struct"
|
3
|
+
require "dry-types"
|
4
|
+
require "json"
|
5
|
+
require "net/http"
|
6
|
+
require "net/https"
|
7
|
+
|
8
|
+
module ABBYY
|
9
|
+
class Cloud
|
10
|
+
require_relative "cloud/struct"
|
11
|
+
require_relative "cloud/types"
|
12
|
+
require_relative "cloud/response_error"
|
13
|
+
require_relative "cloud/argument_error"
|
14
|
+
require_relative "cloud/type_error"
|
15
|
+
require_relative "cloud/connection"
|
16
|
+
require_relative "cloud/operation"
|
17
|
+
require_relative "cloud/operation/translate"
|
18
|
+
|
19
|
+
include Dry::Initializer.define -> do
|
20
|
+
option :id, type: Types::AuthId
|
21
|
+
option :token, type: Types::AuthToken
|
22
|
+
option :version, type: Types::Version, default: proc { 0 }
|
23
|
+
option :engine, type: Types::Engine, default: proc { "Sandbox" }
|
24
|
+
end
|
25
|
+
|
26
|
+
def translate(text, from:, to:, **opts)
|
27
|
+
Operation::Translate.new(connection).call source_text: text,
|
28
|
+
source_language: from,
|
29
|
+
target_language: to,
|
30
|
+
engine: engine,
|
31
|
+
**opts
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def connection
|
37
|
+
@connection ||= Connection.new(version: version, id: id, token: token)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# The exception to be risen when arguments of the operation
|
2
|
+
# mismatches ABBYY Cloud API spec
|
3
|
+
class ABBYY::Cloud
|
4
|
+
class ArgumentError < ::ArgumentError
|
5
|
+
def initialize(link, arguments, details)
|
6
|
+
super <<-MESSAGE.gsub(/ +\|/, "")
|
7
|
+
|The arguments of operation mismatch ABBYY Cloud API specification
|
8
|
+
| arguments: #{arguments}
|
9
|
+
| specification: #{link}
|
10
|
+
| details: #{details}
|
11
|
+
MESSAGE
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Underlying Net::HTTP-based connection responds for sending requests
|
2
|
+
# and handling responses depending on their statuses.
|
3
|
+
#
|
4
|
+
# It validates neither request's, no response's contents.
|
5
|
+
# Those are provided by [ABBYY::Cloud::Operation] and its subclasses.
|
6
|
+
#
|
7
|
+
class ABBYY::Cloud
|
8
|
+
class Connection
|
9
|
+
include Dry::Initializer.define -> do
|
10
|
+
option :version
|
11
|
+
option :id
|
12
|
+
option :token
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(http_method, path, body: nil, headers: nil)
|
16
|
+
uri = root.merge(path).tap { |item| item.scheme = "https" }
|
17
|
+
req = prepare_request(http_method, uri, body.to_h, headers.to_h)
|
18
|
+
|
19
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
20
|
+
handle_response http.request(req)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def root
|
27
|
+
@root ||= URI("https://api.abbyy.cloud").merge("v#{version}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def prepare_request(http_method, uri, body, headers)
|
31
|
+
Net::HTTP.const_get(http_method.capitalize).new(uri).tap do |req|
|
32
|
+
req.body = body
|
33
|
+
req.basic_auth id, token
|
34
|
+
req["accept-charset"] = "utf-8"
|
35
|
+
req["accept"] = "application/json"
|
36
|
+
req["content-type"] = "application/json"
|
37
|
+
headers.each { |key, value| req[key.to_s] = value }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def handle_response(response)
|
42
|
+
raise ResponseError.new(response) unless response.is_a? Net::HTTPSuccess
|
43
|
+
JSON.parse(response.body)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# Base class for specific operations
|
2
|
+
# It validates request and response using corresponding structs
|
3
|
+
#
|
4
|
+
# @abstract
|
5
|
+
#
|
6
|
+
class ABBYY::Cloud
|
7
|
+
class Operation
|
8
|
+
# Contains helper methods to specify concrete operations
|
9
|
+
class << self
|
10
|
+
include Forwardable
|
11
|
+
|
12
|
+
def http_method(value = nil)
|
13
|
+
@http_method = value.to_s.capitalize if value
|
14
|
+
@http_method || "Post"
|
15
|
+
end
|
16
|
+
|
17
|
+
def path(value = nil)
|
18
|
+
value ? @path = value : @path
|
19
|
+
end
|
20
|
+
|
21
|
+
def link(value = nil)
|
22
|
+
value ? @link = value : @link
|
23
|
+
end
|
24
|
+
|
25
|
+
def request_body(struct = nil, &block)
|
26
|
+
provide_struct :@request_body, struct, &block
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_body(struct = nil, &block)
|
30
|
+
provide_struct :@response_body, struct, &block
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def provide_struct(variable, struct, &block)
|
36
|
+
if block || instance_variable_get(variable).nil?
|
37
|
+
struct ||= Class.new(Struct).tap { |item| item.instance_eval(&block) }
|
38
|
+
instance_variable_set(variable, struct)
|
39
|
+
else
|
40
|
+
instance_variable_get(variable)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def_delegators :"self.class",
|
46
|
+
:link,
|
47
|
+
:http_method,
|
48
|
+
:path,
|
49
|
+
:request_body,
|
50
|
+
:response_body
|
51
|
+
|
52
|
+
include Dry::Initializer.define -> do
|
53
|
+
param :connection
|
54
|
+
end
|
55
|
+
|
56
|
+
def call(**data)
|
57
|
+
body = prepare_request_body(data)
|
58
|
+
res = connection.call(http_method, path, body: body)
|
59
|
+
handle_response_body(res)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def prepare_request_body(data)
|
65
|
+
request_body[data]
|
66
|
+
rescue => error
|
67
|
+
raise ArgumentError.new(link, data, error.message)
|
68
|
+
end
|
69
|
+
|
70
|
+
def handle_response_body(data)
|
71
|
+
response_body[data].to_h
|
72
|
+
rescue => error
|
73
|
+
raise TypeError.new(link, data, error.message)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class ABBYY::Cloud
|
2
|
+
class Operation
|
3
|
+
class Translate < Operation
|
4
|
+
link "https://api.abbyy.cloud/swagger/ui/index#!/Order/Order_Translate"
|
5
|
+
path "order/mt/sync"
|
6
|
+
http_method "post"
|
7
|
+
|
8
|
+
request_body do
|
9
|
+
attribute :engine, Types::Engine
|
10
|
+
attribute :source_language, Types::Language
|
11
|
+
attribute :target_language, Types::Language
|
12
|
+
attribute :source_text, Types::Strict::String
|
13
|
+
end
|
14
|
+
|
15
|
+
response_body do
|
16
|
+
attribute :id, Types::OrderId
|
17
|
+
attribute :translation, Types::Strict::String
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# The exception to be risen when ABBYY Cloud API responded with error status.
|
2
|
+
# It takes a raw Net::HTTPResponse and extracts all the data returned by API.
|
3
|
+
class ABBYY::Cloud
|
4
|
+
class ResponseError < StandardError
|
5
|
+
class Data < Struct
|
6
|
+
attribute :request_id, Types::Coercible::String.optional
|
7
|
+
attribute :error, Types::Coercible::String.optional
|
8
|
+
attribute :error_description, Types::Coercible::String.optional
|
9
|
+
attribute :model_state, Types::Hash.optional
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :status, :data
|
13
|
+
|
14
|
+
def initialize(response)
|
15
|
+
@data = Data[JSON.parse(response.body)]
|
16
|
+
@status = response.code.to_i
|
17
|
+
|
18
|
+
super <<-MESSAGE.gsub(/ +\|/, "")
|
19
|
+
|ABBYY Cloud API responded to the request {data.request_id} with a status #{status}
|
20
|
+
| error: #{data.error}
|
21
|
+
| description: #{data.error_description}
|
22
|
+
| model_state: #{data.model_state}
|
23
|
+
MESSAGE
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class ABBYY::Cloud::Struct < Dry::Struct
|
2
|
+
def self.[](data)
|
3
|
+
default = schema.keys.each_with_object({}) { |key, hash| hash[key] = nil }
|
4
|
+
actual = data.to_hash.each_with_object({}) do |(key, val), hash|
|
5
|
+
hash[key.to_sym] = val
|
6
|
+
end
|
7
|
+
|
8
|
+
new default.merge(actual)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# The exception to be risen when ABBYY Cloud API returned data that
|
2
|
+
# mismatches the specification
|
3
|
+
class ABBYY::Cloud
|
4
|
+
class TypeError < ::TypeError
|
5
|
+
def initialize(link, data, details)
|
6
|
+
super <<-MESSAGE.gsub(/ +\|/, "")
|
7
|
+
|The data returned by ABBYY Cloud API mismatch its specification
|
8
|
+
| returned data: #{data}
|
9
|
+
| specification: #{link}
|
10
|
+
| details: #{details}
|
11
|
+
MESSAGE
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ABBYY::Cloud::Types
|
2
|
+
include Dry::Types.module
|
3
|
+
|
4
|
+
ENGINES = %w(Sandbox Bing Google Systran).freeze
|
5
|
+
VERSIONS = [0].freeze
|
6
|
+
LANGUAGE = /\A[a-z]{2}(_[A-Z]{2})?\z/
|
7
|
+
|
8
|
+
# Gem-specific primitive types
|
9
|
+
AuthId = Strict::String
|
10
|
+
AuthToken = Strict::String
|
11
|
+
Engine = Coercible::String.constrained(included_in: ENGINES)
|
12
|
+
Language = Strict::String.constrained(format: LANGUAGE)
|
13
|
+
OrderId = Strict::String
|
14
|
+
Version = Coercible::Int.constrained(included_in: VERSIONS)
|
15
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
RSpec.describe ABBYY::Cloud::Connection do
|
2
|
+
let(:connection) { described_class.new version: 0, id: "baz", token: "qux" }
|
3
|
+
|
4
|
+
subject do
|
5
|
+
connection.call :post,
|
6
|
+
"/foo/bar",
|
7
|
+
body: { question: "The ultimate question" },
|
8
|
+
headers: { "X-Receiver": "Universe" }
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:expected_request) do
|
12
|
+
a_request(:post, "https://api.abbyy.cloud/foo/bar") do |req|
|
13
|
+
expect(req.body).to eq(question: "The ultimate question")
|
14
|
+
expect(req.headers).to include "Accept" => "application/json",
|
15
|
+
"Accept-Charset" => "utf-8",
|
16
|
+
"Authorization" => "Basic YmF6OnF1eA==",
|
17
|
+
"Content-Type" => "application/json",
|
18
|
+
"Id" => "baz",
|
19
|
+
"Token" => "qux",
|
20
|
+
"X-Receiver" => "Universe"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when a server responded with success:" do
|
25
|
+
before do
|
26
|
+
stub_request(:post, "https://api.abbyy.cloud/foo/bar")
|
27
|
+
.with(basic_auth: %w(baz qux))
|
28
|
+
.to_return status: 200,
|
29
|
+
headers: { "Content-Type" => "application/json" },
|
30
|
+
body: JSON(answer: 42)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sends the request" do
|
34
|
+
subject
|
35
|
+
expect(expected_request).to have_been_made
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns the response body" do
|
39
|
+
expect(subject).to eq("answer" => 42)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when a server responded with error:" do
|
44
|
+
before do
|
45
|
+
stub_request(:post, "https://api.abbyy.cloud/foo/bar")
|
46
|
+
.with(basic_auth: %w(baz qux))
|
47
|
+
.to_return status: 400,
|
48
|
+
headers: { "Content-Type" => "application/json" },
|
49
|
+
body: JSON(error: "Wrong question")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "fails" do
|
53
|
+
expect { subject }.to raise_error do |error|
|
54
|
+
expect(error).to be_kind_of ABBYY::Cloud::ResponseError
|
55
|
+
expect(error.status).to eq 400
|
56
|
+
expect(error.data.to_h).to include(error: "Wrong question")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
RSpec.describe ABBYY::Cloud::ResponseError do
|
2
|
+
let(:data) do
|
3
|
+
{
|
4
|
+
request_id: "foobar",
|
5
|
+
error: "Unprocessable entity",
|
6
|
+
error_description: "Wrong language",
|
7
|
+
model_state: { "id" => 0, "language" => "en_FR" }
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:response) do
|
12
|
+
instance_double "Net::HTTPResponse", code: "400", body: JSON(data)
|
13
|
+
end
|
14
|
+
|
15
|
+
subject { described_class.new response }
|
16
|
+
|
17
|
+
it { is_expected.to be_kind_of StandardError }
|
18
|
+
its(:status) { is_expected.to eq 400 }
|
19
|
+
its("data.to_h") { is_expected.to eq data }
|
20
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
RSpec.describe ABBYY::Cloud do
|
2
|
+
let(:options) { { id: "foo", token: "bar" } }
|
3
|
+
let(:cloud) { described_class.new options }
|
4
|
+
|
5
|
+
describe ".new" do
|
6
|
+
subject { cloud }
|
7
|
+
|
8
|
+
its(:version) { is_expected.to eq 0 }
|
9
|
+
its(:engine) { is_expected.to eq "Sandbox" }
|
10
|
+
its(:id) { is_expected.to eq "foo" }
|
11
|
+
its(:token) { is_expected.to eq "bar" }
|
12
|
+
|
13
|
+
context "with custom engine" do
|
14
|
+
before { options[:engine] = "Google" }
|
15
|
+
its(:engine) { is_expected.to eq "Google" }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "with wrong engine" do
|
19
|
+
before { options[:engine] = "Wrong" }
|
20
|
+
|
21
|
+
it "fails" do
|
22
|
+
expect { subject }.to raise_error(StandardError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "without id" do
|
27
|
+
before { options.delete :id }
|
28
|
+
|
29
|
+
it "fails" do
|
30
|
+
expect { subject }.to raise_error(StandardError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "without token" do
|
35
|
+
before { options.delete :token }
|
36
|
+
|
37
|
+
it "fails" do
|
38
|
+
expect { subject }.to raise_error(StandardError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with empty id" do
|
43
|
+
before { options[:id] = nil }
|
44
|
+
|
45
|
+
it "fails" do
|
46
|
+
expect { subject }.to raise_error(StandardError)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with empty token" do
|
51
|
+
before { options[:token] = nil }
|
52
|
+
|
53
|
+
it "fails" do
|
54
|
+
expect { subject }.to raise_error(StandardError)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
RSpec.describe ABBYY::Cloud, "#translate" do
|
2
|
+
let(:client) { described_class.new settings }
|
3
|
+
let(:settings) { { id: "foo", token: "bar", engine: default_engine } }
|
4
|
+
let(:default_engine) { "Google" }
|
5
|
+
let(:custom_engine) { "Bing" }
|
6
|
+
let(:source_language) { "ru" }
|
7
|
+
let(:target_language) { "en_EN" }
|
8
|
+
let(:source_text) { "Бить или не бить" }
|
9
|
+
let(:path) { "https://api.abbyy.cloud/order/mt/sync" }
|
10
|
+
let(:response_status) { 200 }
|
11
|
+
let(:response_model) { { id: "42", translation: "To beat or not to beat" } }
|
12
|
+
let(:params) do
|
13
|
+
{ from: source_language, to: target_language, engine: custom_engine }
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
stub_request(:post, path)
|
18
|
+
.with(basic_auth: %w(foo bar))
|
19
|
+
.to_return status: response_status,
|
20
|
+
headers: { "Content-Type" => "application/json" },
|
21
|
+
body: JSON(response_model)
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { client.translate source_text, params }
|
25
|
+
|
26
|
+
it "sends a request to ABBYY Cloud API and returns translation as a hash" do
|
27
|
+
expect(subject).to eq response_model
|
28
|
+
expect(a_request(:post, path)).to have_been_made
|
29
|
+
end
|
30
|
+
|
31
|
+
context "without custom engine:" do
|
32
|
+
before { params.delete :engine }
|
33
|
+
|
34
|
+
let(:expected_request) do
|
35
|
+
a_request(:post, path).with do |req|
|
36
|
+
expect(req.body).to include engine: "Google"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "uses default engine in the request" do
|
41
|
+
subject
|
42
|
+
expect(expected_request).to have_been_made
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when no engine is set:" do
|
47
|
+
before { settings.delete :engine }
|
48
|
+
before { params.delete :engine }
|
49
|
+
|
50
|
+
let(:expected_request) do
|
51
|
+
a_request(:post, path).with do |req|
|
52
|
+
expect(req.body).to include engine: "Sandbox"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "uses Sandbox engine in the request" do
|
57
|
+
subject
|
58
|
+
expect(expected_request).to have_been_made
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "without text to translate:" do
|
63
|
+
let(:source_text) { nil }
|
64
|
+
|
65
|
+
it "raises ArgumentError before sending a request" do
|
66
|
+
expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
|
67
|
+
expect(a_request(:any, //)).not_to have_been_made
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "with invalid source language:" do
|
72
|
+
let(:source_language) { "ru_12" }
|
73
|
+
|
74
|
+
it "raises ArgumentError before sending a request" do
|
75
|
+
expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
|
76
|
+
expect(a_request(:any, //)).not_to have_been_made
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "without source language:" do
|
81
|
+
before { params.delete :from }
|
82
|
+
|
83
|
+
it "raises ArgumentError before sending a request" do
|
84
|
+
expect { subject }.to raise_error(::ArgumentError)
|
85
|
+
expect(a_request(:any, //)).not_to have_been_made
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "with invalid target language:" do
|
90
|
+
let(:target_language) { "neoru" }
|
91
|
+
|
92
|
+
it "raises ArgumentError before sending a request" do
|
93
|
+
expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
|
94
|
+
expect(a_request(:any, //)).not_to have_been_made
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "without target language:" do
|
99
|
+
before { params.delete :to }
|
100
|
+
|
101
|
+
it "raises ArgumentError before sending a request" do
|
102
|
+
expect { subject }.to raise_error(::ArgumentError)
|
103
|
+
expect(a_request(:any, //)).not_to have_been_made
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when API responded with error:" do
|
108
|
+
let(:response_status) { 400 }
|
109
|
+
|
110
|
+
it "raises ResponseError" do
|
111
|
+
expect { subject }.to raise_error(ABBYY::Cloud::ResponseError)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when API returned data without an id:" do
|
116
|
+
let(:response_model) { { translation: "To beat or not to beat" } }
|
117
|
+
|
118
|
+
it "raises TypeError" do
|
119
|
+
expect { subject }.to raise_error(ABBYY::Cloud::TypeError)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "when API returned data without a translation:" do
|
124
|
+
let(:response_model) { { id: 1 } }
|
125
|
+
|
126
|
+
it "raises TypeError" do
|
127
|
+
expect { subject }.to raise_error(ABBYY::Cloud::TypeError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "rspec"
|
2
|
+
require "rspec/its"
|
3
|
+
require "webmock/rspec"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "pry"
|
7
|
+
rescue LoadError
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
Bundler.require
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.expect_with :rspec do |expectations|
|
15
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
16
|
+
end
|
17
|
+
|
18
|
+
config.filter_run :focus
|
19
|
+
config.run_all_when_everything_filtered = true
|
20
|
+
config.disable_monkey_patching!
|
21
|
+
config.profile_examples = 10
|
22
|
+
config.order = :random
|
23
|
+
|
24
|
+
Kernel.srand config.seed
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abbyy-cloud
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kozin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-initializer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-struct
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.0.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-types
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.12'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-its
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.2'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.2'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.6'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.6'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '2.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '2.0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- andrew.kozin@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files:
|
145
|
+
- README.md
|
146
|
+
files:
|
147
|
+
- ".gitignore"
|
148
|
+
- ".rspec"
|
149
|
+
- ".rubocop.yml"
|
150
|
+
- ".travis.yml"
|
151
|
+
- Gemfile
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- abbyy-cloud.gemspec
|
156
|
+
- lib/abbyy/cloud.rb
|
157
|
+
- lib/abbyy/cloud/argument_error.rb
|
158
|
+
- lib/abbyy/cloud/connection.rb
|
159
|
+
- lib/abbyy/cloud/operation.rb
|
160
|
+
- lib/abbyy/cloud/operation/translate.rb
|
161
|
+
- lib/abbyy/cloud/response_error.rb
|
162
|
+
- lib/abbyy/cloud/struct.rb
|
163
|
+
- lib/abbyy/cloud/type_error.rb
|
164
|
+
- lib/abbyy/cloud/types.rb
|
165
|
+
- spec/abbyy/cloud/connection_spec.rb
|
166
|
+
- spec/abbyy/cloud/response_error_spec.rb
|
167
|
+
- spec/abbyy/cloud_spec.rb
|
168
|
+
- spec/feature/abbyy/cloud_translate_spec.rb
|
169
|
+
- spec/spec_helper.rb
|
170
|
+
homepage: https://github.com/nepalez/abbyy-cloud
|
171
|
+
licenses:
|
172
|
+
- MIT
|
173
|
+
metadata: {}
|
174
|
+
post_install_message:
|
175
|
+
rdoc_options: []
|
176
|
+
require_paths:
|
177
|
+
- lib
|
178
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: 2.3.0
|
183
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
requirements: []
|
189
|
+
rubyforge_project:
|
190
|
+
rubygems_version: 2.6.4
|
191
|
+
signing_key:
|
192
|
+
specification_version: 4
|
193
|
+
summary: HTTP client to ABBYY Cloud API
|
194
|
+
test_files:
|
195
|
+
- spec/abbyy/cloud/connection_spec.rb
|
196
|
+
- spec/abbyy/cloud/response_error_spec.rb
|
197
|
+
- spec/abbyy/cloud_spec.rb
|
198
|
+
- spec/feature/abbyy/cloud_translate_spec.rb
|
199
|
+
- spec/spec_helper.rb
|