Class: RestfulClientConfiguration

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

Constant Summary collapse

DEFAULT_TIMEOUT =
10
DEFAULT_RETRIES =
2
DEFAULT_USER_AGENT =
'RestfulClient - https://github.com/AvnerCohen/restful-client'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_folderObject

Returns the value of attribute config_folder.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def config_folder
  @config_folder
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def data
  @data
end

#env_nameObject

Returns the value of attribute env_name.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def env_name
  @env_name
end

#legacy_postfixObject

Returns the value of attribute legacy_postfix.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def legacy_postfix
  @legacy_postfix
end

#report_methodObject

Returns the value of attribute report_method.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def report_method
  @report_method
end

#retriesObject

Returns the value of attribute retries.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def retries
  @retries
end

#timeoutObject

Returns the value of attribute timeout.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def timeout
  @timeout
end

#use_jynxObject

Returns the value of attribute use_jynx.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def use_jynx
  @use_jynx
end

#user_agentObject

Returns the value of attribute user_agent.



4
5
6
# File 'lib/restful_client_configuration.rb', line 4

def user_agent
  @user_agent
end

Instance Method Details

#envObject



50
51
52
# File 'lib/restful_client_configuration.rb', line 50

def env
  @env_name || 'default'
end

#report_onObject

Dummy method to test reporting phase



46
47
48
# File 'lib/restful_client_configuration.rb', line 46

def report_on
  @report_method.call('RestfulClientConfiguration', "Initialized at: #{Time.now.utc}.")
end

#resetObject



36
37
38
39
40
41
42
43
# File 'lib/restful_client_configuration.rb', line 36

def reset
  @report_method  =    nil
  @timeout        =    nil
  @user_agent     =    nil
  @retries        =    nil
  @use_jynx       =    nil
  @legacy_postfix =    nil
end

#run!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restful_client_configuration.rb', line 9

def run!
  fail 'Configuration directory name must be provided' unless config_folder.class.to_s == 'String'
  file_name = ['restful_services.yml', 'rest_api.yml'].each do |name|
    locale_name = File.join(config_folder, name)
    break locale_name if File.file?(locale_name)
  end

  ## Set Default Values
  @report_method ||= proc { |*_args| nil }
  @timeout ||= DEFAULT_TIMEOUT
  @user_agent ||= DEFAULT_USER_AGENT
  @retries ||= DEFAULT_RETRIES
  @legacy_postfix ||= ''
  @use_jynx         = true if @use_jynx.nil?

  @data = YAML.load(ERB.new(File.read(file_name)).result)[env].each do |name, entry|
    next unless entry.key?('url')
    opts = {
      time_window_in_seconds: entry.fetch(:time_window_in_seconds, 20),
      max_errors: entry.fetch(:max_errors, 10),
      grace_period: entry.fetch(:grace_period, 120)
    }

    ServiceJynx.register!(name.gsub(@legacy_postfix, ''), opts) if @use_jynx
  end
end