bitly 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,7 +1,17 @@
1
+ === 0.7.0 / 2011-1-8
2
+
3
+ * 1 major bug fix
4
+
5
+ * Updates OAuth2 gem and fixes OAuth class
6
+
7
+ * 1 minor enhancement
8
+
9
+ Adds link_history to user model
10
+
1
11
  === 0.6.2 / 2011-11-10
2
12
 
3
13
  * 2 minor enhancements
4
-
14
+
5
15
  * Adds days parameter to clicks_by_minute v3 API call
6
16
  * Converts Readme to markdown
7
17
 
@@ -51,7 +61,7 @@
51
61
  * 1 bug fix
52
62
 
53
63
  * When expanding a short url with the v3 module, user hash and short url weren't being set properly
54
-
64
+
55
65
  === 0.5.0 / 2010-05-12
56
66
 
57
67
  * Loads of major enhancements
@@ -86,7 +96,7 @@
86
96
  * 1 minor enhancement
87
97
 
88
98
  * calling bitly.shorten(:history => 1) will add the url to the api user's history.
89
-
99
+
90
100
  * 1 bug fix
91
101
 
92
102
  * you can no longer call shorten with a keyword, this was unsupported in the API and consequently removed
@@ -96,7 +106,7 @@
96
106
  * 1 enhancement
97
107
 
98
108
  * Depends on Crack instead of JSON, so can run on Jruby
99
-
109
+
100
110
  * 1 bug fix
101
111
 
102
112
  * Does not choke when bit.ly chokes. Returns a BitlyError instead
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## DESCRIPTION:
4
4
 
