upfluence-utils 0.8.3 → 0.9.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 445bf3674db4ae9627f19c3c572a1c6bca6cd89fc4279e570b24d927da6e1d8b
4
- data.tar.gz: 7b9413b8f197ff9ed260865fd1c2acd56276b6b5a863d8987ce9d171ad5a8fe8
3
+ metadata.gz: 9de927439a57bb3fe4965c7ae407a23a3bd2a66c3d77d2cf020d278154cc0233
4
+ data.tar.gz: a28ae01832068a5a06af2810480e272f5120e40c429fd89b8a94d2142cbc2d1d
5
5
  SHA512:
6
- metadata.gz: a4fa4e97b5f8ee0d1f9bdb440946cea644691a66b7387207353d458e257bdfebc9ac0577401957af5fa24094c998a6f17c4f1d0baa7d70c19a3f4578a5734e87
7
- data.tar.gz: 2cd094d1bd2fa605afbcd2e90f32de161cc57cb0ef1304d93364defb7d543ab1f556d8b6dbf2b4721ec4677b3795a8040969791cb70b217cea723fb455a26a7e
6
+ metadata.gz: d246c89ce9474146caf1a31759e898343fffaa36d5c08139e277e8b302a3dfbb3a796b34bfacc47edbb2b34a90afd0332b3477114c93085682e2eeb1fb17db7b
7
+ data.tar.gz: 978b2e1d3b54ed04a58a289ce0aef61e84ee8d2eee4d4285f46be421c15d8c387b6697fc5d4fa3247cd4203a06477eb8ef7cd5b5e3b5a8f361dc559f40d27a56
data/lib/test.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'upfluence/http/server'
2
+
3
+ Upfluence::HTTP::Server.new do
4
+ end.serve
data/lib/upfluence.rb CHANGED
@@ -2,5 +2,4 @@ require 'upfluence/utils'
2
2
  require 'upfluence/endpoint/api_endpoint'
3
3
  require 'upfluence/mixin/strong_parameters'
4
4
  require 'upfluence/mixin/pagination'
5
- require 'upfluence/base/exceptions/validation_error'
6
5
  require 'upfluence/resources'
@@ -1,12 +1,15 @@
1
1
  require 'sinatra'
2
2
  require 'active_record'
3
3
  require 'active_support/hash_with_indifferent_access'
4
+ require 'upfluence/http/endpoint/validation_error'
4
5
 
5
6
  module Upfluence
6
7
  module HTTP
7
8
  module Endpoint
8
9
  class BadRequest < StandardError; end
9
10
  class APIEndpoint < Sinatra::Base
11
+ VALIDATION_ERROR_KLASS = ValidationError
12
+
10
13
  disable :show_exceptions
11
14
  disable :logging
12
15
  disable :dump_errors
@@ -44,7 +47,7 @@ module Upfluence
44
47
  def respond_with(resource, *args)
45
48
  if resource.respond_to?(:errors) && resource.errors.any?
46
49
  status = 422
