Jonathan
Jonathan

Reputation: 123

Is it better to treat the path after the base URL as an instance of URI or string?

I am implementing a REST client in Ruby and am treating base URLs as instances of URI. For the path after the base URL, I am unsure whether to treat it also as a URI instance or as a string.

Approach A

base_url = URI("http://www.foo.com")
path = URI("/someaction")

Approach B

base_url = URI("http://www.foo.com")
path = "/someaction"

With both of the above approaches, I plan to call URI.join(base_url, path) before making my request. Which of the approaches would be considered a better practice?

Upvotes: 2

Views: 424

Answers (1)

the Tin Man
the Tin Man

Reputation: 160601

You're worrying about something not worth worrying about. Let URI do what it's good at and designed to do:

base_url = URI("http://www.foo.com")
base_url.path = "/someaction"

base_url
=> #<URI::HTTP:0x00000102079d58 URL:http://www.foo.com/someaction>

Move along to something else.

If you need to manipulate a path that has been extracted from a URL, look at split, basename, extname and dirname from the File class. They do it in a nice standardized manner.

Upvotes: 1

Related Questions