class SocialSnippet::Config

Constants

ENV_FIELDS
ENV_FLAGS

use “true” / “false”

ENV_PREFIX

prefix of environment variables

Attributes

core[R]
fields[R]
home[R]

Public Class Methods

new(new_core, options = {}) click to toggle source

Constructor

# File lib/social_snippet/config.rb, line 27
def initialize(new_core, options = {})
  @core = new_core
  @fields = {}
  resolve_home

  # env vars > args > config.json
  init_filesystem
  load_file
  load_options options
  load_environment_variables

  # set default values
  set_default :sspm_host, "sspm.herokuapp.com"
  set_default :sspm_version, "v0"
  set_default :sspm_protocol, "https"

  ENV_FIELDS.each do |field_name|
    key = "@#{field_name.to_s}".to_sym
    instance_variable_set key, fields[field_name]
  end

  save_file
end

Public Instance Methods

debug?() click to toggle source
# File lib/social_snippet/config.rb, line 125
def debug?
  get :debug
end
document_path() click to toggle source
# File lib/social_snippet/config.rb, line 95
def document_path
  ::File.join home, "documents.yml"
end
file_path() click to toggle source
# File lib/social_snippet/config.rb, line 105
def file_path
  ::File.join home, "config.json"
end
get(key) click to toggle source
# File lib/social_snippet/config.rb, line 68
def get(key)
  key = normalize_key(key)
  fields[key]
end
init_filesystem() click to toggle source
# File lib/social_snippet/config.rb, line 129
def init_filesystem
  core.storage.mkdir_p home
  core.storage.mkdir_p install_path
  core.storage.mkdir_p repository_cache_path
  core.storage.write file_path, {}.to_json unless core.storage.exists?(file_path)
end
install_path() click to toggle source
# File lib/social_snippet/config.rb, line 117
def install_path
  ::File.join home, "repo"
end
installed_repos_file() click to toggle source
# File lib/social_snippet/config.rb, line 113
def installed_repos_file
  ::File.join home, "installed_repos.yml"
end
load_file() click to toggle source
# File lib/social_snippet/config.rb, line 78
def load_file
  begin
    @fields = ::JSON.parse(core.storage.read file_path)
  rescue ::JSON::ParserError
    raise "error on parsing #{file_path}"
  end
end
package_path(repo_name, rev_hash, path = nil) click to toggle source
# File lib/social_snippet/config.rb, line 99
def package_path(repo_name, rev_hash, path = nil)
  path = "" if path.nil?
  raise "ERROR" if rev_hash.nil?
  ::File.join home, "packages", repo_name, rev_hash, path
end
repository_cache_path() click to toggle source
# File lib/social_snippet/config.rb, line 109
def repository_cache_path
  ::File.join home, "repo_cache"
end
save_file() click to toggle source
# File lib/social_snippet/config.rb, line 73
def save_file
  @fields ||= {}
  core.storage.write file_path, fields.to_json
end
set(key, value) click to toggle source

Set value

# File lib/social_snippet/config.rb, line 57
def set(key, value)
  key = normalize_key(key)
  fields[key] = value
end
set!(key, value) click to toggle source

Set value and save to file

# File lib/social_snippet/config.rb, line 63
def set!(key, value)
  set(key, value)
  save_file
end
set_default(key, value) click to toggle source
# File lib/social_snippet/config.rb, line 51
def set_default(key, value)
  key = normalize_key(key)
  fields[key] ||= value
end
snippet_css() click to toggle source

config helpers

# File lib/social_snippet/config.rb, line 91
def snippet_css
  ::File.join home, "snippet.css"
end
sspm_url() click to toggle source
# File lib/social_snippet/config.rb, line 121
def sspm_url
  "#{get :sspm_protocol}://#{get :sspm_host}/api/#{get :sspm_version}"
end

Private Instance Methods

load_env(sym) click to toggle source
# File lib/social_snippet/config.rb, line 151
def load_env(sym)
  name = sym.to_s.upcase # :foo_bar => FOO_BAR
  key = "#{ENV_PREFIX}#{name}"
  return nil unless ENV.has_key?(key) && (not ENV[key].nil?)
  if ENV_FLAGS.include?(sym)
    ENV[key] === "true"
  else
    ENV[key]
  end
end
load_environment_variables() click to toggle source
# File lib/social_snippet/config.rb, line 144
def load_environment_variables
  ENV_FIELDS.each do |field_sym|
    value = load_env(field_sym)
    set field_sym, value unless value.nil?
  end
end
load_options(options) click to toggle source
# File lib/social_snippet/config.rb, line 162
def load_options(options)
  options.each do |key, value|
    set key, value
  end
end
normalize_key(key) click to toggle source

Key => key :Key => key

# File lib/social_snippet/config.rb, line 140
def normalize_key(key)
  key.to_s.downcase
end
resolve_home() click to toggle source
# File lib/social_snippet/config.rb, line 168
def resolve_home
  @home ||= ENV["SOCIAL_SNIPPET_HOME"]
  @home ||= ::File.join(ENV["HOME"], ".social-snippet")
end