如何在葡萄的一个区块中创建两条路线?

时间:2017-12-11 15:05:06

标签: ruby-on-rails ruby grape grape-api

我想在一个动作块中捕获2个类似的路线。在Rails5中,我可以轻松地做到这一点。我首先声明:

get ':folder/:file' => 'get#index', :file => /.*/, :folder => /.*/
get ':file' => 'get#index', :file => /.*/

这使得我可以捕获:folder,就像文件夹可以像a/b/c/d...:file一样,最后一个文件名。第二个也允许我只捕获文件名。 这两条路线都针对同一行动。

但是,在Grape中,因为它被声明为块而不是路由到方法定义,所以我必须写两次相同的块...

有没有办法在一个路由参数中同时捕获/as/many/folder/and/file.ext/file.ext?我尝试了可选参数,要求。他们都没有工作。

我使用:folder/:file(两次regexp)背后的原因是我可以单独抓取:folder param和:file param,而无需手动拆分它们。

get ':folder/:file', requirements: { file: /.*/, folder: /.*/ } do
  # params[:folder] and params[:file]
end

get ':file', requirements: { file: /.*/ } do
  # params[:file]. [:folder is empty.]
end

^^我想让他们成为一条路线。如果文件夹存在(嵌套),那么它将在文件夹param中获取,否则文件夹将为nil。

2 个答案:

答案 0 :(得分:3)

确定。我通过尝试和寻找refdocs来找到答案。

get '(:folder/):file', requirements: {  folder: /.*/, file: /.*/ } do

这可以按预期工作。

答案 1 :(得分:0)

示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication139
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement (Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResponse GetAvailabilityResponse { get; set; }
    }
    public class GetAvailabilityResponse
    {
        [XmlElement(Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public GetAvailabilityResult GetAvailabilityResult { get; set; }
    }
    public class GetAvailabilityResult
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public AvailabilityDetails[] AvailabilityDetails { get; set; }
    }
    [XmlInclude(typeof(EADAvailabilityDetails))]
    [Serializable, XmlRoot("AvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class AvailabilityDetails
    {
    }
    [Serializable, XmlRoot("EADAvailabilityDetails", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
    public class EADAvailabilityDetails : AvailabilityDetails
    {
        [XmlArray("EADAvailability", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        [XmlArrayItem("EADAvailabilityResult", Namespace = "http://webservices.talktalkplc.com/NetworkProductAvailabilityCheckerService")]
        public EADAvailabilityResult[] EADAvailabilityResult { get; set; }
    }
    public class EADAvailabilityResult
    {
        public string CollectorNodeExchangeCode { get; set; }
        public string CollectorNodeExchangeName { get; set; }
        public int Distance { get; set; }
        public EADBandwidth EADBandwidth { get; set; }
        public string EADSubType { get; set; }
        public string FibreExchangeCode { get; set; }
        public string FibreExchangename { get; set; }
        public string IndicativeECC { get; set; }
        public string IndicativeOrderCategory { get; set; }
        public string LocalExchangeCode { get; set; }
        public string LocalExchangeName { get; set; }
        public int ORLeadTime { get; set; }
        public string OrderCategoryExplanation { get; set; }
        public int TTLeadTime { get; set; }
        public string Zone { get; set; }
    }
    public class EADBandwidth
    {
        [XmlElement(ElementName = "string", Type = typeof(string), Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
        public string String { get; set; }
    }







}

路线:

desc 'Create transaction'
params do
  requires :id, type: String
  requires :from_, type: String
end
post ['/:id/addresses/:from_/transactions', '/:id/transactions'] do

end
相关问题