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
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+ require "oauth2"
3
+ require "base64"
4
+
5
+ module Bitly
6
+ ##
7
+ # The Oauth class handles interacting with the Bitly API to setup OAuth flows
8
+ # to redirect the user to authorize with Bitly and turn the resultant code
9
+ # into an OAuth access token.
10
+ class OAuth
11
+ attr_reader :client_id, :client_secret
12
+
13
+ ##
14
+ # Creates a new Bitly::OAuth client to retrieve user access tokens.
15
+ #
16
+ # To initialize a Bitly::OAuth client you need a client_id and client_secret
17
+ # which you will get when you register your application at
18
+ # https://bitly.com/a/oauth_apps
19
+ #
20
+ # @example
21
+ # oauth = Bitly::OAuth.new(client_id: "test", client_secret: "secret")
22
+ #
23
+ # @param [String] client_id The client ID from
24
+ # https://bitly.com/a/oauth_apps
25
+ # @param [String] client_secret The client_secret from
26
+ # https://bitly.com/a/oauth_apps
27
+ # @return [Bitly::OAuth] An authenticated Bitly::OAuth instance
28
+ # @since 2.0.0
29
+ def initialize(client_id:, client_secret:)
30
+ @client_id = client_id
31
+ @client_secret = client_secret
32
+ @client = OAuth2::Client.new(
33
+ client_id,
34
+ client_secret,
35
+ :site => 'https://api-ssl.bitly.com',
36
+ :token_url => '/oauth/access_token',
37
+ )
38
+ end
39
+
40
+ ##
41
+ # Returns the URL that you need to redirect the user to, so they can give
42
+ # your app permission to use their Bitly account.
43
+ #
44
+ # @example
45
+ # oauth.authorize_uri("https://example.com/oauth/redirect")
46
+ # # => "https://bitly.com/oauth/authorize?client_id=...&redirect_uri=https://example.com/oauth_page"
47
+ #
48
+ # @param [String] redirect_uri The URI you want Bitly to redirect the user
49
+ # back to once they are authorized. This should match the redirect_uri
50
+ # you set in your Bitly OAuth app.
51
+ # @param [String] state An optional state that you can pass to the
52
+ # authorize URI that will be returned in the redirect.
53
+ #
54
+ # @return [String] The authorize URI that you should use to redirect your
55
+ # user.
56
+ # @since 2.0.0
57
+ def authorize_uri(redirect_uri, state: nil)
58
+ params = {
59
+ redirect_uri: redirect_uri,
60
+ client_id: client_id
61
+ }
62
+ params[:state] = state if state
63
+ @client.authorize_url(**params).gsub(/api-ssl\./,'')
64
+ end
65
+
66
+ ##
67
+ # When the user is redirected to your redirect URI, Bitly will include a
68
+ # code parameter. You then use the original redirect URI and the code to
69
+ # retrieve an access token.
70
+ #
71
+ # @example
72
+ # oauth.access_token(redirect_uri: "https://example.com/oauth/redirect", "code")
73
+ # # => "9646c4f76e32c18f0afad3b3ed9524f3c917775b"
74
+ def access_token(redirect_uri: nil, code: nil, username: nil, password: nil)
75
+ begin
76
+ if redirect_uri && code
77
+ access_token_from_code(redirect_uri: redirect_uri, code: code)
78
+ elsif username && password
79
+ access_token_from_credentials(username: username, password: password)
80
+ else
81
+ raise ArgumentError, "not enough arguments, please include a redirect_uri and code or a username and password."
82
+ end
83
+ rescue OAuth2::Error => e
84
+ raise Bitly::Error, Bitly::HTTP::Response.new(status: e.response.status.to_s, body: e.response.body, headers: e.response.headers)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def access_token_from_code(redirect_uri:, code:)
91
+ @client.get_token(
92
+ redirect_uri: redirect_uri,
93
+ code: code
94
+ ).token
95
+ end
96
+
97
+ def access_token_from_credentials(username:, password:)
98
+ @client.password.get_token(username, password, {
99
+ headers: {
100
+ "Authorization" => "Basic #{authorization_header}"
101
+ }
102
+ }).token
103
+ end
104
+
105
+ def authorization_header
106
+ Base64.strict_encode64("#{client_id}:#{client_secret}")
107
+ end
108
+ end
109
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Bitly
2
- VERSION = '1.1.1'
4
+ VERSION = "2.0.1"
3
5
  end
