class Truncus::Client
Attributes
host[RW]
port[RW]
Public Class Methods
expand_token(token)
click to toggle source
Convenience for instantiating a Client
and retrieving a shortened url
# File lib/truncus.rb, line 22 def self.expand_token(token) new.expand_token(token) end
get_token(url)
click to toggle source
Convenience for instantiating a Client
, shortening a url, and returning the shortened token
# File lib/truncus.rb, line 29 def self.get_token(url) new.get_token(url) end
get_url(url)
click to toggle source
Convenience for instantiating a Client
, shortening a url, and returning it as a full url to the shortened version.
# File lib/truncus.rb, line 36 def self.get_url(url) client = new token = client.get_token(url) "http#{'s' if client.use_ssl?}://#{client.host}/#{token}" end
new(options = {})
click to toggle source
# File lib/truncus.rb, line 12 def initialize(options = {}) self.host = options[:host] || ENV['TRUNCT_HOST'] || 'trunc.us' self.port = options[:port] || ENV['TRUNCT_PORT'] || 443 @http ||= Net::HTTP.new(host, port) @http.use_ssl = use_ssl? end
Public Instance Methods
expand_token(token)
click to toggle source
Expand a token: returns the original URL
# File lib/truncus.rb, line 55 def expand_token(token) url = URI.parse("http#{'s' if use_ssl?}://#{host}/#{token}.json") req = Net::HTTP::Get.new(url.path) res = @http.request(req) data = JSON::parse(res.body) data['trunct']['url'] end
get_token(url)
click to toggle source
Shortens a URL, returns the shortened token
# File lib/truncus.rb, line 44 def get_token(url) req = Net::HTTP::Post.new('/', initheader = {'Content-Type' => 'application/json'}) req.body = {url: url, format: 'json'}.to_json res = @http.request(req) data = JSON::parse(res.body) data['trunct']['token'] end
use_ssl?()
click to toggle source
Is this client configured to use ssl/tls?
# File lib/truncus.rb, line 65 def use_ssl? port.to_i == 443 end