abbyy-cloud 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/CHANGELOG.md +24 -0
  4. data/README.md +60 -33
  5. data/abbyy-cloud.gemspec +1 -1
  6. data/lib/abbyy/cloud.rb +10 -16
  7. data/lib/abbyy/cloud/connection.rb +31 -17
  8. data/lib/abbyy/cloud/models/direction.rb +4 -2
  9. data/lib/abbyy/cloud/models/discount.rb +12 -0
  10. data/lib/abbyy/cloud/models/engine.rb +4 -1
  11. data/lib/abbyy/cloud/models/locale.rb +23 -0
  12. data/lib/abbyy/cloud/models/price.rb +22 -0
  13. data/lib/abbyy/cloud/models/source_segment.rb +14 -0
  14. data/lib/abbyy/cloud/models/source_tag.rb +15 -0
  15. data/lib/abbyy/cloud/models/transfer_data.rb +14 -0
  16. data/lib/abbyy/cloud/models/translation.rb +1 -1
  17. data/lib/abbyy/cloud/models/translation_segment.rb +16 -0
  18. data/lib/abbyy/cloud/models/unit_price.rb +13 -0
  19. data/lib/abbyy/cloud/namespaces/machine_translations.rb +47 -1
  20. data/lib/abbyy/cloud/namespaces/prices.rb +29 -0
  21. data/lib/abbyy/cloud/operations/base.rb +20 -6
  22. data/lib/abbyy/cloud/operations/engines.rb +3 -1
  23. data/lib/abbyy/cloud/operations/prices.rb +23 -0
  24. data/lib/abbyy/cloud/operations/translate.rb +5 -3
  25. data/lib/abbyy/cloud/operations/translate_segments.rb +22 -0
  26. data/lib/abbyy/cloud/settings.rb +4 -5
  27. data/lib/abbyy/cloud/types.rb +14 -10
  28. data/spec/abbyy/cloud/connection_spec.rb +1 -1
  29. data/spec/abbyy/cloud/models/discount_spec.rb +32 -0
  30. data/spec/abbyy/cloud/models/locale_spec.rb +55 -0
  31. data/spec/abbyy/cloud/models/price_spec.rb +107 -0
  32. data/spec/abbyy/cloud/models/source_segment_spec.rb +37 -0
  33. data/spec/abbyy/cloud/models/source_tag_spec.rb +56 -0
  34. data/spec/abbyy/cloud/models/transfer_data_spec.rb +40 -0
  35. data/spec/abbyy/cloud/models/translation_segment_spec.rb +38 -0
  36. data/spec/abbyy/cloud/models/unit_price_spec.rb +48 -0
  37. data/spec/abbyy/cloud/settings_spec.rb +2 -24
  38. data/spec/abbyy/cloud_spec.rb +3 -4
  39. data/spec/feature/abbyy/mt_default_engine_spec.rb +12 -0
  40. data/spec/feature/abbyy/mt_engine_spec.rb +15 -0
  41. data/spec/feature/abbyy/mt_translate_segments_spec.rb +136 -0
  42. data/spec/feature/abbyy/{order_translate_spec.rb → mt_translate_spec.rb} +2 -2
  43. data/spec/feature/abbyy/prices_details_spec.rb +70 -0
  44. metadata +39 -5
  45. data/lib/abbyy/cloud/namespaces/orders.rb +0 -25
