Sha256: a104c3f7110c12965aa79565ad7467889982cf0a221258ee36cc4b5eef2e2912

Contents?: true

Size: 1.07 KB

Versions: 46

Compression:

Stored size: 1.07 KB

Contents

#
# bitly.rb - shorten URL by bit.ly.
#
# usage: shorten_url_you_got = bitly( 'long_url' ) || long_url
#        (because bitly returns nil sometime.)
#
# required some options below:
#    @options['biyly.login'] : your login ID of bit.ly.
#    @options['biyly.key']   : your API key of biy.ly.
#
# Copyright (C) 2010, TADA Tadashi <t@tdtds.jp>
# You can redistribute it and/or modify it under GPL.
#

require 'json'
require 'net/http'

def bitly( long_url )
	return nil if !long_url or long_url.empty?

	@bitly_cache ||= {} # cached only on memory
	return @bitly_cache[long_url] if @bitly_cache[long_url]

	login = @conf['bitly.login']
	key = @conf['bitly.key']

	# proxy
	px_host, px_port = (@conf['proxy'] || '').split( /:/ )
	px_port = 80 if px_host and !px_port

	query = "/v3/shorten?longUrl=#{CGI::escape long_url}&login=#{login}&apiKey=#{key}&format=json"
	begin
		Net::HTTP.version_1_2
		res = Net::HTTP::Proxy(px_host, px_port).get('api.bit.ly', "#{query}")
		@bitly_cache[long_url] = JSON::parse(res, &:read)['data']['url']
	rescue TypeError => te# biy.ly returns an error.
		nil
	end
end

Version data entries

46 entries across 40 versions & 2 rubygems

Version Path
tdiary-contrib-5.3.0 plugin/bitly.rb
tdiary-contrib-5.2.4 plugin/bitly.rb
tdiary-contrib-5.2.3 plugin/bitly.rb
tdiary-contrib-5.2.2 plugin/bitly.rb
tdiary-contrib-5.2.1 plugin/bitly.rb
tdiary-contrib-5.2.0 plugin/bitly.rb
tdiary-contrib-5.1.7 plugin/bitly.rb
tdiary-contrib-5.1.6 plugin/bitly.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/plugin/bitly.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/bundler/gems/tdiary-contrib-d1e41204db13/plugin/bitly.rb
tdiary-contrib-5.1.5 plugin/bitly.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/plugin/bitly.rb
tdiary-contrib-5.1.4 plugin/bitly.rb
tdiary-contrib-5.1.3 plugin/bitly.rb
tdiary-contrib-5.1.2 plugin/bitly.rb
tdiary-contrib-5.1.1 plugin/bitly.rb
tdiary-contrib-5.1.0 plugin/bitly.rb
tdiary-contrib-5.0.13 plugin/bitly.rb
tdiary-contrib-5.0.12 plugin/bitly.rb
tdiary-contrib-5.0.11 plugin/bitly.rb