Class: Fsgrowl::Fsgrowl

Inherits:
Object
  • Object
show all
Defined in:
lib/fsgrowl/fsgrowl.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Fsgrowl

Returns a new instance of Fsgrowl.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fsgrowl/fsgrowl.rb', line 12

def initialize(username, password)

  @username = username

  pstore_file = "#{ENV['HOME']}/.fsgrowl.pstore"
  @store = PStore.new(pstore_file)
  if File.exist?(pstore_file)
    File.chmod(0600, pstore_file)
  end

  @store.transaction do
    @cookie = @store[@username]
  end

  unless @cookie
    (username, password)
  end
end

Instance Method Details

#check(all = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fsgrowl/fsgrowl.rb', line 52

def check(all=false)

  #read last since_id
  since_id = 1
  unless all
    @store.transaction do
      since_id = @store["#{@username}:sinceid"]
      since_id = 1 if since_id.nil?
    end
  end

  http = Net::HTTP.new('www.formspring.me')
  path = "/follow/streamrefresh?ajax=1&since_id=#{since_id}&with_content=true"
  headers = {
      'Cookie' => @cookie,
  }
  resp, data = http.get(path, headers)
  content = JSON.parse(data)['content']
  html_doc = Nokogiri::HTML(content)
  #TODO OR selector?
  html_doc.css('li[class~="question"]').each do |elt|
    node = elt.at_css('div[class="responder"] a[class~="username"]')
    if node
      response = elt.at_css('p[rel=response-text]').text
      growl("New response from #{node.text}", response)
    end
  end
  since_id = html_doc.at_css('li[rel]')
  if since_id
    since_id = since_id['rel']
    @store.transaction do
      @store["#{@username}:sinceid"] = since_id
    end
  end
end

#growl(title, message, priority = 0, sticky = false) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/fsgrowl/fsgrowl.rb', line 88

def growl(title, message, priority=0, sticky=false)
  growl = File.join(GEM_PATH, 'growl', 'growlnotify')
  sender = 'Formspring'
  icon = File.join(GEM_PATH, 'img', 'logo.png')
  options = %(-n "#{sender}" --image "#{icon}" -p #{priority} -m "#{message}" "#{title}" #{'-s' if sticky})
  system %(#{growl} #{options} &)
end

#login(username, password) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fsgrowl/fsgrowl.rb', line 31

def (username, password)

  http = Net::HTTP.new('www.formspring.me')
  path = '/account/login?ajax=1'

  # POST request -> logging in
  data = "username=#{username}&password=#{password}&login=true&ajax=1"
  resp, rdata = http.post(path, data)
  if resp.code.to_i != 200
    puts "Error logging in"
    exit(1)
  end

  @cookie = resp.response['set-cookie'].split('; ')[0]

  # Save the cookie
  @store.transaction do
    @store[@username] = @cookie
  end
end