metadata CHANGED
@@ -1,121 +1,107 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Nash
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-23 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: multi_json
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.3'
27
- - !ruby/object:Gem::Dependency
28
- name: httparty
14
+ name: oauth2
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
32
18
  - !ruby/object:Gem::Version
33
- version: 0.7.6
19
+ version: 0.5.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
34
23
  type: :runtime
35
24
  prerelease: false
36
25
  version_requirements: !ruby/object:Gem::Requirement
37
26
  requirements:
38
27
  - - ">="
39
28
  - !ruby/object:Gem::Version
40
- version: 0.7.6
29
+ version: 0.5.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
41
33
  - !ruby/object:Gem::Dependency
42
- name: oauth2
34
+ name: bundler
43
35
  requirement: !ruby/object:Gem::Requirement
44
36
  requirements:
45
- - - "<"
37
+ - - "~>"
46
38
  - !ruby/object:Gem::Version
47
39
  version: '2.0'
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: 0.5.0
51
- type: :runtime
40
+ type: :development
52
41
  prerelease: false
53
42
  version_requirements: !ruby/object:Gem::Requirement
54
43
  requirements:
55
- - - "<"
44
+ - - "~>"
56
45
  - !ruby/object:Gem::Version
57
46
  version: '2.0'
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 0.5.0
61
47
  - !ruby/object:Gem::Dependency
62
- name: bundler
48
+ name: rake
63
49
  requirement: !ruby/object:Gem::Requirement
64
50
  requirements:
65
51
  - - "~>"
66
52
  - !ruby/object:Gem::Version
67
- version: '1.15'
53
+ version: '13.0'
68
54
  type: :development
69
55
  prerelease: false
70
56
  version_requirements: !ruby/object:Gem::Requirement
71
57
  requirements:
72
58
  - - "~>"
73
59
  - !ruby/object:Gem::Version
74
- version: '1.15'
60
+ version: '13.0'
75
61
  - !ruby/object:Gem::Dependency
76
- name: rake
62
+ name: rspec
77
63
  requirement: !ruby/object:Gem::Requirement
78
64
  requirements:
79
- - - ">="
65
+ - - "~>"
80
66
  - !ruby/object:Gem::Version
81
- version: '0'
67
+ version: '3.0'
82
68
  type: :development
83
69
  prerelease: false
84
70
  version_requirements: !ruby/object:Gem::Requirement
85
71
  requirements:
86
- - - ">="
72
+ - - "~>"
87
73
  - !ruby/object:Gem::Version
88
- version: '0'
74
+ version: '3.0'
89
75
  - !ruby/object:Gem::Dependency
90
- name: shoulda
76
+ name: simplecov
91
77
  requirement: !ruby/object:Gem::Requirement
92
78
  requirements:
93
79
  - - "~>"
94
80
  - !ruby/object:Gem::Version
95
- version: 3.5.0
81
+ version: 0.17.1
96
82
  type: :development
97
83
  prerelease: false
98
84
  version_requirements: !ruby/object:Gem::Requirement
99
85
  requirements:
100
86
  - - "~>"
101
87
  - !ruby/object:Gem::Version
102
- version: 3.5.0
88
+ version: 0.17.1
103
89
  - !ruby/object:Gem::Dependency
104
- name: flexmock
90
+ name: webmock
105
91
  requirement: !ruby/object:Gem::Requirement
106
92
  requirements:
107
- - - ">="
93
+ - - "~>"
108
94
  - !ruby/object:Gem::Version
109
- version: '0'
95
+ version: 3.7.6
110
96
  type: :development
111
97
  prerelease: false
112
98
  version_requirements: !ruby/object:Gem::Requirement
113
99
  requirements:
114
- - - ">="
100
+ - - "~>"
115
101
  - !ruby/object:Gem::Version
116
- version: '0'
102
+ version: 3.7.6
117
103
  - !ruby/object:Gem::Dependency
