class RoxClient::RSpec::Config

Attributes

cache_payload[W]
load_warnings[R]
local_mode[W]
print_payload[W]
project[R]
publish[W]
save_payload[W]
server[R]
workspace[R]

Public Class Methods

new() click to toggle source
# File lib/rox-client-rspec/config.rb, line 23
def initialize
  @servers = []
  @project = Project.new
  @publish, @local_mode, @cache_payload, @print_payload, @save_payload = false, false, false, false, false
  @load_warnings = []
end

Public Instance Methods

client_options() click to toggle source
# File lib/rox-client-rspec/config.rb, line 49
def client_options
  {
    publish: @publish,
    local_mode: @local_mode,
    workspace: @workspace,
    cache_payload: @cache_payload,
    print_payload: @print_payload,
    save_payload: @save_payload
  }.select{ |k,v| !v.nil? }
end
load() click to toggle source
# File lib/rox-client-rspec/config.rb, line 60
def load

  @load_warnings = []
  return unless config = load_config_files

  @publish = parse_env_flag :publish, !!config[:publish]
  @server_name = parse_env_option(:server) || config[:server]
  @local_mode = parse_env_flag(:local) || !!config[:local]

  self.workspace = parse_env_option(:workspace) || config[:workspace]
  @cache_payload = parse_env_flag :cache_payload, !!config[:payload][:cache]
  @print_payload = parse_env_flag :print_payload, !!config[:payload][:print]
  @save_payload = parse_env_flag :save_payload, !!config[:payload][:save]

  @servers, @server = build_servers config

  if @servers.empty?
    @load_warnings << "No server defined"
  elsif !@server_name
    @load_warnings << "No server name given"
  elsif !@server
    @load_warnings << "Unknown server '#{@server_name}'"
  end

  project_options = config[:project]
  project_options.merge! api_id: @server.project_api_id if @server and @server.project_api_id
  @project.update project_options

  self
end
servers() click to toggle source
# File lib/rox-client-rspec/config.rb, line 34
def servers
  @servers.dup
end
setup!() click to toggle source

Plugs ROX utilities into RSpec.

# File lib/rox-client-rspec/config.rb, line 39
def setup!
  ::RSpec.configure do |c|
    c.add_formatter Formatter
  end
end
workspace=(dir) click to toggle source
# File lib/rox-client-rspec/config.rb, line 30
def workspace= dir
  @workspace = dir ? File.expand_path(dir) : nil
end

Private Instance Methods

build_servers(config) click to toggle source
# File lib/rox-client-rspec/config.rb, line 93
def build_servers config

  default_server_options = { project_api_id: config[:project][:api_id] }
  servers = config[:servers].inject({}) do |memo,(name, options)|
    memo[name] = Server.new default_server_options.merge(options).merge(name: name)
    memo
  end

  [ servers.values, servers[@server_name.to_s.strip] ]
end
home_config_file() click to toggle source
# File lib/rox-client-rspec/config.rb, line 134
def home_config_file
  File.join File.expand_path('~'), '.rox', 'config.yml'
end
load_config_files() click to toggle source
# File lib/rox-client-rspec/config.rb, line 104
def load_config_files

  configs = [ home_config_file, working_config_file ]
  actual_configs = configs.select{ |f| File.exists? f }

  if actual_configs.empty?
    @load_warnings << %|no config file found, looking for:\n     #{configs.join "\n     "}|
    return false
  end

  actual_configs.collect!{ |f| YAML.load_file f }

  actual_configs.inject({ servers: {} }) do |memo,yml|
    memo.merge! parse_general_options(yml)

    if yml['servers'].kind_of? Hash
      yml['servers'].each_pair do |k,v|
        if v.kind_of? Hash
          memo[:servers][k] = (memo[:servers][k] || {}).merge(parse_server_options(v))
        end
      end
    end

    memo[:payload] = (memo[:payload] || {}).merge parse_payload_options(yml['payload'])
    memo[:project] = (memo[:project] || {}).merge parse_project_options(yml['project'])

    memo
  end
end
parse_env_flag(name, default = false) click to toggle source
# File lib/rox-client-rspec/config.rb, line 142
def parse_env_flag name, default = false
  val = parse_env_option name
  val ? !!val.to_s.strip.match(/\A(1|t|true)\Z/i) : default
end
parse_env_option(name) click to toggle source
# File lib/rox-client-rspec/config.rb, line 147
def parse_env_option name
  var = "ROX_#{name.upcase}"
  ENV.key?(var) ? ENV[var] : nil
end
parse_general_options(h) click to toggle source
# File lib/rox-client-rspec/config.rb, line 152
def parse_general_options h
  parse_options h, %w(publish server local workspace)
end
parse_options(h, keys) click to toggle source
# File lib/rox-client-rspec/config.rb, line 169
def parse_options h, keys
  return {} unless h.kind_of? Hash
  keys.inject({}){ |memo,k| memo[k.gsub(/(.)([A-Z])/, '\1_\2').downcase.to_sym] = h[k] if h.key?(k); memo }
end
parse_payload_options(h) click to toggle source
# File lib/rox-client-rspec/config.rb, line 160
def parse_payload_options h
  parse_options h, %w(save cache print)
end
parse_project_options(h) click to toggle source
# File lib/rox-client-rspec/config.rb, line 164
def parse_project_options h
  # TODO: remove project name once API v0 is dead
  parse_options h, %w(name version apiId category tags tickets)
end
parse_server_options(h) click to toggle source
# File lib/rox-client-rspec/config.rb, line 156
def parse_server_options h
  parse_options h, %w(name apiUrl apiKeyId apiKeySecret apiVersion projectApiId)
end
working_config_file() click to toggle source
# File lib/rox-client-rspec/config.rb, line 138
def working_config_file
  File.expand_path ENV['ROX_CONFIG'] || 'rox.yml', Dir.pwd
end