bitly 1.1.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +36 -3
  3. data/.rspec +3 -0
  4. data/.travis.yml +6 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/History.txt +32 -1
  8. data/LICENSE.md +1 -1
  9. data/README.md +151 -58
  10. data/Rakefile +6 -9
  11. data/bitly.gemspec +36 -32
  12. data/config/env.yml.example +5 -0
  13. data/lib/bitly.rb +9 -7
  14. data/lib/bitly/api.rb +19 -0
  15. data/lib/bitly/api/base.rb +23 -0
  16. data/lib/bitly/api/bitlink.rb +342 -0
  17. data/lib/bitly/api/bitlink/clicks_summary.rb +35 -0
  18. data/lib/bitly/api/bitlink/deeplink.rb +29 -0
  19. data/lib/bitly/api/bitlink/link_click.rb +75 -0
  20. data/lib/bitly/api/bitlink/paginated_list.rb +52 -0
  21. data/lib/bitly/api/bsd.rb +24 -0
  22. data/lib/bitly/api/click_metric.rb +186 -0
  23. data/lib/bitly/api/client.rb +588 -0
  24. data/lib/bitly/api/group.rb +232 -0
  25. data/lib/bitly/api/group/preferences.rb +73 -0
  26. data/lib/bitly/api/list.rb +22 -0
  27. data/lib/bitly/api/oauth_app.rb +26 -0
  28. data/lib/bitly/api/organization.rb +104 -0
  29. data/lib/bitly/api/shorten_counts.rb +61 -0
  30. data/lib/bitly/api/user.rb +107 -0
  31. data/lib/bitly/error.rb +33 -0
  32. data/lib/bitly/http.rb +10 -0
  33. data/lib/bitly/http/adapters.rb +9 -0
  34. data/lib/bitly/http/adapters/net_http.rb +27 -0
  35. data/lib/bitly/http/client.rb +33 -0
  36. data/lib/bitly/http/request.rb +118 -0
  37. data/lib/bitly/http/response.rb +66 -0
  38. data/lib/bitly/oauth.rb +109 -0
  39. data/lib/bitly/version.rb +3 -1
  40. metadata +82 -111
  41. data/Manifest +0 -37
  42. data/lib/bitly/client.rb +0 -145
  43. data/lib/bitly/config.rb +0 -29
  44. data/lib/bitly/url.rb +0 -103
  45. data/lib/bitly/utils.rb +0 -57
  46. data/lib/bitly/v3.rb +0 -14
  47. data/lib/bitly/v3/bitly.rb +0 -7
  48. data/lib/bitly/v3/client.rb +0 -207
  49. data/lib/bitly/v3/country.rb +0 -13
  50. data/lib/bitly/v3/day.rb +0 -13
  51. data/lib/bitly/v3/missing_url.rb +0 -15
  52. data/lib/bitly/v3/oauth.rb +0 -41
  53. data/lib/bitly/v3/realtime_link.rb +0 -18
  54. data/lib/bitly/v3/referrer.rb +0 -13
  55. data/lib/bitly/v3/url.rb +0 -154
  56. data/lib/bitly/v3/user.rb +0 -135
  57. data/test/bitly/test_client.rb +0 -266
  58. data/test/bitly/test_config.rb +0 -28
  59. data/test/bitly/test_url.rb +0 -167
  60. data/test/bitly/test_utils.rb +0 -79
  61. data/test/fixtures/cnn.json +0 -1
  62. data/test/fixtures/cnn_and_google.json +0 -1
  63. data/test/fixtures/expand_cnn.json +0 -1
  64. data/test/fixtures/expand_cnn_and_google.json +0 -1
  65. data/test/fixtures/google_and_cnn_info.json +0 -1
  66. data/test/fixtures/google_info.json +0 -1
  67. data/test/fixtures/google_stats.json +0 -1
  68. data/test/fixtures/shorten_error.json +0 -1
  69. data/test/test_helper.rb +0 -39
