Class: Writeboard

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Writeboard

Returns a new instance of Writeboard.



26
27
28
29
30
# File 'lib/rwriteboard.rb', line 26

def initialize(hash)
  self.password = hash[:password]
  self.path     = hash[:path]
  self.name     = hash[:name]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def body
  @body
end

Returns the value of attribute cookie.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def cookie
  @cookie
end

#headerObject

Returns the value of attribute header.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def header
  @header
end

#httpObject

Returns the value of attribute http.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def http
  @http
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def name
  @name
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def password
  @password
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def path
  @path
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def session
  @session
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/rwriteboard.rb', line 6

def title
  @title
end

Class Method Details

.countObject



89
90
91
# File 'lib/rwriteboard.rb', line 89

def self.count
  @@writeboards.size
end

.create(hash) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rwriteboard.rb', line 67

def self.create(hash)
  password = hash[:password]
  path = hash[:path] || hash[:id] || hash[:address] || ""
  path = "/" + path unless path =~ /^\//
  name = hash[:name] || self.count + 1
  wb = self.new(:password => password, :path => path, :name => name)
  @@writeboards << wb
  if block_given?
    wb.logged_in do |_wb|
      yield _wb
    end
  end
  wb
end

.find(hash) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rwriteboard.rb', line 18

def @@writeboards.find(hash)
  memo = self
  hash.each do |k,v|
    memo = memo.detect {|wb| wb.send(k.to_sym) == v}
  end
  memo
end

.writeboardsObject



93
94
95
# File 'lib/rwriteboard.rb', line 93

def self.writeboards
  @@writeboards
end

Instance Method Details

#getObject



32
33
34
35
36
37
# File 'lib/rwriteboard.rb', line 32

def get
  response_body = self.http.get(self.path, self.cookie).body
  self.title    = response_body.scan(%r{<div class="writeboardtitle">(.*?)</div>}m).to_s.strip_tags
  self.body     = response_body.scan(%r{<div class="writeboardbody">(.*?)</div>}m).to_s.strip_tags
  self
end

#logged_inObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rwriteboard.rb', line 39

def logged_in
 Net::HTTP.start('123.writeboard.com') do |http|
         = self.path + "/login"
    self.header    = "password=#{self.password}"
     = http.post(, self.header)
    cookie         = {'Cookie' => .response['Set-Cookie']}
    self.http      = http
    self.cookie    = cookie
    self.session   = cookie['Cookie'][1]
    yield self
  end
end

#post_without_revision(hash = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rwriteboard.rb', line 52

def post_without_revision(hash={})
  raise "There is no connection" unless self.http
  set_attributes_from_hash(hash)
  self.get unless self.title && self.body
  postdata = "minor_edit=1" + "&" +
  "commit=Save over the current version" + "&" +
  "version[body]=#{self.body.gsub(%r(\\n), "<br />")}" + "&" +
  "version[title]=#{self.title}"
  self.http.post(self.path + "/v/create", postdata, self.cookie)
end

#set_attributes_from_hash(hash) ⇒ Object



63
64
65
# File 'lib/rwriteboard.rb', line 63

def set_attributes_from_hash(hash)
  hash.each {|k,v| self.send(k.to_s.concat("=").to_sym, v)}
end