Class: EC2UsageReport

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2-usage-report.rb

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ EC2UsageReport

Returns a new instance of EC2UsageReport.



6
7
8
9
# File 'lib/ec2-usage-report.rb', line 6

def initialize(email, password)
  @email = email
  @password = password
end

Instance Method Details

#fetch(options = {}) ⇒ Object



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
44
45
46
47
48
49
50
51
52
# File 'lib/ec2-usage-report.rb', line 11

def fetch(options = {})
  options[:end] ||= Time.now
  options[:start] ||= (options[:end] - 7 * 24 * 60 * 60)
  options[:end] = Time.parse(options[:end].to_s)
  options[:start] = Time.parse(options[:start].to_s)

  options[:periodType] ||= :days

  agent = Mechanize.new
  agent.user_agent_alias = 'Windows IE 7'

  page = agent.get('https://aws-portal.amazon.com/gp/aws/developer/account/usage-report.html?productCode=AmazonEC2')

  agent.page.form_with(:name => 'signIn') do |f|
    f.field_with(:name => 'email').value = @email
    f.field_with(:name => 'password').value = @password
    f.click_button
  end

  agent.page.form_with(:name => 'usageReportForm') do |f|
    select(f, :timePeriod, 'aws-portal-custom-date-range')

    select_time(f, 'start', options.delete(:start))
    select_time(f, 'end', options.delete(:end))

    options.each do |name, value|
      select(f, name, value)
    end

    f.click_button(f.button_with(:name => 'download-usage-report-csv'))
  end

  csv_rows = agent.page.kind_of?(Mechanize::File) ? CSV.parse((agent.page.body || '').strip) : nil

  if csv_rows
    csv_rows = csv_rows.map do |row|
      row.map {|i| i.to_s.strip }
    end
  end

  return csv_rows
end