@@ -1,13 +0,0 @@
1
- module Bitly
2
- module V3
3
- class Country
4
- attr_reader :clicks, :country
5
-
6
- def initialize(opts)
7
- ['clicks', 'country'].each do |attribute|
8
- instance_variable_set("@#{attribute}".to_sym, opts[attribute])
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- module Bitly
2
- module V3
3
- # Day objects are created by the clicks_by_day method of a url
4
- class Day
5
- attr_reader :clicks, :day_start
6
-
7
- def initialize(opts)
8
- @clicks = opts['clicks']
9
- @day_start = Time.at(opts['day_start']) if opts['day_start']
10
- end
11
- end
12
- end
13
- end
@@ -1,15 +0,0 @@
1
- module Bitly
2
- module V3
3
- class MissingUrl
4
- attr_accessor :short_url, :user_hash, :long_url, :error
5
- def initialize(opts={})
6
- if opts
7
- @short_url = opts['short_url']
8
- @user_hash = opts['hash']
9
- @long_url = opts['long_url']
10
- @error = opts['error']
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,41 +0,0 @@
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(
13
- @consumer_token,
14
- @consumer_secret,
15
- :site => 'https://api-ssl.bitly.com',
16
- :token_url => '/oauth/access_token'
17
- )
18
- end
19
-
20
- # Get the url to redirect a user to, pass the url you want the user
21
- # to be redirected back to.
22
- def authorize_url(redirect_url, state=nil)
23
- params = {:redirect_uri => redirect_url}
24
- params[:state] = state if state
25
- client.auth_code.authorize_url(params).gsub(/api-ssl\./,'')
26
- end
27
-
28
- # Get the access token. You must pass the exact same redirect_url passed
29
- # to the authorize_url method
30
- def get_access_token_from_code(code,redirect_url)
31
- @access_token ||= client.auth_code.get_token(code, :redirect_uri => redirect_url, :parse => :query)
32
- end
33
-
34
- # If you already have a user token, this method gets the access token
35
- def get_access_token_from_token(token, params={})
36
- params = params.inject({}) { |options, (key, value)| options[key.to_s] = value; options }
37
- @access_token ||= ::OAuth2::AccessToken.new(client, token, params)
38
- end
39
- end
40
- end
41
- end
@@ -1,18 +0,0 @@
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
@@ -1,13 +0,0 @@
1
- module Bitly
2
- module V3
3
- class Referrer
4
- attr_reader :clicks, :referrer, :referrer_app, :url
5
-
6
- def initialize(opts)
7
- ['clicks', 'referrer', 'referrer_app', 'url'].each do |attribute|
8
- instance_variable_set("@#{attribute}".to_sym, opts[attribute])
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,154 +0,0 @@
1
- module Bitly
2
- module V3
3
- # Url objects should only be created by the client object as it collects the correct information
4
- # from the API.
5
- class Url
6
- attr_reader :short_url, :long_url, :user_hash, :global_hash, :aggregate_link
7
-
8
- # Initialize with a bitly client and optional hash to fill in the details for the url.
9
- def initialize(client, opts={})
10
- @client = client
11
- if opts
12
- @short_url = opts['url'] || opts['short_url']
13
- @long_url = opts['long_url']
14
- @user_hash = opts['hash'] || opts['user_hash']
15
- @global_hash = opts['global_hash']
16
- @new_hash = (opts['new_hash'] == 1)
17
- @user_clicks = opts['user_clicks']
18
- @global_clicks = opts['global_clicks']
19
- @title = opts['title']
20
- @created_by = opts['created_by']
21
- @created_at = Time.at opts['created_at'] if opts['created_at']
22
- @aggregate_link = opts['aggregate_link']
23
- @referrers = opts['referrers'].inject([]) do |results, referrer|
24
- results << Bitly::V3::Referrer.new(referrer)
25
- end if opts['referrers']
26
- @countries = opts['countries'].inject([]) do |results, country|
27
- results << Bitly::V3::Country.new(country)
28
- end if opts['countries']
29
- if opts['clicks'] && opts['clicks'][0].is_a?(Hash)
30
- @clicks_by_day = opts['clicks'].inject([]) do |results, day|
31
- results << Bitly::V3::Day.new(day)
32
- end
33
- else
34
- @clicks_by_minute = opts['clicks']
35
- end
36
- end
37
- @short_url = "http://bit.ly/#{@user_hash}" unless @short_url
38
- end
39
-
40
- # Returns true if the user hash was created first for this call
41
- def new_hash?
42
- @new_hash
43
- end
44
-
45
- # If the url already has click statistics, returns the user clicks.
46
- # IF there are no click statistics or <tt>:force => true</tt> is passed,
47
- # updates the stats and returns the user clicks
48
- def user_clicks(opts={})
49
- update_clicks_data if @global_clicks.nil? || opts[:force]
50
- @user_clicks
51
- end
52
-
53
- # If the url already has click statistics, returns the global clicks.
54
- # IF there are no click statistics or <tt>:force => true</tt> is passed,
55
- # updates the stats and returns the global clicks
56
- def global_clicks(opts={})
57
- update_clicks_data if @global_clicks.nil? || opts[:force]
58
- @global_clicks
59
- end
60
-
61
- # If the url already has the title, return it.
62
- # IF there is no title or <tt>:force => true</tt> is passed,
63
- # updates the info and returns the title
64
- def title(opts={})
65
- update_info if @title.nil? || opts[:force]
66
- @title
67
- end
68
-
69
- # If the url already has the creator, return it.
70
- # IF there is no creator or <tt>:force => true</tt> is passed,
71
- # updates the info and returns the creator
72
- def created_by(opts={})
73
- update_info if @created_by.nil? || opts[:force]
74
- @created_by
75
- end
76
-
77
- # If the url already has referrer data, return it.
78
- # IF there is no referrer or <tt>:force => true</tt> is passed,
79
- # updates the referrers and returns them
80
- def referrers(opts={})
81
- update_referrers if @referrers.nil? || opts[:force]
82
- @referrers
83
- end
84
-
85
- # If the url already has country data, return it.
86
- # IF there is no country or <tt>:force => true</tt> is passed,
87
- # updates the countries and returns them
88
- def countries(opts={})
89
- update_countries if @countries.nil? || opts[:force]
90
- @countries
91
- end
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
-
101
- def clicks_by_minute(opts={})
102
- if @clicks_by_minute.nil? || opts[:force]
103
- full_url = @client.clicks_by_minute(@user_hash || @short_url)
104
- @clicks_by_minute = full_url.clicks_by_minute
105
- end
106
- @clicks_by_minute
107
- end
108
-
109
- def clicks_by_day(opts={})
110
- if @clicks_by_day.nil? || opts[:force]
111
- full_url = @client.clicks_by_day(@user_hash || @short_url)
112
- @clicks_by_day = full_url.clicks_by_day
113
- end
114
- @clicks_by_day
115
- end
116
-
117
- def jmp_url
118
- @short_url.nil? ? nil : @short_url.gsub(/bit\.ly/,'j.mp')
119
- end
120
-
121
- # QR code is automatically created and can be incorporated
122
- # into mobile applications.
123
- def qrcode_url(opts={})
124
- qrcode = opts.has_key?(:s) ? ".qrcode?s=#{opts[:s]}" : ".qrcode"
125
- @short_url + qrcode
126
- end
127
-
128
- private
129
-
130
- def update_clicks_data
131
- full_url = @client.clicks(@user_hash || @short_url)
132
- @global_clicks = full_url.global_clicks
133
- @user_clicks = full_url.user_clicks
134
- end
135
-
136
- def update_info
137
- full_url = @client.info(@user_hash || @short_url)
138
- @created_by = full_url.created_by
139
- @title = full_url.title
140
- @created_at = full_url.created_at
141
- end
142
-
143
- def update_referrers
144
- full_url = @client.referrers(@user_hash || @short_url)
145
- @referrers = full_url.referrers
146
- end
147
-
148
- def update_countries
149
- full_url = @client.countries(@user_hash || @short_url)
150
- @countries = full_url.countries
151
- end
152
- end
153
- end
154
- end
@@ -1,135 +0,0 @@
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
- include HTTParty
20
- base_uri 'https://api-ssl.bit.ly/v3/'
21
- attr_accessor :login, :api_key
22
-
23
- def initialize(access_token)
24
- @access_token = access_token
25
- @login = access_token['login'] || access_token['username']
26
- @api_key = access_token['apiKey'] || access_token['api_key']
27
- end
28
-
29
- # OAuth 2 endpoint that provides a list of top referrers (up to 500 per
30
- # day) for a given user's bit.ly links, and the number of clicks per referrer.
31
- #
32
- # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
33
- def referrers(opts={})
34
- if @referrers.nil? || opts.delete(:force)
35
- @referrers = get_method(:referrers, Bitly::V3::Referrer, opts)
36
- end
37
- @referrers
38
- end
39
-
40
- # OAuth 2 endpoint that provides a list of countries from which clicks
41
- # on a given user's bit.ly links are originating, and the number of clicks per country.
42
- #
43
- # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
44
- def countries(opts={})
45
- if @countries.nil? || opts.delete(:force)
46
- @countries = get_method(:countries, Bitly::V3::Country, opts)
47
- end
48
- @countries
49
- end
50
-
51
- # OAuth 2 endpoint that provides a given user's 100 most popular links
52
- # based on click traffic in the past hour, and the number of clicks per link.
53
- #
54
- # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
55
- def realtime_links(opts={})
56
- if @realtime_links.nil? || opts.delete(:force)
57
- opts.merge!(:access_token => @access_token.token)
58
- result = self.class.get("/user/realtime_links", :query => opts)
59
- if result['status_code'] == 200
60
- @realtime_links = result['data']['realtime_links'].map { |rs| Bitly::V3::RealtimeLink.new(rs) }
61
- else
62
- raise BitlyError.new(result['status_txt'], result['status_code'])
63
- end
64
- end
65
- @realtime_links
66
- end
67
-
68
- # OAuth 2 endpoint that provides the total clicks per day on a user's bit.ly links.
69
- #
70
- # http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
71
- def clicks(opts={})
72
- get_clicks(opts)
73
- @clicks
74
- end
75
-
76
- # Displays the total clicks returned from the clicks method.
77
- def total_clicks(opts={})
78
- get_clicks(opts)
79
- @total_clicks
80
- end
81
-
82
- # Returns a Bitly Client using the credentials of the user.
83
- def client
84
- @client ||= Bitly::V3::Client.new(login, api_key)
85
- end
86
-
87
- # OAuth 2 endpoint that provides a given user's link shortening history,
88
- # in reverse chronological order (most recent to least recent).
89
- def link_history(opts={})
90
- opts.merge!(:access_token => @access_token.token)
91
- result = self.class.get("/user/link_history", :query => opts)
92
- if result['status_code'] == 200
93
- results = result['data']['link_history'].inject([]) do |rs, obj|
94
- obj['short_url'] = obj['link']
95
- obj['hash'] = obj['link'].split('/').last
96
- rs << Url.new(client, obj)
97
- end
98
- return results
99
- else
100
- raise BitlyError.new(result['status_txt'], result['status_code'])
101
- end
102
- end
103
-
104
- private
105
-
106
- def get_method(method, klass, opts)
107
- opts.merge!(:access_token => @access_token.token)
108
- result = self.class.get("/user/#{method.to_s}", :query => opts)
109
- if result['status_code'] == 200
110
- results = result['data'][method.to_s].map do |rs|
111
- rs.inject([]) do |results, obj|
112
- results << klass.new(obj)
113
- end
114
- end
115
- return results
116
- else
117
- raise BitlyError.new(result['status_txt'], result['status_code'])
118
- end
119
- end
120
-
121
- def get_clicks(opts={})
122
- if @clicks.nil? || opts.delete(:force)
123
- opts.merge!(:access_token => @access_token.token)
124
- result = self.class.get("/user/clicks", :query => opts)
125
- if result['status_code'] == 200
126
- @clicks = result['data']['clicks'].map { |rs| Bitly::V3::Day.new(rs) }
127
- @total_clicks = result['data']['total_clicks']
128
- else
129
- raise BitlyError.new(result['status_txt'], result['status_code'])
130
- end
131
- end
132
- end
133
- end
134
- end
135
- end
@@ -1,266 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'test_helper.rb')
2
-
3
- class TestClient < Minitest::Test
4
- context "bitly module" do
5
- setup do
6
- Bitly.api_version = nil
7
- end
8
-
9
- should "create a new bitly client" do
10
- b = Bitly.new(login, api_key)
11
- assert_equal Bitly::Client, b.class
12
- end
13
-
14
- should "create a new bitly client with generic OAuth2 access token" do
15
- b = Bitly.new(access_token)
16
- assert_equal Bitly::Client, b.class
17
- end
18
-
19
- should "create a new bitly client and configure with a block" do
20
- Bitly.configure do |config|
21
- config.api_key = api_key
22
- config.login = login
23
- end
24
- b = Bitly.client
25
- assert_kind_of Bitly::Client, b
26
- assert_equal api_key, b.api_key
27
- assert_equal login, b.login
28
- end
29
-
30
- should "create a proper OAuth2 authorization url" do
31
- client = Bitly::V3::OAuth.new('CLIENT_ID', 'CLIENT_SECRET')
32
- url = client.authorize_url('http://www.example.com/oauth_callback')
33
- assert_equal "https://bitly.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=http%3A%2F%2Fwww.example.com%2Foauth_callback&response_type=code", url
34
- end
35
-
36
- should "create a proper OAuth2 authorization url with a state parameter" do
37
- client = Bitly::V3::OAuth.new('CLIENT_ID', 'CLIENT_SECRET')
38
- url = client.authorize_url('http://www.example.com/oauth_callback', 'some_state')
39
- assert_equal "https://bitly.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=http%3A%2F%2Fwww.example.com%2Foauth_callback&response_type=code&state=some_state", url
40
- end
41
-
42
- context "setting api versions" do
43
- teardown do
44
- Bitly.api_version = nil
45
- end
46
-
47
- should "set the api version to 2" do
48
- Bitly.use_api_version_2
49
- assert_equal Bitly.api_version, 2
50
- assert_kind_of Bitly::Client, Bitly.client
51
- end
52
-
53
- should "set the api version to 3" do
54
- Bitly.use_api_version_3
55
- assert_equal Bitly.api_version, 3
56
- assert_kind_of Bitly::V3::Client, Bitly.client
57
- end
58
- end
59
- end
60
- context "using the bitly client" do
61
- setup do
62
- Bitly.use_api_version_2
63
- @bitly = Bitly.new(login, api_key)
64
- end
65
-
66
- context "shortening" do
67
- context "a single link" do
68
- setup do
69
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/shorten\?.*longUrl=.*cnn.com.*$/,"cnn.json")
70
- @url = @bitly.shorten('http://cnn.com')
71
- end
72
- should "return a Bitly::Url" do
73
- assert_kind_of Bitly::Url, @url
74
- end
75
- should "return a short bitly url" do
76
- assert_equal "http://bit.ly/15DlK", @url.short_url
77
- assert_equal "http://bit.ly/15DlK", @url.bitly_url
78
- end
79
- should "return a short jmp url" do
80
- assert_equal "http://j.mp/15DlK", @url.jmp_url
81
- end
82
- should "save the long url" do
83
- assert_equal "http://cnn.com", @url.long_url
84
- end
85
- end
86
- context "multiple links" do
87
- setup do
88
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/shorten\?.*longUrl=.*longUrl=.*$/,"cnn_and_google.json")
89
- @urls = @bitly.shorten(['http://cnn.com', 'http://google.com'])
90
- end
91
- should "return an array of Bitly::Urls" do
92
- assert_kind_of Array, @urls
93
- assert_kind_of Bitly::Url, @urls[0]
94
- end
95
- should "shorten the urls in order" do
96
- assert_equal "http://bit.ly/15DlK", @urls[0].short_url
97
- assert_equal "http://bit.ly/11etr", @urls[1].short_url
98
- end
99
- should "save the long urls" do
100
- assert_equal "http://cnn.com", @urls[0].long_url
101
- assert_equal "http://google.com", @urls[1].long_url
102
- end
103
- end
104
- context "no links" do
105
- should "raise an ArgumentError" do
106
- assert_raises ArgumentError do
107
- @bitly.shorten
108
- end
109
- end
110
- end
111
- end
112
- context "expanding" do
113
- context "a hash" do
114
- setup do
115
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
116
- @url = @bitly.expand("31IqMl")
117
- end
118
- should "return a Bitly::Url" do
119
- assert_kind_of Bitly::Url, @url
120
- end
121
- should "return the expanded url" do
122
- assert_equal "http://cnn.com/", @url.long_url
123
- end
124
- should "save the hash" do
125
- assert_equal "31IqMl", @url.hash
126
- end
127
- should "save the short url" do
128
- assert_equal "http://bit.ly/31IqMl", @url.short_url
129
- end
130
- end
131
- context "a short bitly url" do
132
- setup do
133
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
134
- @url = @bitly.expand("http://bit.ly/31IqMl")
135
- end
136
- should "return a Bitly::Url" do
137
- assert_kind_of Bitly::Url, @url
138
- end
139
- should "return the expanded url" do
140
- assert_equal "http://cnn.com/", @url.long_url
141
- end
142
- should "save the hash" do
143
- assert_equal "31IqMl", @url.hash
144
- end
145
- should "save the bitly url" do
146
- assert_equal "http://bit.ly/31IqMl", @url.bitly_url
147
- end
148
- should "save a jmp url" do
149
- assert_equal "http://j.mp/31IqMl", @url.jmp_url
150
- end
151
- end
152
- context "a short jmp url" do
153
- setup do
154
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/expand\?.*hash=31IqMl.*$/,"expand_cnn.json")
155
- @url = @bitly.expand("http://j.mp/31IqMl")
156
- end
157
- should "return a Bitly::Url" do
158
- assert_kind_of Bitly::Url, @url
159
- end
160
- should "return the expanded url" do
161
- assert_equal "http://cnn.com/", @url.long_url
162
- end
163
- should "save the hash" do
164
- assert_equal "31IqMl", @url.hash
165
- end
166
- should "save the bitly url" do
167
- assert_equal "http://bit.ly/31IqMl", @url.bitly_url
168
- end
169
- should "save a jmp url" do
170
- assert_equal "http://j.mp/31IqMl", @url.jmp_url
171
- end
172
- end
173
- context "multiple hashes" do
174
- setup do
175
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/expand\?.*hash=15DlK.*3j4ir4.*$/,"expand_cnn_and_google.json")
176
- @urls = @bitly.expand(["15DlK","3j4ir4"])
177
- end
178
- should "return an array of Bitly::Urls" do
179
- assert_kind_of Array, @urls
180
- assert_kind_of Bitly::Url, @urls[0]
181
- assert_kind_of Bitly::Url, @urls[1]
182
- end
183
- should "expand the hashes in order" do
184
- assert_equal "http://cnn.com/", @urls[0].long_url
185
- assert_equal "http://google.com/", @urls[1].long_url
186
- end
187
- should "save the hash to each url" do
188
- assert_equal "15DlK", @urls[0].hash
189
- assert_equal "3j4ir4", @urls[1].hash
190
- end
191
- end
192
- end
193
- context "to get info on" do
194
- context "a single link" do
195
- setup do
196
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/info\?.*hash=3j4ir4.*$/,"google_info.json")
197
- @url = @bitly.info('http://bit.ly/3j4ir4')
198
- end
199
- should "return a Bitly::Url" do
200
- assert_kind_of Bitly::Url, @url
201
- end
202
- should "return an info object with the url" do
203
- assert !@url.info.nil?
204
- end
205
- end
206
- context "a single hash" do
207
- setup do
208
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/info\?.*hash=3j4ir4.*$/,"google_info.json")
209
- @url = @bitly.info('3j4ir4')
210
- end
211
- should "return a Bitly::Url" do
212
- assert_kind_of Bitly::Url, @url
213
- end
214
- should "return an info object with the url" do
215
- assert !@url.info.nil?
216
- end
217
- end
218
- context "a list of hashes" do
219
- setup do
220
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/info\?.*hash=3j4ir4.*31IqMl.*$/,"google_and_cnn_info.json")
221
- @urls = @bitly.info(['3j4ir4','31IqMl'])
222
- end
223
- should "return a Bitly::Url" do
224
- assert_kind_of Array, @urls
225
- end
226
- should "return an info object with the url" do
227
- assert !@urls[0].info.nil?
228
- assert !@urls[1].info.nil?
229
- end
230
- end
231
- end
232
- context "to get stats on" do
233
- context "a single link" do
234
- setup do
235
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
236
- @url = @bitly.stats('http://bit.ly/3j4ir4')
237
- end
238
- should "return a Bitly::Url" do
239
- assert_kind_of Bitly::Url, @url
240
- end
241
- should "return an stats object" do
242
- assert !@url.stats.nil?
243
- end
244
- end
245
- context "a single hash" do
246
- setup do
247
- stub_get(/^https:\/\/api-ssl\.bitly\.com\/stats\?.*hash=3j4ir4.*$/,"google_stats.json")
248
- @url = @bitly.stats('3j4ir4')
249
- end
250
- should "return a Bitly::Url" do
251
- assert_kind_of Bitly::Url, @url
252
- end
253
- should "return an stats object" do
254
- assert !@url.stats.nil?
255
- end
256
- end
257
- context "a list of hashes" do
258
- should "return an argument error" do
259
- assert_raises ArgumentError do
260
- @bitly.stats(['3j4ir4','31IqMl'])
261
- end
262
- end
263
- end
264
- end
265
- end
266
- end