Sha256: c82afbbae41c7005d07af1071e4e3daf1e3918bc2eb9e0378534df4d908ef158
Contents?: true
Size: 1019 Bytes
Versions: 1
Compression:
Stored size: 1019 Bytes
Contents
require 'link_shrink/version' require 'typhoeus' require 'json' require 'uri' # @author Jonah Ruiz <jonah@pixelhipsters.com> # Creates a short URL using Google's URL Shortener API module LinkShrink # Returns a short URL or JSON response # # @param url [String] long URL to be shortened # @param options [Hash] format to be returned # @return [String] generated short URL or JSON response def self.shrink_url(url, options = {:json => false}) res = request(URI.encode(url)) options.fetch(:json, nil) ? cleanup_json(res.body) : parse_json(res.body)['id'] rescue 'Problem generating short URL. Try again.' end private def self.request(url) Typhoeus::Request.new( 'https://www.googleapis.com/urlshortener/v1/url', method: :post, body: { 'longUrl' => url }.to_json, headers: { 'Content-Type' => 'application/json' } ).run end def self.parse_json(data) JSON.parse(data) end def self.cleanup_json(data) data.gsub(/\s+/, '') end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
link_shrink-0.0.1 | lib/link_shrink.rb |