Class: IspUsage::Fetchers::AUOptus

Inherits:
Fetcher
  • Object
show all
Defined in:
lib/ispusage/fetchers/au_optus.rb

Instance Attribute Summary

Attributes inherited from Fetcher

#options, #password, #usage_periods, #username

Instance Method Summary collapse

Methods inherited from Fetcher

#to_hash, #to_json

Constructor Details

#initialize(options) ⇒ AUOptus

Returns a new instance of AUOptus.



7
8
9
# File 'lib/ispusage/fetchers/au_optus.rb', line 7

def initialize(options)
  super(options)
end

Instance Method Details

#fetch_usageObject



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
# File 'lib/ispusage/fetchers/au_optus.rb', line 11

def fetch_usage
  agent = Mechanize.new

  page = agent.get('https://my.optus.com.au/web/oscportal.portal?site=personal')
  form = page.form('Login')
  form['USER'] = self.username
  form['PASSWORD'] = self.password

  page = agent.submit(form, form.buttons.first)

   = page.search('h2').text.match(/\d+/)
  services = page.search('ol.service_list')
  services.each do |service|
    phone_no = service.search('div>a').text.strip
    page = agent.get("https://my.optus.com.au/web/oscportal.portal?_nfpb=true&_pageLabel=redirectPage&redirectTo=deeplink_myusage_postpaid&site=personal&pageName=unbilledUsage&virAcctNum=#{phone_no}&accNum=#{}&_activeBA=#{}&_activeSI=#{phone_no}")

    plan_text = page.search("html>body>div>div>div:nth-of-type(3)>div>div:nth-of-type(2)>div:nth-of-type(4)>div>table tr:nth-of-type(5)>td>table tr:nth-of-type(2)>td>div>table tr:nth-of-type(2)>td:nth-of-type(2)").text

    
    downloads = UsagePeriod.new
    self.usage_periods << downloads
    
    downloads.label = phone_no
    downloads.quota = plan_text.match(/(\d+)MB/)[1].to_i
    
    usagesummary = page.search('table.usagesummary')
    usagesummary.search('tr').each do |row|
      row_name =  row.search('td[1]').text 
      case row_name
      when 'Data - Mobile Internet'
        downloads.used = row.search('td[2]').text.match(/\d+\./)[0].to_i
      end
    end
  end
end