Class: OKCupid::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/lonely_coder/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, browser) ⇒ Authentication

Returns a new instance of Authentication.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lonely_coder/authentication.rb', line 10

def initialize(username, password, browser)
  change_to_using_simpler_parser(browser)

  browser.post("https://www.okcupid.com/login", {
    username: username,
    password: password,
    okc_api: 1
  })

  body = JSON.parse(browser.page.body)

  # body['screenname'] will be `nil` for failed auth
  # and equal to the supplied username for success.
  # there may be other cases?
  @success = body['screenname'] != nil

  restore_default_parser(browser)
end

Instance Method Details

#change_to_using_simpler_parser(browser) ⇒ Object



33
34
35
# File 'lib/lonely_coder/authentication.rb', line 33

def change_to_using_simpler_parser(browser)
  browser.pluggable_parser.html = AuthenticationParser
end

#restore_default_parser(browser) ⇒ Object



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

def restore_default_parser(browser)
  browser.pluggable_parser.html = Mechanize::Page
end

#success?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/lonely_coder/authentication.rb', line 29

def success?
  @success
end