Sha256: c907c20c0b08c495dc57cf25e50f6e5a72b1bbdc65d13f04b75f89510ce179d0

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'mechanize'
require 'hpricot'

module Adsense
  class Info

    def initialize( username, password )
      @username = username
      @password = password
      @agent = Mechanize::Mechanize.new
    end

    def login!
      page = @agent.get 'http://www.gmail.com'
      form = page.forms.first
      if form
        form.Email = @username
        form.Passwd = @password
        @agent.submit form
      end
      nil
    end

    def update_values!
      login!
      values = ( get('https://www.google.com/adsense/v3/m/home') / 'span[@class~=value]' )
      @today_so_far = values[0].inner_html
      @yesterday = values[1].inner_html
      @this_month_so_far = values[2].inner_html
      @last_month = values[3].inner_html
    end

    def today_so_far
      update_values! unless @today_so_far
      @today_so_far
    end

    def yesterday
      update_values! unless @yesterday
      @yesterday
    end

    def this_month_so_far
      update_values! unless @this_month_so_far
      @this_month_so_far
    end

    def last_month
      update_values! unless @last_month
      @last_month
    end

    private

    def get( url )
      Hpricot(@agent.get( url ).body)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adsense-info-1.0.4 lib/adsense-info.rb