118
- name: webmock
104
+ name: vcr
119
105
  requirement: !ruby/object:Gem::Requirement
120
106
  requirements:
121
107
  - - ">="
@@ -129,106 +115,91 @@ dependencies:
129
115
  - !ruby/object:Gem::Version
130
116
  version: '0'
131
117
  - !ruby/object:Gem::Dependency
132
- name: minitest
118
+ name: envyable
133
119
  requirement: !ruby/object:Gem::Requirement
134
120
  requirements:
135
- - - "~>"
121
+ - - ">="
136
122
  - !ruby/object:Gem::Version
137
- version: 5.8.3
123
+ version: '0'
138
124
  type: :development
139
125
  prerelease: false
140
126
  version_requirements: !ruby/object:Gem::Requirement
141
127
  requirements:
142
- - - "~>"
128
+ - - ">="
143
129
  - !ruby/object:Gem::Version
144
- version: 5.8.3
145
- description: Use the bit.ly API to shorten or expand URLs
146
- email: philnash@gmail.com
130
+ version: '0'
131
+ description: Use the Bitly API version 4 to shorten or expand URLs. Check out the
132
+ API documentation at https://dev.bitly.com/.
133
+ email:
134
+ - philnash@gmail.com
147
135
  executables: []
148
136
  extensions: []
149
- extra_rdoc_files:
150
- - LICENSE.md
151
- - README.md
137
+ extra_rdoc_files: []
152
138
  files:
153
139
  - ".gitignore"
140
+ - ".rspec"
154
141
  - ".travis.yml"
142
+ - CODE_OF_CONDUCT.md
155
143
  - Gemfile
156
144
  - History.txt
157
145
  - LICENSE.md
158
- - Manifest
159
146
  - README.md
160
147
  - Rakefile
161
148
  - bitly.gemspec
149
+ - config/env.yml.example
162
150
  - lib/bitly.rb
163
- - lib/bitly/client.rb
164
- - lib/bitly/config.rb
165
- - lib/bitly/url.rb
166
- - lib/bitly/utils.rb
167
- - lib/bitly/v3.rb
168
- - lib/bitly/v3/bitly.rb
169
- - lib/bitly/v3/client.rb
170
- - lib/bitly/v3/country.rb
171
- - lib/bitly/v3/day.rb
172
- - lib/bitly/v3/missing_url.rb
173
- - lib/bitly/v3/oauth.rb
174
- - lib/bitly/v3/realtime_link.rb
175
- - lib/bitly/v3/referrer.rb
176
- - lib/bitly/v3/url.rb
177
- - lib/bitly/v3/user.rb
151
+ - lib/bitly/api.rb
152
+ - lib/bitly/api/base.rb
153
+ - lib/bitly/api/bitlink.rb
154
+ - lib/bitly/api/bitlink/clicks_summary.rb
155
+ - lib/bitly/api/bitlink/deeplink.rb
156
+ - lib/bitly/api/bitlink/link_click.rb
157
+ - lib/bitly/api/bitlink/paginated_list.rb
158
+ - lib/bitly/api/bsd.rb
159
+ - lib/bitly/api/click_metric.rb
160
+ - lib/bitly/api/client.rb
161
+ - lib/bitly/api/group.rb
162
+ - lib/bitly/api/group/preferences.rb
163
+ - lib/bitly/api/list.rb
164
+ - lib/bitly/api/oauth_app.rb
165
+ - lib/bitly/api/organization.rb
166
+ - lib/bitly/api/shorten_counts.rb
167
+ - lib/bitly/api/user.rb
168
+ - lib/bitly/error.rb
169
+ - lib/bitly/http.rb
170
+ - lib/bitly/http/adapters.rb
171
+ - lib/bitly/http/adapters/net_http.rb
172
+ - lib/bitly/http/client.rb
173
+ - lib/bitly/http/request.rb
174
+ - lib/bitly/http/response.rb
175
+ - lib/bitly/oauth.rb
178
176
  - lib/bitly/version.rb
