Class: GRI::Spark

Inherits:
Object show all
Defined in:
lib/gri/spark.rb

Instance Method Summary collapse

Instance Method Details

#fetch(url, http_basic_authentication) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/gri/spark.rb', line 54

def fetch url, http_basic_authentication
  if http_basic_authentication
    str = open(url,
               :http_basic_authentication=>http_basic_authentication).read
  else
    str = open(url).read
  end
  obj = YAML.load str
  obj
end

#fix_params(params, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gri/spark.rb', line 38

def fix_params params, options
  etime = params['etime'].to_i
  etime = (Time.now + etime).to_i if etime <= 0
  if (t = options[:t]) and (v = Sgraph::TERMS[t])
    stime = (stime = params['stime']) ? stime.to_i : -v[1]
    stime = (etime + stime).to_i if stime <= 0
  else
    stime = (stime = params['stime']) ? stime.to_i : -28*3600
    stime = (etime + stime).to_i if stime <= 0
  end
  params['stime'] = stime
  params['etime'] = etime
  params['maxrows'] = 70
  params['fmt'] = 'json'
end

#mk_query(params) ⇒ Object



65
66
67
68
69
# File 'lib/gri/spark.rb', line 65

def mk_query params
  params.map {|k, v|
    (Array === v) ? v.map {|vv| "#{k}=#{vv}"} : "#{k}=#{v}"
  }.flatten.join('&')
end

#optparse(opts) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gri/spark.rb', line 79

def optparse opts
  op = OptionParser.new
  op.on('--debug') {$debug = true; STDOUT.sync = true;
    opts['log-level'] = 'debug'}
  op.on('-O OPT_STR') {|arg| (opts['O'] ||= []).push arg}
  op.on('-c', '--config-path=PATH') {|arg| opts[:config_path] = arg}
  op.on('--log-level=LEVEL') {|arg| opts['log-level'] = arg}
  op.on('--nop') {opts['nop'] = true}
  op.on('-t ARG') {|arg| opts[:t] = arg}
  op.on('-u', '--user=USER') {|arg| opts[:user] = arg}
  op.on('-p', '--password=PASSWORD') {|arg| opts[:pass] = arg}
  op
end

#parse_query(qs, params = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/gri/spark.rb', line 71

def parse_query qs, params={}
  (qs || '').split(/[&;] */n).each {|item|
    k, v = item.split('=', 2)
    params[k] = v
  }
  params
end

#run(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gri/spark.rb', line 12

def run options={}
  optparser = optparse options
  optparser.parse!
  exit unless (url_s = ARGV.shift)

  url = URI.parse url_s
  params = GParams.new
  parse_query url.query, params
  fix_params params, options
  url.query = mk_query params
  if options[:user]
    if options[:pass]
      http_basic_authentication = [options[:user], options[:pass]]
    else
      print "Password:"
      system "stty -echo"
      password = $stdin.gets.chop
      system "stty echo"
      http_basic_authentication = [options[:user], password]
    end
  end
  obj = fetch url, http_basic_authentication
  graph = Sgraph.new options
  graph.render obj, {}, url
end