module Octospy::Url

Public Class Methods

github_shortener_endpoint() click to toggle source
# File lib/octospy/url.rb, line 17
def github_shortener_endpoint
  'http://git.io/'
end
google_shortener_endpoint() click to toggle source
# File lib/octospy/url.rb, line 30
def google_shortener_endpoint
  'https://www.googleapis.com/urlshortener/v1/'
end
shorten(url) click to toggle source
# File lib/octospy/url.rb, line 6
def shorten(url)
  case
  when url =~ /https?:\/\/(\w+\.)?github\.com/
    self.shorten_by_github url
  when url =~ /https?:\/\/.+/
    self.shorten_by_google url
  else
    url
  end
end
shorten_by_github(url) click to toggle source
# File lib/octospy/url.rb, line 21
def shorten_by_github(url)
  agent = Sawyer::Agent.new(self.github_shortener_endpoint)
  response = agent.call(:post, '', "url=#{url}")
  response.headers[:location]
rescue => e
  puts e.message
  url
end
shorten_by_google(url) click to toggle source
# File lib/octospy/url.rb, line 34
def shorten_by_google(url)
  agent = Sawyer::Agent.new(self.google_shortener_endpoint) do |http|
    http.headers['Content-Type'] = 'application/json'
  end
  response = agent.call(:post, 'url', longUrl: url)
  response.data.attrs[:id]
rescue => e
  puts e.message
  url
end