1
+ require 'json'
2
+
1
3
class OKCupid
2
-
4
+
3
5
def authenticate ( username , password )
4
6
@authentication = Authentication . new ( username , password , @browser )
5
7
end
6
-
8
+
7
9
class Authentication
8
10
def initialize ( username , password , browser )
9
11
change_to_using_simpler_parser ( browser )
10
-
12
+
11
13
browser . post ( "https://www.okcupid.com/login" , {
12
14
username : username ,
13
- password : password
15
+ password : password ,
16
+ okc_api : 1
14
17
} )
15
18
16
- @success = browser . page . uri . path == '/home'
17
-
19
+ body = JSON . parse ( browser . page . body )
20
+
21
+ # body['screenname'] will be `nil` for failed auth
22
+ # and equal to the supplied username for success.
23
+ # there may be other cases?
24
+ @success = body [ 'screenname' ] != nil
25
+
18
26
restore_default_parser ( browser )
19
27
end
20
-
28
+
21
29
def success?
22
30
@success
23
31
end
24
-
32
+
25
33
def change_to_using_simpler_parser ( browser )
26
34
browser . pluggable_parser . html = AuthenticationParser
27
35
end
28
-
36
+
29
37
def restore_default_parser ( browser )
30
38
browser . pluggable_parser . html = Mechanize ::Page
31
39
end
32
40
end
33
-
34
- class AuthenticationParser < Mechanize ::Page
41
+
42
+ class AuthenticationParser < Mechanize ::Page
35
43
# We're only using page uri to determine successful login, so
36
44
# there's not a lot of value in passing a body string to nokogiri
37
45
def initialize ( uri = nil , response = nil , body = nil , code = nil )
38
46
super ( uri , response , '' , code )
39
47
end
40
48
end
41
- end
49
+ end
0 commit comments