Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

ImperadorSid
Copy link

Closes #3

@@ -48,3 +49,17 @@
flash[:error_title] = 'Unable to locate link'
redirect '/'
end

get '/:page_hash/stats' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
get '/:page_hash/stats' do
get '/:id/stats' do

@@ -72,5 +72,47 @@
page.must_have_content 'Unable to locate link'
end
end

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@stats = GetShortUrlStatsService.new(@page_hash).get_stats
@stats = ShortUrlStatsService.new(@page_hash).stats

@page = ShortUrlByHashService.new(page_hash).find
end

def get_stats

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<p><%= @visits_message %></p>
<p><%= @stats[:visits_message] %></p>


@stats = GetShortUrlStatsService.new(@page_hash).get_stats

if @stats[:visits_count] > 0

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it '' do
it 'display the number of visits' do

end
end

describe 'when there is not a page associated with the link' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe 'when there is not a page associated with the link' do
describe 'when the page is found' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing specs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Track page visits statistics
2 participants