Module: Spunkmeyer::Chrome

Defined in:
lib/spunkmeyer/chrome.rb

Class Method Summary collapse

Class Method Details

currently supports only OSX.



8
9
10
11
12
13
14
15
16
17
# File 'lib/spunkmeyer/chrome.rb', line 8

def self.cookie_path
  case Spunkmeyer.os
  when :osx
    "#{Dir.home}/Library/Application Support/Google/Chrome/Default/Cookies"
  when :linux
    "#{Dir.home}/.config/google-chrome/Default/Cookies"
  else
    raise 'Spunkmeyer::Chrome doesn\'t know this operating system.'
  end
end

.cookies(domain) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/spunkmeyer/chrome.rb', line 19

def self.cookies(domain)
  hk = host_key domain
  db = SQLite3::Database.open cookie_path
  fields = [ :host_key, :path, :name, :value, :expires_utc ]
  sql_fields = fields.map { |f| f.to_s }.join(', ')
  rows = db.execute "select #{sql_fields} from cookies where host_key like '%#{hk}%'"
  rows.map! { |r| Hash[fields.zip r] }
  names = rows.map { |r| r[:name] }
  Hash[names.zip rows]
end

.host_key(domain) ⇒ Object



30
31
32
33
34
35
# File 'lib/spunkmeyer/chrome.rb', line 30

def self.host_key(domain)
  server = domain.scan(/:\/\/([^:#\/\?]+)/).flatten.first
  raise "Malformed domain!" unless server
  parts = server.split(".")
  ".#{parts[-2]}.#{parts[-1]}"
end