Skip to content

Instantly share code, notes, and snippets.

@juangesino
Created April 4, 2016 14:24
Show Gist options
  • Save juangesino/09fadfd3e4cd79a9652878b98eaa49b1 to your computer and use it in GitHub Desktop.
Save juangesino/09fadfd3e4cd79a9652878b98eaa49b1 to your computer and use it in GitHub Desktop.
require 'json'
require 'typhoeus'
class UrlCharts
def initialize(key = nil)
if !key || key.empty?
raise 'No API key defined.'
else
@api_key = key
end
end
def charturl_url(template_slug, options)
url = "https://charturl.com/short-urls.json?api_key=#{@api_key}"
headers = {'Content-Type' => 'application/json'}
body = {template: template_slug, options: options}.to_json
surl_response = Typhoeus::Request.post(url, body: body, headers: headers)
raise("Error creating ShortURL: #{surl_response.inspect}") if !surl_response.success?
JSON.parse(surl_response.body)['short_url']
end
def create_url(slug, data)
url = charturl_url(slug, data: data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment