Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Commit fe3a8fe

Browse files
committed
Fixes Authentication
OKCupid added a form param and now responds with JSON for authentication
1 parent cef6d95 commit fe3a8fe

File tree

4 files changed

+97
-1140
lines changed

4 files changed

+97
-1140
lines changed

Gemfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
source :rubygems
1+
source 'https://rubygems.org'
22
gemspec
33

44
group :test do
5+
gem 'pry'
56
gem 'rspec'
67
gem 'webmock'
78
gem 'vcr'
8-
end
9+
end

lib/lonely_coder/authentication.rb

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
1+
require 'json'
2+
13
class OKCupid
2-
4+
35
def authenticate(username, password)
46
@authentication = Authentication.new(username, password, @browser)
57
end
6-
8+
79
class Authentication
810
def initialize(username, password, browser)
911
change_to_using_simpler_parser(browser)
10-
12+
1113
browser.post("https://www.okcupid.com/login", {
1214
username: username,
13-
password: password
15+
password: password,
16+
okc_api: 1
1417
})
1518

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+
1826
restore_default_parser(browser)
1927
end
20-
28+
2129
def success?
2230
@success
2331
end
24-
32+
2533
def change_to_using_simpler_parser(browser)
2634
browser.pluggable_parser.html = AuthenticationParser
2735
end
28-
36+
2937
def restore_default_parser(browser)
3038
browser.pluggable_parser.html = Mechanize::Page
3139
end
3240
end
33-
34-
class AuthenticationParser < Mechanize::Page
41+
42+
class AuthenticationParser < Mechanize::Page
3543
# We're only using page uri to determine successful login, so
3644
# there's not a lot of value in passing a body string to nokogiri
3745
def initialize(uri = nil, response = nil, body = nil, code =nil)
3846
super(uri, response, '', code)
3947
end
4048
end
41-
end
49+
end

0 commit comments

Comments
 (0)