@@ -0,0 +1,37 @@
1
+ RSpec.describe ABBYY::Cloud::Models::SourceSegment do
2
+ let(:data) do
3
+ {
4
+ text: "to be or not to be",
5
+ tags: [{ number: 1, type: "Start", position: 0 }]
6
+ }
7
+ end
8
+
9
+ subject { described_class.new(data) }
10
+
11
+ it { is_expected.to be_kind_of ABBYY::Cloud::Struct }
12
+ its(:to_h) { is_expected.to eq data }
13
+
14
+ context "without text:" do
15
+ before { data.delete :text }
16
+
17
+ it "fails" do
18
+ expect { subject }.to raise_error(StandardError)
19
+ end
20
+ end
21
+
22
+ context "with invalid tags:" do
23
+ before { data[:tags] = "foo" }
24
+
25
+ it "fails" do
26
+ expect { subject }.to raise_error(StandardError)
27
+ end
28
+ end
29
+
30
+ context "with invalid tag:" do
31
+ before { data[:tags].first[:type] = "foo" }
32
+
33
+ it "fails" do
34
+ expect { subject }.to raise_error(StandardError)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ RSpec.describe ABBYY::Cloud::Models::SourceTag do
2
+ let(:data) { { number: 1, type: "Start", position: 0 } }
3
+
4
+ subject { described_class.new(data) }
5
+
6
+ it { is_expected.to be_kind_of ABBYY::Cloud::Struct }
7
+ its(:to_h) { is_expected.to eq data }
8
+
9
+ context "with invalid number:" do
10
+ before { data[:number] = "foo" }
11
+
12
+ it "fails" do
13
+ expect { subject }.to raise_error(StandardError)
14
+ end
15
+ end
16
+
17
+ context "without number:" do
18
+ before { data.delete :number }
19
+
20
+ it "fails" do
21
+ expect { subject }.to raise_error(StandardError)
22
+ end
23
+ end
24
+
25
+ context "with invalid type:" do
26
+ before { data[:type] = "foo" }
27
+
28
+ it "fails" do
29
+ expect { subject }.to raise_error(StandardError)
30
+ end
31
+ end
32
+
33
+ context "without type:" do
34
+ before { data.delete :type }
35
+
36
+ it "fails" do
37
+ expect { subject }.to raise_error(StandardError)
38
+ end
39
+ end
40
+
41
+ context "with invalid position:" do
42
+ before { data[:position] = "foo" }
43
+
44
+ it "fails" do
45
+ expect { subject }.to raise_error(StandardError)
46
+ end
47
+ end
48
+
49
+ context "without position:" do
50
+ before { data.delete :position }
51
+
52
+ it "fails" do
53
+ expect { subject }.to raise_error(StandardError)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,40 @@
1
+ RSpec.describe ABBYY::Cloud::Models::TransferData do
2
+ let(:data) { { order: 1, position: 0 } }
3
+
4
+ subject { described_class.new(data) }
5
+
6
+ it { is_expected.to be_kind_of ABBYY::Cloud::Struct }
7
+ its(:to_h) { is_expected.to eq data }
8
+
9
+ context "with invalid order:" do
10
+ before { data[:order] = "foo" }
11
+
12
+ it "fails" do
13
+ expect { subject }.to raise_error(StandardError)
14
+ end
15
+ end
16
+
17
+ context "without order:" do
18
+ before { data.delete :order }
19
+
20
+ it "fails" do
21
+ expect { subject }.to raise_error(StandardError)
22
+ end
23
+ end
24
+
25
+ context "with invalid position:" do
26
+ before { data[:position] = "foo" }
27
+
28
+ it "fails" do
29
+ expect { subject }.to raise_error(StandardError)
30
+ end
31
+ end
32
+
33
+ context "without position:" do
34
+ before { data.delete :position }
35
+
36
+ it "fails" do
37
+ expect { subject }.to raise_error(StandardError)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ RSpec.describe ABBYY::Cloud::Models::TranslationSegment do
2
+ let(:data) do
3
+ {
4
+ id: "foo",
5
+ text: "to be or not to be",
6
+ tags_transfer_data: [{ order: 1, position: 0 }]
7
+ }
8
+ end
9
+
10
+ subject { described_class.new(data) }
11
+
12
+ it { is_expected.to be_kind_of ABBYY::Cloud::Struct }
13
+ its(:to_h) { is_expected.to eq data }
14
+
15
+ context "without text:" do
16
+ before { data.delete :text }
17
+
18
+ it "fails" do
19
+ expect { subject }.to raise_error(StandardError)
20
+ end
21
+ end
22
+
23
+ context "with invalid tags_transfer_data:" do
24
+ before { data[:tags_transfer_data] = "foo" }
25
+
26
+ it "fails" do
27
+ expect { subject }.to raise_error(StandardError)
28
+ end
29
+ end
30
+
31
+ context "with invalid tag:" do
32
+ before { data[:tags_transfer_data].first[:order] = nil }
33
+
34
+ it "fails" do
35
+ expect { subject }.to raise_error(StandardError)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ RSpec.describe ABBYY::Cloud::Models::UnitPrice do
2
+ let(:data) { { unit_type: "Words", currency: "USD", amount: 0.03 } }
3
+
4
+ subject { described_class.new(data) }
5
+
6
+ it { is_expected.to be_kind_of ABBYY::Cloud::Struct }
7
+ its(:to_h) { is_expected.to eq data }
8
+
9
+ context "with invalid unit_type:" do
10
+ before { data[:unit_type] = "Miles" }
11
+
12
+ it "fails" do
13
+ expect { subject }.to raise_error(StandardError)
14
+ end
15
+ end
16
+
17
+ context "without unit_type:" do
18
+ before { data.delete :unit_type }
19
+
20
+ it "fails" do
21
+ expect { subject }.to raise_error(StandardError)
22
+ end
23
+ end
24
+
25
+ context "with invalid currency:" do
26
+ before { data[:currency] = "usd" }
27
+
28
+ it "fails" do
29
+ expect { subject }.to raise_error(StandardError)
30
+ end
31
+ end
32
+
33
+ context "without currency:" do
34
+ before { data.delete :currency }
35
+
36
+ it "fails" do
37
+ expect { subject }.to raise_error(StandardError)
38
+ end
39
+ end
40
+
41
+ context "without amount:" do
42
+ before { data.delete :amount }
43
+
44
+ it "fails" do
45
+ expect { subject }.to raise_error(StandardError)
46
+ end
47
+ end
48
+ end
@@ -36,14 +36,6 @@ RSpec.describe ABBYY::Cloud::Settings do
36
36
  expect { subject }.to raise_error(StandardError)
