[WIP] ABBYY::Cloud
JSON HTTP client to the ABBYY Cloud API.
Setup
The library is available as a gem abbyy-cloud
.
Usage
Initialize the client with a corresponding credentials:
require "abbyy/cloud"
CLIENT = ABBYY::Cloud.new(id: "foo", token: "bar")
You can set these options explicitly:
CLIENT = ABBYY::Cloud.new id: "foo",
token: "bar",
engine: "Sandbox" # default engine for translations
And then use the client to provide requests:
res = CLIENT.mt.translate("To be or not to be", from: :en, to: :ru)
res.translation # => "Быть или не быть"
res = CLIENT.mt.translate_segments ["To be or not to be", "That is the question"], from: "en", to: "ru"
res.map(&:text) # => ["Быть или не быть", "Это вопрос"]
Namespaces and Operations
Machine Translations
The namespace mt
contains (synchronous) operations with machine translation.
engines
result = CLIENT.mt.engines
# => [#<ABBYY::Cloud::Models::Engine @name="Sandbox">]
engine
This operation is built on top of the previous one and simply takes settings for the specified engine:
result = CLIENT.mt.engine("Sandbox")
result.class # => ABBYY::Cloud::Models::Engine
result.name # => "Sandbox"
result.languages # => ["en", "ru"]
result.translation_directions # => [#<ABBYY::Cloud::Models::Direction source: "en", target: "ru">]
result.to_h
# => { name: "Sandbox", languages: ["en", "ru"], translation_directions: [{ source: "en", target: "ru" }] }
default_engine
Returns settings for the engine used in the initializer
CLIENT = ABBYY::Cloud.new(id: "foo", token: "bar", engine: "Bing")
settings_for_bing = CLIENT.mt.default_engine
translate
Translates a string.
See the specification.
result = CLIENT.mt.translate("To be or not to be", from: :en, to: :ru)
result.class # => ABBYY::Cloud::Models::Translation
result.translation # => "Быть или не быть"
result.id # => "2832934"
result.to_h # => { id: "2832934", translation: "Быть или не быть" }
translate_segments
Translates an array of strings in one request
result = CLIENT.mt.translate_segments(["To be", "or not to be"], from: :en, to: :ru)
result.class # => ABBYY::Cloud::Models::TranslationSequence
result.map(&:text) # => ["Быть", "или не быть"]
Files
The namespace files
contains operations with files.
upload
Uploads a file
file = File.read("file_to_upload.xml")
result = CLIENT.files.upload file
result.id # => "18323"
result.token # => "foobarbaz"
# ...
download
Dowloads a file by id and token
result = CLIENT.files.download(id: "foo", token: "bar")
# => #<StringIO ...>
Prices
The namespace prices
contains operations with prices details.
details
See the specification.
list = CLIENT.prices.details
list.first.to_h
# {
# id: "foo",
# account_id: "bar",
# type: "qux",
# from: "ru",
# to: "en",
# unit_prices: [{ unit_type: "Words", currency: "USD", amount: 0.03 }],
# discounts: [{ discount_type: "TMTextMatch", discount: 0.01 }],
# created: Time.now
# }
The number of items can be several thousands. You can specify skip
and take
options to take necessary items only, otherwise it will return all prices.
Notice, though, that every single request can return no more than 1000 items. If you request more prices, several requests will be made one-by-one. Parsing all the results can be pretty slow.
Compatibility
[WIP] Compatible to ABBYY Cloud API v.0.
Tested under rubies compatible to MRI 2.3+ using RSpec 3.0+.
License
The gem is available as open source under the terms of the MIT License.