Class: Gfspark::App

Inherits:
Object
  • Object
show all
Includes:
Config, Connection, Graph, Term::ANSIColor
Defined in:
lib/gfspark/app.rb

Constant Summary

Constants included from Graph

Graph::BARS, Graph::RANGES, Graph::RANGE_DEFS

Constants included from Config

Config::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graph

#axis_unit, #bar, #period_title, #range_arg?, #render, #render_x_axis_labels, #seconds_to_label, #to_axis_label

Methods included from Connection

#connection, #fetch_json, #response_success?, #send_request

Methods included from Config

#detect_width_and_height!, #help, #load_default_settings, #load_default_settings_from_home, #load_default_settings_with_traversing_to_root, #opt_parser, #parse_options, #set_ssl_options!, #try_default, #try_path, #try_url

Constructor Details

#initialize(args) ⇒ App

Returns a new instance of App.



10
11
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
37
38
39
40
41
42
43
# File 'lib/gfspark/app.rb', line 10

def initialize(args)
  @opt_parse_obj = opt_parser

  if args.nil? || args.empty?
    @valid = false
    return
  end

  @options = load_default_settings
  try_url(args) || try_path(args) || try_default(args)

  required_args = [:url, :service, :section, :graph]
  unless required_args.map(&:to_s).map(&"@".method(:+)).map(&method(:instance_variable_get)).inject(true, :&)
    puts "Invalid Arguments: check arguments or your .gfspark file"
    required_args.each do |n|
      puts "  #{n} is required" unless instance_variable_get("@#{n}")
    end
    puts ""
    @valid = false
    return
  end

  @options[:t] ||= "d"
  unless RANGES.keys.include?(@options[:t])
    puts "Unknown graph range t=#{@options[:t]}"
    @valid = false
    return
  end

  @options[:y_axis_label] ||= "show"
  @valid = true
  detect_width_and_height!
  set_ssl_options!
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/gfspark/app.rb', line 8

def debug
  @debug
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/gfspark/app.rb', line 8

def options
  @options
end

#validObject

Returns the value of attribute valid.



8
9
10
# File 'lib/gfspark/app.rb', line 8

def valid
  @valid
end

Instance Method Details

#executeObject



45
46
47
48
49
50
51
52
# File 'lib/gfspark/app.rb', line 45

def execute
  json    = fetch(:xport)
  summary = fetch(:summary)

  url = to_url(:view_graph)
  render(json, summary, url)
  true
end

#fetch(api) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gfspark/app.rb', line 65

def fetch(api)
  url = "#{@url}/#{api.to_s}/#{@service}/#{@section}/#{@graph}"
  queries = {}
  queries[:t]     = @options[:t] || "d"
  queries[:width] = @width
  queries[:gmode] = @options[:gmode] if @options[:gmode]
  queries[:from]  = @options[:from]  if @options[:from].nil?
  queries[:to]    = @options[:to]    if @options[:to].nil?

  json = fetch_json(url, {}, queries)
end

#to_url(api) ⇒ Object



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

def to_url(api)
  url = "#{@url}/#{api.to_s}/#{@service}/#{@section}/#{@graph}"
  queries = {}
  queries[:t]     = @options[:t] || "d"
  queries[:width] = @width
  queries[:gmode] = @options[:gmode] if @options[:gmode]
  queries[:from]  = @options[:from]  if @options[:from]
  queries[:to]    = @options[:to]    if @options[:to]
  "#{url}?#{queries.map{|k,v| "#{k}=#{v}"}.join("&")}"
end