ruby-bitly 0.2.1 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ http_interactions:
5
5
  uri: https://api-ssl.bitly.com/v3/shorten
6
6
  body:
7
7
  encoding: UTF-8
8
- string: longURL=http%3A%2F%2Fgoogle.com&login=my-login&apiKey=my-api-key&domain=j.mp
8
+ string: longURL=http%3A%2F%2Fgoogle.com&domain=j.mp&login=my-login&apiKey=my-api-key
9
9
  headers:
10
10
  Accept:
11
11
  - "*/*; q=0.5, application/xml"
@@ -41,6 +41,6 @@ http_interactions:
41
41
  "new_hash": 0 } }
42
42
 
43
43
  '
44
- http_version:
44
+ http_version:
45
45
  recorded_at: Sat, 01 Oct 2016 19:29:25 GMT
46
46
  recorded_with: VCR 3.0.3
@@ -0,0 +1,42 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api-ssl.bitly.com/v3/shorten
6
+ body:
7
+ encoding: UTF-8
8
+ string: longURL=http%3A%2F%2Fbit.ly%2FbcvNe5&login=my-login&apiKey=my-api-key
9
+ headers:
10
+ Accept:
11
+ - "*/*"
12
+ Accept-Encoding:
13
+ - gzip, deflate
14
+ User-Agent:
15
+ - rest-client/2.0.1 (darwin18.2.0 x86_64) ruby/2.6.3p62
16
+ Content-Length:
17
+ - '69'
18
+ Content-Type:
19
+ - application/x-www-form-urlencoded
20
+ Host:
21
+ - api-ssl.bitly.com
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Server:
28
+ - nginx
29
+ Date:
30
+ - Sat, 11 May 2019 22:34:41 GMT
31
+ Content-Type:
32
+ - application/json; charset=utf-8
33
+ Content-Length:
34
+ - '58'
35
+ Connection:
36
+ - keep-alive
37
+ body:
38
+ encoding: UTF-8
39
+ string: '{"status_code":500,"status_txt":"INVALID_LOGIN","data":[]}'
40
+ http_version:
41
+ recorded_at: Sat, 11 May 2019 22:34:42 GMT
42
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :path => '../'
4
+
5
+ gem 'rest-client', '2.0.1'
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ruby-bitly (0.2.1)
5
+ rest-client (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.6.0)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ domain_name (0.5.20180417)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ hashdiff (0.3.9)
18
+ http-cookie (1.0.3)
19
+ domain_name (~> 0.5)
20
+ mime-types (3.2.2)
21
+ mime-types-data (~> 3.2015)
22
+ mime-types-data (3.2019.0331)
23
+ netrc (0.11.0)
24
+ public_suffix (2.0.5)
25
+ rake (12.3.2)
26
+ rest-client (2.0.1)
27
+ http-cookie (>= 1.0.2, < 2.0)
28
+ mime-types (>= 1.16, < 4.0)
29
+ netrc (~> 0.8)
30
+ rspec (3.8.0)
31
+ rspec-core (~> 3.8.0)
32
+ rspec-expectations (~> 3.8.0)
33
+ rspec-mocks (~> 3.8.0)
34
+ rspec-core (3.8.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-expectations (3.8.3)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-mocks (3.8.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-support (3.8.0)
43
+ safe_yaml (1.0.5)
44
+ unf (0.1.4)
45
+ unf_ext
46
+ unf_ext (0.0.7.6)
47
+ vcr (4.0.0)
48
+ webmock (3.5.1)
49
+ addressable (>= 2.3.6)
50
+ crack (>= 0.3.2)
51
+ hashdiff
52
+ wwtd (1.3.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ public_suffix (< 3)
59
+ rake
60
+ rest-client (= 2.0.1)
61
+ rspec
62
+ ruby-bitly!
63
+ vcr
64
+ webmock
65
+ wwtd
66
+
67
+ BUNDLED WITH
68
+ 1.17.3
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :path => '../'
4
+
5
+ gem 'rest-client', '2.0.2'
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ruby-bitly (0.2.1)
5
+ rest-client (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.6.0)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ domain_name (0.5.20180417)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ hashdiff (0.3.9)
18
+ http-cookie (1.0.3)
19
+ domain_name (~> 0.5)
20
+ mime-types (3.2.2)
21
+ mime-types-data (~> 3.2015)
22
+ mime-types-data (3.2019.0331)
23
+ netrc (0.11.0)
24
+ public_suffix (2.0.5)
25
+ rake (12.3.2)
26
+ rest-client (2.0.2)
27
+ http-cookie (>= 1.0.2, < 2.0)
28
+ mime-types (>= 1.16, < 4.0)
29
+ netrc (~> 0.8)
30
+ rspec (3.8.0)
31
+ rspec-core (~> 3.8.0)
32
+ rspec-expectations (~> 3.8.0)
33
+ rspec-mocks (~> 3.8.0)
34
+ rspec-core (3.8.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-expectations (3.8.3)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.8.0)
39
+ rspec-mocks (3.8.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.8.0)
42
+ rspec-support (3.8.0)
43
+ safe_yaml (1.0.5)
44
+ unf (0.1.4)
45
+ unf_ext
46
+ unf_ext (0.0.7.6)
47
+ vcr (4.0.0)
48
+ webmock (3.5.1)
49
+ addressable (>= 2.3.6)
50
+ crack (>= 0.3.2)
51
+ hashdiff
52
+ wwtd (1.3.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ public_suffix (< 3)
59
+ rake
60
+ rest-client (= 2.0.2)
61
+ rspec
62
+ ruby-bitly!
63
+ vcr
64
+ webmock
65
+ wwtd
66
+
67
+ BUNDLED WITH
68
+ 1.17.3
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec :path => '../'
4
+
5
+ gem 'rest-client', '2.1.0.rc1'
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ ruby-bitly (0.2.1)
5
+ rest-client (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.6.0)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
14
+ diff-lcs (1.3)
15
+ domain_name (0.5.20180417)
16
+ unf (>= 0.0.5, < 1.0.0)
17
+ hashdiff (0.3.9)
18
+ http-accept (1.7.0)
19
+ http-cookie (1.0.3)
20
+ domain_name (~> 0.5)
21
+ mime-types (3.2.2)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2019.0331)
24
+ netrc (0.11.0)
25
+ public_suffix (2.0.5)
26
+ rake (12.3.2)
27
+ rest-client (2.1.0.rc1)
28
+ http-accept (>= 1.7.0, < 2.0)
29
+ http-cookie (>= 1.0.2, < 2.0)
30
+ mime-types (>= 1.16, < 4.0)
31
+ netrc (~> 0.8)
32
+ rspec (3.8.0)
33
+ rspec-core (~> 3.8.0)
34
+ rspec-expectations (~> 3.8.0)
35
+ rspec-mocks (~> 3.8.0)
36
+ rspec-core (3.8.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-expectations (3.8.3)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-mocks (3.8.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-support (3.8.0)
45
+ safe_yaml (1.0.5)
46
+ unf (0.1.4)
47
+ unf_ext
48
+ unf_ext (0.0.7.6)
49
+ vcr (4.0.0)
50
+ webmock (3.5.1)
51
+ addressable (>= 2.3.6)
52
+ crack (>= 0.3.2)
53
+ hashdiff
54
+ wwtd (1.3.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ public_suffix (< 3)
61
+ rake
62
+ rest-client (= 2.1.0.rc1)
63
+ rspec
64
+ ruby-bitly!
65
+ vcr
66
+ webmock
67
+ wwtd
68
+
69
+ BUNDLED WITH
70
+ 1.17.3
@@ -6,16 +6,14 @@ require 'ruby-bitly/version'
6
6
 
7
7
  class Bitly < OpenStruct
8
8
 
9
- REST_API_URL = "http://api.bitly.com"
10
- REST_API_URL_SSL = "https://api-ssl.bitly.com"
11
- ACTION_PATH = { :shorten => '/v3/shorten', :expand => '/v3/expand', :clicks => '/v3/clicks' }
9
+ REST_API_URL = 'http://api.bitly.com'
10
+ REST_API_URL_SSL = 'https://api-ssl.bitly.com'
11
+ ACTION_PATH = { shorten: '/v3/shorten', expand: '/v3/expand', clicks: '/v3/clicks' }
12
12
  RestClient.proxy = ENV['http_proxy']
13
13
 
14
14
  class << self
15
15
  attr_accessor :login, :api_key
16
16
  attr_writer :use_ssl
17
- alias :key :api_key
18
- alias :key= :api_key=
19
17
 
20
18
  def use_ssl
21
19
  instance_variable_defined?(:@use_ssl) ? @use_ssl : true
@@ -33,86 +31,72 @@ class Bitly < OpenStruct
33
31
  yield self
34
32
  end
35
33
 
36
- # Old API:
37
- #
38
- # shorten(long_url, login = self.login, api_key = self.api_key)
39
- #
40
- # New API:
41
- #
42
34
  # shorten(options)
43
35
  #
44
- # Options can have:
36
+ # Options are:
45
37
  #
46
- # :long_url
47
- # :login
48
- # :api_key
49
- # :domain
50
- def shorten(long_url, login = self.login, api_key = self.api_key)
51
- options = ensure_options(url: long_url, login: login, api_key: api_key)
38
+ # long_url
39
+ # login
40
+ # api_key
41
+ # domain
42
+ def shorten(options)
43
+ options = ensure_options(options)
52
44
  options.select! { |k,_v| [:longURL, :domain, :login, :apiKey].include?(k) }
53
45
 
54
46
  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:shorten], options)
55
- response.delete("data") if response["data"].empty?
56
-
57
- bitly = new response["data"]
58
-
59
- bitly.hash_path = response["data"]["hash"] if response["status_code"] == 200
60
- bitly.status_code = response["status_code"]
61
- bitly.status_txt = response["status_txt"]
47
+ response['data'] = {} if response['data'].empty?
48
+
49
+ bitly = new({ 'success?' => response['status_code'].to_i == 200 })
50
+
51
+ if bitly.success?
52
+ bitly.send('new_hash?=', response['data']['new_hash'].to_i == 1)
53
+ bitly.short_url = response['data']['url']
54
+ bitly.long_url = response['data']['long_url']
55
+ bitly.global_hash = response['data']['global_hash']
56
+ bitly.user_hash = response['data']['hash']
57
+ else
58
+ bitly.error = response['status_txt'] unless bitly.success?
59
+ end
62
60
 
63
61
  bitly
64
62
  end
65
63
 
66
- # Old API:
67
- #
68
- # expand(short_url, login = self.login, api_key = self.api_key)
69
- #
70
- # New API:
71
- #
72
64
  # expand(options)
73
65
  #
74
- # Options can have:
66
+ # Options are:
75
67
  #
76
- # :long_url
77
- # :login
78
- # :api_key
79
- def expand(short_url, login = self.login, api_key = self.api_key)
80
- options = ensure_options(url: short_url, login: login, api_key: api_key)
68
+ # short_url
69
+ # login
70
+ # api_key
71
+ def expand(options)
72
+ options = ensure_options(options)
81
73
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }
82
74
 
83
75
  response = JSON.parse RestClient.post(rest_api_url + ACTION_PATH[:expand], options)
84
76
 
85
77
  bitly = new(response["data"]["expand"].first)
86
- bitly.status_code = response["status_code"]
87
- bitly.status_txt = response["status_txt"]
78
+ bitly.send('success?=', response["status_code"] == 200)
88
79
  bitly.long_url = bitly.error if bitly.error
89
80
 
90
81
  bitly
91
82
  end
92
83
 
93
- # Old API:
94
- #
95
- # get_clicks(short_url, login = self.login, api_key = self.api_key)
96
- #
97
- # New API:
98
- #
99
84
  # get_clicks(options)
100
85
  #
101
- # Options can have:
86
+ # Options are:
102
87
  #
103
- # :long_url
104
- # :login
105
- # :api_key
106
- def get_clicks(short_url, login = self.login, api_key = self.api_key)
107
- options = ensure_options(url: short_url, login: login, api_key: api_key)
88
+ # short_url
89
+ # login
90
+ # api_key
91
+ def get_clicks(options)
92
+ options = ensure_options(options)
108
93
  options.select! { |k,_v| [:shortURL, :login, :apiKey].include?(k) }
109
94
  options[:shortUrl] = options.delete(:shortURL)
110
95
 
111
96
  response = JSON.parse RestClient.get("#{rest_api_url}#{ACTION_PATH[:clicks]}", params: options)
112
97
 
113
98
  bitly = new(response["data"]["clicks"].first)
114
- bitly.status_code = response["status_code"]
115
- bitly.status_txt = response["status_txt"]
99
+ bitly.send('success?=', response["status_code"] == 200)
116
100
 
117
101
  bitly
118
102
  end
@@ -120,19 +104,17 @@ class Bitly < OpenStruct
120
104
  private
121
105
 
122
106
  def ensure_options(options)
123
- options = options[:url] if options[:url].is_a?(Hash)
124
-
125
107
  response = {}
126
- response[:shortURL] = options[:short_url] || options[:url]
127
- response[:longURL] = options[:long_url] || options[:url]
108
+ response[:shortURL] = options[:short_url]
109
+ response[:longURL] = options[:long_url]
110
+ response[:domain] = options[:domain]
128
111
  response[:login] = options[:login] || self.login
129
112
  response[:apiKey] = options[:api_key] || self.api_key
130
- response[:domain] = options[:domain]
131
113
  response.reject { |_k,v| v.nil? }
132
114
  end
133
115
 
134
116
  def rest_api_url
135
- use_ssl ? REST_API_URL_SSL : REST_API_URL
117
+ { true => REST_API_URL_SSL, false => REST_API_URL }[use_ssl]
136
118
  end
137
119
  end
138
120
  end