abbyy-cloud 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cababb6c66e2c17fe4b3b8536f61ca2273695712
4
- data.tar.gz: aa4eb72f7805fd507c529ffd8fb79d2ad8f3f8dd
3
+ metadata.gz: 1d52d8a388887b3f4e08cf42ef30ae712b906871
4
+ data.tar.gz: a10ee9b0f18b09cb669b1d48f8724f2df89a9ba2
5
5
  SHA512:
6
- metadata.gz: 67a49f25576bbece4ee85226116275642bc5e3b3a1bcf86e4bf0182ee318de16687ef206f43edb59048ceb71151f28138be10d4765f2d8bf600ef7a4b1ecb9da
7
- data.tar.gz: 394413929ad8198cde627b12056c8119aefafa2a358e5f93dd59024604eb9d2bb3203a8a524b69b67ffe898d77ece5e917371bf5c3d0e12c72570010806d1eb5
6
+ metadata.gz: ad61bbb9f0c6fa939e34383692fece8384ed621269eb4761e530907e12d8a95d34b5ce3c36dd001f616b94f500ea0d21fc964bb788b80b79997f8417c2341c2a
7
+ data.tar.gz: 66999b7e975385018c8d40f6f31e67414ece9f4f36781af0359237024dff32f23de0880691f608613aaf4539ca6012316b95bb2fc3f7a6394bae2702be283a3f
data/.rubocop.yml CHANGED
@@ -13,8 +13,10 @@ AllCops:
13
13
  TargetRubyVersion: 2.3
14
14
 
15
15
  RSpec/NamedSubject:
16
- Exclude:
17
- - spec/**/*_spec.rb
16
+ Enabled: false
17
+
18
+ RSpec/DescribeClass:
19
+ Enabled: false
18
20
 
19
21
  Documentation:
20
22
  Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # [v0.0.2 2016-08-18](https://github.com/nepalez/abbyy-cloud/tree/v0.0.2)
