Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 560e825eb8a257b2e9710ccd0730ce2bc4c1eaf1 @threestage committed Oct 19, 2010
Showing with 75 additions and 0 deletions.
  1. +1 −0 README
  2. +4 −0 lib/yourls.rb
  3. +42 −0 lib/yourls/client.rb
  4. +14 −0 lib/yourls/url.rb
  5. +14 −0 lib/yourls/yourls.rb
1 README
@@ -0,0 +1 @@
+Shorten URLs with a custom Yourls installation
@@ -0,0 +1,4 @@
+require 'httparty'
+require 'yourls/yourls'
+require 'yourls/client'
+require 'yourls/url'
@@ -0,0 +1,42 @@
+require 'digest/md5'
+
+module Yourls
+ class Client
+ include HTTParty
+
+ def initialize(host, api_key)
+ @host, @api_key = host, api_key
+ @base_uri = File.join(host, 'yourls-api.php')
+ end
+
+ def stats(options = {})
+ response = get('stats', options)
+ response.parsed_response
+ end
+
+ def expand(short_url, options = {})
+ Yourls::Url.new(get('expand', options.merge!(:shorturl => short_url)).parsed_response)
+ end
+
+ def shorten(long_url, options = {})
+ Yourls::Url.new(get('shorturl', options.merge!(:url => long_url)).parsed_response)
+ end
+
+ def get(action, query = {})
+ # Prepare the signature
+ timestamp = Time.now.to_i
+ signature = Digest::MD5.hexdigest(timestamp.to_s + @api_key)
+
+ query ||= {}
+ query.merge!(:timestamp => timestamp, :signature => signature, :action => action, :format => 'json')
+
+ response = self.class.get(@base_uri, :query => query)
+
+ if response.code == 200
+ return response
+ else
+ raise YourlsError.new(response.message, response.code)
+ end
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module Yourls
+ class Url
+ attr_reader :short_url, :long_url, :keyword
+
+ def initialize(options = {})
+ if options
+ url = options['url'] || Hash.new('')
+ @short_url = options['shorturl']
+ @long_url = options['longurl'] || url['url']
+ @keyword = options['keyword'] || url['keyword']
+ end
+ end
+ end
+end
@@ -0,0 +1,14 @@
+module Yourls
+ def self.new(host, api_key)
+ Yourls::Client.new(host, api_key)
+ end
+end
+
+class YourlsError < StandardError
+ attr_reader :code
+ alias :msg :message
+ def initialize(msg, code)
+ @code = code
+ super("#{msg} - '#{code}'")
+ end
+end

0 comments on commit 560e825

Please sign in to comment.