Class: Presser::BlogPost

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlogPost

Returns a new instance of BlogPost.



6
7
8
9
10
11
12
13
# File 'lib/blog_post.rb', line 6

def initialize
  @categories      = []
  @post_status     = ""
  @postid          = "-1"
  @link            = "http://www.google.com"
  @reading_header  = true
  @post_file_items = {}
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/blog_post.rb', line 4

def body
  @body
end

#categoriesObject

Returns the value of attribute categories.



4
5
6
# File 'lib/blog_post.rb', line 4

def categories
  @categories
end

Returns the value of attribute link.



4
5
6
# File 'lib/blog_post.rb', line 4

def link
  @link
end

#post_statusObject

Returns the value of attribute post_status.



4
5
6
# File 'lib/blog_post.rb', line 4

def post_status
  @post_status
end

#postidObject

Returns the value of attribute postid.



4
5
6
# File 'lib/blog_post.rb', line 4

def postid
  @postid
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/blog_post.rb', line 4

def title
  @title
end

Class Method Details

.from_filename(filename) ⇒ Object



15
16
17
18
19
# File 'lib/blog_post.rb', line 15

def self.from_filename filename
  bp = BlogPost.new
  bp.load_from_file filename
  bp
end

.make_file_from_struct(presserDoc) ⇒ Object



21
22
23
24
25
26
# File 'lib/blog_post.rb', line 21

def self.make_file_from_struct presserDoc
  File.open(BlogPost.new_post_filename, "w") do |f|
    f.puts presserDoc.to_s
  end
  BlogPost.new_post_filename
end

.new_post_filenameObject



117
118
119
120
121
# File 'lib/blog_post.rb', line 117

def self.new_post_filename
  prefix = "presser_post_"
  @numfiles ||= Dir.glob(prefix + "*").length
  @new_file_name ||= prefix + (@numfiles + 1).to_s + ".md"
end

Instance Method Details

#dumpselfObject



127
128
129
130
131
132
# File 'lib/blog_post.rb', line 127

def dumpself
  puts "Dumping self"
  puts "title: #{@title}"
  puts "body: #{@body}"
  puts "link: #{@link}"
end

#load_from_file(filename) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/blog_post.rb', line 28

def load_from_file filename
  parse_all_lines filename
  @title = @post_file_items["title"]     || ""
  @body  = @post_file_items["body"]      || ""
  @link  = @post_file_items["link"]      || ""
  @postid = @post_file_items["postid"]   || ""
end

#parse_all_lines(filename) ⇒ Object

Read through a file put all the items describing its post into @post_file_items



53
54
55
56
57
58
59
# File 'lib/blog_post.rb', line 53

def parse_all_lines filename
  lines = []
  File.readlines(filename).each do |line|
    lines << line
  end
  parse_array_of_lines lines
end

#parse_array_of_lines(lines) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/blog_post.rb', line 61

def parse_array_of_lines lines
  body = ""
  lines.each do |line|
    line = line.strip
    if @reading_header
      parse_header_line line
    else
      body << line << "\n"
    end
  end
  # puts "post_file_items: #{@post_file_items}"
  @title = @post_file_items["title"] || "Default title"
  @post_file_items["body"] = body
end

#parse_header_line(line) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/blog_post.rb', line 76

def parse_header_line line
  if line == "# End of header"
    @reading_header = false
  else
    if line =~ /(.*):(.*)/
      # puts "match: #{line}"
      key = $1.strip
      val = $2.strip
      @post_file_items[key] = val
    else
      # puts "No match: #{line}"
    end
  end
end

#parse_string(str) ⇒ Object



46
47
48
49
# File 'lib/blog_post.rb', line 46

def parse_string str
  lines = str.split("\n")
  parse_all_lines lines
end

#parse_struct(struct) ⇒ Object

get the values from a metaWeblog struct



37
38
39
40
41
42
43
44
# File 'lib/blog_post.rb', line 37

def parse_struct struct
  @title       = struct["title"]        || ""
  @body        = struct["body"]         || ""
  @link        = struct["link"]         || ""
  @postid      = struct["postid"]       || ""
  @post_status = struct["post_status"]  || ""
  @categories  = struct["categories"]   || ""
end

#save_new_postObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/blog_post.rb', line 102

def save_new_post
  str = %Q{# Beginning of header
title: #{@title}
categories: #{@categories}
# End of header

}
  # File.open(BlogPost.new_post_filename, "w") { |file| file.puts str }
  f = File.open(BlogPost.new_post_filename, "w")
  f.puts str
  f.flush
  f.close
  BlogPost.new_post_filename
end

#save_to_file(filename) ⇒ Object



123
124
125
# File 'lib/blog_post.rb', line 123

def save_to_file filename
  File.open(filename, "w") { |file| file.puts to_s }
end

#to_sObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/blog_post.rb', line 91

def to_s
  str = %Q{# Beginning of header
title:  #{@title}
link:   #{@link}
categories: #{@categories}
postid: #{@postid}
post_status: #{@post_status}
# End of header
#{@body}}
end