-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add route to show stats about page visits #9
Conversation
@@ -48,3 +49,17 @@ | |||
flash[:error_title] = 'Unable to locate link' | |||
redirect '/' | |||
end | |||
|
|||
get '/:page_hash/stats' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get '/:page_hash/stats' do | |
get '/:id/stats' do |
@@ -72,5 +72,47 @@ | |||
page.must_have_content 'Unable to locate link' | |||
end | |||
end | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line
get '/:page_hash/stats' do | ||
@page_hash = params['page_hash'] | ||
|
||
@stats = GetShortUrlStatsService.new(@page_hash).get_stats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stats = GetShortUrlStatsService.new(@page_hash).get_stats | |
@stats = ShortUrlStatsService.new(@page_hash).stats |
@page = ShortUrlByHashService.new(page_hash).find | ||
end | ||
|
||
def get_stats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def get_stats | |
def stats | |
page_visits = PageVisit.where(page_id: page.id) | |
visits_count = page_visits.count | |
{ | |
target_url: page.target_url, | |
visits_count: visits_count, | |
recent_visits: page_visits.select(:visited_at).order(Sequel.desc(:visited_at)).limit(3).map(&:visited_at), | |
visits_message: visits_count > 0 ? "Visits: #{visits_count}" : 'This link has no visits yet' | |
} | |
end |
@@ -0,0 +1,9 @@ | |||
<p>URL: <%= @page_hash %></p> | |||
<p>Target: <%= @stats[:target_url] %></p> | |||
<p><%= @visits_message %></p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<p><%= @visits_message %></p> | |
<p><%= @stats[:visits_message] %></p> |
|
||
@stats = GetShortUrlStatsService.new(@page_hash).get_stats | ||
|
||
if @stats[:visits_count] > 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic could live within the service
@@ -0,0 +1,21 @@ | |||
class GetShortUrlStatsService |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think specs are missing for this service
end | ||
|
||
describe 'and has some stats' do | ||
it '' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it '' do | |
it 'display the number of visits' do |
end | ||
end | ||
|
||
describe 'when there is not a page associated with the link' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
describe 'when there is not a page associated with the link' do | |
describe 'when the page is found' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing specs
Closes #3