module Octo

The main Octo module

Model for contact us page on the microsite

Model for Subscribe to us (in the footer), on the microsite

Constants

VERSION

The current version of the library

Public Class Methods

_connect(configuration) click to toggle source

A low level method to connect using a configuration @param [Hash] configuration The configuration hash

# File lib/octocore-mongo.rb, line 110
def self._connect(configuration)

  load_config configuration

  self.logger.info('Octo booting up.')

  # Establish Cequel Connection
  # cassandra_config = Octo.get_config(:cassandra)
  # connection = Cequel.connect(cassandra_config)
  # Cequel::Record.connection = connection

  # Establish Mongo Connection
  mongo_uri = Octo.get_config(:mongo_uri)
  connection = MongoMapper.setup({'production' => {'uri' => mongo_uri}}, 'production')
  # MongoMapper::Connection.connection= connection

  # Establish connection to cache server
  default_cache = {
    host: '127.0.0.1', port: 6379
  }
  cache_config = Octo.get_config(:redis, default_cache)
  MongoMapper::Document.update_cache_config(*cache_config.values_at(:host, :port))

  # Establish connection to statsd server if required
  if configuration.has_key?(:statsd)
    statsd_config = configuration[:statsd]
    set_config :stats, Statsd.new(*statsd_config.values)
  end

  self.logger.info('I\'m connected now.')
  require 'octocore-mongo/callbacks'

  self.logger.info('Setting callbacks.')

  Octo::Callbacks.run_hook(:after_connect)
end
connect(configuration) click to toggle source

Connect using the provided configuration. If you want to extend Octo's connect

method you can override this method with your own. Just make sure to make
a call to self._connect(configuration) so that Octo also connects

@param [Hash] configuration The configuration hash

# File lib/octocore-mongo.rb, line 38
def self.connect(configuration)
  self._connect(configuration)
end
connect_with(location) click to toggle source

Provides a unified interface to connect_with_config_dir

and #connect_with_config_file for convenience
# File lib/octocore-mongo.rb, line 97
def self.connect_with(location)
  if File.directory?(location)
    self.connect_with_config_dir location
  elsif File.file?(location)
    self.connect_with_config_file location
  else
    puts "Invalid location #{ location }"
  end
end
connect_with_config_dir(config_dir) click to toggle source

Connect by reading configuration files from the given directory.

In this case, all *.y*ml files would be read in
Dir.glob order and merged into one unified config
# File lib/octocore-mongo.rb, line 52
def self.connect_with_config_dir(config_dir)
  config = {}
  accepted_formats = Set.new(['.yaml', '.yml'])
  Dir[config_dir + '/*'].each do |file_obj|
    if File.file?(file_obj) and accepted_formats.include?File.extname(file_obj)
      config = self.true_load(config, file_obj, config_dir)
    elsif File.directory?(file_obj)
      Dir[file_obj + '/**/*.y*ml'].each do |file|
        config = self.true_load(config, file, config_dir)
      end
    end
  end
  # As a cleanup step, merge the values of key named "config"
  # with the global config hash
  configConfig = config.delete(:config)
  config = config.deep_merge(configConfig)
  # Now, good to merge the two
  self.connect config
end
connect_with_config_file(config_file) click to toggle source

Connect by reading configuration from the provided file @param [String] config_file Location of the YAML config file

# File lib/octocore-mongo.rb, line 44
def self.connect_with_config_file(config_file)
  config = YAML.load_file(config_file).deep_symbolize_keys
  self.connect(config)
end
logger() click to toggle source

Creates a logger for Octo

# File lib/octocore-mongo.rb, line 148
def self.logger
  unless @logger
    @logger = Logger.new(Octo.get_config(:logfile, $stdout)).tap do |log|
      log.progname = name
    end
  end
  @logger
end
true_load(config, file, config_dir) click to toggle source

Loads the true config. The true config is the hierarchial config @param [Hash] config The base config. Loaded config will be deep merged

with this

@param [String] file The file from which config should be loaded @param [String] config_fir The config dir in which the file is located @return [Hash] The merged config hash

# File lib/octocore-mongo.rb, line 78
def self.true_load(config, file, config_dir)
  _config = YAML.load_file file
  if _config
    $stdout.puts "Loading from Config file: #{ file }"
    # A little bit of hack here.
    # This hack makes sure that if we load config files from nestes
    # directories, the corresponding config is loaded in
    # hierarchy.
    # So, if the file is like /config/search/index.yml, the
    # key would be like [config[search[index]]]
    a = file.gsub(/(\/?#{config_dir}(\/)*(config\/)*|.yml)/, '').split('/')
    _true_config = a.reverse.inject(_config) { |r,e| { e => r } }
    config = config.deep_merge(_true_config.deep_symbolize_keys)
  end
  config
end