Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.5 KiB
Ruby
Raw Permalink Normal View History

2014-11-06 14:51:37 +01:00
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2014-11-06 14:51:37 +01:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2014-11-06 14:51:37 +01:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
2014-11-06 14:51:37 +01:00
#++
module API
2014-12-11 17:16:25 +01:00
module Utilities
class ResourceLinkParser
2015-02-19 10:31:13 +01:00
# N.B. valid characters for URL path segments as of
# http://tools.ietf.org/html/rfc3986#section-3.3
SEGMENT_CHARACTER = '(\w|[-~!$&\'\(\)*+\.,:;=@]|%[0-9A-Fa-f]{2})'.freeze
RESOURCE_REGEX =
"/api/v(?<version>\\d)/(?<namespace>[\\w/]+)/(?<id>#{SEGMENT_CHARACTER}+)\\z".freeze
2014-11-06 14:51:37 +01:00
2015-02-19 13:59:31 +01:00
class << self
def parse(resource_link)
parse_resource(resource_link)
2015-02-19 13:59:31 +01:00
end
def parse_id(resource_link,
2015-06-03 09:30:42 +02:00
property:,
2015-02-19 13:59:31 +01:00
expected_version: nil,
expected_namespace: nil)
2015-06-03 09:30:42 +02:00
raise ArgumentError unless resource_link
2015-02-19 13:59:31 +01:00
resource = parse(resource_link)
if resource
2015-02-19 16:52:07 +01:00
version_valid = matches_expectation?(expected_version, resource[:version])
namespace_valid = matches_expectation?(expected_namespace, resource[:namespace])
2015-02-19 13:59:31 +01:00
end
unless resource && version_valid && namespace_valid
2015-06-03 09:30:42 +02:00
expected_link = make_expected_link(expected_version, expected_namespace)
fail ::API::Errors::InvalidResourceLink.new(property, expected_link, resource_link)
2015-02-19 13:59:31 +01:00
end
resource[:id]
end
2015-02-18 15:36:22 +01:00
2015-02-19 13:59:31 +01:00
private
2015-02-18 15:36:22 +01:00
2015-03-27 10:08:05 +01:00
def parse_resource(resource_link)
match = resource_matcher.match(resource_link)
return nil unless match
{
version: match[:version],
namespace: match[:namespace],
id: unescape(match[:id])
2015-03-27 10:08:05 +01:00
}
end
2015-02-19 13:59:31 +01:00
def resource_matcher
2015-03-27 10:08:05 +01:00
@resource_matcher ||= Regexp.compile(RESOURCE_REGEX)
end
def unescape(string)
@unescaper ||= Addressable::Template.new("{+id}")
@unescaper.extract(string)["id"]
2015-02-19 13:59:31 +01:00
end
2014-12-11 17:16:25 +01:00
2015-02-19 16:52:07 +01:00
# returns whether expectation and actual are identical
# will always be true if there is no expectation (nil)
def matches_expectation?(expected, actual)
expected.nil? || Array(expected).any? { |e| e.to_s == actual }
2015-02-19 16:52:07 +01:00
end
def make_expected_link(version, namespaces)
2015-02-19 13:59:31 +01:00
version = "v#{version}" || ":apiVersion"
namespaces = Array(namespaces || ":resource")
namespaces.map { |namespace| "/api/#{version}/#{namespace}/:id" }
2015-02-19 13:59:31 +01:00
end
2014-11-06 14:51:37 +01:00
end
end
end
end