Class: Achoo

Inherits:
Object
  • Object
show all
Includes:
UI::Commands, UI::ExceptionHandling, UI::RegisterHours
Defined in:
lib/achoo/term/table.rb,
lib/achoo.rb,
lib/achoo.rb,
lib/achoo/ui.rb,
lib/achoo/vcs.rb,
lib/achoo/form.rb,
lib/achoo/ical.rb,
lib/achoo/term.rb,
lib/achoo/awake.rb,
lib/achoo/binary.rb,
lib/achoo/system.rb,
lib/achoo/vcs/git.rb,
lib/achoo/timespan.rb,
lib/achoo/rc_loader.rb,
lib/achoo/term/menu.rb,
lib/achoo/ui/common.rb,
lib/achoo/open_timespan.rb,
lib/achoo/vcs/subversion.rb,
lib/achoo/lock_month_form.rb,
lib/achoo/ui/date_chooser.rb,
lib/achoo/ui/date_choosers.rb,
lib/achoo/ui/month_chooser.rb,
lib/achoo/ui/exception_handling.rb

Overview

Unicode box drawing characters

en.wikipedia.org/wiki/Box-drawing_characters

0 1 2 3 4 5 6 7 8 9 A B C D E F

2500 ─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏

2510 ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟

2520 ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯

2530 ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿

2540 ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ╌ ╍ ╎ ╏

2550 ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟

2560 ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ ╭ ╮ ╯

2570 ╰ ╱ ╲ ╳ ╴ ╵ ╶ ╷ ╸ ╹ ╺ ╻ ╼ ╽ ╾ ╿

Defined Under Namespace

Modules: Binary, RCLoader, System Classes: Awake, Form, HourAdministrationForm, HourRegistrationForm, HourRegistrationFormRanged, ICal, LockMonthForm, OpenTimespan, Term, Timespan, UI, VCS

Instance Method Summary collapse

Methods included from UI::RegisterHours

#all_projects_chooser, #billing_chooser, #hours_chooser, #phase_chooser, #print_hours_help, #print_remark_help, #project_chooser, #register_hours, #remark_chooser, #workperiod_chooser

Methods included from UI::ExceptionHandling

#get_exception_reason, #handle_exception, #handle_fatal_exception

Methods included from UI::DateChoosers

#date_chooser, #month_chooser, #optionally_ranged_date_chooser

Methods included from UI::Commands

#lock_month, #show_flexi_time, #show_holiday_report, #show_registered_hours_for_day, #show_registered_hours_for_week

Methods included from UI::Common

#confirm

Constructor Details

#initialize(log = false) ⇒ Achoo

Returns a new instance of Achoo.



16
17
18
19
20
21
# File 'lib/achoo.rb', line 16

def initialize(log=false)
  @agent = Mechanize.new
  if log
    @agent.log = Logger.new("achoo_http.log")
  end
end

Instance Method Details

#command_loopObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/achoo.rb', line 42

def command_loop
  while true
    begin
      trap("INT", "DEFAULT");
      answer = Term.choose('[1]',
                           ["Register hours",
                            "Show flexitime balance",
                            "Day hour report",
                            "Week hour report",
                            "Holiday balance",
                            "Lock month",
                           ],
                           "Exit",
                           ['q', 'Q', ''])
      dispatch(answer)
    rescue Interrupt
      puts # Add a new line in case we are prompting
    end
  end
end

#dispatch(command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/achoo.rb', line 64

def dispatch(command)
    case command
    when '0', 'q', 'Q'
      exit
    when '1', ''
      register_hours(@agent)
    when '2'
      show_flexi_time(@agent)
    when '3'
      show_registered_hours_for_day(@agent)
    when '4'
      show_registered_hours_for_week(@agent)
    when '5'
      show_holiday_report(@agent)
    when '6'
      lock_month(@agent)
    end
end

#load_cookiesObject



131
132
133
134
135
136
# File 'lib/achoo.rb', line 131

def load_cookies
  cookies_file = "#{ENV['HOME']}/.achoo_cookies.yml"
  if FileTest.exists? cookies_file
    @agent.cookie_jar.load(cookies_file)
  end
end

#loginObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/achoo.rb', line 108

def 
  load_cookies

  puts "Fetching data ..."
  page = @agent.get(RC[:url])

  return if page.forms.empty?

  puts "Logging in ..."

  form = page.forms.first
  form.auth_user = RC[:user]
  form.auth_pw   = RC[:password]
  page = @agent.submit(form, form.buttons.first)

  if page.body.match(/Username and\/or password are incorrect. Please try again./)
    raise "Username and/or password are incorrect."
  end

  @agent.cookie_jar.save_as("#{ENV['HOME']}/.achoo_cookies.yml")
end


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/achoo.rb', line 94

def menu_link_to_url(menu_links, text)
  a_tag = menu_links.find {|a| 
    a.text.strip == text
  }
  
  if a_tag.nil?
    raise Exception.new("Could not find the '#{text}' link in Achievo.\nMake sure that language is 'English' and theme is 'no value' in Achievo's user preferences.\nYou must delete ~/.achoo_cookies.yml for these changes to take affect.")
  end

  url = a_tag.attribute('onclick').value.match(/window\.open\('([^']+)/)[1]
  return "#{RC[:url]}#{url}"
end


37
38
39
# File 'lib/achoo.rb', line 37

def print_welcome
  puts Term.shadowbox("Welcome to Achoo!")
end

#scrape_urlsObject



84
85
86
87
88
89
90
91
92
# File 'lib/achoo.rb', line 84

def scrape_urls 
  page = @agent.get(@agent.current_page.frames.find {|f| f.name == 'menu'}.href)
  menu_links = page.search('a.menuItemLevel2')
  
  RC[:hour_registration_url] = menu_link_to_url(menu_links, 'Time Registration')
  RC[:lock_months_url]       = menu_link_to_url(menu_links, 'Lock months')
  RC[:holiday_report_url]    = menu_link_to_url(menu_links, 'Holiday report')
  RC[:hour_admin_url]        = RC[:hour_registration_url]
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/achoo.rb', line 23

def start
  begin
    print_welcome
    
    scrape_urls
    command_loop
  rescue SystemExit => e
    raise
  rescue Exception => e
    handle_fatal_exception("Something bad happened. Shutting down.", e)
  end
end