Class: Extracare2of::Authentication

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthentication

Returns a new instance of Authentication.



7
8
9
10
11
# File 'lib/ExtraCare2OF/authentication.rb', line 7

def initialize(*)
  load_session if session_exists?
  authenticate unless session_exists?
  @hash = Hash.new
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



5
6
7
# File 'lib/ExtraCare2OF/authentication.rb', line 5

def hash
  @hash
end

Instance Method Details

#authenticateObject



24
25
26
27
28
29
30
# File 'lib/ExtraCare2OF/authentication.rb', line 24

def authenticate
  @agent = Mechanize.new
  
  open("#{ENV['HOME']}/.extracare2of/session.yml", 'w') { |f|
    f.puts @agent.cookie_jar.to_yaml
  }
end

#load_sessionObject



17
18
19
20
21
22
# File 'lib/ExtraCare2OF/authentication.rb', line 17

def load_session
  session_file = open("#{ENV['HOME']}/.extracare2of/session.yml")
  @agent = Mechanize.new do |a|
    a.cookie_jar = YAML.load(session_file)
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ExtraCare2OF/authentication.rb', line 53

def logged_in?
  /Welcome/.match(@page.parser.xpath("//body").to_html) == true
end

#loginObject



32
33
34
35
36
37
38
39
40
# File 'lib/ExtraCare2OF/authentication.rb', line 32

def 
  page = @agent.get("https://m.cvs.com/mt/www.cvs.com/account/login.jsp")
  form = page.forms.first
  form['/atg/userprofiling/ProfileFormHandler.value.login']    = ask("username: ")
  form['/atg/userprofiling/ProfileFormHandler.value.password'] = ask("password: ")
  page = form.submit
  @page = page
  sleep 3
end

#request(link, request_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/ExtraCare2OF/authentication.rb', line 42

def request(link, request_id)
  @hash[request_id] = {ready: false}
  url = URI.escape(link)
  @agent.read_timeout=30
  temp = @agent.get(url)
  # @log.debug "#{@url}"
  returned_body = temp.parser.xpath("//body").to_html.to_s
  @hash[request_id] = {url: url.to_s, body: returned_body, html: temp, ready: true}
  {url: url.to_s, body: returned_body, html: temp, hash: request_id.to_i}
end

#session_exists?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ExtraCare2OF/authentication.rb', line 13

def session_exists?
  File.exist?("#{ENV['HOME']}/.extracare2of/session.yml")
end