Class: Typedown2Blog::BlogPost

Inherits:
Base show all
Defined in:
lib/typedown2blog/blog_post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

log, #log, #merge_to_attributes

Constructor Details

#initialize(options = {}, &block) ⇒ BlogPost

Returns a new instance of BlogPost.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/typedown2blog/blog_post.rb', line 17

def initialize options = {}, &block
  @mail_to = nil
  @mail_from = nil
  @typedown_body = ""
  @format = nil
  @attachments = []

  options.each do |k, v|
    send("#{k}=", v)
  end

  instance_eval &block if block_given?
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



10
11
12
# File 'lib/typedown2blog/blog_post.rb', line 10

def format
  @format
end

#mail_fromObject

Returns the value of attribute mail_from.



10
11
12
# File 'lib/typedown2blog/blog_post.rb', line 10

def mail_from
  @mail_from
end

#mail_toObject

Returns the value of attribute mail_to.



10
11
12
# File 'lib/typedown2blog/blog_post.rb', line 10

def mail_to
  @mail_to
end

#typedown_bodyObject

Returns the value of attribute typedown_body.



10
11
12
# File 'lib/typedown2blog/blog_post.rb', line 10

def typedown_body
  @typedown_body
end

Class Method Details

.add_formatter(name, formatter) ⇒ Object



31
32
33
# File 'lib/typedown2blog/blog_post.rb', line 31

def self.add_formatter name, formatter
  @formatters[ name ] = formatter
end

.formattersObject



36
37
38
# File 'lib/typedown2blog/blog_post.rb', line 36

def self.formatters
  @formatters
end

Instance Method Details

#add_attachment(options = {}) ⇒ Object



40
41
42
43
# File 'lib/typedown2blog/blog_post.rb', line 40

def add_attachment options = {}
  options[:content] = File.read_binary(options[:tmpfile]) unless options[:content]
  @attachments << options
end

#import_mail(filename_or_hash) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/typedown2blog/blog_post.rb', line 82

def import_mail filename_or_hash
  post = nil
  extract = Attachments::Extract.new [ "image/jpeg" ]
  begin
    extract.parse filename_or_hash
    self.mail_from = extract.from

    typedown_root = Typedown::Section.sectionize(extract.text_body, extract.subject)
    self.typedown_body = typedown_root.doc

    extract.files.each do |f|
      self.add_attachment f
    end
  ensure
    extract.close
  end
end

#post!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/typedown2blog/blog_post.rb', line 46

def post!
  mail_to = self.mail_to
  mail_from = self.mail_from
  mail_subject, mail_body, mail_content_type = self.format_body(self.typedown_body)

  mail_attachments = @attachments

  mail = Mail.new do
    from mail_from
    to mail_to
    subject mail_subject

    if mail_attachments.length == 0
      body mail_body
      content_type mail_content_type
    else
      text_part do
        self.charset = "UTF-8"
        body mail_body
      end
      text_part.content_type mail_content_type
    end

    mail_attachments.each do |a|
      add_file(:filename => a[:save_as], :content => a[:content])
      attachments[a[:save_as]][:content_type] = a[:mime_type]
    end

    #text_part.body mail_body
    #text_part.content_type mail_content_type
  end

  log.info((mail_subject || "(No subject)") + " delivered to " + (mail_to || "(nobody)"))
  mail.deliver!
end