cotweet-bitly 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ === 0.5.3 / 2010-07-30
2
+
3
+ * 1 minor enhancement
4
+
5
+ * Added the info call to the version 3 API
6
+
7
+ === 0.5.2 / 2010-07-18
8
+
9
+ * 1 minor enhancement
10
+
11
+ * Added the lookup call to the version 3 API.
12
+
13
+ === 0.5.1 / 2010-05-26
14
+
15
+ * 1 bug fix
16
+
17
+ * When expanding a short url with the v3 module, user hash and short url weren't being set properly
18
+
19
+ === 0.5.0 / 2010-05-12
20
+
21
+ * Loads of major enhancements
22
+
23
+ * Support for version 3 of the bit.ly API
24
+ * deprecation for version 2 client object
25
+
26
+ === 0.4.0 / 2009-12-19
27
+
28
+ * 1 major enhancement
29
+
30
+ * support for j.mp urls
31
+
32
+ === 0.3.2 / 2009-11-15
33
+
34
+ * 1 bug fix
35
+
36
+ * called the wrong variable in case of error in the creation of a Url object
37
+
38
+ === 0.3.1 / 2009-07-29
39
+
40
+ * 1 minor enhancement
41
+
42
+ * removed require 'rubygems' from library (http://gist.github.com/54177)
43
+
44
+ === 0.3 / 2009-07-09
45
+
46
+ * 1 major enhancement
47
+
48
+ * a full set of tests, properly mocked
49
+
50
+ * 1 minor enhancement
51
+
52
+ * calling bitly.shorten(:history => 1) will add the url to the api user's history.
53
+
54
+ * 1 bug fix
55
+
56
+ * you can no longer call shorten with a keyword, this was unsupported in the API and consequently removed
57
+
58
+ === 0.2 / 2009-06-23
59
+
60
+ * 1 enhancement
61
+
62
+ * Depends on Crack instead of JSON, so can run on Jruby
63
+
64
+ * 1 bug fix
65
+
66
+ * Does not choke when bit.ly chokes. Returns a BitlyError instead
67
+
68
+ === 0.1.4 / 2009-04-13
69
+
70
+ * 1 bug fix
71
+
72
+ * Urls with parameters were choking, changed to using CGI.
73
+
74
+ === 0.1.2 / 2009-03-12
75
+
76
+ * 1 minor enhancement
77
+
78
+ * Allows to add a keyword for shortening urls
79
+
80
+ === 0.1.1 / 2009-01-26
81
+
82
+ * 1 bug fix
83
+
84
+ * Didn't include dependency on JSON
85
+
86
+ === 0.1.0 / 2009-01-26
87
+
88
+ * 1 major enhancement
89
+
90
+ * First release
91
+
@@ -0,0 +1,27 @@
1
+ bitly.gemspec
2
+ History.txt
3
+ lib/bitly/client.rb
4
+ lib/bitly/url.rb
5
+ lib/bitly/utils.rb
6
+ lib/bitly/v3/bitly.rb
7
+ lib/bitly/v3/client.rb
8
+ lib/bitly/v3/missing_url.rb
9
+ lib/bitly/v3/url.rb
10
+ lib/bitly/v3.rb
11
+ lib/bitly/version.rb
12
+ lib/bitly.rb
13
+ Manifest
14
+ Rakefile
15
+ README.txt
16
+ test/bitly/test_client.rb
17
+ test/bitly/test_url.rb
18
+ test/bitly/test_utils.rb
19
+ test/fixtures/cnn.json
20
+ test/fixtures/cnn_and_google.json
21
+ test/fixtures/expand_cnn.json
22
+ test/fixtures/expand_cnn_and_google.json
23
+ test/fixtures/google_and_cnn_info.json
24
+ test/fixtures/google_info.json
25
+ test/fixtures/google_stats.json
26
+ test/fixtures/shorten_error.json
27
+ test/test_helper.rb
@@ -0,0 +1,81 @@
1
+ = bitly
2
+
3
+ == DESCRIPTION:
4
+
5
+ A Ruby API for http://bit.ly (and now http://j.mp)
6
+
7
+ http://code.google.com/p/bitly-api/wiki/ApiDocumentation
8
+
9
+ == NOTE:
10
+
11
+ Bitly recently released their version 3 API. From this 0.5.0 release, the gem will continue to work the same but also provide a V3 module, using the version 3 API. The standard module will become deprecated, as Bitly do not plan to keep the version 2 API around forever.
12
+
13
+ To move to using the version 3 API, call:
14
+
15
+ Bitly.use_api_version_3
16
+
17
+ Then, when you call Bitly.new(username, api_key) you will get a Bitly::V3::Client instead, which provides the version 3 api calls (shorten, expand, clicks, validate and bitly_pro_domain). See http://api.bit.ly for details.
18
+
19
+ Eventually, this will become the default version used and finally, the V3 module will disappear, with the version 3 classes replacing the version 2 classes.
20
+
21
+ (Please excuse the lack of tests for the v3 classes, they are fully tested and ready to replace this whole codebase in the v3 branch of the github repo, until I realised it would break everything.)
22
+
23
+ == INSTALLATION:
24
+
25
+ gem install bitly
26
+
27
+ == USAGE:
28
+
29
+ Create a Bitly client using your username and api key as follows:
30
+
31
+ bitly = Bitly.new(username, api_key)
32
+
33
+ You can then use that client to shorten or expand urls or return more information or statistics as so:
34
+
35
+ bitly.shorten('http://www.google.com')
36
+ bitly.shorten('http://www.google.com', :history => 1) # adds the url to the api user's history
37
+ bitly.expand('wQaT')
38
+ bitly.info('http://bit.ly/wQaT')
39
+ bitly.stats('http://bit.ly/wQaT')
40
+
41
+ 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.
42
+
43
+ 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.
44
+
45
+ u = bitly.shorten('http://www.google.com') #=> Bitly::Url
46
+
47
+ u.long_url #=> "http://www.google.com"
48
+ u.short_url #=> "http://bit.ly/Ywd1"
49
+ u.bitly_url #=> "http://bit.ly/Ywd1"
50
+ u.jmp_url #=> "http://j.mp/Ywd1"
51
+ u.user_hash #=> "Ywd1"
52
+ u.hash #=> "2V6CFi"
53
+ u.info #=> a ruby hash of the JSON returned from the API
54
+ u.stats #=> a ruby hash of the JSON returned from the API
55
+
56
+ bitly.shorten('http://www.google.com', 'keyword')
57
+
58
+ == LICENSE:
59
+
60
+ (The MIT License)
61
+
62
+ Copyright (c) 2009 Phil Nash
63
+
64
+ Permission is hereby granted, free of charge, to any person obtaining
65
+ a copy of this software and associated documentation files (the
66
+ 'Software'), to deal in the Software without restriction, including
67
+ without limitation the rights to use, copy, modify, merge, publish,
68
+ distribute, sublicense, and/or sell copies of the Software, and to
69
+ permit persons to whom the Software is furnished to do so, subject to
70
+ the following conditions:
71
+
72
+ The above copyright notice and this permission notice shall be
73
+ included in all copies or substantial portions of the Software.
74
+
75
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
76
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
77
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
78
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
79
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
80
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
81
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require './lib/bitly.rb'
5
+
6
+ Echoe.new('bitly', Bitly::VERSION) do |p|
7
+ p.description = "Use the bit.ly API to shorten or expand URLs"
8
+ p.url = "http://github.com/philnash/bitly"
9
+ p.author = "Phil Nash"
10
+ p.email = "philnash@gmail.com"
11
+ p.extra_deps = [['crack', '>= 0.1.4'], ['httparty', '>= 0.5.2']]
12
+ p.development_dependencies = []
13
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cotweet-bitly}
5
+ s.version = "0.5.2"
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{2010-07-18}
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/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3.rb", "lib/bitly/version.rb", "lib/bitly.rb", "README.txt"]
13
+ s.files = ["bitly.gemspec", "History.txt", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3.rb", "lib/bitly/version.rb", "lib/bitly.rb", "Manifest", "Rakefile", "README.txt", "test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/fixtures/cnn.json", "test/fixtures/cnn_and_google.json", "test/fixtures/expand_cnn.json", "test/fixtures/expand_cnn_and_google.json", "test/fixtures/google_and_cnn_info.json", "test/fixtures/google_info.json", "test/fixtures/google_stats.json", "test/fixtures/shorten_error.json", "test/test_helper.rb"]
14
+ s.homepage = %q{http://github.com/philnash/bitly}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitly", "--main", "README.txt"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{bitly}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{Use the bit.ly API to shorten or expand URLs}
20
+ s.test_files = ["test/bitly/test_client.rb", "test/bitly/test_url.rb", "test/bitly/test_utils.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<crack>, [">= 0.1.4"])
28
+ s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
29
+ else
30
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
31
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
32
+ end
33
+ else
34
+ s.add_dependency(%q<crack>, [">= 0.1.4"])
35
+ s.add_dependency(%q<httparty>, [">= 0.5.2"])
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'crack'
4
+
5
+ require 'bitly/utils'
6
+ require 'bitly/client'
7
+ require 'bitly/url'
8
+ require 'bitly/version'
9
+ require 'bitly/v3'
@@ -0,0 +1,133 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Bitly
6
+ API_URL = 'http://api.bit.ly/'
7
+ API_VERSION = '2.0.1'
8
+
9
+ def self.new(login, api_key)
10
+ if @version == 3
11
+ Bitly::V3::Client.new(login, api_key)
12
+ else
13
+ Bitly::Client.new(login,api_key)
14
+ end
15
+ end
16
+
17
+ def self.use_api_version_3
18
+ @version = 3
19
+ end
20
+
21
+ def self.use_api_version_2
22
+ @version = 2
23
+ end
24
+
25
+ class Client
26
+
27
+ include Bitly::Utils
28
+
29
+ def initialize(login,api_key)
30
+ warn "[DEPRECATION] The bit.ly version 2 API has been superceded by version 3 and will be removed. See the README for details"
31
+ @login = login
32
+ @api_key = api_key
33
+ end
34
+
35
+ def shorten(input, opts={})
36
+ if input.is_a? String
37
+ request = create_url("shorten", :longUrl => input, :history => (opts[:history] ? 1 : nil))
38
+ result = get_result(request)
39
+ result = {:long_url => input}.merge result[input]
40
+ Bitly::Url.new(@login,@api_key,result)
41
+ elsif input.is_a? Array
42
+ request = create_url("shorten", :history => (opts[:history] ? 1 : nil))
43
+ request.query << "&" + input.map { |long_url| "longUrl=#{CGI.escape(long_url)}" }.join("&") unless input.nil?
44
+ result = get_result(request)
45
+ input.map do |long_url|
46
+ new_url = {:long_url => long_url}.merge result[long_url]
47
+ long_url = Bitly::Url.new(@login,@api_key,new_url)
48
+ end
49
+ else
50
+ raise ArgumentError.new("Shorten requires either a url or an array of urls")
51
+ end
52
+ end
53
+
54
+ def expand(input)
55
+ if input.is_a? String
56
+ if input.include?('bit.ly/') || input.include?('j.mp/')
57
+ hash = create_hash_from_url(input)
58
+ request = create_url "expand", :hash => hash
59
+ result = get_result(request)
60
+ result = { :short_url => input, :hash => hash }.merge result[hash]
61
+ else
62
+ request = create_url "expand", :hash => input
63
+ result = get_result(request)
64
+ result = { :hash => input, :short_url => "http://bit.ly/#{input}" }.merge result[input]
65
+ end
66
+ Bitly::Url.new(@login,@api_key,result)
67
+ elsif input.is_a? Array
68
+ request = create_url "expand", :hash => input.join(',')
69
+ result = get_result(request)
70
+ input.map do |hsh|
71
+ new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
72
+ hsh = Bitly::Url.new(@login,@api_key,new_url)
73
+ end
74
+ else
75
+ raise ArgumentError('Expand requires either a short url, a hash or an array of hashes')
76
+ end
77
+ end
78
+
79
+ def info(input)
80
+ if input.is_a? String
81
+ if input.include? "bit.ly/"
82
+ hash = create_hash_from_url(input)
83
+ request = create_url 'info', :hash => hash
84
+ result = get_result(request)
85
+ result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result[hash]
86
+ else
87
+ request = create_url 'info', :hash => input
88
+ result = get_result(request)
89
+ result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result[input]
90
+ end
91
+ Bitly::Url.new(@login,@api_key,result)
92
+ elsif input.is_a? Array
93
+ request = create_url "info", :hash => input.join(',')
94
+ result = get_result(request)
95
+ input.map do |hsh|
96
+ new_url = {:hash => hsh, :short_url => "http://bit.ly/#{hsh}"}.merge result[hsh]
97
+ hsh = Bitly::Url.new(@login,@api_key,:info => new_url)
98
+ end
99
+ else
100
+ raise ArgumentError.new('Info requires either a short url, a hash or an array of hashes')
101
+ end
102
+ end
103
+
104
+ def stats(input)
105
+ if input.is_a? String
106
+ if input.include? "bit.ly/"
107
+ hash = create_hash_from_url(input)
108
+ request = create_url 'stats', :hash => hash
109
+ result = get_result(request)
110
+ result = { :short_url => "http://bit.ly/#{hash}", :hash => hash }.merge result
111
+ else
112
+ request = create_url 'stats', :hash => input
113
+ result = get_result(request)
114
+ result = { :short_url => "http://bit.ly/#{input}", :hash => input }.merge result
115
+ end
116
+ Bitly::Url.new(@login,@api_key,:stats => result)
117
+ else
118
+ raise ArgumentError.new("Stats requires either a short url or a hash")
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
126
+ class BitlyError < StandardError
127
+ attr_reader :code
128
+ alias :msg :message
129
+ def initialize(msg, code)
130
+ @code = code
131
+ super("#{msg} - '#{code}'")
132
+ end
133
+ end
@@ -0,0 +1,104 @@
1
+ module Bitly
2
+
3
+ class Url
4
+ include Bitly::Utils
5
+
6
+ attr_accessor :long_url, :short_url, :hash, :user_hash
7
+ attr_reader :info, :stats
8
+ VARIABLES = ['long_url', 'short_url', 'hash', 'user_hash']
9
+
10
+ def initialize(login,api_key,obj=nil)
11
+ unless obj.nil?
12
+ raise BitlyError.new(obj['errorMessage'],obj['errorCode']) if obj['statusCode'] == "ERROR"
13
+ instance_variablise(obj, VARIABLES)
14
+ @info = obj[:info] if obj[:info]
15
+ @stats = obj[:stats] if obj[:stats]
16
+ end
17
+ @login = login
18
+ @api_key = api_key
19
+ raise ArgumentError.new("Please provide a login and api_key") if @login.nil? || @api_key.nil?
20
+ end
21
+
22
+ def shorten(opts = {})
23
+ return @short_url if @short_url
24
+ unless @long_url.nil?
25
+ request = create_url("shorten", :longUrl => @long_url, :history => (opts[:history] ? 1 : nil))
26
+ result = get_result(request)[@long_url.gsub(/\/$/,'')]
27
+ if result['statusCode'] == "ERROR"
28
+ raise BitlyError.new(result['errorMessage'],result['errorCode'])
29
+ else
30
+ instance_variablise(result,VARIABLES)
31
+ return @short_url
32
+ end
33
+ else
34
+ raise ArgumentError.new("You need a long_url in order to shorten it")
35
+ end
36
+ end
37
+
38
+ def expand
39
+ return @long_url if @long_url
40
+ unless !(@short_url || @hash)
41
+ unless @hash
42
+ @hash = create_hash_from_url(@short_url)
43
+ end
44
+ request = create_url("expand", :hash => @hash)
45
+ result = get_result(request)[@hash]
46
+ if result['statusCode'] == "ERROR"
47
+ raise BitlyError.new(result['errorMessage'],result['errorCode'])
48
+ else
49
+ instance_variablise(result,VARIABLES)
50
+ return @long_url
51
+ end
52
+ else
53
+ raise ArgumentError.new("You need a short_url or a hash in order to expand it")
54
+ end
55
+ end
56
+
57
+ def info
58
+ if @info.nil?
59
+ if @hash
60
+ request = create_url "info", :hash => @hash
61
+ result = get_result(request)[@hash]
62
+ instance_variablise(result, VARIABLES)
63
+ @info = result
64
+ elsif @short_url
65
+ @hash = create_hash_from_url(@short_url)
66
+ request = create_url "info", :hash => hash
67
+ result = get_result(request)[hash]
68
+ instance_variablise(result, VARIABLES)
69
+ @info = result
70
+ else
71
+ raise ArgumentError.new("You need a hash or short_url in order to get info")
72
+ end
73
+ return @info
74
+ else
75
+ @info
76
+ end
77
+ end
78
+
79
+ def stats
80
+ if @stats.nil?
81
+ if @hash
82
+ request = create_url "stats", :hash => @hash
83
+ elsif @short_url
84
+ @hash = create_hash_from_url(@short_url)
85
+ request = create_url "stats", :hash => @hash
86
+ else
87
+ raise ArgumentError.new("You need a hash or short_url in order to get stats")
88
+ end
89
+ @stats = get_result(request)
90
+ else
91
+ @stats
92
+ end
93
+ end
94
+
95
+ def bitly_url
96
+ @short_url.nil? ? nil : @short_url.gsub(/j\.mp/,'bit.ly')
97
+ end
98
+
99
+ def jmp_url
100
+ @short_url.nil? ? nil : @short_url.gsub(/bit\.ly/,'j.mp')
101
+ end
102
+ end
103
+
104
+ end