Sha256: 55332493f4838c1adc9605f3f80912565a639de6b22620b07c61ec5b1460cfff

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 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]) unless saved_cookie[:expires].nil?
    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

9 entries across 9 versions & 1 rubygems

Version Path
fuck_facebook-0.5.4 src/cookie_handler.rb
fuck_facebook-0.5.3 src/cookie_handler.rb
fuck_facebook-0.5.2 src/cookie_handler.rb
fuck_facebook-0.5.1 src/cookie_handler.rb
fuck_facebook-0.5.0 src/cookie_handler.rb
fuck_facebook-0.4.1 src/cookie_handler.rb
fuck_facebook-0.4.0 src/cookie_handler.rb
fuck_facebook-0.3.0 src/cookie_handler.rb
fuck_facebook-0.2.1 src/cookie_handler.rb