bitly 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,9 @@
1
+ === 0.6.0 / 2011-01-03
2
+
3
+ * 1 major enhancement
4
+
5
+ * Added OAuth 2 and User calls for the version 3 API
6
+
1
7
  === 0.5.5 / 2010-12-22
2
8
 
3
9
  * 1 minor bug fix
data/Manifest CHANGED
@@ -13,8 +13,11 @@ lib/bitly/v3/client.rb
13
13
  lib/bitly/v3/country.rb
14
14
  lib/bitly/v3/day.rb
15
15
  lib/bitly/v3/missing_url.rb
16
+ lib/bitly/v3/oauth.rb
17
+ lib/bitly/v3/realtime_link.rb
16
18
  lib/bitly/v3/referrer.rb
17
19
  lib/bitly/v3/url.rb
20
+ lib/bitly/v3/user.rb
18
21
  lib/bitly/version.rb
19
22
  test/bitly/test_client.rb
20
23
  test/bitly/test_url.rb
data/README.txt CHANGED
@@ -26,6 +26,8 @@ gem install bitly
26
26
 
27
27
  == USAGE:
28
28
 
29
+ === Version 2 API
30
+
29
31
  Create a Bitly client using your username and api key as follows:
30
32
 
31
33
  bitly = Bitly.new(username, api_key)
@@ -55,6 +57,10 @@ u.stats #=> a ruby hash of the JSON returned from the API
55
57
 
56
58
  bitly.shorten('http://www.google.com', 'keyword')
57
59
 
60
+ === Version 3 API
61
+
62
+ Please see the Bit.ly API documentation http://api.bit.ly for details on the V3 API
63
+
58
64
  == LICENSE:
59
65
 
60
66
  (The MIT License)
data/Rakefile CHANGED
@@ -8,6 +8,6 @@ Echoe.new('bitly', Bitly::VERSION) do |p|
8
8
  p.url = "http://github.com/philnash/bitly"
9
9
  p.author = "Phil Nash"
10
10
  p.email = "philnash@gmail.com"
11
- p.extra_deps = [['crack', '>= 0.1.4'], ['httparty', '>= 0.5.2']]
11
+ p.extra_deps = [['crack', '>= 0.1.4'], ['httparty', '>= 0.5.2'], ['oauth2', '>= 0']]
12
12
  p.development_dependencies = []
13
13
  end
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{bitly}
5
- s.version = "0.5.5"
5
+ s.version = "0.6.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Phil Nash"]
9
- s.date = %q{2010-12-22}
9
+ s.date = %q{2011-01-03}
10
10
  s.description = %q{Use the bit.ly API to shorten or expand URLs}
11
11
  s.email = %q{philnash@gmail.com}
