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

Adding a couple badge routes #2103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/controllers/badges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class BadgesController < ApplicationController
before_action :find_project, only: [:dependent_packages, :dependent_repositories]

def dependent_packages
render_badge('Dependent Packages', @project.dependents_count, 'brightgreen')
end

def dependent_repositories
render_badge('Dependent Repos', @project.dependent_repos_count, 'brightgreen')
end

private

def render_badge(title, text, colour)
response.headers["Cache-Control"] = "no-cache, no-store"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
style = params[:style] || 'flat'
redirect_to "https://img.shields.io/badge/#{title}-#{text}-#{colour}.svg?style=#{style}", status: 302
end

def render_error_badge
redirect_to "https://img.shields.io/badge/Libraries.io-unknown-lightgrey.svg?style=#{style}", status: 302
end

def find_project
@project = Project.visible.platform(params[:platform]).where(name: params[:name]).first
render_error_badge if @project.nil?
end
end
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
end
end

scope :badges do
get '/:platform/:name/dependent_repositories', to: 'badges#dependent_repositories', constraints: { :platform => PLATFORM_CONSTRAINT, :name => PROJECT_CONSTRAINT }
get '/:platform/:name/dependent_packages', to: 'badges#dependent_packages', constraints: { :platform => PLATFORM_CONSTRAINT, :name => PROJECT_CONSTRAINT }
end

root to: 'projects#index'

get '/404', to: 'errors#not_found'
Expand Down