Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 78 lines (66 sloc) 2.294 kb
6ab0f4a Update gh-pages.
Robert Jackson authored
1 require 'faker'
2 require 'date'
3 require 'csv'
4 require 'pry'
5
6 class String
7 def titleize
8 self.split(/\s/).map(&:capitalize).join(' ')
9 end
10 end
11
12 class DemoCSV
13 attr_reader :cities, :header, :companies, :count
14 def initialize(count, cities=nil)
15 @count = count
16 @header = ["last_name", "first_name", "employer", "billed_amount", "payment_amount", "invoice_date", "last_payment_date", "city", "state", "zip_code"]
17
18 @companies = ["Acme, inc.","Widget Corp","123 Warehousing","Demo Company","Smith and Co.","Foo Bars","ABC Telecom","Fake Brothers","QWERTY Logistics","Sample Company",
19 "Sample, inc","Acme Corp","Mr. Sparkle","Globex Corporation","LexCorp","LuthorCorp","North Central Positronics","Omni Consimer Products",
20 "Praxis Corporation","Sombra Corporation","Wayne Enterprises","Wentworth Industries","ZiffCorp","Bluth Company","Strickland Propane","Water and Power",
21 "Western Gas & Electric","Mammoth Pictures","Mooby Corp","Gringotts"]
22
23 @cities = CSV.table('./lib/csv/cities.csv')
24 end
25
26 def generate_csv
27 CSV.generate do |csv|
28 csv << header
29 count.times do |i|
379d51d Use 5000 records for sample CSV.
Robert Jackson authored
30 invoice_date = bucket
31 last_billed_date = time_rand(invoice_date)
6ab0f4a Update gh-pages.
Robert Jackson authored
32 city = cities[rand(29)]
33
34 csv << [ Faker::Name.last_name,
35 Faker::Name.first_name,
36 companies.sample,
37 "#{rand(1000)}.#{rand(10)}".ljust(2),
38 "#{rand(1000)}.#{rand(10)}".ljust(2),
39 invoice_date.to_date.rfc2822,
40 last_billed_date.to_date.rfc2822,
41 city[:city].titleize,
42 city[:state],
43 city[:zip] ]
44 end
45 end
46 end
47
379d51d Use 5000 records for sample CSV.
Robert Jackson authored
48 def bucket
49 number = rand(0..10)
50 age = case number
51 when 0..4
52 rand(0..30)
53 when 5
54 rand(31..60)
55 when 6
56 rand(61..90)
57 when 7
58 rand(91..120)
59 when 8..10
60 rand(121..600)
61 end
62
63 Time.at(Time.now.to_i - age * 60 * 60 * 24)
64 end
65
6ab0f4a Update gh-pages.
Robert Jackson authored
66
67 def to_s
68 puts generate_csv
69 end
70
71 def to_file(path)
72 File.open(path,"w") {|f| f.write(generate_csv)}
73 end
74
379d51d Use 5000 records for sample CSV.
Robert Jackson authored
75 def time_rand(from=0,to=Time.now)
76 Time.at(rand(from.to_i..to.to_i))
6ab0f4a Update gh-pages.
Robert Jackson authored
77 end
78 end
Something went wrong with that request. Please try again.