informant-common 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3a0530b6e7f368bf9813089d13ae91e6efea52bb86e0aab43b28642988a8ec21
4
+ data.tar.gz: 8e485234656bb89473bdbf7d4ae3d5a25652dc2d86440c96f4f801ce4bc8e5ef
5
+ SHA512:
6
+ metadata.gz: b9c4e2eea975a321d588311fb80fa663e409d787fd5c44bef40ba5e8bceff28ae0c9e0219754410979a08122323d4544b1fb2b3c05a870809c25fb94ab18f38c
7
+ data.tar.gz: 349acd7e71ae351263323dcb7da547ccf8614d338df22466a3d583f98c831eefead70815ac5b226446b02d1cd66190c822aa16ef13e07820908d2fa428e94064
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2016 Informant, LLC
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # Overview
2
+
3
+ ## Informant Common
4
+
5
+ The informant-common gem provides underlying common functionality for building
6
+ a framework-specific client in Ruby.
7
+
8
+ Please see [informant-rails](https://gitlab.com/informantapp/informant-rails) for the official Ruby on Rails client.
9
+
10
+ [![Homepage](https://s3.amazonaws.com/assets.heroku.com/addons.heroku.com/icons/1347/original.png)](https://www.informantapp.com)
11
+
12
+ ## Compatibility
13
+
14
+ The Informant officially supports Ruby 2.5+.
15
+
16
+ [![pipeline status](https://gitlab.com/informantapp/informant-common/badges/master/pipeline.svg)](https://gitlab.com/informantapp/informant-common/-/commits/master)
@@ -0,0 +1,9 @@
1
+ if !ENV['APPRAISAL_INITIALIZED']
2
+ require 'appraisal/task'
3
+ Appraisal::Task.new
4
+ task default: :appraisal
5
+ else
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new
8
+ task default: :spec
9
+ end
@@ -0,0 +1,26 @@
1
+ require 'net/http'
2
+
3
+ if defined?(Rake)
4
+ require 'rake'
5
+ Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each { |rake| load rake }
6
+ end
7
+
8
+ module InformantCommon
9
+ autoload :Client, 'informant-common/client'
10
+ autoload :Config, 'informant-common/config'
11
+ autoload :FieldError, 'informant-common/field_error'
12
+ autoload :ParameterFilter, 'informant-common/parameter_filter'
13
+ autoload :ValidationTracking, 'informant-common/validation_tracking'
14
+
15
+ module Model
16
+ autoload :ActiveModel, 'informant-common/model/active_model'
17
+ autoload :Base, 'informant-common/model/base'
18
+ end
19
+
20
+ module Event
21
+ autoload :Base, 'informant-common/event/base'
22
+ autoload :AgentInfo, 'informant-common/event/agent_info'
23
+ autoload :FormSubmission, 'informant-common/event/form_submission'
24
+ autoload :MockFormSubmission, 'informant-common/event/mock_form_submission'
25
+ end
26
+ end
@@ -0,0 +1,53 @@
1
+ module InformantCommon
2
+ class Client
3
+ SUPPORTED_REQUEST_METHODS = %w[POST PATCH PUT DELETE].freeze
4
+
5
+ def self.enabled?
6
+ @enabled
7
+ end
8
+
9
+ def self.disable!
10
+ @enabled = false
11
+ end
12
+
13
+ def self.enable!
14
+ @enabled = true
15
+ end
16
+ enable!
17
+
18
+ def self.start_transaction!(request_method)
19
+ if enabled? && SUPPORTED_REQUEST_METHODS.include?(request_method)
20
+ Thread.current[:informant_transaction] = InformantCommon::Event::FormSubmission.new
21
+ else
22
+ reset_transaction!
23
+ end
24
+ end
25
+
26
+ def self.current_transaction
27
+ Thread.current[:informant_transaction] ||= InformantCommon::Event::MockFormSubmission.new
28
+ end
29
+
30
+ def self.reset_transaction!
31
+ Thread.current[:informant_transaction] = nil
32
+ end
33
+
34
+ def self.process
35
+ if current_transaction.valid?
36
+ this_transaction = current_transaction
37
+ transmit(this_transaction)
38
+ end
39
+ ensure
40
+ reset_transaction!
41
+ end
42
+
43
+ def self.transmit(event)
44
+ Thread.new(Client) do |client_class|
45
+ Net::HTTP.start(*event.net_http_start_arguments) do |http|
46
+ response = http.request(event.post_request)
47
+ client_class.disable! if response.code == '401'
48
+ response
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,62 @@
1
+ module InformantCommon
2
+ class Config
3
+ def self.instance
4
+ @instance ||= InformantCommon::Config.new
5
+ end
6
+
7
+ attr_accessor :api_token
8
+
9
+ attr_writer :client_identifier
10
+ def client_identifier
11
+ @client_identifier ||= "informant-common-#{InformantCommon::VERSION}"
12
+ end
13
+ alias agent_identifier client_identifier
14
+
15
+ def enabled?
16
+ !api_token.nil? && api_token != ''
17
+ end
18
+
19
+ attr_writer :exclude_models
20
+ def exclude_models
21
+ @exclude_models ||= []
22
+ end
23
+
24
+ attr_writer :filter_parameters
25
+ def filter_parameters
26
+ @filter_parameters ||= []
27
+ end
28
+
29
+ attr_writer :value_tracking
30
+ def value_tracking?
31
+ if !defined?(@value_tracking)
32
+ @value_tracking = true
33
+ else
34
+ @value_tracking
35
+ end
36
+ end
37
+
38
+ def self.collector_host
39
+ @collector_host ||= ENV['INFORMANT_COLLECTOR_HOST'] || 'https://collector-api.informantapp.com'
40
+ end
41
+
42
+ def self.configure
43
+ yield instance
44
+ end
45
+
46
+ def self.method_missing(name, *args)
47
+ if instance.respond_to?(name)
48
+ instance.public_send(name, *args)
49
+ else
50
+ super
51
+ end
52
+ end
53
+
54
+ def self.respond_to_missing?(name)
55
+ instance.respond_to?(name)
56
+ end
57
+
58
+ def self.reset!
59
+ @instance = nil
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ module InformantCommon
2
+ module Event
3
+ class AgentInfo < InformantCommon::Event::Base
4
+ attr_accessor :agent_identifier, :framework_version
5
+
6
+ def initialize(agent_identifier: InformantCommon::Config.client_identifier, framework_version: nil)
7
+ self.agent_identifier = agent_identifier
8
+ self.framework_version = framework_version
9
+ end
10
+
11
+ def as_json(*_args)
12
+ {
13
+ agent_identifier: agent_identifier || InformantCommon::Config.client_identifier,
14
+ framework_version: framework_version,
15
+ runtime_version: "ruby-#{RUBY_VERSION}",
16
+ mongoid_version: defined?(Mongoid) ? Mongoid::VERSION : nil
17
+ }
18
+ end
19
+
20
+ def self.endpoint
21
+ @endpoint ||= URI("#{InformantCommon::Config.collector_host}/v2/client-info")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module InformantCommon
2
+ module Event
3
+ class Base
4
+ def self.endpoint
5
+ raise 'Must implement'
6
+ end
7
+
8
+ def endpoint
9
+ self.class.endpoint
10
+ end
11
+
12
+ def self.authorization_header_value
13
+ @authorization_header_value ||= "Token token=\"#{InformantCommon::Config.api_token}\""
14
+ end
15
+
16
+ def authorization_header_value
17
+ self.class.authorization_header_value
18
+ end
19
+
20
+ def post_request
21
+ Net::HTTP::Post.new(endpoint,
22
+ 'Authorization' => authorization_header_value,
23
+ 'Content-Type' => 'application/json').tap { |r| r.body = to_json }
24
+ end
25
+
26
+ def self.net_http_start_arguments
27
+ @net_http_start_arguments ||= [endpoint.host, endpoint.port, use_ssl: endpoint.scheme == 'https']
28
+ end
29
+
30
+ def net_http_start_arguments
31
+ self.class.net_http_start_arguments
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module InformantCommon
2
+ module Event
3
+ class FormSubmission < InformantCommon::Event::Base
4
+ attr_accessor :handler
5
+
6
+ def process_model(model)
7
+ return if model.nil? || Config.exclude_models.include?(model.name)
8
+
9
+ models.reject! { |container| container.id == model.id }
10
+ models << model
11
+ end
12
+
13
+ def models
14
+ @models ||= []
15
+ end
16
+
17
+ def valid?
18
+ models.any?
19
+ end
20
+
21
+ def as_json(*_args)
22
+ {
23
+ name: handler,
24
+ models: models.map(&:as_json)
25
+ }
26
+ end
27
+
28
+ def to_json(*_args)
29
+ as_json.to_json
30
+ end
31
+
32
+ def self.endpoint
33
+ @endpoint ||= URI("#{InformantCommon::Config.collector_host}/v2/form-submissions")
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ module InformantCommon
2
+ module Event
3
+ class MockFormSubmission < InformantCommon::Event::FormSubmission
4
+ def process_model(_model); end
5
+
6
+ def models
7
+ []
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module InformantCommon
2
+ class FieldError
3
+ attr_accessor :name, :message
4
+ attr_reader :value
5
+
6
+ def initialize(name, value, message = nil)
7
+ self.name = name
8
+ self.value = value
9
+ self.message = message
10
+ end
11
+
12
+ def value=(value)
13
+ @value = InformantCommon::ParameterFilter.filter(name, value)
14
+ end
15
+
16
+ def as_json(*_args)
17
+ { name: name, value: value, message: message }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module InformantCommon
2
+ module Model
3
+ class ActiveModel < Base
4
+ def initialize(active_model)
5
+ self.id = active_model.object_id
6
+ self.name = active_model.class.name
7
+ self.errors = []
8
+ active_model.errors.each do |field, error|
9
+ add_error(field.to_s, extract_value(active_model, field), error)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def extract_value(active_model, field)
16
+ active_model.public_send(field)
17
+ rescue StandardError
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ module InformantCommon
2
+ module Model
3
+ class Base
4
+ attr_accessor :id, :name
5
+ attr_writer :errors
6
+
7
+ def initialize(id: nil, name: nil, field_errors: [])
8
+ self.id = id
9
+ self.name = name
10
+ self.errors = field_errors
11
+ end
12
+
13
+ def errors
14
+ @errors ||= []
15
+ end
16
+
17
+ def add_error(field_name, value, message)
18
+ errors << FieldError.new(field_name, value, message)
19
+ end
20
+
21
+ def as_json(*_args)
22
+ { name: name, errors: errors.map(&:as_json) }
23
+ end
24
+
25
+ def to_json(*_args)
26
+ as_json.to_json
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module InformantCommon
2
+ class ParameterFilter
3
+ def self.filter(name, value)
4
+ Config.value_tracking? && !matcher.match(name) ? value : '[FILTERED]'
5
+ end
6
+
7
+ def self.matcher
8
+ @matcher ||= Regexp.new(
9
+ if Config.filter_parameters.any?
10
+ /#{Config.filter_parameters.join('|')}/
11
+ else
12
+ /$^/
13
+ end
14
+ )
15
+ end
16
+
17
+ def self.reset!
18
+ @matcher = nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module InformantCommon
2
+ module ValidationTracking
3
+ if defined?(ActiveSupport)
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ set_callback(:validate, :after) do
8
+ InformantCommon::Client.current_transaction.process_model(
9
+ InformantCommon::Model::ActiveModel.new(self)
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module InformantCommon
2
+ VERSION = '1.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: informant-common
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Informant, LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem contains the common components for reporting data to the Informant.
14
+ email:
15
+ - support@informantapp.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.markdown
22
+ - Rakefile
23
+ - lib/informant-common.rb
24
+ - lib/informant-common/client.rb
25
+ - lib/informant-common/config.rb
26
+ - lib/informant-common/event/agent_info.rb
27
+ - lib/informant-common/event/base.rb
28
+ - lib/informant-common/event/form_submission.rb
29
+ - lib/informant-common/event/mock_form_submission.rb
30
+ - lib/informant-common/field_error.rb
31
+ - lib/informant-common/model/active_model.rb
32
+ - lib/informant-common/model/base.rb
33
+ - lib/informant-common/parameter_filter.rb
34
+ - lib/informant-common/validation_tracking.rb
35
+ - lib/informant-common/version.rb
36
+ homepage: https://www.informantapp.com
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: The Informant tracks server-side validation errors and gives you metrics
59
+ you never dreamed of.
60
+ test_files: []