bonanza 1.7.0

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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +188 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +95 -0
  10. data/Rakefile +9 -0
  11. data/VERSION +1 -0
  12. data/bonanza.gemspec +27 -0
  13. data/config/locales/de.yml +7 -0
  14. data/config/locales/en.yml +7 -0
  15. data/lib/bonanza.rb +5 -0
  16. data/lib/bonanza/call_link_helpers.rb +24 -0
  17. data/lib/bonanza/engine.rb +15 -0
  18. data/lib/bonanza/link_helpers.rb +65 -0
  19. data/lib/bonanza/link_strategies.rb +14 -0
  20. data/lib/bonanza/link_strategies/base.rb +45 -0
  21. data/lib/bonanza/link_strategies/billing.rb +24 -0
  22. data/lib/bonanza/link_strategies/kuba.rb +11 -0
  23. data/lib/bonanza/link_strategies/mojito.rb +11 -0
  24. data/lib/bonanza/link_strategies/ninevirt.rb +11 -0
  25. data/lib/bonanza/link_strategies/openshift.rb +29 -0
  26. data/lib/bonanza/link_strategies/otrs.rb +25 -0
  27. data/lib/bonanza/link_strategies/redmine.rb +11 -0
  28. data/lib/bonanza/link_strategies/stats.rb +17 -0
  29. data/lib/bonanza/link_strategies/wiki.rb +15 -0
  30. data/lib/bonanza/link_strategies/yimin.rb +15 -0
  31. data/lib/bonanza/urn_link_helpers.rb +40 -0
  32. data/lib/bonanza/validators.rb +3 -0
  33. data/lib/bonanza/validators/domain_name_validator.rb +16 -0
  34. data/lib/bonanza/validators/otrs_number_validator.rb +13 -0
  35. data/lib/bonanza/validators/urn_validator.rb +14 -0
  36. data/spec/bonanza/call_link_helpers_spec.rb +50 -0
  37. data/spec/bonanza/link_helpers_spec.rb +126 -0
  38. data/spec/bonanza/link_strategies/base_spec.rb +50 -0
  39. data/spec/bonanza/link_strategies/billing_spec.rb +35 -0
  40. data/spec/bonanza/link_strategies/openshift_spec.rb +26 -0
  41. data/spec/bonanza/urn_link_helpers_spec.rb +80 -0
  42. data/spec/bonanza/validators/domain_name_validator_spec.rb +59 -0
  43. data/spec/bonanza/validators/otrs_number_validator_spec.rb +53 -0
  44. data/spec/bonanza/validators/urn_validator_spec.rb +64 -0
  45. data/spec/spec_helper.rb +90 -0
  46. metadata +182 -0
