plexus / yaks

Ruby library for building hypermedia APIs
http://rubygems.org/gems/yaks
MIT License
236 stars 26 forks source link

Make URLs absolute #84

Open plexus opened 9 years ago

plexus commented 9 years ago

Using an after :map hooks, it's easy to turn all links and form actions that use relative URLs into absolute ones based on the current request. It would be nice to have this baked in

Yaks.new do
  make_links_absolute
end
danelowe commented 9 years ago

Hey @plexus I'm keen to have a look into this one, if I understand what you're suggesting.

I'm just about to move parts of my API into namespaces, e.g. /admin/jobs/ and /frontend/jobs/

Each would re-use the same endpoints and probably the same mappers in most cases. As part of the process I've had to change all the links and form actions in mappers from absolute to relative (relative to the API namespace) and was thinking I would have to do something to get the namespace back into the absolute URL during serialization.

Would something like this be part of the use case?

plexus commented 9 years ago

This is what I have in mind. That's a basic implementation right there, except that this one only handles links, whereas the full feature should also consider forms.

I think this would work for your use case, as you can use relative URIs that would expand against the URI of the current request.

That example is how I originally imagined it would work, with a setting that would add that hook, but now that there's talk of having Yaks::Behavior::* modules to mix in to mappers I think that might be a better approach, so having include Yaks::Behavior::AbsoluteLinks in your mapper.

plexus commented 9 years ago

Now with link https://gist.github.com/plexus/8adca553e38b96988182