47
- result = Base::Exceptions::ValidationError.from_model(
50
+ result = VALIDATION_ERROR_KLASS.from_model(
48
51
  resource
49
52
  ).to_json
50
53
  else
@@ -0,0 +1,31 @@
1
+ module Upfluence
2
+ module HTTP
3
+ module Endpoint
4
+ class ValidationError
5
+ def initialize(validations)
6
+ @validations = validations
7
+ end
8
+
9
+ class << self
10
+ def from_model(model)
11
+ validations = model.errors.details.map do |error_field, errors|
12
+ errors.map do |error|
13
+ OpenStruct.new(
14
+ ressource: model.model_name.singular,
15
+ field: error_field.to_s,
16
+ code: error[:error].to_s
17
+ )
18
+ end
19
+ end.flatten
20
+
21
+ new(validations)
22
+ end
23
+ end
24
+
25
+ def to_json
26
+ { errors: @validations }.to_json
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,10 +4,7 @@ module Upfluence
4
4
  class ApplicationHeaders
5
5
  def initialize(app, handler)
6
6
  @app = app
7
- @headers = {
8
- "X-Upfluence-Unit-Name" => handler.getName,
9
- "X-Upfluence-Version" => build_version(handler.getVersion)
10
- }
7
+ @headers = handler ? build_headers(handler) : {}
11
8
  end
12
9
 
13
10
  def call(env)
@@ -17,14 +14,19 @@ module Upfluence
17
14
 
18
15
  private
19
16
 
17
+ def build_headers(handler)
18
+ {
19
+ 'X-Upfluence-Unit-Name' => handler.getName,
20
+ 'X-Upfluence-Version' => build_version(handler.getVersion)
21
+ }
22
+ end
23
+
20
24
  def build_version(thrift_version)
21
- if v = thrift_version.semantic_version
22
- return "v#{v.major}.#{v.minor}.#{v.patch}"
23
- end
25
+ v = thrift_version.semantic_version
26
+ return "v#{v.major}.#{v.minor}.#{v.patch}" if v
24
27
 
25
- if v = thrift_version.git_version
26
- return "v0.0.0-#{v.commit}"
27
- end
28
+ v = thrift_version.git_version
29
+ return "v0.0.0-#{v.commit}" if v
28
30
 
29
31
  'undefined'
30
32
  end
@@ -14,7 +14,6 @@ require 'upfluence/http/middleware/application_headers'
14
14
  require 'upfluence/http/middleware/handle_exception'
15
15
  require 'upfluence/http/middleware/prometheus'
16
16
  require 'upfluence/http/middleware/cors'
17
- require 'upfluence/handler/base'
18
17
 
19
18
  module Upfluence
20
19
  module HTTP
@@ -29,13 +28,19 @@ module Upfluence
29
28
  push_gateway_interval: 15, # sec
30
29
  app_name: ENV['APP_NAME'] || 'uhttp-rb-server',
31
30
  unit_name: ENV['UNIT_NAME'] || 'uhttp-rb-server-anonymous',
31
+ base_processor_klass: nil,
32
+ base_handler_klass: nil,
32
33
  debug: ENV['DEBUG']
33
- }.freeze
34
+ }
34
35
 
35
36
  def initialize(options = {}, &block)
36
37
  @options = DEFAULT_OPTIONS.dup.merge(options)
37
38
  opts = @options
38
- base_handler = Handler::Base.new(opts[:interfaces])
39
+ base_handler = nil
40
+
41
+ if opts[:base_handler_klass]
42
+ base_handler = opts[:base_handler_klass].new(@options[:interfaces])
43
+ end
39
44
 
40
45
  @builder = Builder.new do
41
46
  use Middleware::Logger
@@ -54,13 +59,13 @@ module Upfluence
54
59
  run(opts[:healthcheck_endpoint] || Endpoint::Healthcheck.new)
55
60
  end
56
61
 
57
- map '/base' do
58
- run_thrift Base::Base_service::BaseService::Processor, base_handler
62
+ if opts[:base_processor_klass] && base_handler
63
+ map '/base' do
64
+ run_thrift(opts[:base_processor_klass], base_handler)
65
+ end
59
66
  end
60
67
 
61
- map '/debug' do
62
- run Endpoint::Profiler.new
63
- end if opts[:debug]
68
+ map('/debug') { run(Endpoint::Profiler.new) } if opts[:debug]
64
69
 
65
70
  instance_eval(&block)
66
71
  end
@@ -1,23 +1,15 @@
1
1
  module Upfluence
2
- module Ressources
2
+ module Resources
3
3
  COUNTRIES = %w[
4
- US FR GB DE CH AF AL DZ AS AD AO AI
5
- AG AR AM AW AU AT AZ BS BH BD BB BY
6
- BE BZ BJ BM BT BO BA BW BR BN BG BF
7
- BI CV KH CM CA KY CF TD CL CN CO KM
8
- CG CD CK CI HR CU CY CZ DK DJ DM DO
9
- GW GY HT VA HN HK HU IS IN ID IR IQ
10
- IE IM IL IT JM JP JE JO KZ KE KI KP
11
- KR KW KG LA LV LB LI LS LR LY LT LU
12
- MO MK MG MW MY MV ML MT MH MQ MR MU
13
- MX FM MD MC MN ME MS MA MZ MM NA NR
14
- NP NL NC NZ NI NE NG NO OM PK PW PS
15
- PA PG PY PE PH PL PT PR QA RE RO RU
16
- RW KN LC VC WS SM ST SA SN RS SC SL
17
- SG SK SI SB SO ZA ES LK SD SR SZ SE
18
- SY TW TJ TZ TH TL TG TO TT TN TR TM
19
- TC TV UG UA AE GB UY UZ VU VE VN VG
20
- VI EH YE ZM ZW CR
4
+ US FR GB DE CH AF AL DZ AS AD AO AI AG AR AM AW AU AT AZ BS BH BD BB BY BE
5
+ BZ BJ BM BT BO BA BW BR BN BG BF BI CV KH CM CA KY CF TD CL CN CO KM CG CD
6
+ CK CI HR CU CY CZ DK DJ DM DO EC EG SV GQ ER EE ET FO FJ FI PF GA GM GE GH
7
+ GI GR GL GD GP GU GT GG GN GW GY HT VA HN HK HU IS IN ID IR IQ IE IM IL IT
8
+ JM JP JE JO KZ KE KI KP KR KW KG LA LV LB LI LS LR LY LT LU MO MK MG MW MY
9
+ MV ML MT MH MQ MR MU MX FM MD MC MN ME MS MA MZ MM NA NR NP NL NC NZ NI NE
10
+ NG NO OM PK PW PS PA PG PY PE PH PL PT PR QA RE RO RU RW KN LC VC WS SM ST
11
+ SA SN RS SC SL SG SK SI SB SO ZA ES LK SD SR SZ SE SY TW TJ TZ TH TL TG TO
12
+ TT TN TR TM TC TV UG UA AE GB UY UZ VU VE VN VG VI EH YE ZM ZW CR
21
13
  ].freeze
22
14
  end
23
15
  end
@@ -1,5 +1,5 @@
1
1
  module Upfluence
2
2
  module Utils
3
- VERSION = '0.8.3'.freeze
3
+ VERSION = '0.9.3'.freeze
4
4
  end
5
5
  end
data/rbutils.gemspec CHANGED
@@ -19,8 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.12"
20
20
  spec.add_development_dependency "rake", ">= 12.3.3"
21
21
  spec.add_development_dependency "rspec", "~> 3.0"
22
- spec.add_runtime_dependency 'upfluence-thrift', '~> 2.1'
23
- spec.add_runtime_dependency 'base-thrift', '>= 0.1.0'
22
+ spec.add_runtime_dependency 'upfluence-thrift'
24
23
  spec.add_runtime_dependency 'sinatra'
25
24
  spec.add_runtime_dependency 'redis'
26
25
  spec.add_runtime_dependency 'sentry-raven'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upfluence-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Upfluence
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-11 00:00:00.000000000 Z
11
+ date: 2021-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,32 +54,18 @@ dependencies:
54
54
  version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: upfluence-thrift
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.1'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.1'
69
- - !ruby/object:Gem::Dependency
70
- name: base-thrift
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - ">="
74
60
  - !ruby/object:Gem::Version
75
- version: 0.1.0
61
+ version: '0'
76
62
  type: :runtime
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - ">="
81
67
  - !ruby/object:Gem::Version
82
- version: 0.1.0
68
+ version: '0'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: sinatra
85
71
  requirement: !ruby/object:Gem::Requirement
