dudemcbacon/goodwill

View on GitHub
lib/goodwill/mechanize.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

require 'mechanize'

require 'goodwill/urlpaths'

module Goodwill
  module Mechanize
    def mechanize
      Mechanize.mechanize
    end

    class << self
      include URLPaths

      def username
        @username ||= nil
      end

      attr_writer :username, :password

      def password
        @password ||= nil
      end

      def logged_in?
        @logged_in ||= false
      end

      def mechanize
        @mechanize ||= ::Mechanize.new
        login
        @mechanize
      end

      def login
        return true if logged_in?

        @mechanize.get(LOGIN_URL) do |page|
          my_page = page.form_with(action: '/SignIn') do |f|
            f.Username = @username
            f.Password = @password
          end.click_button
          @logged_in = my_page.links.map(&:to_s).include? 'My Shopgoodwill '
        end
      end
    end
  end
end