Sha256: df8d2c5575dbde3242aeffb6c85aed407b5bd0c038f7d56328f3972493431793

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

require 'fileutils'
require 'time'

class CookieHandler
  attr_reader :driver

  def initialize(driver)
    @driver = driver
  end

  def clear!
    File.delete(cookies_filename) if File.exist? cookies_filename

    driver.manage.delete_all_cookies
  end

  def save
    FileUtils.mkdir_p(cookies_dir)
    File.write(cookies_filename, current_cookies.to_json)
  end

  def load
    current_domain = nil
    saved_cookies.each do |saved_cookie|
      new_domain = remove_leading_period(saved_cookie[:domain])

      driver.navigate.to "https://#{new_domain}" unless new_domain == current_domain
      current_domain = new_domain

      driver.manage.add_cookie(saved_cookie)
    end
  end

  def current_cookies
    driver.manage.all_cookies
  end

  def saved_cookies
    return {} unless File.exist? cookies_filename

    saved_cookies = JSON.parse(File.read(cookies_filename), symbolize_names: true)

    saved_cookies.each do |saved_cookie|
      saved_cookie[:expires] = Time.parse(saved_cookie[:expires])
    end

    saved_cookies
  end

  def cookies_filename
    @cookies_filename ||= File.join(cookies_dir, 'cookies.json')
  end

  def cookies_dir
    @cookies_dir ||= File.join(ENV['HOME'], '.local/share/fuck-facebook/')
  end

  private

  def remove_leading_period(str)
    str[1..]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fuck_facebook-0.1.2 src/cookie_handler.rb
fuck_facebook-0.1.1 src/cookie_handler.rb
fuck_facebook-0.1.0 src/cookie_handler.rb