5
- A Ruby API for [http://bit.ly](http://bit.ly) (and now (http://j.mp)[http://j.mp])
5
+ A Ruby API for [http://bit.ly](http://bit.ly)
6
6
 
7
7
  [http://code.google.com/p/bitly-api/wiki/ApiDocumentation](http://code.google.com/p/bitly-api/wiki/ApiDocumentation)
8
8
 
@@ -45,7 +45,7 @@ Each can be used in all the methods described in the API docs, the shorten funct
45
45
  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.
46
46
 
47
47
  u = bitly.shorten('http://www.google.com') #=> Bitly::Url
48
-
48
+
49
49
  u.long_url #=> "http://www.google.com"
50
50
  u.short_url #=> "http://bit.ly/Ywd1"
51
51
  u.bitly_url #=> "http://bit.ly/Ywd1"
@@ -54,7 +54,7 @@ All four functions return a ``Bitly::Url`` object (or an array of ``Bitly::Url``
54
54
  u.hash #=> "2V6CFi"
55
55
  u.info #=> a ruby hash of the JSON returned from the API
56
56
  u.stats #=> a ruby hash of the JSON returned from the API
57
-
57
+
58
58
  bitly.shorten('http://www.google.com', 'keyword')
59
59
 
60
60
  ### Version 3 API
@@ -74,10 +74,10 @@ Please see the Bit.ly API documentation [http://api.bit.ly](http://api.bit.ly) f
74
74
  > distribute, sublicense, and/or sell copies of the Software, and to
75
75
  > permit persons to whom the Software is furnished to do so, subject to
76
76
  > the following conditions:
77
- >
77
+ >
78
78
  > The above copyright notice and this permission notice shall be
79
79
  > included in all copies or substantial portions of the Software.
80
- >
80
+ >
81
81
  > THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
82
  > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
83
  > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
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.7.6'], ['oauth2', '>= 0.1.1']]
11
+ p.extra_deps = [['crack', '>= 0.1.4'], ['httparty', '>= 0.7.6'], ['oauth2', '~> 0.5.0']]
12
12
  p.development_dependencies = []
13
13
  end
data/bitly.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{bitly}
5
- s.version = "0.6.2"
5
+ s.version = "0.7.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{2011-11-10}
9
+ s.date = %q{2012-01-08}
10
10
  s.description = %q{Use the bit.ly API to shorten or expand URLs}
11
11
  s.email = %q{philnash@gmail.com}
12
12
  s.extra_rdoc_files = ["README.md", "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"]
@@ -26,15 +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.7.6"])
29
- s.add_runtime_dependency(%q<oauth2>, [">= 0.1.1"])
29
+ s.add_runtime_dependency(%q<oauth2>, ["~> 0.5.0"])
30
30
  else
31
31
  s.add_dependency(%q<crack>, [">= 0.1.4"])
32
32
  s.add_dependency(%q<httparty>, [">= 0.7.6"])
33
- s.add_dependency(%q<oauth2>, [">= 0.1.1"])
33
+ s.add_dependency(%q<oauth2>, ["~> 0.5.0"])
34
34
  end
35
35
  else
36
36
  s.add_dependency(%q<crack>, [">= 0.1.4"])
37
37
  s.add_dependency(%q<httparty>, [">= 0.7.6"])
38
- s.add_dependency(%q<oauth2>, [">= 0.1.1"])
38
+ s.add_dependency(%q<oauth2>, ["~> 0.5.0"])
39
39
  end
40
40
  end
@@ -11,21 +11,21 @@ module Bitly
11
11
  def initialize(login, api_key)
12
12
  @default_query_opts = { :login => login, :apiKey => api_key }
13
13
  end
14
-
14
+
15
15
  # Validates a login and api key
16
16
  def validate(x_login, x_api_key)
17
17
  response = get('/validate', :query => { :x_login => x_login, :x_apiKey => x_api_key })
18
18
  return response['data']['valid'] == 1
19
19
  end
20
20
  alias :valid? :validate
21
-
21
+
22
22
  # Checks whether a domain is a bitly.Pro domain
23
23
  def bitly_pro_domain(domain)
24
24
  response = get('/bitly_pro_domain', :query => { :domain => domain })
25
25
  return response['data']['bitly_pro_domain']
26
26
  end
27
27
  alias :pro? :bitly_pro_domain
28
-
28
+
29
29
  # Shortens a long url
30
30
  #
31
31
  # Options can be:
@@ -39,26 +39,26 @@ module Bitly
39
39
  response = get('/shorten', :query => query)
40
40
  return Bitly::V3::Url.new(self, response['data'])
41
41
  end
42
-
42
+
43
43
  # Expands either a hash, short url or array of either.
44
44
  #
45
45
  # Returns the results in the order they were entered
46
46
  def expand(input)
47
47
  get_method(:expand, input)
48
48
  end
49
-
49
+
50
50
  # Expands either a hash, short url or array of either and gets click data too.
51
51
  #
52
52
  # Returns the results in the order they were entered
53
53
  def clicks(input)
54
54
  get_method(:clicks, input)
55
55
  end
56
-
56
+
57
57
  # Like expand, but gets the title of the page and who created it
58
58
  def info(input)
59
- get_method(:info, input)
59
+ get_method(:info, input)
60
60
  end
61
-
61
+
62
62
  # Looks up the short url and global hash of a url or array of urls
63
63
  #
64
64
  # Returns the results in the order they were entered
@@ -83,7 +83,7 @@ module Bitly
83
83
  end
84
84
  return results.length > 1 ? results : results[0]
85
85
  end
86
-
86
+
87
87
  # Expands either a short link or hash and gets the referrer data for that link
88
88
  #
89
89
  # This method does not take an array as an input
@@ -108,17 +108,17 @@ module Bitly
108
108
  opts.reject! { |k, v| k.to_s != 'days' }
109
109
  get_method(:clicks_by_day, input, opts)
110
110
  end
111
-
111
+
112
112
  private
113
113
 
114
114
  def arrayize(arg)
115
115
  if arg.is_a?(String)
116
- [arg]
116
+ [arg]
117
117
  else
118
118
  arg.dup
119
119
  end
120
120
  end
121
-
121
+
122
122
  def get(method, opts={})
123
123
  opts[:query] ||= {}
124
124
  opts[:query].merge!(@default_query_opts)
@@ -129,11 +129,11 @@ module Bitly
129
129
  raise BitlyError.new(response['status_txt'], response['status_code'])
130
130
  end
131
131
  end
132
-
132
+
133
133
  def is_a_short_url?(input)
134
134
  input.match(/^http:\/\//)
135
135
  end
136
-
136
+
137
137
  def get_single_method(method, input)
138
138
  raise ArgumentError.new("This method only takes a hash or url input") unless input.is_a? String
139
139
  if is_a_short_url?(input)
@@ -145,7 +145,7 @@ module Bitly
145
145
  response = get(query)
146
146
  return Bitly::V3::Url.new(self,response['data'])
147
147
  end
148
-
148
+
149
149
  def get_method(method, input, opts={})
150
150
  input = arrayize(input)
151
151
  query = input.inject([]) do |query,i|
@@ -1,33 +1,38 @@
1
1
  module Bitly
2
- module V3
2
+ module V3
3
3
  # OAuth consumer for authentication
4
4
  class OAuth
5
5
  attr_reader :access_token
6
6
  def initialize(consumer_token, consumer_secret)
7
7
  @consumer_token, @consumer_secret = consumer_token, consumer_secret
8
8
  end
9
-
9
+
10
10
  # Get the OAuth 2 client
11
11
  def client
12
- @client ||= ::OAuth2::Client.new(@consumer_token, @consumer_secret, :site => 'https://api-ssl.bit.ly')
12
+ @client ||= ::OAuth2::Client.new(
13
+ @consumer_token,
14
+ @consumer_secret,
15
+ :site => 'https://api-ssl.bit.ly',
16
+ :token_url => '/oauth/access_token'
17
+ )
13
18
  end
14
-
19
+
15
20
  # Get the url to redirect a user to, pass the url you want the user
16
21
  # to be redirected back to.
17
22
  def authorize_url(redirect_url)
18
- client.web_server.authorize_url(:redirect_uri => redirect_url).gsub(/api-ssl\./,'')
23
+ client.auth_code.authorize_url(:redirect_uri => redirect_url).gsub(/api-ssl\./,'')
19
24
  end
20
-
25
+
21
26
  # Get the access token. You must pass the exact same redirect_url passed
22
27
  # to the authorize_url method
23
28
  def get_access_token_from_code(code,redirect_url)
24
- @access_token ||= client.web_server.get_access_token(code, :redirect_uri => redirect_url)
29
+ @access_token ||= client.auth_code.get_token(code, :redirect_uri => redirect_url, :parse => :query)
25
30
  end
26
-
31
+
27
32
  # If you already have a user token, this method gets the access token
28
33
  def get_access_token_from_token(token, params={})
29
34
  params = params.inject({}) { |options, (key, value)| options[key.to_s] = value; options }
30
- @access_token ||= ::OAuth2::AccessToken.new(client, token, nil, nil, params)
35
+ @access_token ||= ::OAuth2::AccessToken.new(client, token, params)
31
36
  end
32
37
  end
33
38
  end
data/lib/bitly/v3/url.rb CHANGED
@@ -3,8 +3,8 @@ module Bitly
3
3
  # Url objects should only be created by the client object as it collects the correct information
4
4
  # from the API.
5
5
  class Url
6
- attr_reader :short_url, :long_url, :user_hash, :global_hash
7
-
6
+ attr_reader :short_url, :long_url, :user_hash, :global_hash, :aggregate_link
7
+
8
8
  # Initialize with a bitly client and optional hash to fill in the details for the url.
9
9
  def initialize(client, opts={})
10
10
  @client = client
@@ -18,6 +18,8 @@ module Bitly
18
18
  @global_clicks = opts['global_clicks']
19
19
  @title = opts['title']
20
20
  @created_by = opts['created_by']
21
+ @created_at = Time.at opts['created_at'] if opts['created_at']
22
+ @aggregate_link = opts['aggregate_link']
21
23
  @referrers = opts['referrers'].inject([]) do |results, referrer|
22
24
  results << Bitly::V3::Referrer.new(referrer)
23
25
  end if opts['referrers']
@@ -34,12 +36,12 @@ module Bitly
34
36
  end
35
37
  @short_url = "http://bit.ly/#{@user_hash}" unless @short_url
36
38
  end
37
-
39
+
38
40
  # Returns true if the user hash was created first for this call
39
41
  def new_hash?
40
42
  @new_hash
41
43
  end
42
-
44
+
43
45
  # If the url already has click statistics, returns the user clicks.
44
46
  # IF there are no click statistics or <tt>:force => true</tt> is passed,
45
47
  # updates the stats and returns the user clicks
@@ -47,7 +49,7 @@ module Bitly
47
49
  update_clicks_data if @global_clicks.nil? || opts[:force]
48
50
  @user_clicks
49
51
  end
50
-
52
+
51
53
  # If the url already has click statistics, returns the global clicks.
52
54
  # IF there are no click statistics or <tt>:force => true</tt> is passed,
53
55
  # updates the stats and returns the global clicks
@@ -55,7 +57,7 @@ module Bitly
55
57
  update_clicks_data if @global_clicks.nil? || opts[:force]
56
58
  @global_clicks
57
59
  end
58
-
60
+
59
61
  # If the url already has the title, return it.
60
62
  # IF there is no title or <tt>:force => true</tt> is passed,
61
63
  # updates the info and returns the title
@@ -63,7 +65,7 @@ module Bitly
63
65
  update_info if @title.nil? || opts[:force]
64
66
  @title
65
67
  end
66
-
68
+
67
69
  # If the url already has the creator, return it.
68
70
  # IF there is no creator or <tt>:force => true</tt> is passed,
69
71
  # updates the info and returns the creator
@@ -71,7 +73,7 @@ module Bitly
71
73
  update_info if @created_by.nil? || opts[:force]
72
74
  @created_by
73
75
  end
74
-
76
+
75
77
  # If the url already has referrer data, return it.
76
78
  # IF there is no referrer or <tt>:force => true</tt> is passed,
77
79
  # updates the referrers and returns them
@@ -88,6 +90,14 @@ module Bitly
88
90
  @countries
89
91
  end
90
92
 
93
+ # If the url already has created at data, return it.
94
+ # If there is no created at data or <tt>:force => true</tt> is passed,
95
+ # updates the info and returns it
96
+ def created_at(opts={})
97
+ update_info if @created_at.nil? || opts[:force]
98
+ @created_at
99
+ end
100
+
91
101
  def clicks_by_minute(opts={})
92
102
  if @clicks_by_minute.nil? || opts[:force]
93
103
  full_url = @client.clicks_by_minute(@user_hash || @short_url)
@@ -103,21 +113,22 @@ module Bitly
103
113
  end
104
114
  @clicks_by_day
105
115
  end
106
-
116
+
107
117
  private
108
-
118
+
109
119
  def update_clicks_data
110
120
  full_url = @client.clicks(@user_hash || @short_url)
111
121
  @global_clicks = full_url.global_clicks
112
122
  @user_clicks = full_url.user_clicks
113
123
  end
114
-
124
+
115
125
  def update_info
116
126
  full_url = @client.info(@user_hash || @short_url)
117
127
  @created_by = full_url.created_by
118
128
  @title = full_url.title
129
+ @created_at = full_url.created_at
119
130
  end
120
-
131
+
121
132
  def update_referrers
122
133
  full_url = @client.referrers(@user_hash || @short_url)
123
134
  @referrers = full_url.referrers
data/lib/bitly/v3/user.rb CHANGED
@@ -16,16 +16,18 @@ module Bitly
16
16
  #
17
17
  # u=Bitly::V3::User.new(o.access_token)
18
18
  class User
19
+ include HTTParty
20
+ base_uri 'https://api-ssl.bit.ly/v3/'
19
21
  attr_accessor :login, :api_key
20
-
22
+
21
23
  def initialize(access_token)
22
24
  @access_token = access_token
23
25
  @login = access_token['login'] || access_token['username']
24
26
  @api_key = access_token['apiKey'] || access_token['api_key']
25
27
  end
26
-
28
+
27
29
  # OAuth 2 endpoint that provides a list of top referrers (up to 500 per
28
- # day) for a given user’s bit.ly links, and the number of clicks per referrer.
30
+ # day) for a given user’s bit.ly links, and the number of clicks per referrer.
29
31
  #
30
32
  # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
31
33
  def referrers(opts={})
@@ -34,9 +36,9 @@ module Bitly
34
36
  end
35
37
  @referrers
36
38
  end
37
-
39
+
38
40
  # OAuth 2 endpoint that provides a list of countries from which clicks
39
- # on a given user’s bit.ly links are originating, and the number of clicks per country.
41
+ # on a given user’s bit.ly links are originating, and the number of clicks per country.
40
42
  #
41
43
  # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
42
44
  def countries(opts={})
@@ -45,23 +47,24 @@ module Bitly
45
47
  end
46
48
  @countries
47
49
  end
48
-
50
+
49
51
  # OAuth 2 endpoint that provides a given user’s 100 most popular links
50
- # based on click traffic in the past hour, and the number of clicks per link.
52
+ # based on click traffic in the past hour, and the number of clicks per link.
51
53
  #
52
54
  # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
53
55
  def realtime_links(opts={})
54
56
  if @realtime_links.nil? || opts.delete(:force)
55
- result = Crack::JSON.parse(@access_token.get("/v3/user/realtime_links", opts))
57
+ opts.merge!(:access_token => @access_token.token)
58
+ result = self.class.get("/user/realtime_links", :query => opts)
56
59
  if result['status_code'] == 200
57
60
  @realtime_links = result['data']['realtime_links'].map { |rs| Bitly::V3::RealtimeLink.new(rs) }
58
61
  else
59
- raise BitlyError.new(results['status_txt'], results['status_code'])
62
+ raise BitlyError.new(result['status_txt'], result['status_code'])
60
63
  end
61
64
  end
62
65
  @realtime_links
63
66
  end
64
-
67
+
65
68
  # OAuth 2 endpoint that provides the total clicks per day on a user’s bit.ly links.
66
69
  #
67
70
  # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
@@ -69,22 +72,41 @@ module Bitly
69
72
  get_clicks(opts)
70
73
  @clicks
71
74
  end
72
-
75
+
73
76
  # Displays the total clicks returned from the clicks method.
74
77
  def total_clicks(opts={})
75
78
  get_clicks(opts)
76
79
  @total_clicks
77
80
  end
78
-
81
+
79
82
  # Returns a Bitly Client using the credentials of the user.
80
83
  def client
81
84
  @client ||= Bitly::V3::Client.new(login, api_key)
82
85
  end
83
-
86
+
87
+ # OAuth 2 endpoint that OAuth 2 endpoint that provides a given user’s link
88
+ # shortening history, in reverse chronological order (most recent to least
89
+ # recent).
90
+ def link_history(opts={})
91
+ opts.merge!(:access_token => @access_token.token)
92
+ result = self.class.get("/user/link_history", :query => opts)
93
+ if result['status_code'] == 200
94
+ results = result['data']['link_history'].inject([]) do |rs, obj|
95
+ obj['short_url'] = obj['link']
96
+ obj['hash'] = obj['link'].split('/').last
97
+ rs << Url.new(client, obj)
98
+ end
99
+ return results
100
+ else
101
+ raise BitlyError.new(result['status_txt'], result['status_code'])
102
+ end
103
+ end
104
+
84
105
  private
85
-
106
+
86
107
  def get_method(method, klass, opts)
87
- result = Crack::JSON.parse(@access_token.get("/v3/user/#{method.to_s}", opts))
108
+ opts.merge!(:access_token => @access_token.token)
109
+ result = self.class.get("/user/#{method.to_s}", :query => opts)
88
110
  if result['status_code'] == 200
89
111
  results = result['data'][method.to_s].map do |rs|
90
112
  rs.inject([]) do |results, obj|
@@ -93,18 +115,19 @@ module Bitly
93
115
  end
94
116
  return results
95
117
  else
96
- raise BitlyError.new(results['status_txt'], results['status_code'])
118
+ raise BitlyError.new(result['status_txt'], result['status_code'])
97
119
  end
98
120
  end
99
-
121
+
100
122
  def get_clicks(opts={})
101
123
  if @clicks.nil? || opts.delete(:force)
102
- result = Crack::JSON.parse(@access_token.get("/v3/user/clicks", opts))
124
+ opts.merge!(:access_token => @access_token.token)
125
+ result = self.class.get("/user/clicks", :query => opts)
103
126
  if result['status_code'] == 200
104
127
  @clicks = result['data']['clicks'].map { |rs| Bitly::V3::Day.new(rs) }
105
128
  @total_clicks = result['data']['total_clicks']
106
129
  else
107
- raise BitlyError.new(results['status_txt'], results['status_code'])
130
+ raise BitlyError.new(result['status_txt'], result['status_code'])
108
131
  end
109
132
  end
110
133
  end
data/lib/bitly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bitly
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
3
3
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
8
+ - 7
9
+ - 0
10
+ version: 0.7.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: 2011-11-10 00:00:00 +00:00
18
+ date: 2012-01-08 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,14 +56,14 @@ dependencies:
56
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ">="
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- hash: 25
61
+ hash: 11
62
62
  segments:
63
63
  - 0
64
- - 1
65
- - 1
66
- version: 0.1.1
64
+ - 5
65
+ - 0
66
+ version: 0.5.0
67
67
  type: :runtime
68
68
  version_requirements: *id003
69
69
  description: Use the bit.ly API to shorten or expand URLs