aitor (owner)

Revisions

gist: 9404 Download_button fork
public
Description:
Fake community
Public Clone URL: git://gist.github.com/9404.git
Embed All Files: show embed
Text #
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'faker'
require 'colored'
 
class Object
  def tap
    yield self
    self
  end
end
class Percentage
  attr_reader :amount
  def initialize(amount)
    @amount = amount
  end
 
  def chance
    Chance.new(self)
  end
 
  def of(number)
    (number * (amount / 100.0)).to_i
  end
 
  class Chance
    attr_reader :odds, :happens
    alias :happens? :happens
    
    def initialize(percent)
      @odds = percent.amount
      @happens = @odds > Kernel.rand(100)
    end
    
    def of(&block)
      yield if happens?
    end
  end
end
 
module Kernel
  def rand_within(range)
    rand(range.max - range.min) + range.min
  end
 
  def maybe(percent = 50.percent, &block)
    chances_of_this_happens 50.percent, &block
  end
 
  def frequently(&block)
    chances_of_this_happens 80.percent, &block
  end
 
  def probably(&block)
    #70.percent.chance.of &block
    chances_of_this_happens 70.percent, &block
  end
 
  def rarely(&block)
    chances_of_this_happens 20.percent, &block
  end
 
  def almost_never(&block)
    chances_of_this_happens 5.percent, &block
  end
  
  def chances_of_this_happens(percent = 50.percent, &block)
    if block_given?
      percent.chance.of &block
    else
      percent.chance.happens?
    end
    
  end
  
end
class Numeric
  def percent
    Percentage.new(self)
  end
end
class Range
  def percent
    Percentage.new(rand_within(self))
  end
end
 
 
def log(text, color=:white)
  puts text.send(color)
end
 
def the_community
  User.count
end
 
def create_user
  User.new.tap do |u|
    u.login = Faker::Internet.user_name
    u.email = Faker::Internet.free_email
    u.password = u.password_confirmation = "perron"
    u.register!
    u.activate!
    log "User ##{u.id} generated. Welcome '#{u.login}'"
  end
end
 
def update_her_profile(user)
  user.profile.first_name = Faker::Name.first_name
  user.profile.last_name = Faker::Name.last_name
  user.profile.website = user.profile.blog = "www." + Faker::Internet.domain_name
  with_random_avatar do |avatar|
    user.profile.icon = avatar
    user.profile.save!
  end
end
def with_random_avatar(&block)
  begin
    uri = URI.parse(Faker::Avatar.avatar)
    tmp_file = uri.path[/.*\/(.*)\z/,1]
    open(tmp_file, "wb") { |file|
        file.write(Net::HTTP.get_response(uri).read_body)
    }
    yield File.new(tmp_file)
    File.delete(tmp_file)
  rescue
    log "Unable to get avatar", :red
  end
end
 
def create_a_group(user)
  Group.new.tap do |g|
    g.name = Faker::Company.catch_phrase
    g.description = Faker::Lorem.paragraphs(rand_within(1..4)).join
    g.author = user
    g.private = maybe
    g.moderated = maybe
    g.tag_list.add Faker::Lorem.words(rand_within(0..6))
    g.save!
 
    with_random_avatar do |avatar|
      g.image = avatar
      g.save!
    end
 
    probably{
      g.activate!
    }
  
    log "Group ##{g.id} generated [#{g.state.upcase}]. Topic: '#{g.name}'", :yellow
  end
end
def random_user
  User.find :first, :offset => rand_within(0..User.count - 1)
end
def this_group_would_be_joined_by(group, number)
  number.times do
    u = random_user
    group.join(u, almost_never)
    group.activate_membership(u) if maybe
  end
  log " └ #{number} users joined it!", :yellow
end
def will_have_a_number_of_friends(user, &block)
  friends = yield
  if friends
    friends.times do
      probably {
        user.profile.add_follower(random_user.profile)
      }
      rarely {
        user.profile.add_friend(random_user.profile)
      }
    end
    log " └ #{user.login} has #{friends} friends."
  end
end
 
100.times do
  a_user = create_user
  frequently do
    update_her_profile(a_user)
  end
  will_have_a_number_of_friends(a_user) do
    probably { rand_within(10..50) } || rarely { 20.percent.of(the_community) }
  end
 
  rarely do
    a_group = create_a_group(a_user)
    a_range_of_users = nil
    frequently do
      a_range_of_users = (1..5).percent.of the_community
    end
    almost_never do
      a_range_of_users = (20..30).percent.of the_community
    end
    this_group_would_be_joined_by(a_group, a_range_of_users) if a_range_of_users
  end
end