Top Level Namespace

Defined Under Namespace

Modules: DogEventer

Instance Method Summary collapse

Instance Method Details

#dogeventer(start_time = nil, api_key = nil, application_key = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/dogeventer.rb', line 35

def dogeventer(start_time=nil, api_key=nil, application_key=nil, &block)
  require 'optparse'
  
  all_config = {
    :default => {:start_time => Time.now.to_i - 60*60},
    :file => {},
    :cli => {}
  }
  config_file = "~/.dogrc"
  
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: ruby #{$0} [options]"
    opts.on("-t", "--time TIME", Float, "Start time in seconds since unix epoch. Defaults to the current time minus an hour.") { |v| 
      all_config[:cli][:start_time] = v
    }
    opts.on("-c", "--config CONFIG_FILE", "Path to config file. Defaults to #{config_file}") { |v|
      config_file = v
    }
    opts.on("--api_key API_KEY", "Api key. Overrides config values.") { |v| 
      all_config[:cli][:api_key] = v
    }
    opts.on("--app_key [APP_KEY]", "App key. Overrides config values.") { |v| 
      all_config[:cli][:app_key] = v
    }
    
  end
  optparse.parse!

  if not config_file
    config_file = default_config_file
  end
  
  config_file = File.expand_path config_file
  if File.exists? config_file
    File.open(config_file) do |f| 
      f.readlines.each do |line|
        keyval = line.split "="
        if keyval.length == 2
          key = keyval[0].strip
          val = keyval[1].strip
          if key == 'apikey'
            all_config[:file][:api_key] = val
          elsif key == 'appkey'
            all_config[:file][:app_key] = val
          end
        end
      end
    end
  end
  
  config = [:default, :file, :cli].reduce({}) do |merged, to_merge|
    merged.merge all_config[to_merge]
  end
  
  DogEventer::DogEventer.new(config[:api_key], config[:app_key]).generate config[:start_time], &block
end