wod 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ == 0.0.2
2
+ * Added support for accounts belonging to multiple teams
3
+
4
+ == 0.0.1
5
+ * Initial version
data/lib/wod.rb CHANGED
@@ -1,5 +1,11 @@
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
3
9
  end
4
10
 
5
11
  require 'wod/client'
@@ -14,6 +14,10 @@ module Wod
14
14
  (page.title =~ /sign in/i) == nil && (page.search("span").find {|s| s.text == "Log in"}) == nil
15
15
  end
16
16
 
17
+ def team_selection_page?
18
+ (page.title =~ /select.*team/i)
19
+ end
20
+
17
21
  def search arg
18
22
  @page.search arg
19
23
  end
@@ -22,15 +26,18 @@ module Wod
22
26
  @page.form arg
23
27
  end
24
28
  end
29
+
25
30
 
26
31
  class Client
27
32
  include Wod::Helpers
28
33
 
29
34
  attr_reader :name
35
+ attr_accessor :team
30
36
 
31
- def initialize username, password
37
+ def initialize username, password, team
32
38
  @username = username
33
39
  @password = password
40
+ @team = team
34
41
  end
35
42
 
36
43
  def cookies_file
@@ -47,21 +54,41 @@ module Wod
47
54
  @agent ||= create_agent
48
55
  end
49
56
 
57
+ def login_at page
58
+ puts "Creating session"
59
+
60
+ login_page = page.page.links.find { |l| l.text == 'Log in'}.click
61
+
62
+ f = login_page.form("appleConnectForm")
63
+ f.theAccountName = @username
64
+ f.theAccountPW = @password
65
+ f.submit
66
+ end
67
+
68
+ 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
73
+
74
+ DevCenterPage.new f.click_button f.button_with(:value => /continue/i)
75
+ end
76
+
50
77
  def login_and_reopen url
51
78
  page = DevCenterPage.new agent.get("https://developer.apple.com/devcenter/ios/index.action")
52
79
 
53
80
  unless page.logged_in?
54
- puts "Creating session"
55
- login_page = page.page.links.find { |l| l.text == 'Log in'}.click
56
-
57
- f = login_page.form("appleConnectForm")
58
- f.theAccountName = @username
59
- f.theAccountPW = @password
60
- f.submit
81
+ login_at page
82
+ page = DevCenterPage.new agent.get url
83
+ end
84
+
85
+ if page.team_selection_page?
86
+ select_team_at page
87
+ page = DevCenterPage.new agent.get url
61
88
  end
62
89
 
63
- page = DevCenterPage.new agent.get url
64
90
  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?
65
92
  agent.cookie_jar.save_as cookies_file
66
93
  page
67
94
  end
@@ -74,7 +101,7 @@ module Wod
74
101
 
75
102
  def logged_in?
76
103
  page = get "https://developer.apple.com/devcenter/ios/index.action"
77
- page.logged_in?
104
+ page.logged_in? && !page.team_selection_page?
78
105
  end
79
106
 
80
107
  end
@@ -23,8 +23,10 @@ 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
29
  end
27
-
28
30
  end
29
31
 
30
32
  def self.run_internal(command, args, wod=nil)
@@ -5,7 +5,7 @@ module Wod::Command
5
5
  attr_accessor :credentials
6
6
 
7
7
  def client
8
- @client = Wod::Client.new(user, password)
8
+ @client = Wod::Client.new(user, password, team)
9
9
  end
10
10
 
11
11
  # just a stub; will raise if not authenticated
@@ -18,13 +18,20 @@ module Wod::Command
18
18
  end
19
19
 
20
20
  def user
21
- get_credentials
22
- @credentials[0]
21
+ credential 0
23
22
  end
24
23
 
25
24
  def password
25
+ credential 1
26
+ end
27
+
28
+ def team
29
+ credential 2
30
+ end
31
+
32
+ def credential index
26
33
  get_credentials
27
- @credentials[1]
34
+ @credentials && @credentials.size > index ? @credentials[index] : nil
28
35
  end
29
36
 
30
37
  def credentials_file
@@ -83,6 +90,16 @@ module Wod::Command
83
90
  puts "Authentication failed."
84
91
  return if retry_login?
85
92
  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
86
103
  end
87
104
  end
88
105
 
@@ -49,6 +49,9 @@ module Wod::Command
49
49
  end
50
50
 
51
51
  def usage
52
+ puts "wod v#{Wod::VERSION}"
53
+ puts
54
+
52
55
  longest_command_length = self.class.groups.map do |group|
53
56
  group.map { |g| g.first.length }
54
57
  end.flatten.max
@@ -1,7 +1,7 @@
1
1
  module Wod::Command
2
2
  class Version < Base
3
3
  def index
4
- puts Wod::Client.gem_version_string
4
+ puts "wod v#{Wod::VERSION}"
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Wod
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dave Newman
@@ -35,6 +35,7 @@ extra_rdoc_files: []
35
35
 
36
36
  files:
37
37
  - .gitignore
38
+ - CHANGELOG
38
39
  - Gemfile
39
40
  - README.md
40
41
  - Rakefile