ruby-ant-server 0.4.1 → 0.5.0

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
  SHA256:
3
- metadata.gz: 41ee7dcbdca9427ffd33f13adb474decf81c6dc7a15b6e2737d7bf43bf047183
4
- data.tar.gz: 8ae42f2484ab308f7f44856a5dd580c4d10b5711dffb49e9132483ee8eedcf43
3
+ metadata.gz: 8a2f23c6be6739a4fd249562ffc23438dddaa0a2927035944a3b4b17967c7b9f
4
+ data.tar.gz: e49e44b63c4ef5bf3afd292e82a7e23556bcdd73e8d846ddcd3d5e86b21ee4f1
5
5
  SHA512:
6
- metadata.gz: f04d0fb5168cb1834f87f2b4b4d28f48c8e85dc7ab64b594790c6f6790337782f33b78e23c2da01ccfffc313aecb823a2d39dfccbd912ce5781921e7e5a5665d
7
- data.tar.gz: 07704b5849e8fb0a3c9a903d49d7d7f16719d1009b4bae03d8a11136013d7b01bfd126afe3a3ee85b9861316ad05a1553dd6f6014d7a09cdb531206a1afce153
6
+ metadata.gz: 28de14cddc343f8fee602d0bf9d475954b232fd01c4ff0f4a4aecdd42700da321b3dce9a6b112fd9d749a7fb344bdb5b02c6faa2301e3fca9603feca5df45216
7
+ data.tar.gz: 22826aae6b99887e4adb014d7b1c021ecdd1a0ea4dac0e156bb41f74233dd99e18c1066a2ff67161070cc4559faba66fc03130e0a3c91eead2cc63af2dabd92d
@@ -21,6 +21,15 @@ module Ant
21
21
  end
22
22
  end
23
23
 
24
+ ##
25
+ # Is used to express a success with the client's request.
26
+ class AntSuccess < AntBaseException
27
+ def initialize(message, code = nil, data = {})
28
+ code ||= self.class.name.split('::').last
29
+ super(message, code, data)
30
+ end
31
+ end
32
+
24
33
  ##
25
34
  # Is used to express a problem with the client's request.
26
35
  class AntFail < AntBaseException
@@ -0,0 +1,5 @@
1
+ require_relative 'server/nanoservice/datasource/model'
2
+ require_relative 'server/nanoservice/factory'
3
+ require_relative 'server/nanoservice/datasource/json_repository'
4
+ require_relative 'server/nanoservice/datasource/sequel'
5
+ require_relative 'server/nanoservice/datasource/exceptions'
@@ -13,7 +13,7 @@ module Ant
13
13
  # success means there were no errors during the execution of the request
14
14
  # it sends the result in the data field.
15
15
  def success(response)
16
- { status: :success, data: response.result }
16
+ { status: :success, data: response.result || response.data }
17
17
  end
18
18
 
19
19
  ##
@@ -0,0 +1,25 @@
1
+ module Ant
2
+ module Server
3
+ module Nanoservice
4
+ module Datasource
5
+ module Exceptions
6
+ class ObjectAlreadyExists < Ant::Exceptions::AntFail
7
+ attr_reader :id
8
+ def initialize(id)
9
+ @id = id
10
+ super("Object #{id} already exists", nil, id: id)
11
+ end
12
+ end
13
+
14
+ class ObjectNotFound < Ant::Exceptions::AntFail
15
+ attr_reader :id
16
+ def initialize(id)
17
+ @id = id
18
+ super("Object #{id} does not exist", nil, id: id)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module Ant
2
+ module Server
3
+ module Nanoservice
4
+ module Datasource
5
+ class IDGenerators
6
+ extend DRY::ResourceInjector
7
+ def self.[](key)
8
+ resource(:generators, key)
9
+ end
10
+
11
+ def self.normalize_id(object, key)
12
+ object.is_a?(Hash) ? object[key] : object
13
+ end
14
+
15
+ id_generator = lambda { |id, key| normalize_id(id, key) }
16
+ register(:generators, :id, id_generator)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'repository'
2
+
3
+ module Ant
4
+ module Server
5
+ module Nanoservice
6
+ module Datasource
7
+ class JSONRepository < Repository
8
+ def initialize(folder, id, id_generator)
9
+ @path = folder
10
+ super(id, id_generator)
11
+ end
12
+
13
+ def get(id)
14
+ path = full_path(id)
15
+ raise(ObjectNotFound, id) unless File.file?(path)
16
+ contents = File.read(path)
17
+ JSON.parse(contents, symbolize_names: true)
18
+ end
19
+
20
+ def create_(data)
21
+ store(data)
22
+ data
23
+ end
24
+
25
+ def store(data)
26
+ id = data[@id]
27
+ File.write(full_path(id), data.to_json)
28
+ end
29
+
30
+ private
31
+
32
+ def full_path(id)
33
+ "#{@path}/#{id}.json"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'id_generators'
2
+ module Ant
3
+ module Server
4
+ module Nanoservice
5
+ module Datasource
6
+ class Model
7
+ attr_reader :data
8
+ attr_writer :data
9
+
10
+ def run_validations!
11
+ puts 'WARN: model with no validations'
12
+ end
13
+
14
+ def store
15
+ run_validations!
16
+ @repository.store(@data)
17
+ end
18
+
19
+ def create
20
+ run_validations!
21
+ @repository.create(@data)
22
+ end
23
+
24
+ def initialize(data, repository = nil)
25
+ @data = data
26
+ @repository = repository || default_repository
27
+ end
28
+
29
+ def to_json(options)
30
+ @data.to_json(options)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'exceptions'
2
+
3
+ module Ant
4
+ module Server
5
+ module Nanoservice
6
+ module Datasource
7
+ class Repository
8
+ include Exceptions
9
+ def initialize(id, id_generator)
10
+ @id = id
11
+ @id_generator = id_generator
12
+ end
13
+
14
+ def create_initial_object(id)
15
+ object = { @id => @id_generator.call(id, @id) }
16
+ object.merge!(id) if id.is_a?(Hash)
17
+ object
18
+ end
19
+
20
+ def create(id = nil)
21
+ data = create_initial_object(id)
22
+ raise(ObjectAlreadyExists, data[@id]) if exist?(data[@id])
23
+ create_(data)
24
+ end
25
+
26
+ def exist?(id)
27
+ get(id)
28
+ true
29
+ rescue ObjectNotFound
30
+ false
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'repository'
2
+
3
+ module Ant
4
+ module Server
5
+ module Nanoservice
6
+ module Datasource
7
+ class Sequel < Repository
8
+ def initialize(sequel_object, id, id_generator)
9
+ @sequel = sequel_object
10
+ super(id, id_generator)
11
+ end
12
+
13
+ def get(id)
14
+ result = @sequel.where(@id => id).first
15
+ raise(ObjectNotFound, id) if result.nil?
16
+ result
17
+ end
18
+
19
+ def create_(data)
20
+ id = @sequel.insert(data)
21
+ data[@id] ||= id
22
+ data
23
+ end
24
+
25
+ def store(data)
26
+ data2 = data.dup
27
+ data2.delete(@id)
28
+ @sequel.where(@id => data[@id]).update(data2)
29
+ end
30
+
31
+ def connection
32
+ @sequel
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ module Ant
2
+ module Server
3
+ module Nanoservice
4
+ class Factory
5
+ include Ant::DRY::ResourceInjector
6
+
7
+ def initialize(model)
8
+ @model = model
9
+ end
10
+
11
+ def create(data, source = resource(:default))
12
+ repository = resource(source)
13
+ model = @model.new(data, repository)
14
+ model.create
15
+ model
16
+ end
17
+
18
+ def get(id, source = resource(:default))
19
+ repository = resource(source)
20
+ model = repository.get(id)
21
+ @model.new(model, repository)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -30,6 +30,7 @@ module Ant
30
30
  end
