-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.rb
54 lines (41 loc) · 1.28 KB
/
io.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
def read_from_json filename
results = File.read(filename)
parsed_response_body = JSON.parse(results)
parsed_response_body['statuses']
end
def read_from_csv filename
results = []
File.open(filename).each do |line|
if line.start_with? '<'
results << line
end
end
tweets_json = []
results.each do |line|
fields = line.split()
fake_tweet = Hash.new
fake_tweet["user"] = Hash.new
fake_tweet["user"]["name"] = fields[0]
fake_tweet["text"] = line
tweets_json << fake_tweet
end
tweets_json
end
def add_values_to_hash filename, current_list_of_moods=nil, separator=' '
moods = current_list_of_moods || Hash.new
File.open(filename).each do |line|
fields = line.split(separator)
moods[fields[0]] = fields[1]
end
moods
end
def write_line status
"#{status["user"]["name"]}, #{status["mood"]}, #{status["text"]}"
end
def write_results filename, tweets
File.open(filename, 'w') do |file|
tweets.each do |status|
file.puts write_line status
end
end
end