philnash-bitly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 0.1.0 / 2009-01-26
2
+
3
+ * 1 major enhancement
4
+
5
+ * First release
6
+
data/README.txt ADDED
@@ -0,0 +1,58 @@
1
+ = bitly
2
+
3
+ == DESCRIPTION:
4
+
5
+ A Ruby API for bit.ly
6
+
7
+ http://code.google.com/p/bitly-api/wiki/ApiDocumentation
8
+
9
+ == USAGE:
10
+
11
+ Create a Bitly client using your username and api key as follows:
12
+
13
+ bitly = Bitly.new(username, api_key)
14
+
15
+ You can then use that client to shorten or expand urls or return more information or statistics as so:
16
+
17
+ bitly.shorten('http://www.google.com')
18
+ bitly.expand('wQaT')
19
+ bitly.info('http://bit.ly/wQaT')
20
+ bitly.stats('http://bit.ly/wQaT')
21
+
22
+ Each can be used in all the methods described in the API docs, the shorten function, for example, takes a url or an array of urls.
23
+
24
+ All four functions return a Bitly::Url object (or an array of Bitly::Url objects if you supplied an array as the input). You can then get all the information required from that object.
25
+
26
+ u = bitly.shorten('http://www.google.com') #=> Bitly::Url
27
+
28
+ u.long_url #=> "http://www.google.com"
29
+ u.short_url #=> "http://bit.ly/Ywd1"
30
+ u.user_hash #=> "Ywd1"
31
+ u.hash #=> "2V6CFi"
32
+ u.info #=> a ruby hash of the JSON returned from the API
33
+ u.stats #=> a ruby hash of the JSON returned from the API
34
+
35
+ == LICENSE:
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2009 Phil Nash
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'echoe'
6
+ require './lib/bitly.rb'
7
+
8
+ Echoe.new('bitly', Bitly::VERSION) do |p|
9
+ p.description = "Use the bit.ly API to shorten or expand URLs"
10
+ p.url = "http://github.com/philnash/bitly"
11
+ p.author = "Phil Nash"
12
+ p.email = "philnash@gmail.com"
13
+ p.development_dependencies = []
14
+ end
15
+
16
+ # vim: syntax=Ruby
data/bitly.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{bitly}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Phil Nash"]
9
+ s.date = %q{2009-01-26}
10
+ s.description = %q{Use the bit.ly API to shorten or expand URLs}
11
+ s.email = %q{philnash@gmail.com}
12
+ s.extra_rdoc_files = ["lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/version.rb", "lib/bitly.rb", "README.txt"]
13
+ s.files = ["History.txt", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/version.rb", "lib/bitly.rb", "Rakefile", "README.txt", "test/test_bitly.rb", "Manifest", "bitly.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/philnash/bitly}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitly", "--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{bitly}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Use the bit.ly API to shorten or expand URLs}
21
+ s.test_files = ["test/test_bitly.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
@@ -0,0 +1,151 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ module Bitly
7
+ API_URL = 'http://api.bit.ly/'
8
+ API_VERSION = '2.0.1'
9
+ # login = 'philnash'
10
+ # api_key = 'R_7776acc394294b2b0ad2c261a91c483d'
11
+
12
+ def self.new(login, api_key)
13
+ Bitly::Client.new(login,api_key)
14
+ end
15
+
16
+ class Client
17
+
18
+ include Bitly::Utils
19
+
20
+ def initialize(login,api_key)
21
+ @login = login
22
+ @api_key = api_key
23
+ end
24
+
25
+ def shorten(input)
26
+ if input.is_a? String
27
+ request = create_url "shorten", :longUrl => input
28
+ result = get_result(request)
29
+ result = {:long_url => input}.merge result[input]
30
+ Bitly::Url.new(@login,@api_key,result)
31
+ elsif input.is_a? Array
32
+ request = create_url "shorten"
33
+ request.query << "&" + input.map { |long_url| "longUrl=#{URI.encode(long_url)}" }.join("&") unless input.nil?
34
+ result = get_result(request)
35
+ input.map do |long_url|
36
+ new_url = {:long_url => long_url}.merge result[long_url]
37
+ long_url = Bitly::Url.new(@login,@api_key,new_url)
38
+ end
39
+ else
40
+ raise ArgumentError
41
+ end
42
+ end
43
+
44
+ def expand(input)
45
+ if input.is_a? String
46
+ if input.include? "bit.ly/"
47
+ hash = input.gsub(/^.*bit.ly\//,'')
48
+ request = create_url "expand", :hash => hash
49
+ result = get_result(request)
50
+ result = { :short_url => input, :hash => hash }.merge result[hash]
51
+ else
52
+ request = create_url "expand", :hash => input
53
+ result = get_result(request)
54
+ result = { :hash => input, :short_url => "http://bit.ly/#{input}" }.merge result[input]
55
+ end
56
+ Bitly::Url.new(@login,@api_key,result)
57
+ elsif input.is_a? Array
58
+ request = create_url "expand", :hash => input.join(',')
59
+ result = get_result(request)
60
+ input.map do |hsh|
61
+ new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
62
+ hsh = Bitly::Url.new(@login,@api_key,new_url)
63
+ end
64
+ else
65
+ raise ArgumentError
66
+ end
67
+ end
68
+
69
+ def info(input)
70
+ if input.is_a? String
71
+ if input.include? "bit.ly/"
72
+ hash = input.gsub(/^.*bit.ly\//,'')
73
+ request = create_url 'info', :hash => hash
74
+ result = get_result(request)
75
+ result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result[hash]
76
+ else
77
+ request = create_url 'info', :hash => input
78
+ result = get_result(request)
79
+ result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result[input]
80
+ end
81
+ Bitly::Url.new(@login,@api_key,result)
82
+ elsif input.is_a? Array
83
+ request = create_url "info", :hash => input.join(',')
84
+ result = get_result(request)
85
+ input.map do |hsh|
86
+ new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
87
+ hsh = Bitly::Url.new(@login,@api_key,:info => new_url)
88
+ end
89
+ end
90
+ end
91
+
92
+ def stats(input)
93
+ if input.is_a? String
94
+ if input.include? "bit.ly/"
95
+ hash = input.gsub(/^.*bit.ly\//,'')
96
+ request = create_url 'stats', :hash => hash
97
+ result = get_result(request)
98
+ result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result
99
+ else
100
+ request = create_url 'stats', :hash => input
101
+ result = get_result(request)
102
+ result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result
103
+ end
104
+ Bitly::Url.new(@login,@api_key,:stats => result)
105
+ else
106
+ raise ArgumentError
107
+ end
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+ class BitlyError < StandardError
115
+ attr_reader :code
116
+ alias :msg :message
117
+ def initialize(msg, code, req)
118
+ @code = code
119
+ super("'#{req}' - #{msg}")
120
+ end
121
+ end
122
+
123
+
124
+ # How it should work
125
+ # ==================
126
+ # bitly = Bitly::Base.new('philnash','R_7776acc394294b2b0ad2c261a91c483d')
127
+ # bitly.shorten("http://www.google.com")
128
+ # #=> Bitly::Url
129
+ # bitly.shorten(["http://www.google.com","http://cnn.com"])
130
+ # #=> [Bitly::Url,Bitly::Url]
131
+ #
132
+ # bitly.expand("http://bit.ly/wIRm")
133
+ # #=> Bitly::Url
134
+ # bitly.expand("wIRm")
135
+ # #=> Bitly::Url
136
+ # bitly.expand(["wIRm","sdfsd"])
137
+ # #=> [Bitly::Url,Bitly::Url]
138
+ #
139
+ # bitly.info("http://bit.ly/wIRm") || bitly.info("wIRm")
140
+ # #=> b = Bitly::Url
141
+ # #=> b.info #=> hash of data back from info
142
+ # bitly.info(['wIRm','qsads'])
143
+ # #=> [Bitly::Url, Bitly::Url]
144
+ # also, with any url = Bitly::Url
145
+ # url.info #=> hash of info data
146
+
147
+ # bitly.stats("http://bit.ly/wIRm") || bitly.stats("wIRm")
148
+ # #=> b = Bitly::Url
149
+ # #=> b.stats #=> hash of stats
150
+ # also, any url = Bitly::Url
151
+ # url.stats #=> hash of stats
data/lib/bitly/url.rb ADDED
@@ -0,0 +1,54 @@
1
+ module Bitly
2
+
3
+ class Url
4
+ include Bitly::Utils
5
+
6
+ VARIABLES = ['long_url', 'short_url', 'hash', 'user_hash', 'short_keyword_url']
7
+
8
+ def initialize(login,api_key,obj=nil)
9
+ unless obj.nil?
10
+ instance_variablise(obj, VARIABLES)
11
+ @info = obj[:info] if obj[:info]
12
+ @stats = obj[:stats] if obj[:stats]
13
+ end
14
+ @login = login
15
+ @api_key = api_key
16
+ end
17
+
18
+ def info
19
+ if @info.nil?
20
+ if @hash
21
+ request = create_url "info", :hash => @hash
22
+ result = get_result(request)[@hash]
23
+ instance_variablise(result, VARIABLES)
24
+ @info = result
25
+ elsif @short_url
26
+ hash = @short_url.gsub(/^.*bit.ly\//,'')
27
+ request = create_url "info", :hash => hash
28
+ result = get_result(request)[hash]
29
+ instance_variablise(result, VARIABLES)
30
+ @info = result
31
+ else
32
+ nil
33
+ end
34
+ else
35
+ @info
36
+ end
37
+ end
38
+
39
+ def stats
40
+ if @stats.nil?
41
+ if @hash
42
+ request = create_url "stats", :hash => @hash
43
+ elsif @short_url
44
+ request = create_url "stats", :shortUrl => @short_url
45
+ end
46
+ @stats = get_result(request)
47
+ else
48
+ @stats
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,49 @@
1
+ module Bitly
2
+ module Utils
3
+ private
4
+ def underscore(camel_cased_word) # stolen from rails
5
+ camel_cased_word.to_s.gsub(/::/, '/').
6
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
8
+ tr("-", "_").
9
+ downcase
10
+ end
11
+
12
+ def attr_define(k,v)
13
+ instance_variable_set("@#{k}", v)
14
+ meta = class << self; self; end
15
+ meta.class_eval { attr_reader k.to_sym }
16
+ end
17
+
18
+ def instance_variablise(obj,variables)
19
+ if obj.is_a? Hash
20
+ obj.each do |k,v|
21
+ if v.is_a? Hash
22
+ instance_variablise(v,variables)
23
+ else
24
+ attr_define(underscore(k),v) if variables.include?(underscore(k))
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def create_url(resource="",args={})
31
+ args = args.merge({:login => @login, :apiKey => @api_key, :version => API_VERSION})
32
+ url = URI.join(API_URL,resource)
33
+ long_urls = args.delete(:long_urls)
34
+ url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
35
+ url.query << "&" + long_urls.map { |long_url| "longUrl=#{URI.encode(long_url)}" }.join("&") unless long_urls.nil?
36
+ url
37
+ end
38
+
39
+ def get_result(request)
40
+ result = JSON.parse(Net::HTTP.get(request))
41
+ if result['statusCode'] == "OK"
42
+ result = result['results']
43
+ else
44
+ raise BitlyError.new(result['errorMessage'],result['errorCode'],'expand')
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Bitly
2
+ VERSION = '0.1.0'
3
+ end
data/lib/bitly.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'bitly/utils'
4
+ require 'bitly/client'
5
+ require 'bitly/url'
6
+ require 'bitly/version'
@@ -0,0 +1,41 @@
1
+ require 'test/unit'
2
+ require 'bitly'
3
+
4
+ class TestBitly < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @api_key = 'R_7776acc394294b2b0ad2c261a91c483d'
8
+ @login = 'philnash'
9
+ @bitly = Bitly.new(@login,@api_key)
10
+ end
11
+
12
+ # not a good test, but it makes sure things are working for now.
13
+ def test_returns_short_url
14
+ url = @bitly.shorten("http://google.com")
15
+ assert_equal url.class, Bitly::Url
16
+ assert_equal url.long_url, "http://google.com"
17
+ assert_equal url.short_url, "http://bit.ly/wQaT"
18
+ urls = @bitly.shorten(["http://google.com","http://cnn.com"])
19
+ assert_equal urls[0].long_url, "http://google.com"
20
+ assert_equal urls[0].short_url, "http://bit.ly/wQaT"
21
+ end
22
+
23
+ def test_returns_a_long_url
24
+ urls = @bitly.expand(["2bYgqR","1RmnUT"])
25
+ assert_equal urls[0].class, Bitly::Url
26
+ assert_equal urls[0].long_url, "http://cnn.com"
27
+ assert_equal urls[0].hash, "2bYgqR"
28
+ assert_equal urls[1].long_url, "http://google.com"
29
+ assert_equal urls[1].hash, "1RmnUT"
30
+ url = @bitly.expand("http://bit.ly/wQaT")
31
+ assert_equal url.class, Bitly::Url
32
+ assert_equal url.short_url, "http://bit.ly/wQaT"
33
+ assert_equal url.long_url, "http://google.com/"
34
+ assert_equal url.hash, "wQaT"
35
+ url2 = @bitly.expand("wQaT")
36
+ assert_equal url2.class, Bitly::Url
37
+ assert_equal url2.hash, "wQaT"
38
+ assert_equal url2.short_url, "http://bit.ly/wQaT"
39
+ assert_equal url2.long_url, "http://google.com/"
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philnash-bitly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Phil Nash
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-26 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Use the bit.ly API to shorten or expand URLs
17
+ email: philnash@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/bitly/client.rb
24
+ - lib/bitly/url.rb
25
+ - lib/bitly/utils.rb
26
+ - lib/bitly/version.rb
27
+ - lib/bitly.rb
28
+ - README.txt
29
+ files:
30
+ - History.txt
31
+ - lib/bitly/client.rb
32
+ - lib/bitly/url.rb
33
+ - lib/bitly/utils.rb
34
+ - lib/bitly/version.rb
35
+ - lib/bitly.rb
36
+ - Rakefile
37
+ - README.txt
38
+ - test/test_bitly.rb
39
+ - Manifest
40
+ - bitly.gemspec
41
+ has_rdoc: true
42
+ homepage: http://github.com/philnash/bitly
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - Bitly
49
+ - --main
50
+ - README.txt
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "1.2"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: bitly
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Use the bit.ly API to shorten or expand URLs
72
+ test_files:
73
+ - test/test_bitly.rb