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.



27
28
29
30
31
32
33
# File 'lib/rwriteboard.rb', line 27

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#httpObject

Returns the value of attribute http.



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

def http
  @http
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.countObject



91
92
93
# File 'lib/rwriteboard.rb', line 91

def self.count
  @@writeboards.size
end

.create(hash) ⇒ Object



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

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)
  if block_given?
    wb.logged_in do |_wb|
      yield _wb
    end
  end
  wb
end

.find(hash) ⇒ Object



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

def @@writeboards.find(hash)
  memo = self
  hash.each do |k,v|
    memo = [memo.detect {|wb| !wb.nil? && wb.respond_to?(k.to_sym) && wb.send(k.to_sym) == v}]
  end
  memo.first
end

.writeboardsObject



95
96
97
# File 'lib/rwriteboard.rb', line 95

def self.writeboards
  @@writeboards
end

Instance Method Details

#getObject



35
36
37
38
39
40
# File 'lib/rwriteboard.rb', line 35

def get
  response_body = self.http.get(self.path, self.cookie).body.html_entity_quotes
  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.unescape_newlines
  self
end

#logged_inObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rwriteboard.rb', line 42

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



55
56
57
58
59
60
61
62
63
64
# File 'lib/rwriteboard.rb', line 55

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



66
67
68
# File 'lib/rwriteboard.rb', line 66

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