class Object
Public Instance Methods
to_short(access_token, options = {})
click to toggle source
Attributes¶ ↑
-
access_token
- Bit.ly API key.
Options¶ ↑
You may which to break out options as a separate item since there maybe multiple items. Note options are prefixed with a colon, denoting them as a
-
:domain
- The short domain to use; either bit.ly, j.mp, or bitly.com or a custom short domain. The default for this parameter is the short domain selected by each user in their bitly account settings.
Examples¶ ↑
URI('http://example.org/').to_short('YOUR_API_KEY')
# File lib/short_url/core_ext.rb, line 27 def to_short(access_token, options = {}) uri = URI.parse('https://api-ssl.bitly.com'.concat('/v3/shorten?'.concat({:longUrl => self.to_s, :access_token => access_token}.merge(options).collect { |k, v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')))) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(uri.request_uri) res = https.request(request) begin sh_uri = JSON.parse(res.body)['data']['url'] rescue self end if sh_uri =~ URI::regexp URI(sh_uri) else self end end