Class: Faceoff::Note

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

Constant Summary collapse

CONTENT_CLASSES =
"note_content text_align_ltr direction_ltr clearfix"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, title, body, date = nil) ⇒ Note

Returns a new instance of Note.



73
74
75
76
77
78
# File 'lib/faceoff/note.rb', line 73

def initialize id, title, body, date=nil
  @fid   = id
  @title = @name = title
  @body  = body
  @date  = date || Time.now
end

Instance Attribute Details

#bodyObject

Note contents.



67
68
69
# File 'lib/faceoff/note.rb', line 67

def body
  @body
end

#dateObject

Date note was created at.



70
71
72
# File 'lib/faceoff/note.rb', line 70

def date
  @date
end

#fidObject

Facebook id of the note.



61
62
63
# File 'lib/faceoff/note.rb', line 61

def fid
  @fid
end

#nameObject

Note title.



64
65
66
# File 'lib/faceoff/note.rb', line 64

def name
  @name
end

#titleObject

Note title.



64
65
66
# File 'lib/faceoff/note.rb', line 64

def title
  @title
end

Class Method Details

.notes_at_index(faceoff, index, &block) ⇒ Object

Get notes on one page starting at a note index.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/faceoff/note.rb', line 39

def self.notes_at_index faceoff, index, &block
  agent = faceoff.agent
  page  = agent.get "/notes.php?id=#{faceoff.profile_id}&start=#{index}"

  notes = []

  page.search("div[@class='note_body']").each do |div|
    title_link = div.search("div[@class='note_title']/a").first
    id    = $1 if title_link['href'] =~ %r{/note\.php\?note_id=(\d+)}
    title = title_link.text
    date  = Time.parse div.search("div[@class='byline']").first.text
    body  = div.search("div[@class='#{CONTENT_CLASSES}']/div").first.text

    notes << new(id, title, body, date)
    yield notes.last if block_given?
  end

  notes
end

.retrieve_all(faceoff, options = {}, &block) ⇒ Object

Returns an array of notes.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/faceoff/note.rb', line 10

def self.retrieve_all faceoff, options={}, &block
  agent = faceoff.agent
  limit = options[:limit]
  start = options[:start] || 0

  note_count = limit
  index = start

  notes = []
  page  = agent.current_page

  while !limit || notes.length < limit && page.link_with(:text => "Next") do
    notes = notes.concat notes_at_index(faceoff, index, &block)
    page  = agent.current_page
    index = start + notes.length

    last_link = page.link_with(:text => 'Last')
    limit ||= $1.to_i + 10 if
      last_link && last_link.href =~ %r{/notes.php\?id=\d+&start=(\d+)}
    limit ||= 10
  end

  notes[0..(limit-1)]
end

Instance Method Details

#save!(target = "./Notes") ⇒ Object

Saves the note to the provided file path.



84
85
86
87
88
89
90
# File 'lib/faceoff/note.rb', line 84

def save! target="./Notes"
  filename = File.join(target, "#{@title}.txt")

  Faceoff.safe_save(filename) do |file|
    file.write self.to_s
  end
end

#to_sObject

Returns the object as a string with title, date, and body.



96
97
98
# File 'lib/faceoff/note.rb', line 96

def to_s
  "#{@title}\n#{@date.to_s}\n\n#{@body}"
end