def rubygems_api_url(name, version=None, repo='https://rubygems.org/api'):
    """
    Return a package API data URL given a name, an optional version and a base
    repo API URL.

    For instance:
    https://rubygems.org/api/v2/rubygems/action_tracker/versions/1.0.2.json

    If no version, we return:
    https://rubygems.org/api/v1/versions/turbolinks.json

    Unused:
    https://rubygems.org/api/v1/gems/mqlight.json
    """
    repo = repo.rstrip('/')
    if version:
        api_url = '{repo}/v2/rubygems/{name}/versions/{version}.json'
    else:
        api_url = '{repo}/v1/versions/{name}.json'
    return api_url.format(**locals())