Class: AliceInvoice

Inherits:
Object
  • Object
show all
Defined in:
lib/alice_invoice.rb,
lib/alice_invoice/cache/lazy.rb,
lib/alice_invoice/link_builder.rb

Defined Under Namespace

Classes: Cache, LinkBuilder

Constant Summary collapse

VERSION =
'1.0.0'
CACHE_API_VERSION =
1
CUSTOMER_LOUNGE_URL =
"https://www.alice-dsl.de/kundencenter/"
INVOICE_BASE_URL =
"https://rechnung.alice-dsl.de/asp/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AliceInvoice

Returns a new instance of AliceInvoice.


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
# File 'lib/alice_invoice.rb', line 21

def initialize(options)
  @login    = options[:login]
  @password = options[:password]

  @debug          = options[:debug]
  @storage_path   = options[:storage_path]
  @cache_delegate = options[:cache_delegate]
  
  raise "No :login given!"        unless @login
  raise "No :password given!"     unless @password
  raise "No :storage_path given!" unless @storage_path
  
  @b = WWW::Mechanize.new { |agent|
    agent.user_agent_alias = 'Mac Safari'
  }
  
  if @cache_delegate
    unless @cache_delegate.implements_api_version?(CACHE_API_VERSION)
      raise "cache delegate #{@cache_delegate.class} does not support the " <<
            "required API revision #{CACHE_API_VERSION}" 
    end        
    def cache_hook(event, data={})
      @cache_delegate.event(event, data)
    end
  else
    def cache_hook(event, data={})
      true
    end
  end
end

Instance Attribute Details

#bObject

Returns the value of attribute b


9
10
11
# File 'lib/alice_invoice.rb', line 9

def b
  @b
end

#cache_delegateObject

Returns the value of attribute cache_delegate


13
14
15
# File 'lib/alice_invoice.rb', line 13

def cache_delegate
  @cache_delegate
end

#debugObject

Returns the value of attribute debug


10
11
12
# File 'lib/alice_invoice.rb', line 10

def debug
  @debug
end

Returns the value of attribute download_links


11
12
13
# File 'lib/alice_invoice.rb', line 11

def download_links
  @download_links
end

#logged_inObject

Returns the value of attribute logged_in


14
15
16
# File 'lib/alice_invoice.rb', line 14

def logged_in
  @logged_in
end

#loginObject

Returns the value of attribute login


7
8
9
# File 'lib/alice_invoice.rb', line 7

def 
  @login
end

#passwordObject

Returns the value of attribute password


8
9
10
# File 'lib/alice_invoice.rb', line 8

def password
  @password
end

#storage_pathObject

Returns the value of attribute storage_path


12
13
14
# File 'lib/alice_invoice.rb', line 12

def storage_path
  @storage_path
end

Instance Method Details

#download_invoices_and_signatures!Object


69
70
71
# File 'lib/alice_invoice.rb', line 69

def download_invoices_and_signatures!
  fuck!
end

#fuck!Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/alice_invoice.rb', line 73

def fuck!
  login! unless @logged_in
  
  if cache_hook(:needs_list_of_invoices)
    url = CUSTOMER_LOUNGE_URL << "invoice/MonthlyOverview.do?selID=AL_MR_Monat"
    url = "https://www.alice-dsl.de/kundencenter/invoice/MonthlyOverview.do?selID=AL_MR_Monat"
    
    invoice_frame_url = ""
    invoice_url       = ""

    @b.get(url) do ||
      invoice_frame_url = .iframes.first.src

      @b.get(invoice_frame_url) do |invoice_frame_page|
        invoice_url = invoice_frame_page.frames.first.href
        invoice_url.gsub!(/^\.\//, INVOICE_BASE_URL)

        @b.get(invoice_url) do |invoice_links_page|
          links = invoice_links_page.links.map{ |l| l.href.gsub(/;/,'') }.uniq
          @download_links = links.map { |l| INVOICE_BASE_URL + AliceInvoice::LinkBuilder.parse_and_build(l) }
          @download_links.each { |f| retrieve_and_save_file(f) }
        end
      end 
    end
  else
    cache_hook(:cached_invoices_list)
  end  
end

#login!Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/alice_invoice.rb', line 52

def login!
  if cache_hook(:needs_login)
    url = CUSTOMER_LOUNGE_URL << "Login.do?selID=none"
    @b.get(url) do |page|
      @lounge_home = page.form_with(:name => 'LoginForm') do |f|
        f.benutzername = @login
        f.passwort     = @password
      end.submit
      
      # TODO check if login was successful
      file_out(@lounge_home.content) if @debug
    end
  end  

  @logged_in = true
end