@@ -0,0 +1,65 @@
1
+ require 'active_support/core_ext'
2
+ require 'action_view/helpers'
3
+ require 'bonanza/link_strategies'
4
+
5
+ module Bonanza
6
+ module LinkHelpers
7
+ include ActionView::Helpers
8
+
9
+ class InvalidAppError < StandardError; end
10
+
11
+ def nine_app_url(app, entity, reference = nil)
12
+ strategy = strategy_from_app_name(app, entity)
13
+ strategy.url_to entity, reference
14
+ end
15
+
16
+ def link_to_otrs_ticket(name, ticket_number, options = {})
17
+ link_to_application :otrs, :ticket, name, ticket_number, options
18
+ end
19
+
20
+ def link_to_otrs_customer(name, account_number, options = {})
21
+ link_to_application :otrs, :customer, name, account_number, options
22
+ end
23
+
24
+ def link_to_billing(name, account_number, options = {})
25
+ link_to_application :billing, :debtor, name, account_number, options
26
+ end
27
+
28
+ def link_to_stats(name, account_number, options = {})
29
+ link_to_application :stats, :customer, name, account_number, options
30
+ end
31
+
32
+ def link_to_redmine(name, redmine_issue, options = {})
33
+ link_to_application :redmine, :issue, name, redmine_issue, options
34
+ end
35
+
36
+ def link_to_kuba_account(name, account_number, options = {})
37
+ link_to_application :kuba, :account, name, account_number, options
38
+ end
39
+
40
+ def link_to_wiki_customer(name, login_name, options = {})
41
+ link_to_application :wiki, :customer, name, login_name, options
42
+ end
43
+
44
+ def link_to_migration_item(name, item_id, options = {})
45
+ link_to_application :yimin, :item, name, item_id, options
46
+ end
47
+
48
+ private
49
+
50
+ def link_to_application(app, entity, name, reference, options = {})
51
+ options.reverse_merge! target: '_blank'
52
+ link_to name, nine_app_url(app, entity, reference), options
53
+ end
54
+
55
+ def strategy_from_app_name(app_name, entity)
56
+ raise(InvalidAppError, "Invalid app #{app_name}") unless valid_app?(app_name)
57
+ Bonanza::LinkStrategies.const_get(app_name.capitalize).new(entity)
58
+ end
59
+
60
+ def valid_app?(app_name)
61
+ return false if app_name.nil?
62
+ Bonanza::LinkStrategies.constants.include?(app_name.capitalize)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ module LinkStrategies
2
+ end
3
+
4
+ require 'bonanza/link_strategies/base'
5
+ require 'bonanza/link_strategies/stats'
6
+ require 'bonanza/link_strategies/otrs'
7
+ require 'bonanza/link_strategies/kuba'
8
+ require 'bonanza/link_strategies/ninevirt'
9
+ require 'bonanza/link_strategies/openshift'
10
+ require 'bonanza/link_strategies/billing'
11
+ require 'bonanza/link_strategies/redmine'
12
+ require 'bonanza/link_strategies/wiki'
13
+ require 'bonanza/link_strategies/yimin'
14
+ require 'bonanza/link_strategies/mojito'
@@ -0,0 +1,45 @@
1
+ require 'active_support/inflector'
2
+ require 'action_view/helpers/output_safety_helper'
3
+
4
+ module Bonanza
5
+ module LinkStrategies
6
+ class Base
7
+ include ActionView::Helpers::OutputSafetyHelper
8
+
9
+ attr_reader :entity
10
+
11
+ def initialize(entity = nil)
12
+ @entity = entity.to_s
13
+ end
14
+
15
+ def app
16
+ self.class.name.demodulize.downcase
17
+ end
18
+
19
+ def paths
20
+ {}
21
+ end
22
+
23
+ def domain
24
+ case environment.to_s
25
+ when 'development'
26
+ "http://#{app}.dev"
27
+ when 'test'
28
+ "http://#{app}.test"
29
+ when 'staging'
30
+ "https://#{app}-staging.nine.ch"
31
+ when 'production'
32
+ "https://#{app}.nine.ch"
33
+ end
34
+ end
35
+
36
+ def url_to(entity, reference = nil)
37
+ raw [domain, paths[entity]].join % { reference: reference }
38
+ end
39
+
40
+ def environment
41
+ ENV.fetch('RAILS_ENV', 'production')
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Billing < Base
4
+ def paths
5
+ {
6
+ debtor: "/DebitorEdit.aspx?DebiNbr=%{reference}",
7
+ }
8
+ end
9
+
10
+ def domain
11
+ case environment.to_s
12
+ when 'development'
13
+ "http://billing.dev"
14
+ when 'test'
15
+ "http://billing.test"
16
+ when 'staging'
17
+ 'http://billing.dev'
18
+ when 'production'
19
+ 'https://billingfw.nine.ch'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Kuba < Base
4
+ def paths
5
+ {
6
+ account: '/accounts/%{reference}',
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Mojito < Base
4
+ def paths
5
+ {
6
+ 'aws:account': '/aws_accounts/%{reference}'
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Ninevirt < Base
4
+ def paths
5
+ {
6
+ virtdomain: "/virt_domains/%{reference}",
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Openshift < Base
4
+ def paths
5
+ {
6
+ :'openshift:project:es34' => '/console/project/%{reference}'
7
+ }
8
+ end
9
+
10
+ def domain
11
+ case environment.to_s
12
+ when 'development'
13
+ 'http://openshift.dev'
14
+ when 'test'
15
+ 'https://openshift.test'
16
+ when 'staging'
17
+ 'https://openshift-staging.nine.ch:8443'
18
+ when 'production'
19
+ case entity
20
+ when 'openshift:project:es34'
21
+ 'https://openshift.nine.ch:8443'
22
+ else
23
+ 'https://aws-openshift.nine.ch:8443'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Otrs < Base
4
+ def paths
5
+ {
6
+ customer: "/index.pl?Action=AgentCustomerInformationCenter;CustomerID=%{reference}",
7
+ ticket: "/index.pl?Action=AgentTicketZoom;TicketNumber=%{reference}",
8
+ }
9
+ end
10
+
11
+ def domain
12
+ case environment.to_s
13
+ when 'development'
14
+ 'http://otrs.nine.ch/otrs'
15
+ when 'test'
16
+ 'http://otrs.test'
17
+ when 'staging'
18
+ 'https://otrs-staging.nine.ch/otrs'
19
+ when 'production'
20
+ 'https://otrs.nine.ch/otrs'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Redmine < Base
4
+ def paths
5
+ {
6
+ issue: '/issues/%{reference}',
7
+ }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Stats < Base
4
+ def paths
5
+ {
6
+ customer: '/admin/customerdetail.php?id=%{reference}',
7
+ server: '/admin/changeserver.php?id=%{reference}',
8
+ vserver: '/admin/vserverdetail.php?id=%{reference}',
9
+ rack: '/admin/changerack.php?id=%{reference}',
10
+ port: '/admin/changeport.php?id=%{reference}',
11
+ :'ip:v4:subnet' => '/admin/changesubnet.php?version=4&id=%{reference}',
12
+ :'ip:v6:subnet' => '/admin/changesubnet.php?version=6&id=%{reference}',
13
+ }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Wiki < Base
4
+ def paths
5
+ {
6
+ customer: '/display/CUS/%{reference}',
7
+ }
8
+ end
9
+
10
+ def domain
11
+ 'https://wiki.nine.ch'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Bonanza
2
+ module LinkStrategies
3
+ class Yimin < Base
4
+ def paths
5
+ {
6
+ item: '/items/%{reference}',
7
+ }
8
+ end
9
+
10
+ def domain
11
+ 'https://migration.nine.ch'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext'
4
+ require 'action_view/helpers'
5
+
6
+ require 'bonanza/link_helpers'
7
+
8
+ module Bonanza
9
+ module UrnLinkHelpers
10
+ include Bonanza::LinkHelpers
11
+
12
+ URN_APPS = {
13
+ server: :stats,
14
+ vserver: :stats,
15
+ virtdomain: :ninevirt,
16
+ rack: :stats,
17
+ port: :stats,
18
+ :'ip:v4:subnet' => :stats,
19
+ :'ip:v6:subnet' => :stats,
20
+ :'aws:account' => :mojito,
21
+ :'openshift:project:es34' => :openshift
22
+ }.freeze
23
+
24
+ def link_to_urn(name, urn, options = {})
25
+ urn = ::InfrastructureClient::Urn.new(urn) unless urn.is_a?(::InfrastructureClient::Urn)
26
+ name = urn.short if name.blank?
27
+
28
+ url = urn.valid? ? url_for_urn(urn) : ''
29
+
30
+ options.reverse_merge! target: '_blank'
31
+ link_to name, url, options
32
+ end
33
+
34
+ def url_for_urn(urn)
35
+ nine_app_url URN_APPS[urn.type.to_sym], urn.type.to_sym, urn.id
36
+ rescue Bonanza::LinkHelpers::InvalidAppError
37
+ ''
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ require 'bonanza/validators/otrs_number_validator'
2
+ require 'bonanza/validators/domain_name_validator'
3
+ require 'bonanza/validators/urn_validator'
@@ -0,0 +1,16 @@
1
+ require 'active_model'
2
+ require 'simpleidn'
3
+
4
+ module Bonanza
5
+ module Validators
6
+ class DomainNameValidator < ActiveModel::EachValidator
7
+ DOMAIN_REGEX = /^((?=[a-z0-9-]{1,63}\.)(xn--(--)?)?[a-z0-9]+(-[a-z0-9]+)*\.)+((xn--)?[a-z0-9]{2,63})$/
8
+
9
+ def validate_each(record, attribute, value)
10
+ punycode = SimpleIDN.to_ascii(value.to_s)
11
+ return true if punycode =~ DOMAIN_REGEX
12
+ record.errors.add attribute, :not_a_valid_domain_name
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_model'
2
+
3
+ module Bonanza
4
+ module Validators
5
+ class OtrsNumberValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ unless value.to_s =~ /^\d{16}$/i
8
+ record.errors.add attribute, :not_an_otrs_ticket
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_model'
2
+
3
+ module Bonanza
4
+ module Validators
5
+ class UrnValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ return unless value.present? if options[:allow_empty]
8
+ unless InfrastructureClient::Urn.new(value.to_s).valid?
9
+ record.errors.add attribute, :invalid_urn
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'bonanza/call_link_helpers'
3
+
4
+ class FakeCallLinkHelpersClass
5
+ include Bonanza::CallLinkHelpers
6
+ end
7
+
8
+ RSpec.describe FakeCallLinkHelpersClass do
9
+ subject { described_class.new }
10
+
11
+ describe '#call_to' do
12
+ subject { super().call_to(phone_number) }
13
+
14
+ context 'valid phone number' do
15
+ let(:phone_number) { '+41123456789' }
16
+
17
+ it 'generates a link to the phone call service' do
18
+ expect(subject).to eq(
19
+ '<a href="http://pbx.nine.ch/call.php?ext=0041123456789">+41123456789</a>'
20
+ )
21
+ end
22
+
23
+ it 'replaces the + sign with 00' do
24
+ expect(subject).to include '0041123456789'
25
+ end
26
+ end
27
+
28
+ context 'phone number with unnecessary spaces' do
29
+ let(:phone_number) { '+41 12 34 56 78 91' }
30
+
31
+ it 'removes the unnecessary spaces' do
32
+ expect(subject.to_s).to include '0041123456789'
33
+ end
34
+ end
35
+
36
+ describe 'invalid phone numbers' do
37
+ context 'empty phone number' do
38
+ let(:phone_number) { '' }
39
+
40
+ specify { expect(subject).to eq '' }
41
+ end
42
+
43
+ context 'nil phone number' do
44
+ let(:phone_number) { nil }
45
+
46
+ specify { expect(subject).to eq '' }
47
+ end
48
+ end
49
+ end
50
+ end