wod 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.0.3
2
+ * An unhandled error now causes wod to save the last page for inspection
3
+
1
4
  == 0.0.2
2
5
  * Added support for accounts belonging to multiple teams
3
6
 
data/bin/wod CHANGED
@@ -4,8 +4,15 @@ $:.unshift File.join File.dirname(__FILE__), '../lib'
4
4
 
5
5
  require "wod"
6
6
 
7
- args = ARGV.dup
8
- ARGV.clear
7
+ args = []
8
+ options = []
9
+
10
+ ARGV.each do |a|
11
+ a =~ /^-/ ? options << a : args << a
12
+ end
13
+
9
14
  command = args.shift.strip rescue 'help'
10
15
 
16
+ ARGV.clear
17
+
11
18
  Wod::Command.run command, args
data/lib/wod.rb CHANGED
@@ -1,11 +1,5 @@
1
1
  module Wod
2
2
  class InvalidCredentials < RuntimeError; end
3
- class NoTeamSelected < RuntimeError
4
- attr_reader :teams
5
- def initialize(teams)
6
- @teams = teams
7
- end
8
- end
9
3
  end
10
4
 
11
5
  require 'wod/client'
@@ -31,13 +31,14 @@ module Wod
31
31
  class Client
32
32
  include Wod::Helpers
33
33
 
34
- attr_reader :name
35
- attr_accessor :team
34
+ def self.last_page
35
+ @@last_page
36
+ end
36
37
 
37
- def initialize username, password, team
38
- @username = username
39
- @password = password
40
- @team = team
38
+ attr_reader :collect_login_deets, :collect_team_selection
39
+ def initialize options
40
+ @collect_login_deets = options[:collect_login_deets]
41
+ @collect_team_selection = options[:collect_team_selection]
41
42
  end
42
43
 
43
44
  def cookies_file
@@ -55,23 +56,25 @@ module Wod
55
56
  end
56
57
 
57
58
  def login_at page
58
- puts "Creating session"
59
-
60
59
  login_page = page.page.links.find { |l| l.text == 'Log in'}.click
61
60
 
61
+ username, password = collect_login_deets.call
62
+
62
63
  f = login_page.form("appleConnectForm")
63
- f.theAccountName = @username
64
- f.theAccountPW = @password
64
+ f.theAccountName = username
65
+ f.theAccountPW = password
65
66
  f.submit
66
67
  end
67
68
 
68
69
  def select_team_at page
69
- raise NoTeamSelected.new(page.search("select[name='memberDisplayId'] option").map{|o| {:name => o.text, :value => o[:value]} }) unless self.team
70
- f = page.page.form("saveTeamSelection")
71
- select_list = f.fields.first
72
- select_list.value = self.team
70
+ f = page.form "saveTeamSelection"
71
+ select_list = f.field('memberDisplayId')
72
+ teams = select_list.options.map(&:text)
73
73
 
74
- DevCenterPage.new f.click_button f.button_with(:value => /continue/i)
74
+ selected_team = collect_team_selection.call(teams)
75
+ select_list.option_with(:text => selected_team).select
76
+
77
+ f.click_button f.button_with(:value => /continue/i)
75
78
  end
76
79
 
77
80
  def login_and_reopen url
@@ -88,7 +91,6 @@ module Wod
88
91
  end
89
92
 
90
93
  raise InvalidCredentials unless page.logged_in?
91
- raise NoTeamSelected.new(page.search("select[name='memberDisplayId'] option").map{|o| {:name => o.text, :value => o[:value]} }) if page.team_selection_page?
92
94
  agent.cookie_jar.save_as cookies_file
93
95
  page
94
96
  end
@@ -96,7 +98,7 @@ module Wod
96
98
  def get url
97
99
  page = DevCenterPage.new agent.get(url)
98
100
  page = login_and_reopen(url) unless page.logged_in?
99
- page
101
+ @@last_page = page
100
102
  end
101
103
 
102
104
  def logged_in?
@@ -23,9 +23,14 @@ module Wod
23
23
  else
24
24
  error "Authentication failure"
25
25
  end
26
- rescue Wod::NoTeamSelected
27
- STDERR.puts "No team selected"
28
- run command, args, retries + 1
26
+ rescue Exception => e
27
+ File.open(last_page_file, 'w') do |f|
28
+ f.chmod(0600)
29
+ f.puts Client.last_page.page.send(:html_body)
30
+ end
31
+ puts "Error: Last page saved to #{last_page_file}"
32
+
33
+ raise e
29
34
  end
30
35
  end
31
36
 
@@ -5,7 +5,8 @@ module Wod::Command
5
5
  attr_accessor :credentials
6
6
 
7
7
  def client
8
- @client = Wod::Client.new(user, password, team)
8
+ @client = Wod::Client.new :collect_login_deets => lambda { [user, password] },
9
+ :collect_team_selection => lambda { |team_names| team || collect_team_selection(team_names) }
9
10
  end
10
11
 
11
12
  # just a stub; will raise if not authenticated
@@ -90,19 +91,22 @@ module Wod::Command
90
91
  puts "Authentication failed."
91
92
  return if retry_login?
92
93
  exit 1
93
- rescue ::Wod::NoTeamSelected => e
94
- teams = e.teams
95
- puts "This account belongs to the following teams:"
96
- puts teams.map.with_index{|t, i| "#{i+1}. #{t[:name]}"}.join("\n")
97
- STDOUT << "Select team [1]: "
98
- selection = gets.strip
99
- selection = "1" if selection.empty? || selection.to_i == 0
100
- client.team = @credentials[2] = teams[selection.to_i-1][:value]
101
- write_credentials
102
- # check
103
94
  end
104
95
  end
105
96
 
97
+ def collect_team_selection team_names
98
+ puts "This account belongs to the following teams:"
99
+ team_names.each.with_index do |team, i|
100
+ puts "#{i+1}. #{team}"
101
+ end
102
+ print "Select team (1): "
103
+ selection = ask
104
+ selection = "1" if selection.empty? || selection.to_i == 0
105
+ @credentials[2] = team_names[selection.to_i-1]
106
+ write_credentials
107
+ @credentials[2]
108
+ end
109
+
106
110
  def retry_login?
107
111
  @login_attempts ||= 0
108
112
  @login_attempts += 1
@@ -111,10 +115,10 @@ module Wod::Command
111
115
 
112
116
  def write_credentials
113
117
  FileUtils.mkdir_p(File.dirname(credentials_file))
114
- f = File.open(credentials_file, 'w')
115
- f.chmod(0600)
116
- f.puts self.credentials
117
- f.close
118
+ File.open(credentials_file, 'w') do |f|
119
+ f.chmod(0600)
120
+ f.puts self.credentials
121
+ end
118
122
  set_credentials_permissions
119
123
  end
120
124
 
@@ -3,6 +3,14 @@ module Wod
3
3
  def home_directory
4
4
  ENV['HOME']
5
5
  end
6
+
7
+ def wod_directory
8
+ File.join home_directory, ".wod"
9
+ end
10
+
11
+ def last_page_file
12
+ File.join wod_directory, "last_page.html"
13
+ end
6
14
 
7
15
  def error(msg)
8
16
  STDERR.puts(msg)
@@ -1,3 +1,3 @@
1
1
  module Wod
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wod
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dave Newman
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-01 00:00:00 +11:00
13
+ date: 2011-04-02 00:00:00 +11:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency