summaryrefslogtreecommitdiff
path: root/lib/api/licenses.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/api/licenses.rb')
-rw-r--r--lib/api/licenses.rb57
1 files changed, 44 insertions, 13 deletions
diff --git a/lib/api/licenses.rb b/lib/api/licenses.rb
index 7fffbef84e4..dac29df30f9 100644
--- a/lib/api/licenses.rb
+++ b/lib/api/licenses.rb
@@ -1,27 +1,58 @@
module API
# Licenses API
class Licenses < Grape::API
- YEAR_TEMPLATE_REGEX = /(\[|<|{)(year|yyyy)(\]|>|})/
- FULLNAME_TEMPLATE_REGEX = /\[fullname\]/
+ PROJECT_TEMPLATE_REGEX =
+ /[\<\{\[]
+ (project|description|
+ one\sline\s.+\swhat\sit\sdoes\.) # matching the start and end is enough here
+ [\>\}\]]/xi
+ YEAR_TEMPLATE_REGEX = /[<{\[](year|yyyy)[>}\]]/i
+ FULLNAME_TEMPLATE_REGEX =
+ /[\<\{\[]
+ (fullname|name\sof\s(author|copyright\sowner))
+ [\>\}\]]/xi
+
+ # Get the list of the available license templates
+ #
+ # Parameters:
+ # popular - Filter licenses to only the popular ones
+ #
+ # Example Request:
+ # GET /licenses
+ # GET /licenses?popular=1
+ get 'licenses' do
+ options = {
+ featured: params[:popular].present? ? true : nil
+ }
+ present Licensee::License.all(options), with: Entities::License
+ end
# Get text for specific license
#
# Parameters:
# key (required) - The key of a license
- # fullname - Reository owner fullname
+ # project - Copyrighted project name
+ # fullname - Full name of copyright holder
+ #
# Example Request:
# GET /licenses/mit
- get 'licenses/:key', requirements: { key: /[\w.-]*/ } do
- env['api.format'] = :txt
- license = Licensee::License.find(params[:key]).try(:text)
+ #
+ get 'licenses/:key', requirements: { key: /[\w\.-]+/ } do
+ required_attributes! [:key]
+
+ not_found!('License') unless Licensee::License.find(params[:key])
+
+ # We create a fresh Licensee::License object since we'll modify its
+ # content in place below.
+ license = Licensee::License.new(params[:key])
+
+ license.content.gsub!(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
+ license.content.gsub!(PROJECT_TEMPLATE_REGEX, params[:project]) if params[:project].present?
+
+ fullname = params[:fullname].presence || current_user.try(:name)
+ license.content.gsub!(FULLNAME_TEMPLATE_REGEX, fullname) if fullname
- if license
- license
- .gsub(YEAR_TEMPLATE_REGEX, Time.now.year.to_s)
- .gsub(FULLNAME_TEMPLATE_REGEX, params[:fullname])
- else
- error!('License not found', 404)
- end
+ present license, with: Entities::License
end
end
end