Class: TelstraUsage
- Inherits:
-
Object
- Object
- TelstraUsage
- Defined in:
- lib/tellme/telstra.rb
Instance Attribute Summary collapse
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#percent ⇒ Object
readonly
Returns the value of attribute percent.
-
#percent_of_date ⇒ Object
readonly
Returns the value of attribute percent_of_date.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
-
#used ⇒ Object
readonly
Returns the value of attribute used.
Instance Method Summary collapse
- #fetch(timeout = 30) ⇒ Object
-
#initialize(pik, password) ⇒ TelstraUsage
constructor
A new instance of TelstraUsage.
Constructor Details
#initialize(pik, password) ⇒ TelstraUsage
Returns a new instance of TelstraUsage.
16 17 18 19 20 21 |
# File 'lib/tellme/telstra.rb', line 16 def initialize(pik, password) @pik = pik @password = password @agent = WWW::Mechanize.new #{|a| a.log = Logger.new($stderr)} end |
Instance Attribute Details
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
13 14 15 |
# File 'lib/tellme/telstra.rb', line 13 def end_date @end_date end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
12 13 14 |
# File 'lib/tellme/telstra.rb', line 12 def left @left end |
#percent ⇒ Object (readonly)
Returns the value of attribute percent.
9 10 11 |
# File 'lib/tellme/telstra.rb', line 9 def percent @percent end |
#percent_of_date ⇒ Object (readonly)
Returns the value of attribute percent_of_date.
10 11 12 |
# File 'lib/tellme/telstra.rb', line 10 def percent_of_date @percent_of_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
14 15 16 |
# File 'lib/tellme/telstra.rb', line 14 def start_date @start_date end |
#used ⇒ Object (readonly)
Returns the value of attribute used.
11 12 13 |
# File 'lib/tellme/telstra.rb', line 11 def used @used end |
Instance Method Details
#fetch(timeout = 30) ⇒ Object
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tellme/telstra.rb', line 23 def fetch(timeout = 30) @agent.open_timeout = timeout @agent.read_timeout = timeout page = @agent.get("https://www.telstraclear.co.nz/usagemeter/") form = page.forms[1] form.fields.find{|f| f.name == 'acc' }.value = @pik form.fields.find{|f| f.name == 'pik' }.value = @password page = @agent.submit(form, form..first) doc = Hpricot(page.body) if page.body =~ /main menu for account/i link = (doc/"a").detect do |a| a.inner_text =~ /\d+\s+-\s+today/i end link = link.attributes['href'] page = @agent.get(link) doc = Hpricot(page.body) end text_elements = (doc/"#usg_content_info").first text = text_elements.inner_text text =~ /you have used\s+(\d+(\.\d+)?)\s?(MB|GB)/i used = $1.to_f used = used / 1024 if $2 == 'MB' @used = used text =~ /(\d+)%/ @percent = $1.to_i text =~ /(\d+(\.\d+)?)\s?(MB|GB)\s+left/ left = $1.to_f left = left / 1024 if $2 == 'MB' @left = left text_elements = (doc/".usg_content_hdr_txt").first text = text_elements.inner_text text =~ /Usage Summary Graph:(.*?)- Today/i @start_date = Chronic.parse($1) text =~ /ends on:\s*(.*)/i @end_date = Chronic.parse($1) percent_date = ((((Time.now - @start_date).to_i).to_f / ((@end_date - @start_date).to_i).to_f)*100).round @percent_of_date = percent_date end |