Class: EC2UsageReport
- Inherits:
-
Object
- Object
- EC2UsageReport
- Defined in:
- lib/ec2-usage-report.rb
Instance Method Summary collapse
- #fetch(options = {}) ⇒ Object
-
#initialize(email, password) ⇒ EC2UsageReport
constructor
A new instance of EC2UsageReport.
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( = {}) [:end] ||= Time.now [:start] ||= ([:end] - 7 * 24 * 60 * 60) [:end] = Time.parse([:end].to_s) [:start] = Time.parse([:start].to_s) [: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. end agent.page.form_with(:name => 'usageReportForm') do |f| select(f, :timePeriod, 'aws-portal-custom-date-range') select_time(f, 'start', .delete(:start)) select_time(f, 'end', .delete(:end)) .each do |name, value| select(f, name, value) end f.(f.(: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 |