Open snackle opened 11 years ago
Maybe. Things to consider:
I have a few thoughts on how this could be done.
We could implement a script which receives the command, gets the URL parameter, and outputs the shortened URL. I'm thinking it could be written to fire an event with the received URL, which another script could then receive and act on, passing the shortened URL back to be replied.
The handler could raise an error in the event of failure which the script could receive and output differently, or if multiple scripts listen for the event, it could be passed to the next one.
This would eliminate the problem of restricting users to a specific URL shortener service, as well as allow multiple services to be loaded at once in case of one failing.
I may not understand the ins and outs of how events work in Terminus-Bot yet but if this is possible, it would be a good solution in my opinion (it may work well with other scripts like dictionary.rb which recently had to be modified to use a new service).
Regarding implementation, I personally feel altering outgoing messages with URLs in them is a bad idea. What if the output is supposed to be exactly a certain URL, and a shortener would change that URL? What if the URL appears in quotes, or is at the end of a parenthesized expression? If you're not careful, the message My Thing (http://url-to-thing.tumblr.com)
could become My Thing (http://short/12345
, which expands to http://url-to-thing.tumblr.com)
, which is obviously not the intention.
I think it would be much better as an opt-in service for scripts. Shortener modules announce that they are capable of shortening URLs, and then the various scripts can invoke the shortener and use the returned URL in their output. That way there is no confusion as to what should and shouldn't be shortened, the exact string that is being shortened, and its exact position in the output.
I agree with everything @aji said. We should provide a new method of outputting URLs rather than try and overlay the shortener on top of the old method.
Doing it @aji's way, we would need to use a module. It could provide a function to scripts like shorten_url
which returns a shortened URL, or the original URL if there is some failure. Bot configuration could include a minimum_length
option which specifies the minimum length at which a URL should be shortened (no sense in feeding something like google.com
to it and getting something even larger back).
Optional module dependency is not something that exists right now, so we would need to add something to allow for this. It might look like:
if module_loaded? 'url_shortener'
url = shorten_url url
end
reply url
This would clutter up scripts that use it, but I would generally be okay with it.
My rationale for suggesting we avoid this method is that I do not like the clutter. That said, URI.extract is not perfect, so @aji is right...
Yeah, that would definitely be a lot of clutter. I can't think of a way to avoid if module_loaded?
tests without making URL shortening part of the core.
However, if making it a core feature is fine, then perhaps we could provide scripts with an always present shorten_url
method. This method would use any loaded URL shortener modules, and would return the original URL if none are loaded or willing to shorten the given URL.
I think we will just go with module_loaded?
and shorten from there.
Other deal: we need to choose one of:
Personally, I am including to give people a choice, and I would rather not do it with scripts. Maybe. Need input here.
By the way, the reason I am dragging this out in this manner is that (1) I've noticed people can be picky about what provider is used for aesthetic reasons and (2) users may want to choose a specific provider due to some privacy or connectivity concerns.
Unfortunately, there is no real standardized API for URL shorteners, so we can't just have a configuration value that gives some POST URL that always returns the same type of reply. That does apply to many, but unfortunately not enough. If you disagree, I am open to that as well, though.
Would it be possible to include a module for shrinking returned URL's? Would be useful for RSS links, google output and the like.
These gem's may be of assistance: https://github.com/philnash/bitly https://github.com/zigotto/googl