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,232 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./base"
3
+ require_relative "./list"
4
+ require_relative "./group/preferences"
5
+ require_relative "./shorten_counts"
6
+
7
+ module Bitly
8
+ module API
9
+ ##
10
+ # A Group represents a subdivision of an Organization. Most API actions are
11
+ # taken on behalf of a user and group and groups become a container for
12
+ # Bitlinks and metrics.
13
+ class Group
14
+ include Base
15
+
16
+ ##
17
+ # A Group::List is a container for a list of groups.
18
+ class List < Bitly::API::List ; end
19
+
20
+ ##
21
+ # Get a list of groups from the API. It receives an authorized
22
+ # `Bitly::API::Client` object and uses it to request the `/groups`
23
+ # endpoint, optionally passing an organization guid.
24
+ # [`GET /v4/groups`](https://dev.bitly.com/v4/#operation/getGroups)
25
+ #
26
+ # @example
27
+ # groups = Bitly::API::Group.list(client: client)
28
+ #
29
+ # @param client [Bitly::API::Client] An authorized API client
30
+ # @param organization [Bitly::API::Organization | String] An organization
31
+ # object or a String representing an organization guid
32
+ #
33
+ # @return [Bitly::API::Group::List]
34
+ def self.list(client:, organization_guid: nil)
35
+ params = { "organization_guid" => organization_guid }
36
+ response = client.request(path: "/groups", params: params)
37
+ groups = response.body["groups"].map do |group|
38
+ Group.new(data: group, client: client)
39
+ end
40
+ List.new(items: groups, response: response)
41
+ end
42
+
43
+ ##
44
+ # Retrieve a group from the API. It receives an authorized
45
+ # `Bitly::API::Client` object and a group guid and uses it to request
46
+ # the `/groups/:group_guid` endpoint.
47
+ # [`GET /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/getGroup)
48
+ #
49
+ # @example
50
+ # group = Bitly::API::Group.fetch(client: client, guid: guid)
51
+ #
52
+ # @param client [Bitly::API::Client] An authorized API client
53
+ # @param guid [String] A group guid
54
+ #
55
+ # @return [Bitly::API::Group]
56
+ def self.fetch(client:, group_guid:)
57
+ response = client.request(path: "/groups/#{group_guid}")
58
+ Group.new(data: response.body, client: client, response: response)
59
+ end
60
+
61
+ # @return [Array<Symbol>] The attributes the API returns for a group
62
+ def self.attributes
63
+ [:name, :guid, :is_active, :role, :bsds, :organization_guid]
64
+ end
65
+ # @return [Array<Symbol>] The attributes the API returns that need to be
66
+ # converted to `Time` objects.
67
+ def self.time_attributes
68
+ [:created, :modified]
69
+ end
70
+
71
+ attr_reader(*(attributes + time_attributes))
72
+
73
+ ##
74
+ # Creates a new `Bitly::API::Group` object.
75
+ #
76
+ # @example
77
+ # group = Bitly::API::Group.new(data: group_data, client: client)
78
+ #
79
+ # @param data [Hash<String, String|Boolean>] Data returned from the API
80
+ # about the group
81
+ # @param client [Bitly::API::Client] An authorized API client
82
+ # @param response [Bitly::HTTP::Response] The response object from an API
83
+ # call
84
+ # @param organization [Bitly::API::Organization]
85
+ #
86
+ # @return [Bitly::API::Group]
87
+ def initialize(data:, client:, response: nil, organization: nil)
88
+ assign_attributes(data)
89
+ @client = client
90
+ @response = response
91
+ @organization = organization
92
+ end
93
+
94
+ ##
95
+ # Fetch the organization for the group.
96
+ # [`GET /v4/organizations/{organization_guid}`)](https://dev.bitly.com/v4/#operation/getOrganization)
97
+ #
98
+ # @return [Bitly::API::Organization]
99
+ def organization
100
+ @organization ||= Organization.fetch(client: @client, organization_guid: organization_guid)
101
+ end
102
+
103
+ ##
104
+ # Fetch the group's preferences.
105
+ # [`GET /v4/groups/{group_guid}/preferences`](https://dev.bitly.com/v4/#operation/getGroupPreferences)
106
+ #
107
+ # @return [Bitly::API::Group::Preferences]
108
+ def preferences
109
+ @preferences ||= Group::Preferences.fetch(client: @client, group_guid: guid)
110
+ end
111
+
112
+ ##
113
+ # Fetch the group's tags
114
+ # [`GET /v4/groups/{group_guid}/tags`](https://dev.bitly.com/v4/#operation/getGroupTags)
115
+ #
116
+ # @return [Array<String>]
117
+ def tags
118
+ @tags ||= @client.request(path: "/groups/#{guid}/tags").body["tags"]
119
+ end
120
+
121
+ ##
122
+ # Allows you to update the group's name, organization or BSDs.
123
+ # If you update the organization guid and have already loaded the
124
+ # organization, it is nilled out so it can be reloaded with the correct
125
+ # guid
126
+ # [`PATCH /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/updateGroup)
127
+ #
128
+ # @example
129
+ # group.update(name: "New Name", organization_guid: "aaabbb")
130
+ #
131
+ # @param name [String] A new name
132
+ # @param organization_guid [String] A new organization guid
133
+ # @param bsds [Array<String>] An array of branded short domains
134
+ #
135
+ # @return [Bitly::API::Group]
136
+ def update(name: nil, organization_guid: nil, bsds: nil)
137
+ params = {
138
+ "name" => name,
139
+ "bsds" => bsds
140
+ }
141
+ if organization_guid
142
+ params["organization_guid"] = organization_guid
143
+ @organization = nil
144
+ end
145
+ @response = @client.request(path: "/groups/#{guid}", method: "PATCH", params: params)
146
+ assign_attributes(@response.body)
147
+ self
148
+ end
149
+
150
+ ##
151
+ # Deletes the group.
152
+ # [`DELETE /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/deleteGroup)
153
+ #
154
+ # @example
155
+ # group.delete
156
+ #
157
+ # @return [Nil]
158
+ def delete
159
+ @response = @client.request(path: "/groups/#{guid}", method: "DELETE")
160
+ return nil
161
+ end
162
+
163
+ ##
164
+ # Get the shorten counts for the group.
165
+ # # [`GET /v4/groups/{group_guid}/shorten_counts`](https://dev.bitly.com/v4/#operation/getGroupShortenCounts)
166
+ #
167
+ # @return [Bitly::API::ShortenCounts]
168
+ def shorten_counts
169
+ ShortenCounts.by_group(client: @client, group_guid: guid)
170
+ end
171
+
172
+ ##
173
+ # Gets the Bitlinks for the group.
174
+ # [`GET /v4/groups/{group_guid}/bitlinks`](https://dev.bitly.com/v4/#operation/getBitlinksByGroup)
175
+ #
176
+ # @return [Bitly::API::Bitlink::List]
177
+ def bitlinks
178
+ Bitly::API::Bitlink.list(client: @client, group_guid: guid)
179
+ end
180
+
181
+ ##
182
+ # Gets the referring networks for the group.
183
+ # [`GET /v4/groups/{group_guid}/referring_networks`](https://dev.bitly.com/v4/#operation/GetGroupMetricsByReferringNetworks)
184
+ #
185
+ # @param unit [String] A unit of time. Default is "day" and can be
186
+ # "minute", "hour", "day", "week" or "month"
187
+ # @param units [Integer] An integer representing the time units to query
188
+ # data for. pass -1 to return all units of time. Defaults to -1.
189
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
190
+ # most recent time for which to pull metrics. Will default to current
191
+ # time.
192
+ # @param size [Integer] The number of links to be returned. Defaults to 50
193
+ #
194
+ # @return [Bitly::API::ClickMetric::List]
195
+ def referring_networks(unit: nil, units: nil, unit_reference: nil, size: nil)
196
+ ClickMetric.list_referring_networks(
197
+ client: @client,
198
+ group_guid: guid,
199
+ unit: unit,
200
+ units: units,
201
+ unit_reference: unit_reference,
202
+ size: size
203
+ )
204
+ end
205
+
206
+ ##
207
+ # Gets the country click metrics for the group.
208
+ # [`GET /v4/groups/{group_guid}/countries`](https://dev.bitly.com/v4/#operation/getGroupMetricsByCountries)
209
+ #
210
+ # @param unit [String] A unit of time. Default is "day" and can be
211
+ # "minute", "hour", "day", "week" or "month"
212
+ # @param units [Integer] An integer representing the time units to query
213
+ # data for. pass -1 to return all units of time. Defaults to -1.
214
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
215
+ # most recent time for which to pull metrics. Will default to current
216
+ # time.
217
+ # @param size [Integer] The number of links to be returned. Defaults to 50
218
+ #
219
+ # @return [Bitly::API::ClickMetric::List]
220
+ def countries(unit: nil, units: nil, unit_reference: nil, size: nil)
221
+ ClickMetric.list_countries_by_group(
222
+ client: @client,
223
+ group_guid: guid,
224
+ unit: unit,
225
+ units: units,
226
+ unit_reference: unit_reference,
227
+ size: size
228
+ )
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./../base.rb"
3
+
4
+ module Bitly
5
+ module API
6
+ class Group
7
+ ##
8
+ # The Preferences object represents the account preferences of a Group.
9
+ # It includes the ability to find the domain preference for the group and
10
+ # to update it.
11
+ class Preferences
12
+ include Base
13
+
14
+ # @return [Array<Symbol>] The attributes the API returns for a group
15
+ def self.attributes ; [:group_guid, :domain_preference] ; end
16
+ attr_reader(*attributes)
17
+
18
+ ##
19
+ # Retrieve the preferences for a Group, given by the group guid
20
+ #
21
+ # @example
22
+ # preferences = Bitly::API::Group::Preferences.fetch(client: client, group_guid: group_guid)
23
+ #
24
+ # @param client [Bitly::API::Client] An authorized API client
25
+ # @param group_guid [String] The guid of the groups
26
+ #
27
+ # @return [Bitly::API::Group::Preferences]
28
+ def self.fetch(client:, group_guid:)
29
+ response = client.request(path: "/groups/#{group_guid}/preferences")
30
+ new(data: response.body, client: client, response: response)
31
+ end
32
+
33
+ ##
34
+ # Creates a new Bitly::API::Group::Preferences object from the data,
35
+ # API client and optional response.
36
+ # [`GET /v4/groups/{group_guid}/preferences`](https://dev.bitly.com/v4/#operation/getGroupPreferences)
37
+ #
38
+ # @example
39
+ # preferences = Bitly::API::Group::Preferences.new(data: data, client: client)
40
+ #
41
+ # @param data [Hash<String, String>] The preferences data from the API
42
+ # @param client [Bitly::API::Client] An authorized API client
43
+ # @param response [Bitly::HTTP::Response] The API response object
44
+ def initialize(data:, client:, response: nil)
45
+ assign_attributes(data)
46
+ @client = client
47
+ @response = response
48
+ end
49
+
50
+ ##
51
+ # Updates the preferences via the API
52
+ # [`PATCH /v4/groups/{group_guid}/preferences`](https://dev.bitly.com/v4/#operation/updateGroupPreferences)
53
+ #
54
+ # @example
55
+ # preferences.update(domain_preference: 'bit.ly')
56
+ #
57
+ # @param domain_preference [String] The new domain preference for this
58
+ # group
59
+ #
60
+ # @return [Bitly::API::Group::Preferences]
61
+ def update(domain_preference:)
62
+ @response = @client.request(
63
+ path: "/groups/#{group_guid}/preferences",
64
+ method: "PATCH",
65
+ params: { domain_preference: domain_preference }
66
+ )
67
+ assign_attributes(response.body)
68
+ self
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bitly
4
+ module API
5
+ ##
6
+ # A base class for lists of API resources. Implements Enumerable.
7
+ class List
8
+ include Enumerable
9
+
10
+ attr_reader :response
11
+
12
+ def initialize(items:, response:)
13
+ @items = items
14
+ @response = response
15
+ end
16
+
17
+ def each
18
+ @items.each { |item| yield item }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./base"
3
+
4
+ module Bitly
5
+ module API
6
+ class OAuthApp
7
+ include Base
8
+
9
+ def self.attributes
10
+ [:name, :description, :link, :client_id]
11
+ end
12
+ attr_reader(*attributes)
13
+
14
+ def self.fetch(client:, client_id:)
15
+ response = client.request(path: "/apps/#{client_id}")
16
+ new(data: response.body, client: client, response: response)
17
+ end
18
+
19
+ def initialize(data:, client:, response: nil)
20
+ assign_attributes(data)
21
+ @client = client
22
+ @response = response
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./base"
3
+ require_relative "./list"
4
+
5
+ module Bitly
6
+ module API
7
+ ##
8
+ # An Organization is the top level of the Bitly user hierarchy. Both Users
9
+ # and Groups live within an organization.
10
+ class Organization
11
+ include Base
12
+
13
+ ##
14
+ # An Organization::List is a container for a list of organizations.
15
+ class List < Bitly::API::List ; end
16
+
17
+ ##
18
+ # Get a list of organizations from the API. It receives an authorized
19
+ # `Bitly::API::Client` object and uses it to request the `/organizations`
20
+ # endpoint.
21
+ # [`GET /v4/organizations`](https://dev.bitly.com/v4/#operation/getOrganizations)
22
+ #
23
+ # @example
24
+ # organizations = Bitly::API::Organization.list(client: client)
25
+ #
26
+ # @param client [Bitly::API::Client] An authorized API client
27
+ #
28
+ # @return [Bitly::API::Organization::List]
29
+ def self.list(client:)
30
+ response = client.request(path: '/organizations')
31
+ organizations = response.body['organizations'].map do |org|
32
+ Organization.new(data: org, client: client)
33
+ end
34
+ List.new(items: organizations, response: response)
35
+ end
36
+
37
+ ##
38
+ # Retrieve an organization from the API. It receives an authorized
39
+ # `Bitly::API::Client` object and an organization guid and uses it to
40
+ # request the `/organizations/:organization_guid` endpoint.
41
+ # [`GET /v4/organizations/{organization_guid}`](https://dev.bitly.com/v4/#operation/getOrganization)
42
+ #
43
+ # @example
44
+ # organization = Bitly::API::Organization.fetch(client: client, organization_guid: guid)
45
+ #
46
+ # @param client [Bitly::API::Client] An authorized API client
47
+ # @param organization_guid [String] An organization guid
48
+ #
49
+ # @return [Bitly::API::Organization]
50
+ def self.fetch(client:, organization_guid:)
51
+ response = client.request(path: "/organizations/#{organization_guid}")
52
+ Organization.new(data: response.body, client: client, response: response)
53
+ end
54
+
55
+ # @return [Array<Symbol>] The attributes the API returns for an
56
+ # organization
57
+ def self.attributes
58
+ [:name, :guid, :is_active, :tier, :tier_family, :tier_display_name, :role, :bsds]
59
+ end
60
+ # @return [Array<Symbol>] The attributes the API returns that need to be
61
+ # converted to `Time` objects.
62
+ def self.time_attributes
63
+ [:created, :modified]
64
+ end
65
+ attr_reader(*(attributes + time_attributes))
66
+
67
+ ##
68
+ # Creates a new `Bitly::API::Organization` object
69
+ #
70
+ # @example
71
+ # organization = Bitly::API::Organization.new(data: org_data, client: client)
72
+ #
73
+ # @param data [Hash<String, String|Boolean>] Data returned from the API
74
+ # about the organization
75
+ # @param client [Bitly::API::Client] An authorized API client
76
+ # @param response [Bitly::HTTP::Response] The response object from an API
77
+ # call
78
+ #
79
+ # @return [Bitly::API::Organization]
80
+ def initialize(data:, client:, response: nil)
81
+ assign_attributes(data)
82
+ @client = client
83
+ @response = response
84
+ end
85
+
86
+ # @return [Bitly::API::Group::List]
87
+ def groups
88
+ @groups ||= Group.list(client: @client, organization: self)
89
+ end
90
+
91
+ ##
92
+ # Shorten counts by organization
93
+ # [`GET /v4/organizations/{organization_guid}/shorten_counts`](https://dev.bitly.com/v4/#operation/getOrganizationShortenCounts)
94
+ #
95
+ # @example
96
+ # shorten_counts = organization.shorten_counts
97
+ #
98
+ # @return [Bitly::API::ShortenCounts]
99
+ def shorten_counts
100
+ ShortenCounts.by_organization(client: @client, organization_guid: guid)
101
+ end
102
+ end
103
+ end
104
+ end