37
37
  end
38
38
  end
39
-
40
- context "with wrong version" do
41
- before { options[:id] = 1 }
42
-
43
- it "fails" do
44
- expect { subject }.to raise_error(StandardError)
45
- end
46
- end
47
39
  end
48
40
 
49
41
  describe "#id" do
@@ -69,25 +61,11 @@ RSpec.describe ABBYY::Cloud::Settings do
69
61
  end
70
62
  end
71
63
 
72
- describe "#version" do
73
- subject { settings.version }
74
-
75
- context "by default:" do
76
- it { is_expected.to eq 0 }
77
- end
78
-
79
- context "when assigned:" do
80
- before { options[:version] = 0 }
81
- it { is_expected.to eq 0 }
82
- end
83
- end
84
-
85
64
  describe "#connection" do
86
65
  subject { settings.connection }
87
66
 
88
67
  it { is_expected.to be_instance_of ABBYY::Cloud::Connection }
89
- its(:version) { is_expected.to eq settings.version }
90
- its(:id) { is_expected.to eq settings.id }
91
- its(:token) { is_expected.to eq settings.token }
68
+ its(:id) { is_expected.to eq settings.id }
69
+ its(:token) { is_expected.to eq settings.token }
92
70
  end
93
71
  end
@@ -6,9 +6,8 @@ RSpec.describe ABBYY::Cloud do
6
6
  subject { cloud.settings }
7
7
 
8
8
  it { is_expected.to be_a ABBYY::Cloud::Settings }
9
- its(:version) { is_expected.to eq 0 }
10
- its(:engine) { is_expected.to eq "Sandbox" }
11
- its(:id) { is_expected.to eq "foo" }
12
- its(:token) { is_expected.to eq "bar" }
9
+ its(:engine) { is_expected.to eq "Sandbox" }
10
+ its(:id) { is_expected.to eq "foo" }
11
+ its(:token) { is_expected.to eq "bar" }
13
12
  end
14
13
  end
