Help with reorganizing my files structures (hash_routes)

32 views
Skip to first unread message

Diego Viola

unread,
Oct 29, 2021, 12:27:20 AM10/29/21
to Roda
Hi,

I have the following API code that I've been working on:


I'm currently stuck trying to extract the v1 part to its own file, I'd like to re-use the authentication from the external.rb and split out v1 so that if I were to add v2, the file won't grow too big.

I'm kind of new to Roda and am stuck with this, I would appreciate any suggestions.

Thanks in advance.

Diego

Jeremy Evans

unread,
Oct 29, 2021, 1:05:12 AM10/29/21
to ruby...@googlegroups.com
The important thing to realize is that the default argument for r.hash_routes is the already matched path.  You can make things easier by making the namespace for your hash routes use the same path. You should be able to do:

# routes/api.rb
hash_branch 'api' do |r|
  r.hash_routes
end

# routes/api/external.rb
hash_branch '/api', 'external' do |r|
  auth_token = ...
  JWT.decode()

  r.hash_routes
end

# routes/api/external/v1.rb
hash_branch '/api/external', 'v1' do |r|
  r.is 'hostesses' do
    # rest of block abbreviated
  end
end

Thanks,
Jeremy


Diego Viola

unread,
Oct 29, 2021, 1:35:35 AM10/29/21
to ruby...@googlegroups.com
On Fri, Oct 29, 2021 at 2:05 AM Jeremy Evans <jeremy...@gmail.com> wrote:
>
> On Thu, Oct 28, 2021 at 9:27 PM Diego Viola <diego...@gmail.com> wrote:
>>
>> Hi,
>>
>> I have the following API code that I've been working on:
>>
>> https://gist.github.com/diegoviola/41732846978bf843ad705ce73d0097f6#file-external-rb-L17-L22
>>
>> I'm currently stuck trying to extract the v1 part to its own file, I'd like to re-use the authentication from the external.rb and split out v1 so that if I were to add v2, the file won't grow too big.
>>
>> I'm kind of new to Roda and am stuck with this, I would appreciate any suggestions.
>
>
> The important thing to realize is that the default argument for r.hash_routes is the already matched path. You can make things easier by making the namespace for your hash routes use the same path. You should be able to do:

Makes sense, thank you so much Jeremy. That indeed works.

Diego
Reply all
Reply to author
Forward
0 new messages