Class: Redwood::DraftLoader

Inherits:
Source show all
Defined in:
lib/sup/draft.rb

Instance Attribute Summary collapse

Attributes inherited from Source

#poll_lock

Instance Method Summary collapse

Methods inherited from Source

#==, #file_path, #go_idle, #is_source_for?, parse_raw_email_header, #read?, #usual, #valid?

Constructor Details

#initializeDraftLoader

Returns a new instance of DraftLoader.



35
36
37
38
39
40
41
# File 'lib/sup/draft.rb', line 35

def initialize
  dir = Redwood::DRAFT_DIR
  Dir.mkdir dir unless File.exists? dir
  super DraftManager.source_name, true, false
  @dir = dir
  @cur_offset = 0
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



32
33
34
# File 'lib/sup/draft.rb', line 32

def dir
  @dir
end

Instance Method Details

#each_raw_message_line(offset) ⇒ Object



93
94
95
96
97
# File 'lib/sup/draft.rb', line 93

def each_raw_message_line offset
  File.open(fn_for_offset(offset)) do |f|
    yield f.gets until f.eof?
  end
end

#end_offsetObject



104
105
106
107
# File 'lib/sup/draft.rb', line 104

def end_offset
  ids = get_ids
  ids.empty? ? 0 : (ids.last + 1)
end

#fn_for_offset(o) ⇒ Object



68
# File 'lib/sup/draft.rb', line 68

def fn_for_offset o; File.join(@dir, o.to_s); end

#gen_offsetObject



60
61
62
63
64
65
66
# File 'lib/sup/draft.rb', line 60

def gen_offset
  i = 0
  while File.exists? fn_for_offset(i)
    i += 1
  end
  i
end

#idObject



43
# File 'lib/sup/draft.rb', line 43

def id; DraftManager.source_id; end

#load_header(offset) ⇒ Object



70
71
72
# File 'lib/sup/draft.rb', line 70

def load_header offset
  File.open(fn_for_offset(offset)) { |f| parse_raw_email_header f }
end

#load_message(offset) ⇒ Object

Raises:



74
75
76
77
78
79
80
81
# File 'lib/sup/draft.rb', line 74

def load_message offset
  raise SourceError, "Draft not found" unless File.exists? fn_for_offset(offset)
  File.open fn_for_offset(offset) do |f|
    RMail::Mailbox::MBoxReader.new(f).each_message do |input|
      return RMail::Parser.read(input)
    end
  end
end

#pollObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sup/draft.rb', line 47

def poll
  ids = get_ids
  ids.each do |id|
    if id >= @cur_offset
      @cur_offset = id + 1
      yield :add,
        :info => id,
        :labels => [:draft, :inbox],
        :progress => 0.0
    end
  end
end

#raw_header(offset) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/sup/draft.rb', line 83

def raw_header offset
  ret = ""
  File.open fn_for_offset(offset) do |f|
    until f.eof? || (l = f.gets) =~ /^$/
      ret += l
    end
  end
  ret
end

#raw_message(offset) ⇒ Object



99
100
101
# File 'lib/sup/draft.rb', line 99

def raw_message offset
  IO.read(fn_for_offset(offset))
end

#start_offsetObject



103
# File 'lib/sup/draft.rb', line 103

def start_offset; 0; end

#to_sObject



44
# File 'lib/sup/draft.rb', line 44

def to_s; DraftManager.source_name; end

#uriObject



45
# File 'lib/sup/draft.rb', line 45

def uri; DraftManager.source_name; end