debian-mirror-gitlab/app/presenters/packages/nuget/service_index_presenter.rb

130 lines
3.4 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
module Nuget
class ServiceIndexPresenter
include API::Helpers::RelatedResourcesHelpers
SERVICE_VERSIONS = {
download: %w[PackageBaseAddress/3.0.0],
search: %w[SearchQueryService SearchQueryService/3.0.0-beta SearchQueryService/3.0.0-rc],
publish: %w[PackagePublish/2.0.0],
metadata: %w[RegistrationsBaseUrl RegistrationsBaseUrl/3.0.0-beta RegistrationsBaseUrl/3.0.0-rc]
}.freeze
SERVICE_COMMENTS = {
download: 'Get package content (.nupkg).',
search: 'Filter and search for packages by keyword.',
publish: 'Push and delete (or unlist) packages.',
metadata: 'Get package metadata.'
}.freeze
2021-03-08 18:12:59 +05:30
VERSION = '3.0.0'
2020-07-28 23:09:34 +05:30
2021-03-08 18:12:59 +05:30
PROJECT_LEVEL_SERVICES = %i[download publish].freeze
GROUP_LEVEL_SERVICES = %i[search metadata].freeze
def initialize(project_or_group)
@project_or_group = project_or_group
2020-07-28 23:09:34 +05:30
end
def version
VERSION
end
def resources
2021-03-08 18:12:59 +05:30
available_services.map { |service| build_service(service) }
.flatten
2020-07-28 23:09:34 +05:30
end
private
2021-03-08 18:12:59 +05:30
def available_services
case scope
when :group
GROUP_LEVEL_SERVICES
when :project
(GROUP_LEVEL_SERVICES + PROJECT_LEVEL_SERVICES).flatten
end
end
2020-07-28 23:09:34 +05:30
def build_service(service_type)
url = build_service_url(service_type)
comment = SERVICE_COMMENTS[service_type]
SERVICE_VERSIONS[service_type].map do |version|
{ :@id => url, :@type => version, :comment => comment }
end
end
def build_service_url(service_type)
full_path = case service_type
when :download
2021-03-08 18:12:59 +05:30
download_service_url
2020-07-28 23:09:34 +05:30
when :search
2021-03-08 18:12:59 +05:30
search_service_url
2020-07-28 23:09:34 +05:30
when :metadata
2021-03-08 18:12:59 +05:30
metadata_service_url
2020-07-28 23:09:34 +05:30
when :publish
2021-03-08 18:12:59 +05:30
publish_service_url
2020-07-28 23:09:34 +05:30
end
expose_url(full_path)
end
2021-03-08 18:12:59 +05:30
def scope
return :project if @project_or_group.is_a?(::Project)
return :group if @project_or_group.is_a?(::Group)
end
def download_service_url
params = {
id: @project_or_group.id,
package_name: nil,
package_version: nil,
package_filename: nil
}
api_v4_projects_packages_nuget_download_package_name_package_version_package_filename_path(
params,
true
)
end
def metadata_service_url
params = {
id: @project_or_group.id,
package_name: nil,
package_version: nil
}
case scope
when :group
api_v4_groups___packages_nuget_metadata_package_name_package_version_path(
params,
true
)
when :project
api_v4_projects_packages_nuget_metadata_package_name_package_version_path(
params,
true
)
end
end
def search_service_url
case scope
when :group
api_v4_groups___packages_nuget_query_path(id: @project_or_group.id)
when :project
api_v4_projects_packages_nuget_query_path(id: @project_or_group.id)
end
end
def publish_service_url
api_v4_projects_packages_nuget_path(id: @project_or_group.id)
end
2020-07-28 23:09:34 +05:30
end
end
end