12
- s.extra_rdoc_files = ["README.txt", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/version.rb"]
13
- s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "bitly.gemspec", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/version.rb", "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"]
12
+ s.extra_rdoc_files = ["README.txt", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/oauth.rb", "lib/bitly/v3/realtime_link.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3/user.rb", "lib/bitly/version.rb"]
13
+ s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "bitly.gemspec", "lib/bitly.rb", "lib/bitly/client.rb", "lib/bitly/url.rb", "lib/bitly/utils.rb", "lib/bitly/v3.rb", "lib/bitly/v3/bitly.rb", "lib/bitly/v3/client.rb", "lib/bitly/v3/country.rb", "lib/bitly/v3/day.rb", "lib/bitly/v3/missing_url.rb", "lib/bitly/v3/oauth.rb", "lib/bitly/v3/realtime_link.rb", "lib/bitly/v3/referrer.rb", "lib/bitly/v3/url.rb", "lib/bitly/v3/user.rb", "lib/bitly/version.rb", "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
14
  s.homepage = %q{http://github.com/philnash/bitly}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitly", "--main", "README.txt"]
16
16
  s.require_paths = ["lib"]
@@ -26,12 +26,15 @@ Gem::Specification.new do |s|
26
26
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
27
  s.add_runtime_dependency(%q<crack>, [">= 0.1.4"])
28
28
  s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
29
+ s.add_runtime_dependency(%q<oauth2>, [">= 0"])
29
30
  else
30
31
  s.add_dependency(%q<crack>, [">= 0.1.4"])
31
32
  s.add_dependency(%q<httparty>, [">= 0.5.2"])
33
+ s.add_dependency(%q<oauth2>, [">= 0"])
32
34
  end
33
35
  else
34
36
  s.add_dependency(%q<crack>, [">= 0.1.4"])
35
37
  s.add_dependency(%q<httparty>, [">= 0.5.2"])
38
+ s.add_dependency(%q<oauth2>, [">= 0"])
36
39
  end
37
40
  end
@@ -2,6 +2,7 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'httparty'
4
4
  require 'cgi'
5
+ require 'oauth2'
5
6
 
6
7
  require 'v3/bitly'
7
8
  require 'v3/client'
@@ -9,4 +10,7 @@ require 'v3/url'
9
10
  require 'v3/referrer'
10
11
  require 'v3/day'
11
12
  require 'v3/country'
12
- require 'v3/missing_url'
13
+ require 'v3/missing_url'
14
+ require 'v3/realtime_link'
15
+ require 'v3/oauth'
16
+ require 'v3/user'
@@ -0,0 +1,33 @@
1
+ module Bitly
2
+ module V3
3
+ # OAuth consumer for authentication
4
+ class OAuth
5
+ attr_reader :access_token
6
+ def initialize(consumer_token, consumer_secret)
7
+ @consumer_token, @consumer_secret = consumer_token, consumer_secret
8
+ end
9
+
10
+ # Get the OAuth 2 client
11
+ def client
12
+ @client ||= ::OAuth2::Client.new(@consumer_token, @consumer_secret, :site => 'https://api-ssl.bit.ly')
13
+ end
14
+
15
+ # Get the url to redirect a user to, pass the url you want the user
16
+ # to be redirected back to.
17
+ def authorize_url(redirect_url)
18
+ client.web_server.authorize_url(:redirect_uri => redirect_url).gsub(/api-ssl\./,'')
19
+ end
20
+
21
+ # Get the access token. You must pass the exact same redirect_url passed
22
+ # to the authorize_url method
23
+ def get_access_token_from_code(code,redirect_url)
24
+ @access_token ||= client.web_server.get_access_token(code, :redirect_uri => redirect_url)
25
+ end
26
+
27
+ # If you already have a user token, this method gets the access token
28
+ def get_access_token_from_token(token)
29
+ @access_token ||= ::OAuth2::AccessToken.new(client, token)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ module Bitly
2
+ module V3
3
+ # Day objects are created by the realtime_links method of a user
4
+ class RealtimeLink
5
+ attr_reader :clicks, :user_hash
6
+
7
+ def initialize(opts)
8
+ @clicks = opts['clicks']
9
+ @user_hash = opts['user_hash']
10
+ end
11
+
12
+ # A convenience method to create a Bitly::Url from the data
13
+ def create_url(client)
14
+ Bitly::V3::Url.new(client, 'user_clicks' => clicks, 'user_hash' => user_hash)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,104 @@
1
+ module Bitly
2
+ module V3
3
+ # A user requires an oauth access token. The flow is as follows:
4
+ #
5
+ # o = Bitly::V3::OAuth.new(consumer_token, consumer_secret)
6
+ # o.authorize_url(redirect_url)
7
+ # #=> "https://bit.ly/oauth/authorize?client_id=#{consumer_token}&type=web_server&redirect_uri=http%3A%2F%2Ftest.local%2Fbitly%2Fauth"
8
+ # Redirect your users to this url, when they authorize your application
9
+ # they will be redirected to the url you provided with a code parameter.
10
+ # Use that parameter, and the exact same redirect url as follows:
11
+ #
12
+ # o.get_access_token_from_code(params[:code], redirect_url)
13
+ # #=> #<OAuth2::AccessToken ...>
14
+ #
15
+ # Then use that access token to create your user object.
16
+ #
17
+ # u=Bitly::V3::User.new(o.access_token)
18
+ class User
19
+ def initialize(access_token)
20
+ @access_token = access_token
21
+ end
22
+
23
+ # OAuth 2 endpoint that provides a list of top referrers (up to 500 per
24
+ # day) for a given user’s bit.ly links, and the number of clicks per referrer.
25
+ #
26
+ # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
27
+ def referrers(opts={})
28
+ if @referrers.nil? || opts.delete(:force)
29
+ @referrers = get_method(:referrers, Bitly::V3::Referrer, opts)
30
+ end
31
+ @referrers
32
+ end
33
+
34
+ # OAuth 2 endpoint that provides a list of countries from which clicks
35
+ # on a given user’s bit.ly links are originating, and the number of clicks per country.
36
+ #
37
+ # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
38
+ def countries(opts={})
39
+ if @countries.nil? || opts.delete(:force)
40
+ @countries = get_method(:countries, Bitly::V3::Country, opts)
41
+ end
42
+ @countries
43
+ end
44
+
45
+ # OAuth 2 endpoint that provides a given user’s 100 most popular links
46
+ # based on click traffic in the past hour, and the number of clicks per link.
47
+ #
48
+ # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
49
+ def realtime_links(opts={})
50
+ if @realtime_links.nil? || opts.delete(:force)
51
+ result = Crack::JSON.parse(@access_token.get("/v3/user/realtime_links", opts))
52
+ if result['status_code'] == 200
53
+ @realtime_links = result['data']['realtime_links'].map { |rs| Bitly::V3::RealtimeLink.new(rs) }
54
+ else
55
+ raise BitlyError.new(results['status_txt'], results['status_code'])
56
+ end
57
+ end
58
+ @realtime_links
59
+ end
60
+
61
+ # OAuth 2 endpoint that provides the total clicks per day on a user’s bit.ly links.
62
+ #
63
+ # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
64
+ def clicks(opts={})
65
+ get_clicks(opts)
66
+ @clicks
67
+ end
68
+
69
+ # Displays the total clicks returned from the clicks method.
70
+ def total_clicks(opts={})
71
+ get_clicks(opts)
72
+ @total_clicks
73
+ end
74
+
75
+ private
76
+
77
+ def get_method(method, klass, opts)
78
+ result = Crack::JSON.parse(@access_token.get("/v3/user/#{method.to_s}", opts))
79
+ if result['status_code'] == 200
80
+ results = result['data'][method.to_s].map do |rs|
81
+ rs.inject([]) do |results, obj|
82
+ results << klass.new(obj)
83
+ end
84
+ end
85
+ return results
86
+ else
87
+ raise BitlyError.new(results['status_txt'], results['status_code'])
88
+ end
89
+ end
90
+
91
+ def get_clicks(opts={})
92
+ if @clicks.nil? || opts.delete(:force)
93
+ result = Crack::JSON.parse(@access_token.get("/v3/user/clicks", opts))
94
+ if result['status_code'] == 200
95
+ @clicks = result['data']['clicks'].map { |rs| Bitly::V3::Day.new(rs) }
96
+ @total_clicks = result['data']['total_clicks']
97
+ else
98
+ raise BitlyError.new(results['status_txt'], results['status_code'])
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,3 +1,3 @@
1
1
  module Bitly
2
- VERSION = '0.5.5'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitly
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 5
10
- version: 0.5.5
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Phil Nash
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-22 00:00:00 +00:00
18
+ date: 2011-01-03 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,20 @@ dependencies:
50
50
  version: 0.5.2
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: oauth2
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id003
53
67
  description: Use the bit.ly API to shorten or expand URLs
54
68
  email: philnash@gmail.com
55
69
  executables: []
@@ -68,8 +82,11 @@ extra_rdoc_files:
68
82
  - lib/bitly/v3/country.rb
69
83
  - lib/bitly/v3/day.rb
70
84
  - lib/bitly/v3/missing_url.rb
85
+ - lib/bitly/v3/oauth.rb
86
+ - lib/bitly/v3/realtime_link.rb
71
87
  - lib/bitly/v3/referrer.rb
72
88
  - lib/bitly/v3/url.rb
89
+ - lib/bitly/v3/user.rb
73
90
  - lib/bitly/version.rb
74
91
  files:
75
92
  - History.txt
@@ -87,8 +104,11 @@ files:
87
104
  - lib/bitly/v3/country.rb
88
105
  - lib/bitly/v3/day.rb
89
106
  - lib/bitly/v3/missing_url.rb
107
+ - lib/bitly/v3/oauth.rb
108
+ - lib/bitly/v3/realtime_link.rb
90
109
  - lib/bitly/v3/referrer.rb
91
110
  - lib/bitly/v3/url.rb
111
+ - lib/bitly/v3/user.rb
92
112
  - lib/bitly/version.rb
93
113
  - test/bitly/test_client.rb
94
114
  - test/bitly/test_url.rb