2
+
3
+ ### Changed
4
+
5
+ * Move `#translation` from root to `#orders` namespace (nepalez)
6
+
7
+ Instead of `client.translation(*)` use `client.orders.translation(*)`
8
+
9
+ * Make `#translation` return model `ABBYY::Cloud::Models::Translation` instead of hash (nepalez)
10
+
11
+ ### Internal
12
+
13
+ * Extract settings from root object and isolate them in the shareable container `ABBYY::Cloud::Settings` (nepalez)
14
+ * Add namespaces for operations (nepalez)
15
+ * Add models for objects returned by operations (nepalez)
16
+
17
+ [Compare v0.0.1...v0.0.2](https://github.com/nepalez/abbyy-cloud/compare/v0.0.1...v0.0.2)
18
+
19
+ # [v0.0.1 2016-08-16](https://github.com/nepalez/abbyy-cloud/tree/v0.0.1)
20
+
21
+ First public release
data/README.md CHANGED
@@ -31,22 +31,25 @@ CLIENT = ABBYY::Cloud.new id: "foo",
31
31
  version: 0 # the only supported version
32
32
  ```
33
33
 
34
- ### Instant Translation
34
+ ### Orders
35
+
36
+ #### Instant Translation
35
37
 
36
38
  See [the specification](https://api.abbyy.cloud/swagger/ui/index#!/Order/Order_Translate).
37
39
 
38
40
  ```ruby
39
- result = CLIENT.translate("To be or not to be", from: :en, to: :ru)
41
+ result = CLIENT.orders.translate("To be or not to be", from: :en, to: :ru)
40
42
 
41
- result.class # => ABBYY::Cloud::Translation
43
+ result.class # => ABBYY::Cloud::Models::Translation
42
44
  result.translation # => "Быть или не быть"
43
- result.order_id # => 2832934
45
+ result.id # => "2832934"
46
+ result.to_h # => { id: "2832934", translation: "Быть или не быть" }
44
47
  ```
45
48
 
46
49
  You can specify an engine (different from default):
47
50
 
48
51
  ```ruby
49
- CLIENT.translate("To be or not to be", from: :en, to: :ru, engine: "Bing")
52
+ CLIENT.orders.translate("To be or not to be", from: :en, to: :ru, engine: "Bing")
50
53
  ```
51
54
 
52
55
  The method raises an exception in case the cloud responded with error.
@@ -54,13 +57,19 @@ The method raises an exception in case the cloud responded with error.
54
57
  ```ruby
55
58
  error = \
56
59
  begin
57
- CLIENT.translate("To be or not to be")
60
+ CLIENT.orders.translate("To be or not to be")
58
61
  rescue ABBYY::Cloud::Error => error
59
62
  error
60
63
  end
61
64
 
62
65
  error.class # => ABBYY::Cloud::ResponceError
63
66
  error.status # => 400
67
+ ```
68
+
69
+ The exception carries returned error model in its `#data` attribute:
70
+
71
+ ```ruby
72
+ error.data # => ABBYY::Cloud::Models::Error
64
73
  error.data.to_h
65
74
  # => {
66
75
  # model_state: {},
data/abbyy-cloud.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "abbyy-cloud"
3
- gem.version = "0.0.1"
3
+ gem.version = "0.0.2"
4
4
  gem.authors = ["Andrew Kozin"]
5
5
  gem.email = ["andrew.kozin@gmail.com"]
6
6
  gem.summary = "HTTP client to ABBYY Cloud API"
@@ -16,7 +16,6 @@ Gem::Specification.new do |gem|
16
16
 
17
17
  gem.add_runtime_dependency "dry-initializer", "~> 0.4.0"
18
18
  gem.add_runtime_dependency "dry-struct", "~> 0.0.1"
19
- gem.add_runtime_dependency "dry-types", "~> 0.8.0"
20
19
 
21
20
  gem.add_development_dependency "bundler", "~> 1.12"
22
21
  gem.add_development_dependency "rake", "~> 10.0"
data/lib/abbyy/cloud.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "dry-initializer"
2
2
  require "dry-struct"
3
- require "dry-types"
4
3
  require "json"
5
4
  require "net/http"
6
5
  require "net/https"
@@ -9,32 +8,32 @@ module ABBYY
9
8
  class Cloud
10
9
  require_relative "cloud/struct"
11
10
  require_relative "cloud/types"
12
- require_relative "cloud/response_error"
13
- require_relative "cloud/argument_error"
14
- require_relative "cloud/type_error"
15
11
  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
12
+ require_relative "cloud/settings"
13
+
14
+ require_relative "cloud/exceptions/response_error"
15
+ require_relative "cloud/exceptions/argument_error"
16
+ require_relative "cloud/exceptions/type_error"
17
+
18
+ require_relative "cloud/models/error"
19
+ require_relative "cloud/models/translation"
20
+
21
+ require_relative "cloud/operations/base"
22
+ require_relative "cloud/operations/translate"
23
+
24
+ require_relative "cloud/namespaces/base"
25
+ require_relative "cloud/namespaces/orders"
26
+
27
+ attr_reader :settings
25
28
 
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
29
+ def orders
30
+ Namespaces::Orders.new(settings)
32
31
  end
33
32
 
34
33
  private
35
34
 
36
- def connection
37
- @connection ||= Connection.new(version: version, id: id, token: token)
35
+ def initialize(*args)
36
+ @settings ||= Settings.new(*args)
38
37
  end
39
38
  end
40
39
  end
@@ -2,17 +2,10 @@
2
2
  # It takes a raw Net::HTTPResponse and extracts all the data returned by API.
3
3
  class ABBYY::Cloud
4
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
5
  attr_reader :status, :data
13
6
 
14
7
  def initialize(response)
15
- @data = Data[JSON.parse(response.body)]
8
+ @data = Models::Error[JSON.parse(response.body)]
16
9
  @status = response.code.to_i
17
10
 
18
11
  super <<-MESSAGE.gsub(/ +\|/, "")
@@ -0,0 +1,12 @@
1
+ class ABBYY::Cloud
2
+ # Collection of models returned in requests
3
+ module Models
4
+ # An error returned by the server in case of wrong response
5
+ class Error < 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
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class ABBYY::Cloud
2
+ # Collection of models returned in requests
3
+ module Models
4
+ # Result of the order translation
5
+ class Translation < Struct
6
+ attribute :id, Types::OrderId
7
+ attribute :translation, Types::Strict::String
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ class ABBYY::Cloud
2
+ module Namespaces
3
+ # Base class for namespaces
4
+ # Every namespace carries shared settings and defines its own methods
5
+ class Base
6
+ include Dry::Initializer.define -> do
7
+ param :settings
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ class ABBYY::Cloud
2
+ module Namespaces
3
+ # Namespace for operations with orders
4
+ # @see [https://api.abbyy.cloud/swagger/ui/index#!/Order] ABBYY Cloud API
5
+ class Orders < Base
6
+ # Instantly (synchronously) translates the text
7
+ #
8
+ # @example
9
+ # translate "Hello world!", from: "en", to: "fr_FR"
10
+ #
11
+ # @param [String] text
12
+ # @option [ABBYY::Cloud::Types::Language] :from Source language
13
+ # @option [ABBYY::Cloud::Types::Language] :to Target language
14
+ # @return [Hash<Symbol, Object>] translation
15
+ #
16
+ def translate(text, from:, to:, **opts)
17
+ Operations::Translate.new(settings).call source_text: text,
18
+ source_language: from,
19
+ target_language: to,
20
+ engine: settings.engine,
21
+ **opts
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,78 @@
1
+ # Base class for specific operations
2
+ # It validates request and response using corresponding structs
3
+ #
4
+ # @abstract
5
+ #
6
+ class ABBYY::Cloud
7
+ module Operations
8
+ class Base
9
+ # Contains helper methods to specify concrete operations
10
+ class << self
11
+ include Forwardable
12
+
13
+ def http_method(value = nil)
14
+ @http_method = value.to_s.capitalize if value
15
+ @http_method || "Post"
16
+ end
17
+
18
+ def path(value = nil)
19
+ value ? @path = value : @path
20
+ end
21
+
22
+ def link(value = nil)
23
+ value ? @link = value : @link
24
+ end
25
+
26
+ def request_body(struct = nil, &block)
27
+ provide_struct :@request_body, struct, &block
28
+ end
29
+
30
+ def response_body(struct = nil, &block)
31
+ provide_struct :@response_body, struct, &block
32
+ end
33
+
34
+ private
35
+
36
+ def provide_struct(variable, struct, &block)
37
+ if block || instance_variable_get(variable).nil?
38
+ struct ||= Class.new(Struct).tap { |obj| obj.instance_eval(&block) }
39
+ instance_variable_set(variable, struct)
40
+ else
41
+ instance_variable_get(variable)
42
+ end
43
+ end
44
+ end
45
+
46
+ def_delegators :"self.class",
47
+ :link,
48
+ :http_method,
49
+ :path,
50
+ :request_body,
51
+ :response_body
52
+
53
+ include Dry::Initializer.define -> do
54
+ param :settings
55
+ end
56
+
57
+ def call(**data)
58
+ body = prepare_request_body(data)
59
+ res = settings.connection.call(http_method, path, body: body)
60
+ handle_response_body(res)
61
+ end
62
+
63
+ private
64
+
65
+ def prepare_request_body(data)
66
+ request_body[data]
67
+ rescue => error
68
+ raise ArgumentError.new(link, data, error.message)
69
+ end
70
+
71
+ def handle_response_body(data)
72
+ response_body[data]
73
+ rescue => error
74
+ raise TypeError.new(link, data, error.message)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,6 +1,6 @@
1
1
  class ABBYY::Cloud
2
- class Operation
3
- class Translate < Operation
2
+ module Operations
3
+ class Translate < Base
4
4
  link "https://api.abbyy.cloud/swagger/ui/index#!/Order/Order_Translate"
5
5
  path "order/mt/sync"
6
6
  http_method "post"
@@ -12,10 +12,7 @@ class ABBYY::Cloud
12
12
  attribute :source_text, Types::Strict::String
13
13
  end
14
14
 
15
- response_body do
16
- attribute :id, Types::OrderId
17
- attribute :translation, Types::Strict::String
18
- end
15
+ response_body Models::Translation
19
16
  end
20
17
  end
21
18
  end
@@ -0,0 +1,16 @@
1
+ # Container for settings, that is initialized by [ABBYY::Cloud#initializer]
2
+ # and then shared by namespaces and operations.
3
+ class ABBYY::Cloud
4
+ class Settings
5
+ include Dry::Initializer.define -> do
6
+ option :id, type: Types::AuthId
7
+ option :token, type: Types::AuthToken
8
+ option :version, type: Types::Version, default: proc { 0 }
9
+ option :engine, type: Types::Engine, default: proc { "Sandbox" }
10
+ end
11
+
12
+ def connection
13
+ @connection ||= Connection.new(version: version, id: id, token: token)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,101 @@
1
+ RSpec.describe ABBYY::Cloud::Settings do
2
+ let(:options) { { id: "foo", token: "bar" } }
3
+ let(:settings) { described_class.new options }
4
+
5
+ describe ".new" do
6
+ subject { settings }
7
+
8
+ context "with wrong engine" do
9
+ before { options[:engine] = "Wrong" }
10
+
11
+ it "fails" do
12
+ expect { subject }.to raise_error(StandardError)
13
+ end
14
+ end
15
+
16
+ context "without id" do
17
+ before { options.delete :id }
18
+
19
+ it "fails" do
20
+ expect { subject }.to raise_error(StandardError)
21
+ end
22
+ end
23
+
24
+ context "without token" do
25
+ before { options.delete :token }
26
+
27
+ it "fails" do
28
+ expect { subject }.to raise_error(StandardError)
29
+ end
30
+ end
31
+
32
+ context "with empty id" do
33
+ before { options[:id] = nil }
34
+
35
+ it "fails" do
36
+ expect { subject }.to raise_error(StandardError)
37
+ end
38
+ end
39
+
40
+ context "with empty token" do
41
+ before { options[:token] = nil }
42
+
43
+ it "fails" do
44
+ expect { subject }.to raise_error(StandardError)
45
+ end
46
+ end
47
+
48
+ context "with wrong version" do
49
+ before { options[:id] = 1 }
50
+
51
+ it "fails" do
52
+ expect { subject }.to raise_error(StandardError)
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#id" do
58
+ subject { settings.id }
59
+ it { is_expected.to eq "foo" }
60
+ end
61
+
62
+ describe "#token" do
63
+ subject { settings.token }
64
+ it { is_expected.to eq "bar" }
65
+ end
66
+
67
+ describe "#engine" do
68
+ subject { settings.engine }
69
+
70
+ context "by default:" do
71
+ it { is_expected.to eq "Sandbox" }
72
+ end
73
+
74
+ context "when assigned:" do
75
+ before { options[:engine] = "Google" }
76
+ it { is_expected.to eq "Google" }
77
+ end
78
+ end
79
+
80
+ describe "#version" do
81
+ subject { settings.version }
82
+
83
+ context "by default:" do
84
+ it { is_expected.to eq 0 }
85
+ end
86
+
87
+ context "when assigned:" do
88
+ before { options[:version] = 0 }
89
+ it { is_expected.to eq 0 }
90
+ end
91
+ end
92
+
93
+ describe "#connection" do
94
+ subject { settings.connection }
95
+
96
+ it { is_expected.to be_instance_of ABBYY::Cloud::Connection }
97
+ its(:version) { is_expected.to eq settings.version }
98
+ its(:id) { is_expected.to eq settings.id }
99
+ its(:token) { is_expected.to eq settings.token }
100
+ end
101
+ end
@@ -2,57 +2,13 @@ RSpec.describe ABBYY::Cloud do
2
2
  let(:options) { { id: "foo", token: "bar" } }
3
3
  let(:cloud) { described_class.new options }
4
4
 
5
- describe ".new" do
6
- subject { cloud }
5
+ describe "#settings" do
6
+ subject { cloud.settings }
7
7
 
8
+ it { is_expected.to be_a ABBYY::Cloud::Settings }
8
9
  its(:version) { is_expected.to eq 0 }
9
10
  its(:engine) { is_expected.to eq "Sandbox" }
10
11
  its(:id) { is_expected.to eq "foo" }
11
12
  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
13
  end
58
14
  end
@@ -1,5 +1,5 @@
1
- RSpec.describe ABBYY::Cloud, "#translate" do
2
- let(:client) { described_class.new settings }
1
+ RSpec.describe "orders.translate" do
2
+ let(:client) { ABBYY::Cloud.new settings }
3
3
  let(:settings) { { id: "foo", token: "bar", engine: default_engine } }
4
4
  let(:default_engine) { "Google" }
5
5
  let(:custom_engine) { "Bing" }
@@ -21,13 +21,18 @@ RSpec.describe ABBYY::Cloud, "#translate" do
21
21
  body: JSON(response_model)
22
22
  end
23
23
 
24
- subject { client.translate source_text, params }
24
+ subject { client.orders.translate source_text, params }
25
25
 
26
- it "sends a request to ABBYY Cloud API and returns translation as a hash" do
27
- expect(subject).to eq response_model
26
+ it "sends a request to ABBYY Cloud API" do
27
+ subject
28
28
  expect(a_request(:post, path)).to have_been_made
29
29
  end
30
30
 
31
+ it "returns translation model" do
32
+ expect(subject).to be_instance_of ABBYY::Cloud::Models::Translation
33
+ expect(subject.to_h).to eq response_model
34
+ end
35
+
31
36
  context "without custom engine:" do
32
37
  before { params.delete :engine }
33
38
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abbyy-cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-16 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
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
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -143,29 +129,37 @@ executables: []
143
129
  extensions: []
144
130
  extra_rdoc_files:
145
131
  - README.md
132
+ - CHANGELOG.md
146
133
  files:
147
134
  - ".gitignore"
148
135
  - ".rspec"
149
136
  - ".rubocop.yml"
150
137
  - ".travis.yml"
138
+ - CHANGELOG.md
151
139
  - Gemfile
152
140
  - LICENSE.txt
153
141
  - README.md
154
142
  - Rakefile
155
143
  - abbyy-cloud.gemspec
156
144
  - lib/abbyy/cloud.rb
157
- - lib/abbyy/cloud/argument_error.rb
158
145
  - 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
146
+ - lib/abbyy/cloud/exceptions/argument_error.rb
147
+ - lib/abbyy/cloud/exceptions/response_error.rb
148
+ - lib/abbyy/cloud/exceptions/type_error.rb
149
+ - lib/abbyy/cloud/models/error.rb
150
+ - lib/abbyy/cloud/models/translation.rb
151
+ - lib/abbyy/cloud/namespaces/base.rb
152
+ - lib/abbyy/cloud/namespaces/orders.rb
153
+ - lib/abbyy/cloud/operations/base.rb
154
+ - lib/abbyy/cloud/operations/translate.rb
155
+ - lib/abbyy/cloud/settings.rb
162
156
  - lib/abbyy/cloud/struct.rb
163
- - lib/abbyy/cloud/type_error.rb
164
157
  - lib/abbyy/cloud/types.rb
165
158
  - spec/abbyy/cloud/connection_spec.rb
166
159
  - spec/abbyy/cloud/response_error_spec.rb
160
+ - spec/abbyy/cloud/settings_spec.rb
167
161
  - spec/abbyy/cloud_spec.rb
168
- - spec/feature/abbyy/cloud_translate_spec.rb
162
+ - spec/feature/abbyy/order_translate_spec.rb
169
163
  - spec/spec_helper.rb
170
164
  homepage: https://github.com/nepalez/abbyy-cloud
171
165
  licenses:
@@ -194,6 +188,7 @@ summary: HTTP client to ABBYY Cloud API
194
188
  test_files:
195
189
  - spec/abbyy/cloud/connection_spec.rb
196
190
  - spec/abbyy/cloud/response_error_spec.rb
191
+ - spec/abbyy/cloud/settings_spec.rb
197
192
  - spec/abbyy/cloud_spec.rb
198
- - spec/feature/abbyy/cloud_translate_spec.rb
193
+ - spec/feature/abbyy/order_translate_spec.rb
199
194
  - spec/spec_helper.rb
@@ -1,76 +0,0 @@
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