class Cumulus::CLI

Public Instance Methods

billing() click to toggle source
# File lib/cumulus.rb, line 46
def billing
  c = Client.new(auth_token: token)
  count = options[:count] || 10
  by = options[:by] || 'period'

  if options[:period]
    reports = c.billing_report(by: by, period: options[:period])
  else
    reports = c.billing_report(by: by)
  end

  reports = reports[0..count].reverse

  reports.sort_by!{ |r| r.spend }.reverse! # sort by spend
  rows = []

  reports.each do |report|
    dimention = dimention_to_attribute(by)
    rows << [report[dimention], '$' + number_with_delimiter(report.spend).to_s]
  end

  puts Terminal::Table.new(headings: [by.capitalize, 'Spend'], rows: rows)
end
budgets() click to toggle source
# File lib/cumulus.rb, line 13
def budgets
  budgets = Client.new(auth_token: token).budgets

  budgets.select!{|b| b.type.downcase == options[:type] } if options[:type]

  rows = []
  budgets.each do |budget|
    if budget.is_active
      rows << [c_to_d(budget.predicted_monthly_spend.cents),
        '$' + number_with_delimiter(budget.threshold.to_i).to_s,
        budget.type, budget.subject]
    end
  end

  puts Terminal::Table.new(headings: ['Predicted spend', 'Budget threshold', 'Type', 'Subject'], rows: rows)
end
credentials() click to toggle source
# File lib/cumulus.rb, line 31
def credentials
  credentials = Client.new(auth_token: token).credentials
  rows = []

  credentials.each do |cred|
    rows << [cred.vendor_key, cred.nickname]
  end

  puts Terminal::Table.new(headings: ['Vendor key', 'Nickname'], rows: rows)
end
invites() click to toggle source
# File lib/cumulus.rb, line 73
def invites
  invites = Client.new(auth_token: token).organization_invitations

  invites.select!{|i| i.organization_role.label.downcase == options[:role] } if options[:role]
  invites.select!{|i| i.state.downcase == options[:state] } if options[:state]

  rows = []
  invites.each do |invite|
    rows << [invite.user.full_name, invite.user.email, invite.organization_role.label, invite.state]
  end

  puts Terminal::Table.new(headings: ['Name', 'Email', 'Role', 'State'], rows: rows)
end
set_token() click to toggle source
# File lib/cumulus.rb, line 89
def set_token
  n = Netrc.read

  if options[:token]
    n.new_item_prefix = "# Added by the Cumulus gem\n"
    n["app.cloudability.com"] = options[:token], options[:token]
    n.save
    puts "Your token has been saved."
  else
    puts "Please enter your Cloudability API token and hit enter: "
    token = STDIN.gets.chomp
    n.new_item_prefix = "# Added by the Cumulus gem\n"
    n["app.cloudability.com"] = token, token
    n.save
    puts "Your token has been saved."
  end
end

Private Instance Methods

c_to_d(cents) click to toggle source

Convert cents to dollars

# File lib/cumulus.rb, line 142
def c_to_d(cents)
  dollars = number_with_delimiter(cents.to_i / 100)
  '$' + dollars.to_s
end
dimention_to_attribute(dimention) click to toggle source

Maps dimention to attribute for billing command.

@param [String] dimention @return [Symbol] attribute

# File lib/cumulus.rb, line 113
def dimention_to_attribute(dimention)
  case dimention
  when 'period'
    :period
  else
    (dimention + '_name').to_sym
  end
end
get_token() click to toggle source
# File lib/cumulus.rb, line 131
def get_token
  puts "Please enter your Cloudability API token and hit enter: "
  token = STDIN.gets.chomp
  n = Netrc.read
  n.new_item_prefix = "# Added by the Cumulus gem\n"
  n["app.cloudability.com"] = token, token
  n.save
  token
end
number_with_delimiter(number, delimiter=",", separator=".") click to toggle source

Source: gist.github.com/jpemberthy/484764

# File lib/cumulus.rb, line 148
def number_with_delimiter(number, delimiter=",", separator=".")
  begin
    parts = number.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  rescue
   number
  end
end
token() click to toggle source
# File lib/cumulus.rb, line 122
def token
  n = Netrc.read
  if !n["app.cloudability.com"].nil? && token = n["app.cloudability.com"][0]
    return token
  else
    get_token
  end
end