Class: Gorynich::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/gorynich/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Config

Create instance of config

Parameters:

  • fetcher (Fetcher)

    data loader



15
16
17
18
19
20
21
# File 'lib/gorynich/config.rb', line 15

def initialize(**opts)
  @default = 'default'
  @fetcher = opts.fetch(:fetcher, Fetcher.new)
  @mx = Mutex.new

  actualize
end

Instance Attribute Details

#databasesObject (readonly)

Returns the value of attribute databases.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def databases
  @databases
end

#defaultObject (readonly)

Returns the value of attribute default.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def default
  @default
end

#fetcherObject (readonly)

Returns the value of attribute fetcher.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def fetcher
  @fetcher
end

#hostsObject (readonly)

Returns the value of attribute hosts.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def hosts
  @hosts
end

#tenantsObject (readonly)

Returns the value of attribute tenants.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def tenants
  @tenants
end

#urisObject (readonly)

Returns the value of attribute uris.



3
4
5
# File 'lib/gorynich/config.rb', line 3

def uris
  @uris
end

Instance Method Details

#actualizeObject

Update configs from data source



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gorynich/config.rb', line 26

def actualize
  cfg = fetcher.fetch.fetch(Rails.env)

  @mx.synchronize do
    @tenants = tenants_from_config(cfg)
    @databases = databases_from_config(cfg)
    @secrets = secrets_from_config(cfg)
    @uris = uris_from_config(@secrets)
    @hosts = hosts_from_config(@secrets)
  end
end

#config(tenant) ⇒ Hash

Full config from data source by tenant

Parameters:

  • tenant (String, Symbol)

Returns:

  • (Hash)


119
120
121
122
123
124
125
# File 'lib/gorynich/config.rb', line 119

def config(tenant)
  {
    tenant: tenant.to_s,
    database: database(tenant),
    secrets: secrets(tenant)
  }
end

#connects_to_configHash

For connection to ActiveRecord

Returns:

  • (Hash)


169
170
171
172
173
174
# File 'lib/gorynich/config.rb', line 169

def connects_to_config
  actualize
  tenants.each_with_object({ default: :default }) do |tenant, cfg|
    cfg[tenant.to_sym] = tenant.to_sym
  end
end

#database(tenant) ⇒ Hash

Database config

Parameters:

  • tenant (String, Symbol)

Returns:

  • (Hash)


45
46
47
48
49
# File 'lib/gorynich/config.rb', line 45

def database(tenant)
  databases.fetch(tenant.to_s)
rescue StandardError
  raise TenantNotFound, tenant
end

#database_config(env = nil, fail_ignore: false, dummy_value: nil) ⇒ String

Database config for database.yml If used <%= Gorynich.instance.database_config(your_enviroment) %> without gorynich config it will raise an error. You can use <%= Gorynich.instance.database_config(your_enviroment, fail_ignore: true) %> if there is no configuration for the environment and dummy_value depending on whether you have a configuration in the database.yml under this environment (dummy_value: nil when there is and dummy_value: {} if there is not. Default dummy_value is nil)

Parameters:

  • env (String) (defaults to: nil)

    enviroment

  • fail_ignore (Boolean) (defaults to: false)

    ignore if there is no configuration for the environment

  • dummy_value (nil or Hash) (defaults to: nil)

    value for YAML if there is no config.

Returns:

  • (String)

    yaml result



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gorynich/config.rb', line 139

def database_config(env = nil, fail_ignore: false, dummy_value: nil)
  envs = Dir.glob(Rails.root.join('config/environments/*.rb').to_s).map { |f| File.basename(f, '.rb') }
  cfg = fetcher.fetch.extract!(*envs)

  result =
    if env.nil?
      cfg.to_h do |cfg_env, tenant_cfg|
        [
          cfg_env,
          configs_sort(tenant_cfg).to_h { |t, c| [t, c.fetch('db_config')] }
        ]
      end
    else
      if fail_ignore && cfg[env].nil?
        { env => dummy_value }
      else
        {
          env => configs_sort(cfg.fetch(env)).to_h { |t, c| [t, c.fetch('db_config')] }
        }
      end
    end

  result.to_yaml.gsub(/^---/, '')
end

#tenant_by_host(host) ⇒ String

Find tenant by host

Parameters:

  • host (String)

Returns:

  • (String)

Raises:



87
88
89
90
91
92
# File 'lib/gorynich/config.rb', line 87

def tenant_by_host(host)
  tenant = hosts.select { |t, h| t if h.include?(host) }.keys.first
  raise HostNotFound, host if tenant.nil?

  tenant
end

#tenant_by_uri(uri) ⇒ String

Find tenant by URI

Parameters:

  • uri (String)

Returns:

  • (String)

Raises:



69
70
71
72
73
74
75
76
77
78
# File 'lib/gorynich/config.rb', line 69

def tenant_by_uri(uri)
  uri = URI(uri)
  search_tenant = uris.select do |tenant, tenant_uris|
    tenant if tenant_uris.map { |t_uri| URI(t_uri) }.include?(uri)
  end.keys.first

  raise UriNotFound, uri.host if search_tenant.nil?

  search_tenant
end

#uri_by_host(host, tenant = nil) ⇒ String

Find URI by host

Parameters:

  • host (String)
  • tenant (String, Symbol) (defaults to: nil)

    tenant of config (optional)

Returns:

  • (String)

Raises:



102
103
104
105
106
107
108
109
110
# File 'lib/gorynich/config.rb', line 102

def uri_by_host(host, tenant = nil)
  tenant ||= tenant_by_host(host)
  tenant_uris = uris(tenant)
  search_uri = tenant_uris.select { |uri| uri.include?(host) }.first

  raise UriNotFound, search_uri.host if search_uri.nil?

  search_uri
end