179
- - test/bitly/test_client.rb
180
- - test/bitly/test_config.rb
181
- - test/bitly/test_url.rb
182
- - test/bitly/test_utils.rb
183
- - test/fixtures/cnn.json
184
- - test/fixtures/cnn_and_google.json
185
- - test/fixtures/expand_cnn.json
186
- - test/fixtures/expand_cnn_and_google.json
187
- - test/fixtures/google_and_cnn_info.json
188
- - test/fixtures/google_info.json
189
- - test/fixtures/google_stats.json
190
- - test/fixtures/shorten_error.json
191
- - test/test_helper.rb
192
- homepage: http://github.com/philnash/bitly
177
+ homepage: https://github.com/philnash/bitly
193
178
  licenses:
194
179
  - MIT
195
- metadata: {}
180
+ metadata:
181
+ bug_tracker_uri: https://github.com/philnash/bitly/issues
182
+ changelog_uri: https://github.com/philnash/bitly/blob/master/History.txt
183
+ documentation_uri: https://www.rubydoc.info/gems/bitly/
184
+ homepage_uri: https://github.com/philnash/bitly
185
+ source_code_uri: https://github.com/philnash/bitly
196
186
  post_install_message:
197
- rdoc_options:
198
- - "--line-numbers"
199
- - "--title"
200
- - Bitly
201
- - "--main"
202
- - README.md
187
+ rdoc_options: []
203
188
  require_paths:
204
189
  - lib
205
190
  required_ruby_version: !ruby/object:Gem::Requirement
206
191
  requirements:
207
192
  - - ">="
208
193
  - !ruby/object:Gem::Version
209
- version: 1.8.7
194
+ version: 2.3.0
210
195
  required_rubygems_version: !ruby/object:Gem::Requirement
211
196
  requirements:
212
197
  - - ">="
213
198
  - !ruby/object:Gem::Version
214
199
  version: '0'
215
200
  requirements: []
216
- rubyforge_project:
217
- rubygems_version: 2.6.8
201
+ rubygems_version: 3.0.3
218
202
  signing_key:
219
203
  specification_version: 4
220
- summary: Use the bit.ly API to shorten or expand URLs
221
- test_files:
222
- - test/bitly/test_client.rb
223
- - test/bitly/test_config.rb
224
- - test/bitly/test_url.rb
225
- - test/bitly/test_utils.rb
226
- - test/fixtures/cnn.json
227
- - test/fixtures/cnn_and_google.json
228
- - test/fixtures/expand_cnn.json
229
- - test/fixtures/expand_cnn_and_google.json
230
- - test/fixtures/google_and_cnn_info.json
231
- - test/fixtures/google_info.json
232
- - test/fixtures/google_stats.json
233
- - test/fixtures/shorten_error.json
234
- - test/test_helper.rb
204
+ summary: Use the Bitly API to shorten or expand URLs
205
+ test_files: []
data/Manifest DELETED
@@ -1,37 +0,0 @@
1
- Gemfile
2
- History.txt
3
- LICENSE.md
4
- Manifest
5
- README.md
6
- Rakefile
7
- bitly.gemspec
8
- lib/bitly.rb
9
- lib/bitly/client.rb
10
- lib/bitly/config.rb
11
- lib/bitly/url.rb
12
- lib/bitly/utils.rb
13
- lib/bitly/v3.rb
14
- lib/bitly/v3/bitly.rb
15
- lib/bitly/v3/client.rb
16
- lib/bitly/v3/country.rb
17
- lib/bitly/v3/day.rb
18
- lib/bitly/v3/missing_url.rb
19
- lib/bitly/v3/oauth.rb
20
- lib/bitly/v3/realtime_link.rb
21
- lib/bitly/v3/referrer.rb
22
- lib/bitly/v3/url.rb
23
- lib/bitly/v3/user.rb
24
- lib/bitly/version.rb
25
- test/bitly/test_client.rb
26
- test/bitly/test_config.rb
27
- test/bitly/test_url.rb
28
- test/bitly/test_utils.rb
29
- test/fixtures/cnn.json
30
- test/fixtures/cnn_and_google.json
31
- test/fixtures/expand_cnn.json
32
- test/fixtures/expand_cnn_and_google.json
33
- test/fixtures/google_and_cnn_info.json
34
- test/fixtures/google_info.json
35
- test/fixtures/google_stats.json
36
- test/fixtures/shorten_error.json
37
- test/test_helper.rb