Class: Extracare2of::Authentication
- Inherits:
-
Object
- Object
- Extracare2of::Authentication
- Defined in:
- lib/ExtraCare2OF/authentication.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#initialize ⇒ Authentication
constructor
A new instance of Authentication.
- #load_session ⇒ Object
- #logged_in? ⇒ Boolean
- #login ⇒ Object
- #request(link, request_id) ⇒ Object
- #session_exists? ⇒ Boolean
Constructor Details
#initialize ⇒ Authentication
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
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
5 6 7 |
# File 'lib/ExtraCare2OF/authentication.rb', line 5 def hash @hash end |
Instance Method Details
#authenticate ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/ExtraCare2OF/authentication.rb', line 24 def authenticate @agent = Mechanize.new login open("#{ENV['HOME']}/.extracare2of/session.yml", 'w') { |f| f.puts @agent..to_yaml } end |
#load_session ⇒ Object
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. = YAML.load(session_file) end end |
#logged_in? ⇒ Boolean
53 54 55 |
# File 'lib/ExtraCare2OF/authentication.rb', line 53 def logged_in? /Welcome/.match(@page.parser.xpath("//body").to_html) == true end |
#login ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/ExtraCare2OF/authentication.rb', line 32 def login 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
13 14 15 |
# File 'lib/ExtraCare2OF/authentication.rb', line 13 def session_exists? File.exist?("#{ENV['HOME']}/.extracare2of/session.yml") end |