Permalink
Browse files

Initial release

  • Loading branch information...
1 parent 07339d5 commit 82bacf5e2cec891a171aca79a186fa3a90cdb1ac @ariejan committed Jun 6, 2010
Showing with 109 additions and 6 deletions.
  1. +1 −0 .rvmrc
  2. +3 −0 HISTORY
  3. +4 −3 Rakefile
  4. +7 −0 lib/firefly-client.rb
  5. +59 −0 lib/firefly/client.rb
  6. +1 −0 lib/firefly_client.rb
  7. +31 −3 spec/firefly-client_spec.rb
  8. +3 −0 spec/spec_helper.rb
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm 1.8.7-p249@firefly-client
View
@@ -0,0 +1,3 @@
+= HEAD
+
+ * 2010-06-06 - Initial release [ariejan]
View
@@ -5,13 +5,14 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "firefly-client"
- gem.summary = %Q{A client for the FireFly URL shortener server}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{A client for the Firefly URL shortener server}
+ gem.description = %Q{This is a client Gem for the Firefly URL shortener. It allows you Ruby application to call Firefly directly to shorten URLs.}
gem.email = "ariejan@ariejan.net"
gem.homepage = "http://github.com/ariejan/firefly-client"
gem.authors = ["Ariejan de Vroom"]
+ gem.add_dependency "httparty", ">= 0.5.2"
gem.add_development_dependency "rspec", ">= 1.2.9"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ gem.add_development_dependency "fakeweb", ">= 1.2.8"
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
@@ -0,0 +1,7 @@
+require 'rubygems'
+require 'httparty'
+
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+require 'firefly/client'
View
@@ -0,0 +1,59 @@
+module Firefly
+
+ # Firefly::Client allows you to shorten URLs quickily through a Firefly server.
+ #
+ # Usage is easy:
+ #
+ # firefly = Firefly::Client.new(url, api_key)
+ # firefly.shorten("http://google.com")
+ #
+ class Client
+
+ # Creates a new instance to shorten Firefly URLs
+ #
+ # +url+ is the URL to your Firefly server. E.g. "http://aj.gs".
+ # Note: do not include a trailing slash
+ # +api_key+ is your Firefly API Key.
+ def initialize(url, api_key)
+ @firefly_url = url
+ @api_key = api_key
+ end
+
+ # Shortens the given URL or array of URLs.
+ #
+ # shorten("http://google.com") => "http://aj.gs/7dZ"
+ #
+ # shorten(["http://google.com", "http://yahoo.com"])
+ # => { "http://google.com/" => "http://aj.gs/7dZ",
+ # "http://yahoo.com/" => "http://aj.gs/8Yt" }
+ def shorten(input)
+ if input.is_a?(String)
+ return create_url(input)
+ elsif input.is_a?(Array)
+ result = {}
+ input.each do |inp|
+ result[inp] = create_url(inp)
+ end
+ return result
+ else
+ raise ArgumentError.new('Shorten requires either a url or an array of urls')
+ end
+ end
+
+ protected
+
+ # Shortend +long_url+ and returns the short_url on success
+ def create_url(long_url)
+ begin
+ options = { :query => { :url => long_url, :api_key => @api_key } }
+ result = HTTParty.post(@firefly_url + '/api/add', options)
+
+ if result =~ /Permission denied/i
+ raise "Permission denied. Is your API Key set correctly?" if result.status = 401
+ else
+ return result
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1 @@
+require 'firefly-client'
@@ -1,7 +1,35 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "FireflyClient" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
+
+ describe "successful calls" do
+ before(:each) do
+ FakeWeb.register_uri(:post, /^http:\/\/tst\.hst\/api\/add.*google\.com$/, :body => "http://tst.hst/7dX")
+ FakeWeb.register_uri(:post, /^http:\/\/tst\.hst\/api\/add.*yahoo\.com$/, :body => "http://tst.hst/rX9")
+ @client = Firefly::Client.new('http://tst.hst', 'test')
+ end
+
+ it "should call http://test.host/api/add correctly" do
+ @client.shorten("http://google.com").should eql("http://tst.hst/7dX")
+ end
+
+ it "should shorten multiple urls correctly" do
+ result = @client.shorten(["http://google.com", "http://yahoo.com"])
+ result["http://google.com"].should eql("http://tst.hst/7dX")
+ result["http://yahoo.com"].should eql("http://tst.hst/rX9")
+ end
+ end
+
+ describe "API permission errors" do
+ before(:each) do
+ FakeWeb.register_uri(:post, /^http:\/\/tst\.hst\/api\/add.*google\.com$/, :status => 401, :body => "Permission denied")
+ @client = Firefly::Client.new('http://tst.hst', 'fail')
+ end
+
+ it "should raise an error" do
+ lambda {
+ @client.shorten("http://google.com")
+ }.should raise_error
+ end
+ end
end
View
@@ -3,6 +3,9 @@
require 'firefly-client'
require 'spec'
require 'spec/autorun'
+require 'fakeweb'
+
+FakeWeb.allow_net_connect = false
Spec::Runner.configure do |config|

0 comments on commit 82bacf5

Please sign in to comment.