upfluence-utils 0.8.1 → 0.9.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
  SHA256:
3
- metadata.gz: ef4e2db1636205d5dc5985b0ba6fd5a42f73e6e09f59bc8726af03c860ccf106
4
- data.tar.gz: a5ae9ce7e4c35d4cdae827307587a60c9b70ce1ec04b552cf030913d9a9254d1
3
+ metadata.gz: 35be0f17a3c73784afa8e178a740bed54d6491d182d592177904bbfbf31e0b83
4
+ data.tar.gz: 9d7bebee3fbdead4d8b32e3314adbfba61e282811249b023fc2d0488dfb0a538
5
5
  SHA512:
6
- metadata.gz: 90e59aac5789abe24831bae11947388a76696fb33bc85e8bf0f781e77010b59d3feee76d21a97cf6986d16d4070b07297da7ff2dc981dae30b071cd64ba20d9a
7
- data.tar.gz: 7c321b4f2e7dfa24ba3ad4fc618cd8acded86ba22d87a9d68cc92990d196ef847949e2fd88c529d302c571c8fe437f5ee0a20c0b6a16fa17ddfb662a8f425597
6
+ metadata.gz: 423c2ef58a5363705bb5f90ec629648000a2d25c14c2536da02afb1def3f14c946db657ca244887ec3cc5400d2b78feaf6be5c92103b8853a0af420cdf6bbffc
7
+ data.tar.gz: e559829e31729eaa0ed30bde6d3d4fcda417d7833324a93c1ac923e4bf9e8ff3fcbf0abd0b4f0357f4c967e2d9a9b165d1c33c2fe542a4bb9f36ac5212ac836c
@@ -0,0 +1,30 @@
1
+ ### What does this PR do?
2
+
3
+ <!-- A brief description of the context of this pull request and its purpose. -->
4
+
5
+ Fixes #<!-- enter issue number here -->
6
+
7
+ ### What are the observable changes?
8
+ <!-- This question could be adequate with multiple use cases, for example: -->
9
+
10
+ <!-- Frontend: explain the feature created / updated, give instructions telling how to see the change in staging -->
11
+ <!-- Performance: what metric should be impacted, link to the right graphana dashboard for exemple -->
12
+ <!-- Bug: a given issue trail on sentry should stop happening -->
13
+ <!-- Feature: Implements X thrift service / Z HTTP REST API added, provide instructions on how leverage your feature from staging or your workstation -->
14
+
15
+ ### Good PR checklist
16
+
17
+ - [ ] Title makes sense
18
+ - [ ] Is against the correct branch
19
+ - [ ] Only addresses one issue
20
+ - [ ] Properly assigned
21
+ - [ ] Added/updated tests
22
+ - [ ] Added/updated documentation
23
+ - [ ] Properly labeled
24
+
25
+ ### Additional Notes
26
+
27
+ <!--
28
+ You can add anything you want here, an explanation on the way you built your implementation,
29
+ precisions on the origin of the bug, gotchas you need to mention.
30
+ -->
@@ -0,0 +1,4 @@
1
+ require 'upfluence/http/server'
2
+
3
+ Upfluence::HTTP::Server.new do
4
+ end.serve
@@ -2,4 +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'
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
@@ -0,0 +1 @@
1
+ require 'upfluence/resources/countries'
@@ -0,0 +1,23 @@
1
+ module Upfluence
2
+ module Resources
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
21
+ ].freeze
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  module Upfluence
2
2
  module Utils
3
- VERSION = '0.8.1'.freeze
3
+ VERSION = '0.9.2'.freeze
4
4
  end
5
5
  end
@@ -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.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Upfluence
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-24 00:00:00.000000000 Z
11
+ date: 2021-01-29 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
@@ -234,13 +220,14 @@ dependencies:
234
220
  - - ">="
235
221
  - !ruby/object:Gem::Version
236
222
  version: '0'
237
- description:
223
+ description:
238
224
  email:
239
225
  - dev@upfluence.com
240
226
  executables: []
241
227
  extensions: []
242
228
  extra_rdoc_files: []
243
229
  files:
230
+ - ".github/pull_request_template.md"
244
231
  - ".gitignore"
245
232
  - ".rspec"
246
233
  - ".travis.yml"
@@ -248,19 +235,19 @@ files:
248
235
  - LICENSE.txt
249
236
  - README.md
250
237
  - Rakefile
238
+ - lib/test.rb
251
239
  - lib/upfluence.rb
252
240
  - lib/upfluence/amqp/server.rb
253
- - lib/upfluence/base/exceptions/validation_error.rb
254
241
  - lib/upfluence/endpoint/api_endpoint.rb
255
242
  - lib/upfluence/environment.rb
256
243
  - lib/upfluence/error_logger.rb
257
244
  - lib/upfluence/error_logger/null.rb
258
245
  - lib/upfluence/error_logger/sentry.rb
259
- - lib/upfluence/handler/base.rb
260
246
  - lib/upfluence/http/builder.rb
261
247
  - lib/upfluence/http/endpoint/api_endpoint.rb
262
248
  - lib/upfluence/http/endpoint/healthcheck.rb
263
249
  - lib/upfluence/http/endpoint/profiler.rb
250
+ - lib/upfluence/http/endpoint/validation_error.rb
264
251
  - lib/upfluence/http/middleware/application_headers.rb
265
252
  - lib/upfluence/http/middleware/cors.rb
266
253
  - lib/upfluence/http/middleware/handle_exception.rb
@@ -272,6 +259,8 @@ files:
272
259
  - lib/upfluence/mixin/strong_parameters.rb
273
260
  - lib/upfluence/peer.rb
274
261
  - lib/upfluence/pool.rb
262
+ - lib/upfluence/resources.rb
263
+ - lib/upfluence/resources/countries.rb
275
264
  - lib/upfluence/utils.rb
276
265
  - lib/upfluence/utils/http/middleware/null.rb
277
266
  - lib/upfluence/utils/semaphore.rb
@@ -288,7 +277,7 @@ homepage: https://github.com/upfluence/rbutils
288
277
  licenses:
289
278
  - MIT
290
279
  metadata: {}
291
- post_install_message:
280
+ post_install_message:
292
281
  rdoc_options: []
293
282
  require_paths:
294
283
  - lib
@@ -304,7 +293,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
293
  version: '0'
305
294
  requirements: []
306
295
  rubygems_version: 3.0.3
307
- signing_key:
296
+ signing_key:
308
297
  specification_version: 4
309
298
  summary: Upfluence common utils for Ruby projects
310
299
  test_files: []
@@ -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