Class: Redwood::DraftLoader

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

Instance Attribute Summary collapse

Attributes inherited from Source

#usual

Instance Method Summary collapse

Methods inherited from Source

#==, #file_path, #go_idle, #is_source_for?, #labels?, parse_raw_email_header, #read?, #supported_labels?, #synchronize, #try_lock, #unlock, #valid?

Constructor Details

#initialize(dir = Redwood::DRAFT_DIR) ⇒ DraftLoader

Returns a new instance of DraftLoader.



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

def initialize dir=Redwood::DRAFT_DIR
  Dir.mkdir dir unless File.exist? 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



96
97
98
99
100
# File 'lib/sup/draft.rb', line 96

def each_raw_message_line offset
  File.open(fn_for_offset(offset), "r:UTF-8") do |f|
    yield f.gets until f.eof?
  end
end

#end_offsetObject



107
108
109
110
# File 'lib/sup/draft.rb', line 107

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

#fn_for_offset(o) ⇒ Object



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

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

#gen_offsetObject



63
64
65
66
67
68
69
# File 'lib/sup/draft.rb', line 63

def gen_offset
  i = @cur_offset
  while File.exist? fn_for_offset(i)
    i += 1
  end
  i
end

#idObject



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

def id; DraftManager.source_id; end

#load_header(offset) ⇒ Object



73
74
75
# File 'lib/sup/draft.rb', line 73

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

#load_message(offset) ⇒ Object

Raises:



77
78
79
80
81
82
83
84
# File 'lib/sup/draft.rb', line 77

def load_message offset
  raise SourceError, "Draft not found" unless File.exist? 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



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sup/draft.rb', line 50

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

#properly_initialized?Boolean

Returns:

  • (Boolean)


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

def properly_initialized?
  !!(@dir && @cur_offset)
end

#raw_header(offset) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/sup/draft.rb', line 86

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

#raw_message(offset) ⇒ Object



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

def raw_message offset
  IO.read(fn_for_offset(offset), :encoding => "UTF-8")
end

#start_offsetObject



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

def start_offset; 0; end

#to_sObject



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

def to_s; DraftManager.source_name; end

#uriObject



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

def uri; DraftManager.source_name; end