-
Notifications
You must be signed in to change notification settings - Fork 0
/
theblog.rb
94 lines (86 loc) · 2.93 KB
/
theblog.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'io/console'
require 'colorize'
#Blog class. the array holds every post created under the blog
class Blog
def initialize
@theseposts = Array.new
end
def add_new_post(post)
@theseposts << post
end
#create_front_page prepares arrays to be shown in pages. Creates pagination and indexes.
def create_front_page
@theseposts = @theseposts.reverse
@arraystoshow = Array.new
@theseposts.each_slice(3) do |post|
@arraystoshow << post
end
end
#publish_front_page extracts each post inserted in the arrays of arrays of posts and publishes them
def publish_front_page
color = :green
@arraystoshow.each_with_index do |little_array, page|
little_array.each do|post|
puts post.show_post(color)
end
puts ("*****************************THIS IS THE PAGE #{page + 1} \n\n\n").colorize(color)
color = color_for_page(page)
sleep 0.1 while STDIN.getch != "["
end
end
#This method is called in publish_front_page just to assign the color to the page. It returns the value of color (line 27)
def color_for_page(page)
if page.odd?
:green
else
:blue
end
end
end
#Post class
class Post
attr_accessor :title
attr_accessor :post
def initialize(title, text)
@title = title
@text = text
@date = Time.new.strftime("Published on %m/%d/%Y")
end
def show_post(color)
puts (@title).colorize(color)
puts "****************".colorize(color)
puts (@text).colorize(color)
puts "------------".colorize(color)
puts (@date).colorize(color)
end
end
#Advertising Post class. Ads format is different than Posts format.
class Ad < Post
def show_post(color)
puts "Advertise Disclaimer: This is a Paid Post".colorize(color)
puts ("\n*********" + @title + "*********").colorize(color)
puts (@text).colorize(color)
puts "------------".colorize(color)
puts (@date).colorize(color)
end
end
#Creating and showing stuff (in the future it would be nice to create a methods to get it from keyboard)
my_blog = Blog.new
first_post = Post.new("Hello world", "This is my first post")
second_post = Post.new("The Second Post", "Just imagine I have something to say")
paid_post = Ad.new("GET 1000$ CASH (and a very old flipflop)", "You just have to be really annoying to get them. Hurry up!!!")
third_post = Post.new("The Third Wheel", "So you don't fall if you ride a bike")
fourth_post = Post.new("The Fourth Hockey", "The Apocallypsis Now is coming")
fifth_post = Post.new("The Fifth Element", "Well, now this is stupid and I have NOTHING to say. This should be a file")
sixth_post = Post.new("The Sixth Killer", "Six is the devil number")
paid_post_2 = Ad.new("GET 50000$ CASH", "SEND US YOUR ZIP CODE AND A GREEN MONKEY!!!")
my_blog.add_new_post(first_post)
my_blog.add_new_post(second_post)
my_blog.add_new_post(paid_post)
my_blog.add_new_post(third_post)
my_blog.add_new_post(fourth_post)
my_blog.add_new_post(fifth_post)
my_blog.add_new_post(sixth_post)
my_blog.add_new_post(paid_post_2)
my_blog.create_front_page
my_blog.publish_front_page