Skip to content

Commit 731212c

Browse files
committed
Added logic in to protect from nil attributes
1 parent 3feb8d4 commit 731212c

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

app/presenters/user_presenter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def starred_repos
2323
end
2424

2525
def commit_mess
26-
if user.commit_messages == "[]"
26+
if user.commit_messages == "[\"User has no commits\"]"
2727
["You have no commit messages"]
2828
else
2929
user.commit_messages.gsub("\"","").split(", ")[2..-2]
@@ -67,7 +67,7 @@ def longest_streaks
6767
end
6868

6969
def followers_activities
70-
if user.follower_messages == "[]"
70+
if user.follower_messages == "{}"
7171
["None of your friends are coding"]
7272
else
7373
user.follower_messages.gsub("\"","").gsub("\\","").gsub("{","").gsub("}","").split(", ")

app/services/github_service.rb

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,59 @@ def total_starred_repos(user)
4242
end
4343

4444
def commit_message(user)
45-
list = parse(connection.get("/users/#{user.nickname}/events"))
46-
events = list.select { |item| item[:type] == "PushEvent" }
47-
commits = events.map{ |event| event[:payload][:commits] }.flatten!
48-
messages = commits.collect{ |commit| commit[:message] }
49-
messages[0..9]
45+
list = parse(connection.get("/users/#{user.nickname}/events"))
46+
if list == "[]"
47+
["User has no events"]
48+
else
49+
find_events_from_list(list)
50+
end
5051
end
5152

5253
def followers_commit_message(name)
5354
list = parse(connection.get("/users/#{name}/events"))
5455
if list == "[]"
5556
["User has no events"]
5657
else
57-
events = list.select { |item| item[:type] == "PushEvent" }
58-
commits = events.map{ |event| event[:payload][:commits] }.flatten!
59-
messages = commits.collect{ |commit| commit[:message] }
60-
messages[0]
58+
find_events_from_list(list)
6159
end
6260
end
6361

62+
def find_events_from_list(list)
63+
events = list.select { |item| item[:type] == "PushEvent" }
64+
if events == "[]"
65+
["User has no Push Events"]
66+
else
67+
find_commits_from_events(events)
68+
end
69+
end
70+
71+
def find_commits_from_events(events)
72+
commits = events.map{ |event| event[:payload][:commits] }.flatten!
73+
if commits.nil?
74+
["User has no commits"]
75+
else
76+
find_messages_from_commits_user(commits)
77+
end
78+
end
79+
80+
def find_messages_from_commits_user(commits)
81+
messages = commits.collect{ |commit| commit[:message] }
82+
if messages == "[]"
83+
["User has no commit messages"]
84+
else
85+
messages[0..9]
86+
end
87+
end
88+
89+
# def find_messages_from_commits(commits)
90+
# messages = commits.collect{ |commit| commit[:message] }
91+
# if messages == "[]"
92+
# ["User has no commit messages"]
93+
# else
94+
# messages[0]
95+
# end
96+
# end
97+
6498
def followers_activity(user)
6599
names = find_user_follows(user).collect {|item| item[:login] }
66100
messages = names.map do |name|

0 commit comments

Comments
 (0)