31
31
 
32
32
  def configure_defaults!
33
+ recover_from!(Exceptions::AntSuccess, :success)
33
34
  recover_from!(Exceptions::AntFail, :fail)
34
35
  recover_from!(Exceptions::AntError, :error)
35
36
  register(:loggers, :cute_logger, Server::CuteLogger.new)
data/lib/ant/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ant
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-ant-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-24 00:00:00.000000000 Z
11
+ date: 2018-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cute_logger
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sequel
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: simplecov
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +192,20 @@ dependencies:
178
192
  - - ">="
179
193
  - !ruby/object:Gem::Version
180
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: sqlite3
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
181
209
  - !ruby/object:Gem::Dependency
182
210
  name: webmock
183
211
  requirement: !ruby/object:Gem::Requirement
@@ -204,20 +232,25 @@ files:
204
232
  - lib/ant/client/format/format.rb
205
233
  - lib/ant/client/format/json_format.rb
206
234
  - lib/ant/client/format/url_encoded.rb
207
- - lib/ant/client/format/xml_format.rb
208
235
  - lib/ant/client/rest_client.rb
209
236
  - lib/ant/client/session/base.rb
210
237
  - lib/ant/client/session/basic_auth.rb
211
- - lib/ant/client/session/ssl_client_certificate.rb
212
238
  - lib/ant/client/validator.rb
213
239
  - lib/ant/client/validator/jsend.rb
214
240
  - lib/ant/client/validator/no_validator.rb
215
- - lib/ant/dry/daemon.rb
216
241
  - lib/ant/dry/resource_injector.rb
217
242
  - lib/ant/exceptions.rb
243
+ - lib/ant/nanoservice.rb
218
244
  - lib/ant/server/format.rb
219
245
  - lib/ant/server/grape.rb
220
246
  - lib/ant/server/logger.rb
247
+ - lib/ant/server/nanoservice/datasource/exceptions.rb
248
+ - lib/ant/server/nanoservice/datasource/id_generators.rb
249
+ - lib/ant/server/nanoservice/datasource/json_repository.rb
250
+ - lib/ant/server/nanoservice/datasource/model.rb
251
+ - lib/ant/server/nanoservice/datasource/repository.rb
252
+ - lib/ant/server/nanoservice/datasource/sequel.rb
253
+ - lib/ant/server/nanoservice/factory.rb
221
254
  - lib/ant/server/request_response.rb
222
255
  - lib/ant/server/response.rb
223
256
  - lib/ant/version.rb
@@ -241,7 +274,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
274
  version: '0'
242
275
  requirements: []
243
276
  rubyforge_project:
244
- rubygems_version: 2.7.3
277
+ rubygems_version: 2.7.6
245
278
  signing_key:
246
279
  specification_version: 4
247
280
  summary: Implements ANT format on server applications
File without changes
@@ -1,11 +0,0 @@
1
- module Ant
2
- module Client
3
- module Session
4
- module SSLClientCertificate
5
- def ssl_certificate(_cert, _key)
6
- puts 'TODO: write cert'
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,31 +0,0 @@
1
- module Ant
2
- module DRY
3
- class Daemon
4
- def initialize(wait_time, attach, proc)
5
- @proc = proc
6
- @wait_time = wait_time
7
- @attach = attach
8
- @finish = false
9
- end
10
-
11
- def task
12
- loop do
13
- begin
14
- @proc.exec
15
- rescue StandarError => ex
16
- log_error('Unexpected error', error: ex)
17
- end
18
- sleep(@wait_time)
19
- end
20
- end
21
-
22
- def run
23
- if @attach
24
- task
25
- else
26
- Thread.new(&:task)
27
- end
28
- end
29
- end
30
- end
31
- end