module Rbitter

Constants

BOOTSTRAP_ARGS
DEFAULT_CONFIG_JSON
PRODUCT_NAME
RPC_PREFIX
VERSION

Public Class Methods

[](k) click to toggle source
# File lib/rbitter/env.rb, line 12
def self.[](k)
  @@env[k]
end
bootstrap(args=[]) click to toggle source
# File lib/rbitter.rb, line 38
def self.bootstrap args=[]
  return nil if args.length < 1
  
  if args[0] == "serve"
    Rbitter.config_initialize
    
    archive_server = Rbitter::ArcServer.new
    archive_server.main_loop
  elsif args[0] == "help"
    rbitter_help_msg
  elsif args[0] == "configure"
    bootstrap_configs
  elsif args[0] == "console"
    Rbitter.config_initialize

    con = Rbitter::Console.new
    con.start
  elsif args[0] == "logs"
    # show log in stdout
    puts "Log buffer feature is in heavy development. Sorry."
  else
    fail StandardError, "Invalid bootstrap parameter: #{args[0]}"
  end
end
bootstrap_configs() click to toggle source
# File lib/rbitter.rb, line 30
def self.bootstrap_configs
  require "rbitter/default/config_json"

  open(File.join(Dir.pwd, "config.json"), "w") { |io|
    io.write(DEFAULT_CONFIG_JSON)
  }
end
rbitter_header() click to toggle source
# File lib/rbitter.rb, line 14
def self.rbitter_header
  puts "Rbitter #{VERSION} on #{RUBY_VERSION} (#{RUBY_PLATFORM})"
end
rbitter_help_msg() click to toggle source
# File lib/rbitter.rb, line 18
def self.rbitter_help_msg  
  puts "Rbitter is a Twitter streaming archiver, with XMLRPC access."
  puts "-------"
  puts "Usage: rbitter (application-mode)"
  puts "application-mode's are:"
  puts "|- serve    : Launch Rbitter full system (Streaming + Database + XMLRPC)"
  puts "|- console  : Launch console application utilizing XMLRPC"
  puts "|- configure: Write default configuration file 'config.json' in current folder"
  puts "|- help     : Show this message"
  puts "`- logs     : Show Rbitter internal logs"
end

Public Instance Methods

config_initialize(json_path=nil) click to toggle source
# File lib/rbitter/env.rb, line 81
def config_initialize json_path=nil
  env_reset

  unless json_path.nil?
    begin
      open(json_path, 'r') { |file|
        @@env = JSON.parse(file.read)
      }

      return @@env if env_valid?
      fail StandardError, "Void configuration. Maybe failure on load?"
    rescue => e
      fail ConfigFileError, "Config error on (#{json_path}): #{e.to_s}"
    end
  end

  # Configuration default location
  # 1. (current_dir)/config.json
  # 2. (current_dir)/.rbitter/config.json
  locations = ["config.json", ".rbitter/config.json"]
  locations.collect! { |base| File.join(Dir.pwd, base) }

  for location in locations
    next unless File.file?(location)
    open(location, 'r') { |file|
      @@env = JSON.parse(file.read)
    }
    break unless @@env.empty?
  end

  fail ConfigFileError, "No config.json on #{locations.join(' or ')}" if @@env.empty?
  fail ConfigFileError, "Configuration outdated. Please see above messages to update it" if not env_valid?

  puts "[config.json] JSON data structure is safe to go."
end
env() click to toggle source
# File lib/rbitter/env.rb, line 17
def env
  @@env
end
env_listfields(hash) click to toggle source
# File lib/rbitter/env.rb, line 25
def env_listfields hash
  path_stack = ['']
  generated = []

  until path_stack.empty?
    path = path_stack.pop

    if path == ''
      o = hash
    else
      nodes = path.strip.split('->')
      o = hash
      until nodes.empty?
        o = o[nodes.shift]
      end
    end

    o.each_key { |k|
      if o[k].is_a?(Hash)
        path_stack << "#{k}" if path.empty?
        path_stack << path + "->#{k}" unless path.empty?
      else
        generated << "#{k}" if path.empty?
        generated << path + "->#{k}" unless path.empty?
      end
    }
  end

  generated
end
env_reset() click to toggle source
# File lib/rbitter/env.rb, line 21
def env_reset
  @@env.clear
end
env_valid?() click to toggle source
# File lib/rbitter/env.rb, line 56
def env_valid?
  defaults = env_listfields(JSON.parse(DEFAULT_CONFIG_JSON))
  currents = env_listfields(@@env)
  not_errored = true

  # Cross checking (2 phases)
  # In current exists, default does not: redundant configuration
  # Level: warning since it is not utilized at all.
  currents.each { |conf|
    unless defaults.include?(conf)
      warn "[config.json] Unsupported field: #{conf}"
    end
  }

  # In default exists, current does not: missing configuration
  # Level: error and program should stop. (return false for this)
  defaults.each { |conf|
    unless currents.include?(conf)
      warn "[config.json] Error! Missing field: #{conf}"
      not_errored = false
    end
  }
  not_errored
end
major() click to toggle source
# File lib/rbitter/version.rb, line 5
def major
  VERSION.match(/^([0-9]+)\./)[1]
end
minor() click to toggle source
# File lib/rbitter/version.rb, line 9
def minor
  VERSION.match(/\.([0-9]+)\./)[1]
end
patchlv() click to toggle source
# File lib/rbitter/version.rb, line 13
def patchlv
  VERSION.match(/\.([0-9]+)$/)[1]
end
version_string() click to toggle source
# File lib/rbitter/version.rb, line 17
def version_string
  "#{PRODUCT_NAME} #{VERSION}"
end