@@ -249,19 +235,19 @@ files:
249
235
  - LICENSE.txt
250
236
  - README.md
251
237
  - Rakefile
238
+ - lib/test.rb
252
239
  - lib/upfluence.rb
253
240
  - lib/upfluence/amqp/server.rb
254
- - lib/upfluence/base/exceptions/validation_error.rb
255
241
  - lib/upfluence/endpoint/api_endpoint.rb
256
242
  - lib/upfluence/environment.rb
257
243
  - lib/upfluence/error_logger.rb
258
244
  - lib/upfluence/error_logger/null.rb
259
245
  - lib/upfluence/error_logger/sentry.rb
260
- - lib/upfluence/handler/base.rb
261
246
  - lib/upfluence/http/builder.rb
262
247
  - lib/upfluence/http/endpoint/api_endpoint.rb
263
248
  - lib/upfluence/http/endpoint/healthcheck.rb
264
249
  - lib/upfluence/http/endpoint/profiler.rb
250
+ - lib/upfluence/http/endpoint/validation_error.rb
265
251
  - lib/upfluence/http/middleware/application_headers.rb
266
252
  - lib/upfluence/http/middleware/cors.rb
267
253
  - lib/upfluence/http/middleware/handle_exception.rb
@@ -1,35 +0,0 @@
1
- require 'thrift/exceptions'
2
- require 'base/exceptions/exceptions_types'
3
-
4
- module Base
5
- module Exceptions
6
- class ValidationError < ::Thrift::Exception
7
- class << self
8
- attr_accessor :domain
9
-
10
- def from_model(model)
11
- validation_errors = model.errors.details.map do |error_field, errors|
12
- errors.map do |error|
13
- Base::Exceptions::Validation.new(
14
- domain: domain,
15
- model: model.model_name.singular,
16
- field: error_field.to_s,
17
- error: error[:error].to_s
18
- )
19
- end
20
- end.flatten
21
-
22
- new(validations: validation_errors)
23
- end
24
- end
25
-
26
- def to_json
27
- {
28
- errors: validations.map do |v|
29
- { resource: v.model, field: v.field, code: v.error }
30
- end
31
- }.to_json
32
- end
33
- end
34
- end
35
- end
@@ -1,56 +0,0 @@
1
- require 'base/base_service/base_service'
2
- require 'base/version/version_types'
3
- require 'base/version'
4
-
5
- module Upfluence
6
- module Handler
7
- class Base
8
- def initialize(modules = [])
9
- @alive_since = Time.now.to_i
10
- @modules = modules.reduce({ 'base' => ::Base::VERSION }) do |acc, cur|
11
- acc.merge cur.name.downcase => cur::VERSION
12
- end
13
- end
14
-
15
- def getVersion
16
- semantic_version = if ENV['SEMVER_VERSION']
17
- major, minor, patch = ENV['SEMVER_VERSION'].split('.')
18
- ::Base::Version::SemanticVersion.new(
19
- major: major[1..-1].to_i,
20
- minor: minor.to_i,
21
- patch: patch.to_i
22
- )
23
- end
24
-
25
- git_version = if ENV['GIT_COMMIT']
26
- ::Base::Version::GitVersion.new(
27
- commit: ENV['GIT_COMMIT'],
28
- branch: ENV['GIT_BRANCH'],
29
- remote: ENV['GIT_REMOTE']
30
- )
31
- end
32
-
33
- ::Base::Version::Version.new(
34
- semantic_version: semantic_version,
35
- git_version: git_version
36
- )
37
- end
38
-
39
- def getName
40
- ENV['UNIT_NAME'] || 'default'
41
- end
42
-
43
- def getStatus
44
- ::Base::Base_service::Status::ALIVE
45
- end
46
-
47
- def aliveSince
48
- @alive_since
49
- end
50
-
51
- def getInterfaceVersions
52
- @modules
53
- end
54
- end
55
- end
56
- end