bitly 1.1.2 → 2.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +36 -3
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/History.txt +18 -1
  8. data/LICENSE.md +1 -1
  9. data/README.md +151 -58
  10. data/Rakefile +4 -9
  11. data/bin/console +14 -0
  12. data/bin/oauth +10 -0
  13. data/bin/setup +8 -0
  14. data/bitly.gemspec +24 -29
  15. data/config/env.yml.example +5 -0
  16. data/docs/authentication.md +51 -0
  17. data/docs/bitlinks.md +2 -0
  18. data/docs/branded_short_domains.md +20 -0
  19. data/docs/groups.md +80 -0
  20. data/docs/oauth_apps.md +20 -0
  21. data/docs/organizations.md +74 -0
  22. data/docs/premium_apis.md +0 -0
  23. data/docs/users.md +1 -0
  24. data/lib/bitly.rb +6 -7
  25. data/lib/bitly/api.rb +16 -0
  26. data/lib/bitly/api/base.rb +22 -0
  27. data/lib/bitly/api/bitlink.rb +341 -0
  28. data/lib/bitly/api/bitlink/clicks_summary.rb +35 -0
  29. data/lib/bitly/api/bitlink/deeplink.rb +29 -0
  30. data/lib/bitly/api/bitlink/link_click.rb +74 -0
  31. data/lib/bitly/api/bitlink/paginated_list.rb +51 -0
  32. data/lib/bitly/api/bsd.rb +24 -0
  33. data/lib/bitly/api/click_metric.rb +185 -0
  34. data/lib/bitly/api/client.rb +588 -0
  35. data/lib/bitly/api/group.rb +232 -0
  36. data/lib/bitly/api/group/preferences.rb +73 -0
  37. data/lib/bitly/api/list.rb +22 -0
  38. data/lib/bitly/api/oauth_app.rb +26 -0
  39. data/lib/bitly/api/organization.rb +104 -0
  40. data/lib/bitly/api/shorten_counts.rb +61 -0
  41. data/lib/bitly/api/user.rb +107 -0
  42. data/lib/bitly/error.rb +33 -0
  43. data/lib/bitly/http.rb +6 -0
  44. data/lib/bitly/http/adapters/net_http.rb +27 -0
  45. data/lib/bitly/http/client.rb +33 -0
  46. data/lib/bitly/http/request.rb +116 -0
  47. data/lib/bitly/http/response.rb +65 -0
  48. data/lib/bitly/oauth.rb +109 -0
  49. data/lib/bitly/version.rb +3 -1
  50. metadata +88 -108
  51. data/Manifest +0 -37
  52. data/lib/bitly/url.rb +0 -103
  53. data/lib/bitly/utils.rb +0 -57
  54. data/lib/bitly/v3.rb +0 -14
  55. data/lib/bitly/v3/bitly.rb +0 -7
  56. data/lib/bitly/v3/country.rb +0 -13
  57. data/lib/bitly/v3/day.rb +0 -13
  58. data/lib/bitly/v3/missing_url.rb +0 -15
  59. data/lib/bitly/v3/oauth.rb +0 -41
  60. data/lib/bitly/v3/realtime_link.rb +0 -18
  61. data/lib/bitly/v3/referrer.rb +0 -13
  62. data/lib/bitly/v3/url.rb +0 -154
  63. data/test/bitly/test_client.rb +0 -266
  64. data/test/bitly/test_config.rb +0 -28
  65. data/test/bitly/test_url.rb +0 -167
  66. data/test/bitly/test_utils.rb +0 -79
  67. data/test/fixtures/cnn.json +0 -1
  68. data/test/fixtures/cnn_and_google.json +0 -1
  69. data/test/fixtures/expand_cnn.json +0 -1
  70. data/test/fixtures/expand_cnn_and_google.json +0 -1
  71. data/test/fixtures/google_and_cnn_info.json +0 -1
  72. data/test/fixtures/google_info.json +0 -1
  73. data/test/fixtures/google_stats.json +0 -1
  74. data/test/fixtures/shorten_error.json +0 -1
  75. 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.2'
4
+ VERSION = "2.0.0.beta.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.2
4
+ version: 2.0.0.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Nash
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-19 00:00:00.000000000 Z
11
+ date: 2020-02-15 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: '10.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: '10.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,100 @@ 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 bit.ly API to shorten or expand URLs. Check out the API documentation
132
+ 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
148
+ - bin/console
149
+ - bin/oauth
150
+ - bin/setup
161
151
  - bitly.gemspec
152
+ - config/env.yml.example
153
+ - docs/authentication.md
154
+ - docs/bitlinks.md
155
+ - docs/branded_short_domains.md
156
+ - docs/groups.md
157
+ - docs/oauth_apps.md
158
+ - docs/organizations.md
159
+ - docs/premium_apis.md
160
+ - docs/users.md
162
161
  - lib/bitly.rb
162
+ - lib/bitly/api.rb
163
+ - lib/bitly/api/base.rb
164
+ - lib/bitly/api/bitlink.rb
165
+ - lib/bitly/api/bitlink/clicks_summary.rb
166
+ - lib/bitly/api/bitlink/deeplink.rb
167
+ - lib/bitly/api/bitlink/link_click.rb
168
+ - lib/bitly/api/bitlink/paginated_list.rb
169
+ - lib/bitly/api/bsd.rb
170
+ - lib/bitly/api/click_metric.rb
171
+ - lib/bitly/api/client.rb
172
+ - lib/bitly/api/group.rb
173
+ - lib/bitly/api/group/preferences.rb
174
+ - lib/bitly/api/list.rb
175
+ - lib/bitly/api/oauth_app.rb
176
+ - lib/bitly/api/organization.rb
177
+ - lib/bitly/api/shorten_counts.rb
178
+ - lib/bitly/api/user.rb
163
179
  - lib/bitly/client.rb
164
180
  - 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
181
+ - lib/bitly/error.rb
182
+ - lib/bitly/http.rb
183
+ - lib/bitly/http/adapters/net_http.rb
184
+ - lib/bitly/http/client.rb
185
+ - lib/bitly/http/request.rb
186
+ - lib/bitly/http/response.rb
187
+ - lib/bitly/oauth.rb
169
188
  - 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
189
  - lib/bitly/v3/user.rb
178
190
  - 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
191
+ homepage: https://github.com/philnash/bitly
193
192
  licenses:
194
193
  - MIT
195
194
  metadata: {}
196
195
  post_install_message:
197
- rdoc_options:
198
- - "--line-numbers"
199
- - "--title"
200
- - Bitly
201
- - "--main"
202
- - README.md
196
+ rdoc_options: []
203
197
  require_paths:
204
198
  - lib
205
199
  required_ruby_version: !ruby/object:Gem::Requirement
206
200
  requirements:
207
201
  - - ">="
208
202
  - !ruby/object:Gem::Version
209
- version: 1.8.7
203
+ version: '0'
210
204
  required_rubygems_version: !ruby/object:Gem::Requirement
211
205
  requirements:
212
- - - ">="
206
+ - - ">"
213
207
  - !ruby/object:Gem::Version
214
- version: '0'
208
+ version: 1.3.1
215
209
  requirements: []
216
- rubyforge_project:
217
- rubygems_version: 2.6.8
210
+ rubygems_version: 3.0.3
218
211
  signing_key:
219
212
  specification_version: 4
220
213
  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
214
+ 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