altmetric / embiggen

A Ruby library to expand shortened URLs
https://rubygems.org/gems/embiggen
MIT License
124 stars 6 forks source link

Simplify interface to Configuration.shorteners #2

Closed mudge closed 8 years ago

mudge commented 9 years ago

As raised by @sferik on Twitter:

I’d encourage you to reconsider the necessity of the existence of the shorteners class method. It will be a maintenance nightmare.

It still seems valuable to provide a default list of shorteners but to encourage users to supply their own.

The current implementation interrogates Configuration.shorteners with any? and expects to be yielded a series of domains that respond to to_s.

If the interface between Embiggen::URI and Configuration.shorteners was simplified to include? (or something similar) then it would be possible to replace a static list of shorteners with much more powerful options, e.g.

require 'bitly'

class BitlyDomains
  extend Forwardable
  attr_reader :client
  def_delegator :client, :pro?, :include?

  def initialize(client)
    @client = client
  end
end

Bitly.use_api_version_3
Bitly.configure do |config|
  config.api_version = 3
  config.access_token = ENV.fetch('BITLY_ACCESS_TOKEN')
end

Embiggen.configure do |config|
  config.shorteners = BitlyDomains.new(Bitly.client)
end
mudge commented 8 years ago

Fixed in 1.0.0.