@@ -0,0 +1,12 @@
1
+ RSpec.describe "mt.default_engine" do
2
+ let(:client) { ABBYY::Cloud.new id: "foo", token: "bar", engine: "Bing" }
3
+ let(:namespace) { client.mt }
4
+ let(:opts) { { languages: [], translation_directions: [] } }
5
+ let(:bing) { ABBYY::Cloud::Models::Engine.new(name: "Bing", **opts) }
6
+ let(:google) { ABBYY::Cloud::Models::Engine.new(name: "Google", **opts) }
7
+
8
+ before { allow(namespace).to receive(:engines) { [bing, google] } }
9
+ subject { namespace.default_engine }
10
+
11
+ it { is_expected.to eql bing }
12
+ end
@@ -0,0 +1,15 @@
1
+ RSpec.describe "mt.engine" do
2
+ let(:client) { ABBYY::Cloud.new id: "foo", token: "bar" }
3
+ let(:namespace) { client.mt }
4
+ let(:opts) { { languages: [], translation_directions: [] } }
5
+ let(:bing) { ABBYY::Cloud::Models::Engine.new(name: "Bing", **opts) }
6
+ let(:google) { ABBYY::Cloud::Models::Engine.new(name: "Google", **opts) }
7
+
8
+ before { allow(namespace).to receive(:engines) { [bing, google] } }
9
+
10
+ it "works" do
11
+ expect(namespace.engine("Bing")).to eq bing
12
+ expect(namespace.engine("Google")).to eq google
13
+ expect(namespace.engine("Unknonw")).to be_nil
14
+ end
15
+ end
@@ -0,0 +1,136 @@
1
+ RSpec.describe "mt.translate_segments" do
2
+ let(:client) { ABBYY::Cloud.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" }
8
+ let(:source_text) { "Бить или не бить" }
9
+ let(:path) { "https://api.abbyy.cloud/v1/order/mt/sync" }
10
+ let(:response_status) { 200 }
11
+ let(:response_model) { { id: "42", text: "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.mt.translate_segments [source_text], params }
25
+
26
+ it "sends a request to ABBYY Cloud API" do
27
+ subject
28
+ expect(a_request(:post, path)).to have_been_made
29
+ end
30
+
31
+ it "returns translation model" do
32
+ item = subject.first
33
+ expect(item).to be_kind_of ABBYY::Cloud::Models::TranslationSegment
34
+ expect(item.to_h).to include response_model
35
+ end
36
+
37
+ context "without custom engine:" do
38
+ before { params.delete :engine }
39
+
40
+ let(:expected_request) do
41
+ a_request(:post, path).with do |req|
42
+ expect(JSON.parse(req.body)).to include "engine" => "Google"
43
+ end
44
+ end
45
+
46
+ it "uses default engine in the request" do
47
+ subject
48
+ expect(expected_request).to have_been_made
49
+ end
50
+ end
51
+
52
+ context "when no engine is set:" do
53
+ before { settings.delete :engine }
54
+ before { params.delete :engine }
55
+
56
+ let(:expected_request) do
57
+ a_request(:post, path).with do |req|
58
+ expect(JSON.parse(req.body)).to include "engine" => "Sandbox"
59
+ end
60
+ end
61
+
62
+ it "uses Sandbox engine in the request" do
63
+ subject
64
+ expect(expected_request).to have_been_made
65
+ end
66
+ end
67
+
68
+ context "without text to translate:" do
69
+ let(:source_text) { nil }
70
+
71
+ it "raises ArgumentError before sending a request" do
72
+ expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
73
+ expect(a_request(:any, //)).not_to have_been_made
74
+ end
75
+ end
76
+
77
+ context "with invalid source language:" do
78
+ let(:source_language) { "1" }
79
+
80
+ it "raises ArgumentError before sending a request" do
81
+ expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
82
+ expect(a_request(:any, //)).not_to have_been_made
83
+ end
84
+ end
85
+
86
+ context "without source language:" do
87
+ before { params.delete :from }
88
+
89
+ it "raises ArgumentError before sending a request" do
90
+ expect { subject }.to raise_error(::ArgumentError)
91
+ expect(a_request(:any, //)).not_to have_been_made
92
+ end
93
+ end
94
+
95
+ context "with invalid target language:" do
96
+ let(:target_language) { "1" }
97
+
98
+ it "raises ArgumentError before sending a request" do
99
+ expect { subject }.to raise_error(ABBYY::Cloud::ArgumentError)
100
+ expect(a_request(:any, //)).not_to have_been_made
101
+ end
102
+ end
103
+
104
+ context "without target language:" do
105
+ before { params.delete :to }
106
+
107
+ it "raises ArgumentError before sending a request" do
108
+ expect { subject }.to raise_error(::ArgumentError)
109
+ expect(a_request(:any, //)).not_to have_been_made
110
+ end
111
+ end
112
+
113
+ context "when API responded with error:" do
114
+ before { stub_request(:post, path).to_return status: 500 }
115
+
116
+ it "raises ResponseError" do
117
+ expect { subject }.to raise_error(ABBYY::Cloud::ResponseError)
118
+ end
119
+ end
120
+
121
+ context "when API returned data without an id:" do
122
+ let(:response_model) { { translation: "To beat or not to beat" } }
123
+
124
+ it "raises TypeError" do
125
+ expect { subject }.to raise_error(ABBYY::Cloud::TypeError)
126
+ end
127
+ end
128
+
129
+ context "when API returned data without a translation:" do
130
+ let(:response_model) { { id: 1 } }
131
+
132
+ it "raises TypeError" do
133
+ expect { subject }.to raise_error(ABBYY::Cloud::TypeError)
134
+ end
135
+ end
136
+ end