Class: WssAgent::Configure

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Defined in:
lib/wss_agent/configure.rb

Constant Summary collapse

DEFAULT_CONFIG_FILE =
'default.yml'.freeze
CUSTOM_DEFAULT_CONFIG_FILE =
'custom_default.yml'.freeze
CURRENT_CONFIG_FILE =
'wss_agent.yml'.freeze
API_PATH =
'/agent'.freeze

Class Method Summary collapse

Class Method Details

.api_pathObject



74
75
76
77
78
# File 'lib/wss_agent/configure.rb', line 74

def api_path
  @uri = uri
  @url_path = @uri.path
  @url_path == '' ? API_PATH : @url_path
end

.coordinatesObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/wss_agent/configure.rb', line 101

def coordinates
  return {} unless current['project_token'].to_s.strip.empty?
  coordinates_config = current['coordinates']
  coordinates_artifact_id = coordinates_config['artifact_id']
  coordinates_version = coordinates_config['version']
  if coordinates_artifact_id.to_s.strip.empty?
    coordinates_artifact_id = project_meta.project_name
    coordinates_version = project_meta.project_version
  end
  { 'artifactId' => coordinates_artifact_id,
    'version' => coordinates_version }
end

.currentObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wss_agent/configure.rb', line 37

def current
  unless File.exist?(current_path)
    return raise NotFoundConfigFile, WssAgentError::NOT_FOUND_CONFIGFILE
  end

  @current_config = Psych.safe_load(File.read(current_path))

  unless @current_config
    return raise InvalidConfigFile, WssAgentError::INVALID_CONFIG_FORMAT
  end

  default.merge(@current_config)
end

.current_pathObject



33
34
35
# File 'lib/wss_agent/configure.rb', line 33

def current_path
  Bundler.root.join(CURRENT_CONFIG_FILE).to_s
end

.custom_default_pathObject



18
19
20
21
22
23
# File 'lib/wss_agent/configure.rb', line 18

def custom_default_path
  File.join(
    File.expand_path('../..', __FILE__), 'config',
    CUSTOM_DEFAULT_CONFIG_FILE
  )
end

.defaultObject



29
30
31
# File 'lib/wss_agent/configure.rb', line 29

def default
  exist_default_config? ? Psych.safe_load(File.read(default_path)) : {}
end

.default_pathObject



12
13
14
15
16
# File 'lib/wss_agent/configure.rb', line 12

def default_path
  File.join(
    File.expand_path('../..', __FILE__), 'config', DEFAULT_CONFIG_FILE
  )
end

.exist_default_config?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/wss_agent/configure.rb', line 25

def exist_default_config?
  File.exist?(default_path)
end

.portObject



61
62
63
# File 'lib/wss_agent/configure.rb', line 61

def port
  uri.port || 80
end

.project_metaObject



97
98
99
# File 'lib/wss_agent/configure.rb', line 97

def project_meta
  @project_meta ||= WssAgent::Project.new
end

.ssl?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/wss_agent/configure.rb', line 70

def ssl?
  uri.scheme == 'https'
end

.tokenObject



80
81
82
83
84
85
86
87
# File 'lib/wss_agent/configure.rb', line 80

def token
  if current['token'].nil? || (current['token'] == '') ||
     (current['token'] == default['token'])
    raise TokenNotFound, WssAgentError::CANNOT_FIND_TOKEN
  else
    current['token']
  end
end

.uriObject



51
52
53
54
55
56
57
58
59
# File 'lib/wss_agent/configure.rb', line 51

def uri
  @url = current['url']
  if @url.nil? || @url == ''
    raise ApiUrlNotFound, WssAgentError::CANNOT_FIND_URL
  end
  URI(@url)
rescue URI::Error
  raise ApiUrlInvalid, WssAgentError::URL_INVALID
end

.urlObject



65
66
67
68
# File 'lib/wss_agent/configure.rb', line 65

def url
  @uri = uri
  [@uri.scheme, @uri.host].join('://')
end

.user_keyObject



89
90
91
# File 'lib/wss_agent/configure.rb', line 89

def user_key
  current['user_key'].to_s.strip
end

.user_key?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/wss_agent/configure.rb', line 93

def user_key?
  !user_key.empty?
end