Permalink
Browse files

Shorten url ok

  • Loading branch information...
1 parent bef4d38 commit d5cecda872140649a061ab379e5e6f8b9705dd79 Rafael committed Jul 1, 2010
Showing with 227 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +17 −0 README.rdoc
  3. +45 −0 Rakefile
  4. +51 −0 lib/ruby-bitly.rb
  5. +53 −0 ruby-bitly.gemspec
  6. +31 −0 spec/ruby-bitly_spec.rb
  7. +1 −0 spec/spec.opts
  8. +9 −0 spec/spec_helper.rb
View
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Rafael
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,17 @@
+= ruby-bitly
+
+Description goes here.
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 Rafael. See LICENSE for details.
View
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "ruby-bitly"
+ gem.summary = %Q{TODO: one-line summary of your gem}
+ gem.description = %Q{TODO: longer description of your gem}
+ gem.email = "rafaeldx7@gmail.com"
+ gem.homepage = "http://github.com/rafaeldx7/ruby-bitly"
+ gem.authors = ["Rafael"]
+ gem.add_development_dependency "rspec", ">= 1.2.9"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :spec => :check_dependencies
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "ruby-bitly #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
@@ -0,0 +1,51 @@
+require 'rubygems'
+require 'yaml'
+require 'restclient'
+require 'json'
+
+class Bitly
+
+ attr_reader :response, :long_url, :status_code, :status_txt, :new_hash, :global_hash, :hash
+
+ PERSONAL_FILE_PATH = "#{ENV['HOME']}/.bitly"
+ REST_API_URL = "http://api.bit.ly"
+ ACTION_PATH = { :shorten => '/v3/shorten' }
+
+ def initialize(url)
+ @long_url = url
+ Bitly.load_personal_data
+ end
+
+ def Bitly.load_personal_data
+ personal_data = YAML.load(File.read(PERSONAL_FILE_PATH))
+
+ if personal_data
+ @@login = personal_data["login"]
+ @@key = personal_data["key"]
+ personal_data
+ end
+ end
+
+ def shorten
+ unless @response
+ @response = Bitly.shorten(@long_url)
+
+ @status_code = response["status_code"]
+ @status_txt = response["status_txt"]
+ @new_hash = response["data"]["new_hash"]
+ @global_hash = response["data"]["global_hash"]
+ @hash = response["data"]["hash"]
+ @bitly = response["data"]["url"]
+ end
+
+ @bitly
+ end
+
+ def Bitly.shorten(new_long_url)
+ Bitly.load_personal_data
+ response = RestClient.post(REST_API_URL + ACTION_PATH[:shorten], { :longURL => new_long_url, :login => @@login, :apiKey => @@key })
+ JSON.parse(response)
+ end
+
+ alias :bitly :shorten
+end
View
@@ -0,0 +1,53 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ruby-bitly}
+ s.version = ""
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Rafael"]
+ s.date = %q{2010-07-01}
+ s.description = %q{TODO: longer description of your gem}
+ s.email = %q{rafaeldx7@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "lib/ruby-bitly.rb",
+ "spec/ruby-bitly_spec.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb"
+ ]
+ s.homepage = %q{http://github.com/rafaeldx7/ruby-bitly}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{TODO: one-line summary of your gem}
+ s.test_files = [
+ "spec/ruby-bitly_spec.rb",
+ "spec/spec_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ end
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ end
+end
+
@@ -0,0 +1,31 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "RubyBitly" do
+
+ it "Load personal data from ~/.bitly" do
+ Bitly.load_personal_data.should == { "key" => "R_59c1b174b21d92b2beeb4787a6d7ebaf", "login" => "rafaeldx7" }
+ end
+
+ it "Shorten log url staticaly" do
+ response = Bitly.shorten("http://google.com")
+
+ response["status_code"].should == 200
+ response["status_txt"].should == "OK"
+ response["data"]["new_hash"].should == 0
+ response["data"]["global_hash"].should == "zzzzzzz"
+ response["data"]["hash"].length.should_not == 0
+ response["data"]["url"].should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
+ end
+
+ it "Shorten log url OO" do
+ url = Bitly.new("http://google.com")
+ url.shorten.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
+
+ url.status_code.should == 200
+ url.status_txt.should == "OK"
+ url.new_hash.should == 0
+ url.global_hash == "zzzzzzz"
+ url.hash.length.should_not == 0
+ url.bitly.should match /^http:\/\/bit\.ly\/[A-Za-z0-9]*/
+ end
+end
View
@@ -0,0 +1 @@
+--color
View
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'ruby-bitly'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end

0 comments on commit d5cecda

Please sign in to comment.