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,35 @@
1
+ # frozen_string_literal: true
2
+ require_relative "../base"
3
+
4
+ module Bitly
5
+ module API
6
+ class Bitlink
7
+ class ClicksSummary
8
+ include Base
9
+
10
+ def self.fetch(client:, bitlink:, unit: nil, units: nil, unit_reference: nil, size: nil)
11
+ response = client.request(
12
+ path: "/bitlinks/#{bitlink}/clicks/summary",
13
+ params: {
14
+ "unit" => unit,
15
+ "units" => units,
16
+ "unit_reference" => unit_reference,
17
+ "size" => size
18
+ }
19
+ )
20
+ new(data: response.body, response: response)
21
+ end
22
+
23
+ def self.attributes
24
+ [:units, :unit, :unit_reference, :total_clicks]
25
+ end
26
+ attr_reader(*attributes)
27
+
28
+ def initialize(data:, response: nil)
29
+ assign_attributes(data)
30
+ @response = response
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ require_relative "../base"
3
+
4
+ module Bitly
5
+ module API
6
+ class Bitlink
7
+ class Deeplink
8
+ include Base
9
+
10
+ def self.attributes
11
+ [:app_uri_path, :install_type, :install_url, :app_id]
12
+ end
13
+ attr_reader(*attributes)
14
+
15
+ def initialize(data:)
16
+ assign_attributes(data)
17
+ end
18
+
19
+ def to_json(opts=nil)
20
+ self.class.attributes.reduce({}) do |memo, key|
21
+ value = instance_variable_get("@#{key}")
22
+ memo[key] = value if value
23
+ memo
24
+ end.to_json(opts)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+ require "time"
3
+ require_relative "../base"
4
+ require_relative '../list'
5
+
6
+ module Bitly
7
+ module API
8
+ class Bitlink
9
+ class LinkClick
10
+ include Base
11
+
12
+ class List < Bitly::API::List
13
+ attr_reader :units, :unit_reference, :unit
14
+ def initialize(items:, response:, units:, unit_reference:, unit:)
15
+ super(items: items, response: response)
16
+ @units = units
17
+ @unit_reference = Time.parse(unit_reference) if unit_reference
18
+ @unit = unit
19
+ end
20
+ end
21
+
22
+ ##
23
+ # Get the clicks for a bitlink.
24
+ # [`GET /v4/bitlink/{bitlink}/clicks`](https://dev.bitly.com/v4/#operation/getClicksForBitlink)
25
+ #
26
+ # @param client [Bitly::API::Client] An authorized API client
27
+ # @param bitlink [String] The Bitlink for which you want the clicks
28
+ # @param sort [String] The data to sort on. Default and only option is
29
+ # "clicks".
30
+ # @param unit [String] A unit of time. Default is "day" and can be
31
+ # "minute", "hour", "day", "week" or "month"
32
+ # @param units [Integer] An integer representing the time units to query
33
+ # data for. pass -1 to return all units of time. Defaults to -1.
34
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
35
+ # most recent time for which to pull metrics. Will default to current
36
+ # time.
37
+ # @param size [Integer] The number of links to be returned. Defaults to 50
38
+ #
39
+ # @return [Bitly::API::Bitlink::LinkClick::List]
40
+ def self.list(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
41
+ response = client.request(
42
+ path: "/bitlinks/#{bitlink}/clicks",
43
+ params: {
44
+ "unit" => unit,
45
+ "units" => units,
46
+ "unit_reference" => unit_reference,
47
+ "size" => size
48
+ }
49
+ )
50
+ body = response.body
51
+ items = body["link_clicks"].map { |link_click| new(data: link_click) }
52
+ Bitly::API::Bitlink::LinkClick::List.new(
53
+ items: items,
54
+ response: response,
55
+ unit: body["unit"],
56
+ units: body["units"],
57
+ unit_reference: body["unit_reference"]
58
+ )
59
+ end
60
+
61
+ def self.attributes
62
+ [:clicks]
63
+ end
64
+ def self.time_attributes
65
+ [:date]
66
+ end
67
+ attr_reader(*(attributes + time_attributes))
68
+
69
+ def initialize(data:)
70
+ assign_attributes(data)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ require "cgi"
3
+ require "uri"
4
+
5
+ module Bitly
6
+ module API
7
+ class Bitlink
8
+ class PaginatedList < Bitly::API::List
9
+ attr_reader :next_url, :prev_url, :size, :page, :total
10
+
11
+ def initialize(items:, response: , client:)
12
+ super(items: items, response: response)
13
+ @client = client
14
+ if response.body["pagination"]
15
+ pagination = response.body["pagination"]
16
+ @next_url = pagination["next"]
17
+ @prev_url = pagination["prev"]
18
+ @size = pagination["size"]
19
+ @page = pagination["page"]
20
+ @total = pagination["total"]
21
+ end
22
+ end
23
+
24
+ def has_next_page?
25
+ !next_url.nil? && !next_url.empty?
26
+ end
27
+
28
+ def has_prev_page?
29
+ !prev_url.nil? && !prev_url.empty?
30
+ end
31
+
32
+ def next_page
33
+ has_next_page? ? get_page(uri: URI(next_url)) : nil
34
+ end
35
+
36
+ def prev_page
37
+ has_prev_page? ? get_page(uri: URI(prev_url)) : nil
38
+ end
39
+
40
+ private
41
+
42
+ def get_page(uri:)
43
+ response = @client.request(path: uri.path.gsub(/\/v4/, ""), params: CGI.parse(uri.query))
44
+ bitlinks = response.body["links"].map do |link|
45
+ Bitlink.new(data: link, client: @client)
46
+ end
47
+ self.class.new(items: bitlinks, response: response, client: @client)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require_relative "./list"
3
+
4
+ module Bitly
5
+ module API
6
+ module BSD
7
+ class List < Bitly::API::List ; end
8
+
9
+ ##
10
+ # Fetch Branded Short Domains (BSDs).
11
+ # [`GET /v4/bsds`](https://dev.bitly.com/v4/#operation/getBSDs)
12
+ #
13
+ # @example
14
+ # bsds = Bitly::API::BSD.list(client: client)
15
+ #
16
+ # @return [Array<String>]
17
+ def self.list(client:)
18
+ response = client.request(path: "/bsds")
19
+ bsds = response.body["bsds"]
20
+ List.new(items: bsds, response: response)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,186 @@
1
+ # frozen_string_literal: true
2
+ require "time"
3
+ require_relative "./base"
4
+ require_relative './list'
5
+
6
+ module Bitly
7
+ module API
8
+ class ClickMetric
9
+ include Base
10
+
11
+ class List < Bitly::API::List
12
+ attr_reader :units, :unit_reference, :unit, :facet
13
+ def initialize(items:, response:, units:, unit_reference:, unit:, facet:)
14
+ super(items: items, response: response)
15
+ @units = units
16
+ # It looks like the API for referrers_by_domain returns the
17
+ # unit_reference in seconds, not a string, like every other endpoint.
18
+ begin
19
+ @unit_reference = Time.parse(unit_reference) if unit_reference
20
+ rescue TypeError
21
+ @unit_reference = Time.at(unit_reference)
22
+ end
23
+ @unit = unit
24
+ @facet = facet
25
+ end
26
+ end
27
+
28
+ class Referrers < Bitly::API::List
29
+ attr_reader :network
30
+ def initialize(items:, response:, network:)
31
+ super(items: items, response: response)
32
+ @network = network
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Gets the referring networks for the group.
38
+ # [`GET /v4/groups/{group_guid}/referring_networks`](https://dev.bitly.com/v4/#operation/GetGroupMetricsByReferringNetworks)
39
+ #
40
+ # @param client [Bitly::API::Client] An authorized API client
41
+ # @param group_guid [String] The guid of the group
42
+ # @param unit [String] A unit of time. Default is "day" and can be
43
+ # "minute", "hour", "day", "week" or "month"
44
+ # @param units [Integer] An integer representing the time units to query
45
+ # data for. pass -1 to return all units of time. Defaults to -1.
46
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
47
+ # most recent time for which to pull metrics. Will default to current
48
+ # time.
49
+ # @param size [Integer] The number of links to be returned. Defaults to 50
50
+ #
51
+ # @return [Bitly::API::ClickMetric::List]
52
+ def self.list_referring_networks(client:, group_guid:, unit: nil, units: nil, size: nil, unit_reference: nil)
53
+ list_metrics(
54
+ client: client,
55
+ path: "/groups/#{group_guid}/referring_networks",
56
+ unit: unit,
57
+ units: units,
58
+ unit_reference: unit_reference,
59
+ size: size
60
+ )
61
+ end
62
+
63
+ ##
64
+ # Gets the country click metrics for the group.
65
+ # [`GET /v4/groups/{group_guid}/countries`](https://dev.bitly.com/v4/#operation/getGroupMetricsByCountries)
66
+ #
67
+ # @param client [Bitly::API::Client] An authorized API client
68
+ # @param group_guid [String] The guid of the group
69
+ # @param unit [String] A unit of time. Default is "day" and can be
70
+ # "minute", "hour", "day", "week" or "month"
71
+ # @param units [Integer] An integer representing the time units to query
72
+ # data for. pass -1 to return all units of time. Defaults to -1.
73
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
74
+ # most recent time for which to pull metrics. Will default to current
75
+ # time.
76
+ # @param size [Integer] The number of links to be returned. Defaults to 50
77
+ #
78
+ # @return [Bitly::API::ClickMetric::List]
79
+ def self.list_countries_by_group(client:, group_guid:, unit: nil, units: nil, size: nil, unit_reference: nil)
80
+ list_metrics(
81
+ client: client,
82
+ path: "/groups/#{group_guid}/countries",
83
+ unit: unit,
84
+ units: units,
85
+ unit_reference: unit_reference,
86
+ size: size
87
+ )
88
+ end
89
+
90
+ def self.list_referrers(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
91
+ list_metrics(
92
+ client: client,
93
+ path: "/bitlinks/#{bitlink}/referrers",
94
+ unit: unit,
95
+ units: units,
96
+ unit_reference: unit_reference,
97
+ size: size
98
+ )
99
+ end
100
+
101
+ def self.list_countries_by_bitlink(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
102
+ list_metrics(
103
+ client: client,
104
+ path: "/bitlinks/#{bitlink}/countries",
105
+ unit: unit,
106
+ units: units,
107
+ unit_reference: unit_reference,
108
+ size: size
109
+ )
110
+ end
111
+
112
+ def self.list_referring_domains(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
113
+ list_metrics(
114
+ client: client,
115
+ path: "/bitlinks/#{bitlink}/referring_domains",
116
+ unit: unit,
117
+ units: units,
118
+ unit_reference: unit_reference,
119
+ size: size
120
+ )
121
+ end
122
+
123
+ def self.list_referrers_by_domain(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
124
+ response = client.request(
125
+ path: "/bitlinks/#{bitlink}/referrers_by_domains",
126
+ params: {
127
+ "unit" => unit,
128
+ "units" => units,
129
+ "unit_reference" => unit_reference,
130
+ "size" => size
131
+ }
132
+ )
133
+ body = response.body
134
+ referrers = body["referrers_by_domain"].map do |referrer|
135
+ click_metrics = referrer["referrers"].map do |metric|
136
+ ClickMetric.new(data: metric)
137
+ end
138
+ Referrers.new(items: click_metrics, response: response, network: referrer["network"])
139
+ end
140
+ List.new(
141
+ items: referrers,
142
+ response: response,
143
+ unit: body["unit"],
144
+ units: body["units"],
145
+ unit_reference: body["unit_reference"],
146
+ facet: body["facet"]
147
+ )
148
+ end
149
+
150
+ def self.attributes
151
+ [:clicks, :value]
152
+ end
153
+ attr_reader(*attributes)
154
+
155
+ def initialize(data:)
156
+ assign_attributes(data)
157
+ end
158
+
159
+ private
160
+
161
+ def self.list_metrics(client:, path:, unit:, units:, size:, unit_reference:)
162
+ response = client.request(
163
+ path: path,
164
+ params: {
165
+ "unit" => unit,
166
+ "units" => units,
167
+ "unit_reference" => unit_reference,
168
+ "size" => size
169
+ }
170
+ )
171
+ body = response.body
172
+ click_metrics = body["metrics"].map do |metric|
173
+ ClickMetric.new(data: metric)
174
+ end
175
+ List.new(
176
+ items: click_metrics,
177
+ response: response,
178
+ unit: body["unit"],
179
+ units: body["units"],
180
+ unit_reference: body["unit_reference"],
181
+ facet: body["facet"]
182
+ )
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,588 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bitly
4
+ module API
5
+ ##
6
+ # The class that all the API requests are made through. The
7
+ # Bitly::API::Client is authorized with an OAuth token and takes care of
8
+ # setting the correct path, parameters and headers when making requests to
9
+ # the API.
10
+ #
11
+ # This class will be used the most and includes short cut methods to request
12
+ # the popular objects in the system.
13
+ class Client
14
+ USER_AGENT = "Ruby Bitly/#{Bitly::VERSION}"
15
+
16
+ ##
17
+ # Creates a new Bitly::API::Client, authorized with an OAuth token and
18
+ # with an optional HTTP client.
19
+ #
20
+ # @example
21
+ # client = Bitly::API::Client.new(token: oauth_token)
22
+ #
23
+ # @param http [Bitly::HTTP::Client] An HTTP client, you can pass your own
24
+ # if you have a custom client you would like to use. Custom clients
25
+ # must have a request method that takes a Bitly::HTTP::Request object
26
+ # as a single parameter and returns a Bitly::HTTP::Response object (or
27
+ # an object that behaves like one).
28
+ # @param token [String] An OAuth token for a user authorized with the API.
29
+ #
30
+ # @return [Bitly::API::Client]
31
+ def initialize(http: Bitly::HTTP::Client.new, token:)
32
+ @http = http
33
+ @token = token
34
+ end
35
+
36
+ ##
37
+ # Makes a request to the API by building up a Bitly::HTTP::Request and
38
+ # using the HTTP client to make that request and return a
39
+ # Bitly::HTTP::Response.
40
+ #
41
+ # @example
42
+ # client.request("/shorten", method: "POST", params: { long_url => "https://example.com" })
43
+ #
44
+ # @param path [String] The resource path
45
+ # @param method [String] The HTTP method to use to request the path
46
+ # @param params [Hash<String, String>] The parameters to pass to the API
47
+ # @param headers [Hash<String, String>] Custom headers for the request
48
+ #
49
+ # @return [Bitly::HTTP::Response]
50
+ def request(path:, method: 'GET', params: {}, headers: {})
51
+ params = params.select { |k,v| !v.nil? }
52
+ headers = default_headers.merge(headers)
53
+ uri = Bitly::API::BASE_URL.dup
54
+ uri.path += path
55
+ request = Bitly::HTTP::Request.new(uri: uri, method: method, params: params, headers: headers)
56
+ @http.request(request)
57
+ end
58
+
59
+ ##
60
+ # Shortens a long URL.
61
+ # [`POST /v4/shorten`](https://dev.bitly.com/v4/#operation/createBitlink)
62
+ #
63
+ # @example
64
+ # client.shorten(long_url: "http://example.com")
65
+ #
66
+ # @param long_url [String] The URL you want to shorten
67
+ # @param domain [String] The domain you want to shorten the URL with.
68
+ # "bit.ly" by default.
69
+ # @param group_guid [String] The group you want shorten this URL under
70
+ #
71
+ # @return [Bitly::API::Bitlink]
72
+ def shorten(long_url:, domain: nil, group_guid: nil)
73
+ Bitlink.shorten(client: self, long_url: long_url, domain: domain, group_guid: group_guid)
74
+ end
75
+
76
+ ##
77
+ # Creates a Bitlink with more options than #shorten.
78
+ # [`POST /v4/bitlinks`](https://dev.bitly.com/v4/#operation/createFullBitlink)
79
+ #
80
+ # @example
81
+ # bitlink = client.create_bitlink(long_url: "http://example.com", title: "An example")
82
+ #
83
+ # @param long_url [String] A long URL that you want shortened
84
+ # @param domain [String] The bitly domain you want to shorten, API default
85
+ # is "bit.ly"
86
+ # @param group_guid [String] The GUID of the group for which you want to
87
+ # shorten this URL
88
+ # @param title [String] A descriptive title for the link
89
+ # @param tags [Array<String>] Some tags for the Bitlink
90
+ # @param deeplinks [Array<Bitly::API::Bitlink::Deeplink>]
91
+ #
92
+ # @return [Bitly::API::Bitlink]
93
+ def create_bitlink(long_url:, domain: nil, group_guid: nil, title: nil, tags: nil, deeplinks: nil)
94
+ Bitlink.create(client: self, long_url: long_url, domain: domain, group_guid: group_guid, title: title, tags: tags, deeplinks: deeplinks)
95
+ end
96
+
97
+ ##
98
+ # Return information about a bitlink
99
+ # [`GET /v4/bitlink/{bitlink}`](https://dev.bitly.com/v4/#operation/getBitlink)
100
+ #
101
+ # @example
102
+ # bitlink = client.bitlink(bitlink: "bit.ly/example")
103
+ #
104
+ # @param bitlink [String] The bitlink you want information about
105
+ #
106
+ # @return [Bitly::API::Bitlink]
107
+ def bitlink(bitlink:)
108
+ Bitlink.fetch(client: self, bitlink: bitlink)
109
+ end
110
+
111
+ ##
112
+ # Return public information about a bitlink.
113
+ # [`POST /v4/expand`](https://dev.bitly.com/v4/#operation/expandBitlink)
114
+ #
115
+ # @example
116
+ # bitlink = client.expand(bitlink: "bit.ly/example")
117
+ #
118
+ # @param bitlink [String] The bitlink you want information about
119
+ #
120
+ # @return [Bitly::API::Bitlink]
121
+ def expand(bitlink:)
122
+ Bitlink.expand(client: self, bitlink: bitlink)
123
+ end
124
+
125
+ ##
126
+ # Returns a list of Bitlinks sorted by clicks.
127
+ # [`GET /v4/groups/{group_guid}/bitlinks/{sort}`](https://dev.bitly.com/v4/#operation/getSortedBitlinks)
128
+ #
129
+ # The API returns a separate list of the links and the click counts, but
130
+ # this method assigns the number of clicks for each link to the Bitlink
131
+ # object and sorts the resulting list in descending order.
132
+ #
133
+ # Sorted lists are not paginated, so do not have any pagination detail.
134
+ #
135
+ # @example
136
+ # links = client.sorted_list(group_guid: guid)
137
+ #
138
+ # @param group_guid [String] The group for which you want to return links
139
+ # @param sort [String] The data to sort on. Default and only option is
140
+ # "clicks".
141
+ # @param unit [String] A unit of time. Default is "day" and can be
142
+ # "minute", "hour", "day", "week" or "month"
143
+ # @param units [Integer] An integer representing the time units to query
144
+ # data for. pass -1 to return all units of time. Defaults to -1.
145
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
146
+ # most recent time for which to pull metrics. Will default to current
147
+ # time.
148
+ # @param size [Integer] The number of links to be returned. Defaults to 50
149
+ #
150
+ # @returns [Bitly::API::Bitlink::List]
151
+ def sorted_bitlinks(
152
+ group_guid:,
153
+ sort: "clicks",
154
+ unit: nil,
155
+ units: nil,
156
+ unit_reference: nil,
157
+ size: nil
158
+ )
159
+ Bitlink.sorted_list(
160
+ client: self,
161
+ group_guid: group_guid,
162
+ sort: sort,
163
+ unit: unit,
164
+ units: units,
165
+ unit_reference: unit_reference,
166
+ size: size
167
+ )
168
+ end
169
+
170
+ ##
171
+ # Update a Bitlink.
172
+ # [`PATCH /v4/bitlink/{bitlink}`](https://dev.bitly.com/v4/#operation/updateBitlink)
173
+ #
174
+ # The parameters listed below are from the documentation. Some only work
175
+ # if you have a Bitly Pro account.
176
+ #
177
+ # @example
178
+ # client.update_bitlink(bitlink: bitlink, title: "New title")
179
+ #
180
+ # @param archived [Boolean]
181
+ # @param tags [Array<String>]
182
+ # @param created_at [String]
183
+ # @param title [String]
184
+ # @param deeplinks [Array<Bitly::API::Bitlink::Deeplink>]
185
+ # @param created_by [String]
186
+ # @param long_url [String]
187
+ # @param client_id [String]
188
+ # @param custom_bitlinks [Array<String>]
189
+ # @param link [String]
190
+ # @param id [String]
191
+ # @param references [Hash<String, String>]
192
+ #
193
+ # @returns [Bitly::API::Bitlink]
194
+ def update_bitlink(
195
+ bitlink:,
196
+ archived: nil,
197
+ tags: nil,
198
+ created_at: nil,
199
+ title: nil,
200
+ deeplinks: nil,
201
+ created_by: nil,
202
+ long_url: nil,
203
+ client_id: nil,
204
+ custom_bitlinks: nil,
205
+ link: nil,
206
+ id: nil,
207
+ references: nil
208
+ )
209
+ bitlink = Bitlink.new(data: { "id" => bitlink }, client: self)
210
+ bitlink.update(
211
+ archived: archived,
212
+ tags: tags,
213
+ created_at: created_at,
214
+ title: title,
215
+ deeplinks: deeplinks,
216
+ created_by: created_by,
217
+ long_url: long_url,
218
+ client_id: client_id,
219
+ custom_bitlinks: custom_bitlinks,
220
+ link: link,
221
+ id: id,
222
+ references: references
223
+ )
224
+ end
225
+
226
+ ##
227
+ # Get the clicks for a bitlink.
228
+ # [`GET /v4/bitlink/{bitlink}/clicks`](https://dev.bitly.com/v4/#operation/getClicksForBitlink)
229
+ #
230
+ # @param bitlink [String] The Bitlink for which you want the clicks
231
+ # @param sort [String] The data to sort on. Default and only option is
232
+ # "clicks".
233
+ # @param unit [String] A unit of time. Default is "day" and can be
234
+ # "minute", "hour", "day", "week" or "month"
235
+ # @param units [Integer] An integer representing the time units to query
236
+ # data for. pass -1 to return all units of time. Defaults to -1.
237
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
238
+ # most recent time for which to pull metrics. Will default to current
239
+ # time.
240
+ # @param size [Integer] The number of links to be returned. Defaults to 50
241
+ #
242
+ # @return [Bitly::API::Bitlink::LinkClick::List]
243
+ def bitlink_clicks(bitlink:, unit: nil, units: nil, unit_reference: nil, size: nil)
244
+ Bitlink::LinkClick.list(
245
+ client: self,
246
+ bitlink: bitlink,
247
+ unit: unit,
248
+ units: units,
249
+ unit_reference: unit_reference,
250
+ size: size
251
+ )
252
+ end
253
+
254
+ ##
255
+ # Get a list of organizations from the API.
256
+ # [`GET /v4/organizations`](https://dev.bitly.com/v4/#operation/getOrganizations)
257
+ #
258
+ # @example
259
+ # organizations = client.organizations
260
+ #
261
+ # @return [Bitly::API::Organization::List]
262
+ def organizations
263
+ Organization.list(client: self)
264
+ end
265
+
266
+ ##
267
+ # Retrieve an organization from the API.
268
+ # [`GET /v4/organizations/{organization_guid}`](https://dev.bitly.com/v4/#operation/getOrganization)
269
+ #
270
+ # @example
271
+ # organization = client.organization(organization_guid: guid)
272
+ #
273
+ # @param organization_guid [String] An organization guid
274
+ #
275
+ # @return [Bitly::API::Organization]
276
+ def organization(organization_guid:)
277
+ Organization.fetch(client: self, organization_guid: organization_guid)
278
+ end
279
+
280
+ ##
281
+ # Shorten counts by organization
282
+ # [`GET /v4/organizations/{organization_guid}/shorten_counts`](https://dev.bitly.com/v4/#operation/getOrganizationShortenCounts)
283
+ #
284
+ # @example
285
+ # shorten_counts = client.organization_shorten_counts(organization_guid: organization_guid)
286
+ #
287
+ # @param organization_guid [String] The guid of the organization for which
288
+ # you want shorten counts
289
+ #
290
+ # @return [Bitly::API::ShortenCounts]
291
+ def organization_shorten_counts(organization_guid:)
292
+ Bitly::API::ShortenCounts.by_organization(client: self, organization_guid: organization_guid)
293
+ end
294
+
295
+ ##
296
+ # Gets the authorized user from the API.
297
+ # [`GET /v4/user`](https://dev.bitly.com/v4/#operation/getUser)
298
+ #
299
+ # @example
300
+ # user = client.user
301
+ #
302
+ # @return [Bitly::API::User]
303
+ def user
304
+ User.fetch(client: self)
305
+ end
306
+
307
+ ##
308
+ # Allows you to update the authorized user's name or default group guid.
309
+ # [`PATCH /v4/user`](https://dev.bitly.com/v4/#operation/updateUser)]
310
+ #
311
+ # @example
312
+ # client.update_user(name: "New Name", default_group_guid: "aaabbb")
313
+ #
314
+ # @param name [String] A new name
315
+ # @param default_group_guid [String] A new default guid
316
+ #
317
+ # @return [Bitly::API::User]
318
+ def update_user(name: nil, default_group_guid: nil)
319
+ user = Bitly::API::User.new(client: self, data: {})
320
+ user.update(name: name, default_group_guid: default_group_guid)
321
+ end
322
+
323
+ ##
324
+ # Lists groups the authorized user can see.
325
+ # [`GET /v4/groups`](https://dev.bitly.com/v4/#operation/getGroups)
326
+ #
327
+ # @example
328
+ # groups = client.groups
329
+ #
330
+ # @param organization [String] The organization guid of the organization
331
+ # for which you want the available groups.
332
+ #
333
+ # @return [Bitly::API::Group::List]
334
+ def groups(organization_guid: nil)
335
+ Group.list(client: self, organization_guid: organization_guid)
336
+ end
337
+
338
+ ##
339
+ # Fetch a particular group.
340
+ # [`GET /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/getGroup)
341
+ #
342
+ # @example
343
+ # group = client.group(guid)
344
+ #
345
+ # @param guid [String] The guid of the group you want.
346
+ #
347
+ # @return [Bitly::API::Group]
348
+ def group(group_guid:)
349
+ Group.fetch(client: self, group_guid: group_guid)
350
+ end
351
+
352
+ ##
353
+ # Fetch the shorten counts for a group.
354
+ # [`GET /v4/groups/{group_guid}/shorten_counts`](https://dev.bitly.com/v4/#operation/getGroupShortenCounts)
355
+ #
356
+ # @example
357
+ # shorten_counts = client.group_shorten_counts(guid: group_guid)
358
+ #
359
+ # @param guid [String] The guid of the group for which you want the
360
+ # shorten counts.
361
+ #
362
+ # @return [Bitly::API::ShortenCounts]
363
+ def group_shorten_counts(group_guid:)
364
+ Bitly::API::ShortenCounts.by_group(client: self, group_guid: group_guid)
365
+ end
366
+
367
+ ##
368
+ # Fetch a group's preferences.
369
+ # [`GET /v4/groups/{group_guid}/preferences`](https://dev.bitly.com/v4/#operation/getGroupPreferences)
370
+ #
371
+ # @param group_guid [String] The group's guid
372
+ #
373
+ # @return [Bitly::API::Group::Preferences]
374
+ def group_preferences(group_guid:)
375
+ Group::Preferences.fetch(client: self, group_guid: group_guid)
376
+ end
377
+
378
+ ##
379
+ # Update a group's preferences.
380
+ # [`PATCH /v4/groups/{group_guid}/preferences`](https://dev.bitly.com/v4/#operation/updateGroupPreferences)
381
+ #
382
+ # @param group_guid [String] The group's guid
383
+ # @param domain_preference [String] The new domain preference for this
384
+ # group
385
+ #
386
+ # @return [Bitly::API::Group::Preferences]
387
+ def update_group_preferences(group_guid:, domain_preference:)
388
+ group_preferences = Group::Preferences.new(data: { "group_guid" => group_guid }, client: self)
389
+ group_preferences.update(domain_preference: domain_preference)
390
+ end
391
+
392
+ ##
393
+ # Allows you to update a group's name, organization or BSDs.
394
+ # [`PATCH /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/updateGroup)
395
+ #
396
+ # @example
397
+ # client.update_group(group_guid: group_guid, name: "New Name", organization_guid: "aaabbb")
398
+ #
399
+ # @param group_guid [String] The group's guid
400
+ # @param name [String] A new name
401
+ # @param organization_guid [String] A new organization guid
402
+ # @param bsds [Array<String>] An array of branded short domains
403
+ #
404
+ # @return [Bitly::API::Group]
405
+ def update_group(group_guid:, name: nil, organization_guid: nil, bsds: nil)
406
+ group = Group.new(data: { "guid" => group_guid }, client: self)
407
+ group.update(
408
+ name: name,
409
+ organization_guid: organization_guid,
410
+ bsds: bsds
411
+ )
412
+ end
413
+
414
+ ##
415
+ # Retrieve a list of bitlinks by group
416
+ # [`GET /v4/groups/{group_guid}/bitlinks`](https://dev.bitly.com/v4/#operation/getBitlinksByGroup)
417
+ #
418
+ # @example
419
+ # bitlinks = client.group_bitlinks(group_guid: guid)
420
+ #
421
+ # @param client [Bitly::API::Client] An authorized API client
422
+ # @param group_guid [String] The group guid for which you want bitlinks
423
+ # @param size [Integer] The number of Bitlinks to return, max 100
424
+ # @param page [Integer] The page of bitlinks to request
425
+ # @param keyword [String] Custom keyword to filter on history entries
426
+ # @param query [String] A value to search for Bitlinks
427
+ # @param created_before [Integer] Timestamp as an integer unix epoch
428
+ # @param created_after [Integer] Timestamp as an integer unix epoch
429
+ # @param modified_after [Integer] Timestamp as an integer unix epoch
430
+ # @param archived [String] Whether or not to include archived Bitlinks.
431
+ # One of "on", "off" or "both". Defaults to "off".
432
+ # @param deeplinks [String] Filter to only Bitlinks that contain
433
+ # deeplinks. One of "on", "off" or "both". Defaults to "both".
434
+ # @param domain_deeplinks [String] Filter to only Bitlinks that contain
435
+ # deeplinks configured with a custom domain. One of "on", "off" or
436
+ # "both". Defaults to "both".
437
+ # @param campaign_guid [String] Filter to return only links for the given
438
+ # campaign GUID, can be provided
439
+ # @param channel_guid [String] Filter to return only links for the given
440
+ # channel GUID, can be provided, overrides all other parameters
441
+ # @param custom_bitlink [String] Filter to return only custom Bitlinks.
442
+ # One of "on", "off" or "both". Defaults to "both".
443
+ # @param tags [Array<String>] Filter by the given tags.
444
+ # @param encoding_login [Array<String>] Filter by the login of the
445
+ # authenticated user that created the Bitlink.
446
+ #
447
+ # @return [Bitly::API::Bitlink::PaginatedList]
448
+ def group_bitlinks(
449
+ group_guid:,
450
+ size: nil,
451
+ page: nil,
452
+ keyword: nil,
453
+ query: nil,
454
+ created_before: nil,
455
+ created_after: nil,
456
+ modified_after: nil,
457
+ archived: nil,
458
+ deeplinks: nil,
459
+ domain_deeplinks: nil,
460
+ campaign_guid: nil,
461
+ channel_guid: nil,
462
+ custom_bitlink: nil,
463
+ tags: nil,
464
+ encoding_login: nil
465
+ )
466
+ Bitlink.list(
467
+ client: self,
468
+ group_guid: group_guid,
469
+ size: size,
470
+ page: page,
471
+ keyword: keyword,
472
+ query: query,
473
+ created_before: created_before,
474
+ created_after: created_after,
475
+ modified_after: modified_after,
476
+ archived: archived,
477
+ deeplinks: deeplinks,
478
+ domain_deeplinks: domain_deeplinks,
479
+ campaign_guid: campaign_guid,
480
+ channel_guid: channel_guid,
481
+ custom_bitlink: custom_bitlink,
482
+ tags: tags,
483
+ encoding_login: encoding_login
484
+ )
485
+ end
486
+
487
+ ##
488
+ # Deletes a group.
489
+ # [`DELETE /v4/groups/{group_guid}`](https://dev.bitly.com/v4/#operation/deleteGroup)
490
+ #
491
+ # @example
492
+ # client.delete_group(group_guid: group_guid)
493
+ #
494
+ # @return [Nil]
495
+ def delete_group(group_guid:)
496
+ group = Group.new(data: { "guid" => group_guid }, client: self)
497
+ group.delete
498
+ end
499
+
500
+ ##
501
+ # Gets the referring networks for the group.
502
+ # [`GET /v4/groups/{group_guid}/referring_networks`](https://dev.bitly.com/v4/#operation/GetGroupMetricsByReferringNetworks)
503
+ #
504
+ # @param group_guid [String] The guid of the group
505
+ # @param unit [String] A unit of time. Default is "day" and can be
506
+ # "minute", "hour", "day", "week" or "month"
507
+ # @param units [Integer] An integer representing the time units to query
508
+ # data for. pass -1 to return all units of time. Defaults to -1.
509
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
510
+ # most recent time for which to pull metrics. Will default to current
511
+ # time.
512
+ # @param size [Integer] The number of links to be returned. Defaults to 50
513
+ #
514
+ # @return [Bitly::API::ClickMetric::List]
515
+ def group_referring_networks(group_guid:, unit: nil, units: nil, size: nil, unit_reference: nil)
516
+ ClickMetric.list_referring_networks(
517
+ client: self,
518
+ group_guid: group_guid,
519
+ unit: unit,
520
+ units: units,
521
+ unit_reference: unit_reference,
522
+ size: size
523
+ )
524
+ end
525
+
526
+ ##
527
+ # Gets the country click metrics for the group.
528
+ # [`GET /v4/groups/{group_guid}/countries`](https://dev.bitly.com/v4/#operation/getGroupMetricsByCountries)
529
+ #
530
+ # @param group_guid [String] The guid of the group
531
+ # @param unit [String] A unit of time. Default is "day" and can be
532
+ # "minute", "hour", "day", "week" or "month"
533
+ # @param units [Integer] An integer representing the time units to query
534
+ # data for. pass -1 to return all units of time. Defaults to -1.
535
+ # @param unit_reference [String] An ISO-8601 timestamp, indicating the
536
+ # most recent time for which to pull metrics. Will default to current
537
+ # time.
538
+ # @param size [Integer] The number of links to be returned. Defaults to 50
539
+ #
540
+ # @return [Bitly::API::ClickMetric::List]
541
+ def group_countries(group_guid:, unit: nil, units: nil, size: nil, unit_reference: nil)
542
+ ClickMetric.list_countries_by_group(
543
+ client: self,
544
+ group_guid: group_guid,
545
+ unit: unit,
546
+ units: units,
547
+ unit_reference: unit_reference,
548
+ size: size
549
+ )
550
+ end
551
+
552
+ ##
553
+ # Fetch Branded Short Domains (BSDs).
554
+ # [`GET /v4/bsds`](https://dev.bitly.com/v4/#operation/getBSDs)
555
+ #
556
+ # @example
557
+ # bsds = client.bsds
558
+ #
559
+ # @return [Array<String>]
560
+ def bsds
561
+ BSD.list(client: self)
562
+ end
563
+
564
+ ##
565
+ # Fetch OAuth application by client ID
566
+ # [`GET /v4/apps/{client_id}`)](https://dev.bitly.com/v4/#operation/getOAuthApp)
567
+ #
568
+ # @example
569
+ # app = client.oauth_app(client_id: "client_id")
570
+ #
571
+ # @return Bitly::API::OAuthApp
572
+ def oauth_app(client_id:)
573
+ OAuthApp.fetch(client: self, client_id: client_id)
574
+ end
575
+
576
+ private
577
+
578
+ def default_headers
579
+ {
580
+ "User-Agent" => USER_AGENT,
581
+ "Authorization" => "Bearer #{@token}",
582
+ "Accept" => "application/json",
583
+ "Content-Type" => "application/json"
584
+ }
585
+ end
586
+ end
587
+ end
588
+ end