forked from ankyAgrawal/refactoring-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunding_raised.rb
executable file
·138 lines (124 loc) · 3.5 KB
/
funding_raised.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
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
require 'csv'
require 'rspec'
require 'rspec/expectations'
require 'pry'
class FundingRaised
def self.where(options = {})
csv_data = CSV.read("../startup_funding.csv")
@funding = []
if options[:company_name]
csv_data.select! do |row|
row[1] == options.fetch(:company_name)
end
end
if options[:city]
csv_data.select! do |row|
row[4] == options.fetch(:city)
end
end
if options[:state]
csv_data.select! do |row|
row[5] == options.fetch(:state)
end
end
if options[:round]
csv_data.select! do |row|
row[9] == options.fetch(:round)
end
end
@output = []
csv_data.each do |row|
mapped = {}
mapped['permalink'] = row[0]
mapped['company_name'] = row[1]
mapped['number_employees'] = row[2]
mapped['category'] = row[3]
mapped['city'] = row[4]
mapped['state'] = row[5]
mapped['funded_date'] = row[6]
mapped['raised_amount'] = row[7]
mapped['raised_currency'] = row[8]
mapped['round'] = row[9]
@output << mapped
end
@output
end
def self.find_by(options)
csv_data = CSV.read("../startup_funding.csv")
if options[:company_name]
csv_data.each do |row|
if row[1] == options.fetch(:company_name)
mapped = {}
mapped['permalink'] = row[0]
mapped['company_name'] = row[1]
mapped['number_employees'] = row[2]
mapped['category'] = row[3]
mapped['city'] = row[4]
mapped['state'] = row[5]
mapped['funded_date'] = row[6]
mapped['raised_amount'] = row[7]
mapped['raised_currency'] = row[8]
mapped['round'] = row[9]
return mapped
end
end
end
if options[:city]
csv_data.each do |row|
if row[4] == options.fetch(:city)
mapped = {}
mapped['permalink'] = row[0]
mapped['company_name'] = row[1]
mapped['number_employees'] = row[2]
mapped['category'] = row[3]
mapped['city'] = row[4]
mapped['state'] = row[5]
mapped['funded_date'] = row[6]
mapped['raised_amount'] = row[7]
mapped['raised_currency'] = row[8]
mapped['round'] = row[9]
return mapped
end
end
end
if options[:state]
csv_data.each do |row|
if row[5] == options.fetch(:state)
mapped = {}
mapped['permalink'] = row[0]
mapped['company_name'] = row[1]
mapped['number_employees'] = row[2]
mapped['category'] = row[3]
mapped['city'] = row[4]
mapped['state'] = row[5]
mapped['funded_date'] = row[6]
mapped['raised_amount'] = row[7]
mapped['raised_currency'] = row[8]
mapped['round'] = row[9]
return mapped
end
end
end
if options[:round]
csv_data.each do |row|
if row[9] == options.fetch(:round)
mapped = {}
mapped['permalink'] = row[0]
mapped['company_name'] = row[1]
mapped['number_employees'] = row[2]
mapped['category'] = row[3]
mapped['city'] = row[4]
mapped['state'] = row[5]
mapped['funded_date'] = row[6]
mapped['raised_amount'] = row[7]
mapped['raised_currency'] = row[8]
mapped['round'] = row[9]
return mapped
end
end
end
raise RecordNotFound
end
end
class RecordNotFound < StandardError
end