Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require "osx/cocoa"
include OSX

class UpdatePastieCookie
  URL         = NSURL.URLWithString("http://pastie.org/")
  COOKIE_NAME = "pasties"

  def self.with pastie_id
    # Create a dictionary (hash) to create the new cookie with
    new_cookie_values = self.existing_cookie.properties.mutableCopy
    # Set our new value
    new_cookie_values[NSHTTPCookieValue] = self.existing_cookie.value + "&#{pastie_id}"
    # Create a new cookie with our attributes
    new_cookie = NSHTTPCookie.cookieWithProperties(new_cookie_values)
    # Set our cookie, overwriting the old one
    self.cookieStorage.setCookie(new_cookie)
  end

  def self.existing_cookie
    @cookie ||= self.cookieStorage.cookiesForURL(URL).select {|c| c.name == COOKIE_NAME }.first
  end

  def self.cookieStorage
    @storage ||= NSHTTPCookieStorage.sharedHTTPCookieStorage
  end
end