Class: Wovnrb::Store

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wovnrb/store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



38
39
40
41
42
# File 'lib/wovnrb/store.rb', line 38

def initialize
  @settings = Settings.new
  @config_loaded = false
  reset
end

Class Method Details

.default_settingsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wovnrb/store.rb', line 14

def self.default_settings
  Settings.new.merge({
    'project_token' => '',
    'log_path' => 'log/wovn_error.log',
    'ignore_paths' => [],
    'ignore_globs' => [],
    'url_pattern' => 'path',
    'url_pattern_reg' => "/(?<lang>[^/.?]+)",
    'query' => [],
    'ignore_class' => [],
    'api_url' => 'https://api.wovn.io/v0/values',
    'api_timeout_seconds' => 0.5,
    'default_lang' => 'en',
    'supported_langs' => ['en'],
    'test_mode' => false,
    'test_url' => '',
    'cache_megabytes' => nil,
    'ttl_seconds' => nil,
    'use_proxy' => false,  # use env['HTTP_X_FORWARDED_HOST'] instead of env['HTTP_HOST'] and env['SERVER_NAME'] when this setting is true.
    'custom_lang_aliases' => {},
    'wovn_dev_mode' => false
  })
end

Instance Method Details

#resetnil

Reset @settings and @config_loaded variables to default.

Returns:

  • (nil)


47
48
49
50
51
# File 'lib/wovnrb/store.rb', line 47

def reset
  @settings = Store.default_settings
  # When Store is initialized, the Rails.configuration object is not yet initialized
  @config_loaded = false
end

#settings(*opts) ⇒ Hash

Returns the settings object, pulling from Rails config the first time this is called

Returns:

  • (Hash)

    The settings which are pulled from the config file given by the user and filled in by defaults



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/wovnrb/store.rb', line 115

def settings(*opts)
  if !opts.first.nil?
    @settings.merge!(opts.first)
    @config_loaded = false
  end

  if @config_loaded
    return @settings
  end

  # get Rails config.wovnrb
  if Object.const_defined?('Rails') && Rails.configuration.respond_to?(:wovnrb)
    config_settings = Rails.configuration.wovnrb.stringify_keys
    if config_settings.has_key?('url_pattern')
      if config_settings['url_pattern'] == 'query' || config_settings['url_pattern'] == 'subdomain' || config_settings['url_pattern'] == 'path'
        config_settings['url_pattern'] = config_settings['url_pattern']
        config_settings.delete('url_pattern')
      end
    end
    @settings.merge!(Rails.configuration.wovnrb.stringify_keys)
  end

  # fix settings object
  @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
  if !@settings.has_key?('supported_langs')
    @settings['supported_langs'] = [@settings['default_lang']]
  end

  if @settings.has_key?('user_token') && @settings['project_token'].empty?
    @settings['project_token'] = @settings['user_token']
  end
  @settings.delete('user_token')

  if @settings['url_pattern'] == 'path'
    @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
  elsif @settings['url_pattern'] == 'query'
    @settings['url_pattern_reg'] = "((\\?.*&)|\\?)wovn=(?<lang>[^&]+)(&|$)"
  elsif @settings['url_pattern'] == 'subdomain'
    @settings['url_pattern_reg'] = "^(?<lang>[^.]+)\."
  end

  if @settings['test_mode'] != true || @settings['test_mode'] != 'on'
    @settings['test_mode'] = false
  else
    @settings['test_mode'] = true
  end

  if @settings.has_key?('custom_lang_aliases')
    stringify_keys! @settings['custom_lang_aliases']
  end

  if wovn_dev_mode? && @settings['api_url'] == Store.default_settings['api_url']
    @settings['api_url'] = "#{wovn_protocol}://api.#{wovn_host}/v0/values"
  end

  @config_loaded = true
  @settings
end

#valid_settings?Boolean

Returns true or false based on whether the settings are valid or not, logs any invalid settings to ../error.log

Returns:

  • (Boolean)

    Returns true if the settings are valid, and false if they are not



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wovnrb/store.rb', line 63

def valid_settings?
  valid = true
  errors = [];
  #if valid_token?(!settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
  if !valid_token?(settings['project_token'])
    valid = false
    errors.push("Project token #{settings['project_token']} is not valid.")
  end
  if settings.has_key?('ignore_paths') && !settings['ignore_paths'].kind_of?(Array)
    valid = false
    errors.push("Ignore Paths #{settings['ignore_paths']} should be Array.")
  end
  if !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
    valid = false
    errors.push("Url pattern #{settings['url_pattern']} is not valid.")
  end
  if !settings.has_key?('query') || !settings['query'].kind_of?(Array)
    valid = false
    errors.push("query config #{settings['query']} is not valid.")
  end
  if !settings.has_key?('ignore_class') || !settings['ignore_class'].kind_of?(Array)
    valid = false
    errors.push("ignore_class config #{settings['ignore_class']} should be Array.")
  end
  if !settings.has_key?('api_url') || settings['api_url'].length == 0
    valid = false
    errors.push("API URL is not configured.")
  end
  if !settings.has_key?('default_lang') || settings['default_lang'].length == 0
    valid = false
    errors.push("Default lang #{settings['default_lang']} is not valid.")
  end
  if !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
    valid = false
    errors.push("Supported langs configuration is not valid.")
  end
  if !settings.has_key?('custom_lang_aliases') || !settings['custom_lang_aliases'].kind_of?(Hash)
    valid = false
    errors.push("Custom lang aliases is not valid.")
  end
  # log errors
  if errors.length > 0
    errors.each do |e|
      WovnLogger.instance.error(e)
    end
  end
  return valid
end

#valid_token?(token) ⇒ Boolean

Returns true or false based on whether the token is valid or not

Returns:

  • (Boolean)

    Returns true if the token is valid, and false if it is not



56
57
58
# File 'lib/wovnrb/store.rb', line 56

def valid_token?(token)
  return !token.nil? && (token.length == 5 || token.length == 6)
end

#wovn_dev_mode?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/wovnrb/store.rb', line 174

def wovn_dev_mode?
  @settings['wovn_dev_mode']
end

#wovn_hostObject



182
183
184
# File 'lib/wovnrb/store.rb', line 182

def wovn_host
  wovn_dev_mode? ? 'dev-wovn.io:3000' : 'wovn.io'
end

#wovn_protocolObject



178
179
180
# File 'lib/wovnrb/store.rb', line 178

def wovn_protocol
  wovn_dev_mode? ? 'http' : 'https'
end