Class: Bitly::API::Bitlink
- Inherits:
-
Object
- Object
- Bitly::API::Bitlink
- Includes:
- Base
- Defined in:
- lib/bitly/api/bitlink.rb,
lib/bitly/api/bitlink/utils.rb,
lib/bitly/api/bitlink/deeplink.rb,
lib/bitly/api/bitlink/link_click.rb,
lib/bitly/api/bitlink/clicks_summary.rb,
lib/bitly/api/bitlink/paginated_list.rb
Overview
A Bitlink represents a shortened link within Bitly.
Defined Under Namespace
Modules: Utils Classes: ClicksSummary, Deeplink, LinkClick, List, PaginatedList
Instance Attribute Summary collapse
-
#clicks ⇒ Object
readonly
Returns the value of attribute clicks.
-
#deeplinks ⇒ Object
readonly
Returns the value of attribute deeplinks.
Attributes included from Base
Class Method Summary collapse
- .attributes ⇒ Object
-
.create(client:, long_url:, domain: nil, group_guid: nil, title: nil, tags: nil, deeplinks: nil) ⇒ Bitly::API::Bitlink
Creates a new Bitlink from a long URL.
-
.expand(client:, bitlink:) ⇒ Bitly::API::Bitlink
Return public information about a bitlink [‘POST /v4/expand`](dev.bitly.com/api-reference/#expandBitlink).
-
.fetch(client:, bitlink:) ⇒ Bitly::API::Bitlink
Return information about a bitlink [‘GET /v4/bitlinks/bitlink`](dev.bitly.com/api-reference/#getBitlink).
-
.list(client:, group_guid:, size: nil, page: nil, keyword: nil, query: nil, created_before: nil, created_after: nil, modified_after: nil, archived: nil, deeplinks: nil, domain_deeplinks: nil, campaign_guid: nil, channel_guid: nil, custom_bitlink: nil, tags: nil, encoding_login: nil) ⇒ Bitly::API::Bitlink::PaginatedList
Retrieve a list of bitlinks by group [‘GET /v4/groups/group_guid/bitlinks`](dev.bitly.com/api-reference/#getBitlinksByGroup).
-
.shorten(client:, long_url:, domain: nil, group_guid: nil) ⇒ Bitly::API::Bitlink
Shortens a long url.
-
.sorted_list(client:, group_guid:, sort: "clicks", unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Object
Returns a list of Bitlinks sorted by clicks.
- .time_attributes ⇒ Object
Instance Method Summary collapse
-
#click_metrics_by_country(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::ClickMetric::List
Get metrics for a Bitlink by country [‘GET /v4/bitlinks/bitlink/countries`](dev.bitly.com/api-reference/#getMetricsForBitlinkByCountries).
-
#clicks_summary(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::Bitlink::ClicksSummary
[‘GET /v4/bitlinks/bitlink/clicks/summary`](dev.bitly.com/api-reference/#getClicksSummaryForBitlink).
-
#initialize(data:, client:, response: nil, clicks: nil) ⇒ Bitlink
constructor
A new instance of Bitlink.
-
#link_clicks(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::Bitlink::LinkClick::List
Get the clicks for the bitlink.
-
#update(archived: nil, tags: nil, created_at: nil, title: nil, deeplinks: nil, created_by: nil, long_url: nil, client_id: nil, custom_bitlinks: nil, link: nil, id: nil, references: nil) ⇒ Object
Update the Bitlink.
Methods included from Base
Constructor Details
#initialize(data:, client:, response: nil, clicks: nil) ⇒ Bitlink
Returns a new instance of Bitlink.
245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/bitly/api/bitlink.rb', line 245 def initialize(data:, client:, response: nil, clicks: nil) assign_attributes(data) @id = Utils.normalise_bitlink(bitlink: @id) if data["deeplinks"] @deeplinks = data["deeplinks"].map { |data| Deeplink.new(data: data) } else @deeplinks = [] end @clicks = clicks @client = client @response = response end |
Instance Attribute Details
#clicks ⇒ Object (readonly)
Returns the value of attribute clicks.
243 244 245 |
# File 'lib/bitly/api/bitlink.rb', line 243 def clicks @clicks end |
#deeplinks ⇒ Object (readonly)
Returns the value of attribute deeplinks.
243 244 245 |
# File 'lib/bitly/api/bitlink.rb', line 243 def deeplinks @deeplinks end |
Class Method Details
.attributes ⇒ Object
236 237 238 |
# File 'lib/bitly/api/bitlink.rb', line 236 def self.attributes [:archived, :tags, :title, :created_by, :long_url, :client_id, :custom_bitlinks, :link, :id] end |
.create(client:, long_url:, domain: nil, group_guid: nil, title: nil, tags: nil, deeplinks: nil) ⇒ Bitly::API::Bitlink
Creates a new Bitlink from a long URL. Similar to #shorten but takes more parameters. [‘POST /v4/bitlinks`](dev.bitly.com/api-reference/#createFullBitlink)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bitly/api/bitlink.rb', line 66 def self.create(client:, long_url:, domain: nil, group_guid: nil, title: nil, tags: nil, deeplinks: nil) response = client.request( path: "/bitlinks", method: "POST", params: { "long_url" => long_url, "domain" => domain, "group_guid" => group_guid, "title" => title, "tags" => , "deeplinks" => deeplinks } ) new(data: response.body, client: client, response: response) end |
.expand(client:, bitlink:) ⇒ Bitly::API::Bitlink
Return public information about a bitlink [‘POST /v4/expand`](dev.bitly.com/api-reference/#expandBitlink)
110 111 112 113 114 |
# File 'lib/bitly/api/bitlink.rb', line 110 def self.(client:, bitlink:) bitlink = Utils.normalise_bitlink(bitlink: bitlink) response = client.request(path: "/expand", method: "POST", params: { "bitlink_id" => bitlink }) new(data: response.body, client: client, response: response) end |
.fetch(client:, bitlink:) ⇒ Bitly::API::Bitlink
Return information about a bitlink [‘GET /v4/bitlinks/bitlink`](dev.bitly.com/api-reference/#getBitlink)
93 94 95 96 97 |
# File 'lib/bitly/api/bitlink.rb', line 93 def self.fetch(client:, bitlink:) bitlink = Utils.normalise_bitlink(bitlink: bitlink) response = client.request(path: "/bitlinks/#{bitlink}") new(data: response.body, client: client, response: response) end |
.list(client:, group_guid:, size: nil, page: nil, keyword: nil, query: nil, created_before: nil, created_after: nil, modified_after: nil, archived: nil, deeplinks: nil, domain_deeplinks: nil, campaign_guid: nil, channel_guid: nil, custom_bitlink: nil, tags: nil, encoding_login: nil) ⇒ Bitly::API::Bitlink::PaginatedList
Retrieve a list of bitlinks by group [‘GET /v4/groups/group_guid/bitlinks`](dev.bitly.com/api-reference/#getBitlinksByGroup)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/bitly/api/bitlink.rb', line 150 def self.list( client:, group_guid:, size: nil, page: nil, keyword: nil, query: nil, created_before: nil, created_after: nil, modified_after: nil, archived: nil, deeplinks: nil, domain_deeplinks: nil, campaign_guid: nil, channel_guid: nil, custom_bitlink: nil, tags: nil, encoding_login: nil ) params = { "size" => size, "page" => page, "keyword" => keyword, "query" => query, "created_before" => created_before, "created_after" => created_after, "modified_after" => modified_after, "archived" => archived, "deeplinks" => deeplinks, "domain_deeplinks" => domain_deeplinks, "campaign_guid" => campaign_guid, "channel_guid" => channel_guid, "custom_bitlink" => custom_bitlink, "tags" => , "encoding_login" => encoding_login } response = client.request(path: "/groups/#{group_guid}/bitlinks", params: params) bitlinks = response.body["links"].map do |link| new(data: link, client: client) end PaginatedList.new(items: bitlinks, response: response, client: client) end |
.shorten(client:, long_url:, domain: nil, group_guid: nil) ⇒ Bitly::API::Bitlink
Shortens a long url. [‘POST /v4/shorten`](dev.bitly.com/api-reference/#createBitlink)
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bitly/api/bitlink.rb', line 35 def self.shorten(client:, long_url:, domain: nil, group_guid: nil) response = client.request( path: "/shorten", method: "POST", params: { "long_url" => long_url, "domain" => domain, "group_guid" => group_guid }) new(data: response.body, client: client, response: response) end |
.sorted_list(client:, group_guid:, sort: "clicks", unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Object
Returns a list of Bitlinks sorted by clicks. [‘GET /v4/groups/group_guid/bitlinks/sort`](dev.bitly.com/api-reference/#getSortedBitlinks)
The API returns a separate list of the links and the click counts, but this method assigns the number of clicks for each link to the Bitlink object and sorts the resulting list in descending order.
Sorted lists are not paginated, so do not have any pagination detail.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/bitly/api/bitlink.rb', line 220 def self.sorted_list(client:, group_guid:, sort: "clicks", unit: nil, units: nil, unit_reference: nil, size: nil) params = { "unit" => unit, "units" => units, "unit_reference" => unit_reference, "size" => size } response = client.request(path: "/groups/#{group_guid}/bitlinks/#{sort}", params: params) link_clicks = response.body["sorted_links"] bitlinks = response.body["links"].map do |link| clicks = link_clicks.find { |c| c["id"] == link["id"] }["clicks"] new(data: link, client: client, clicks: clicks) end.sort { |a, b| b.clicks <=> a.clicks } List.new(items: bitlinks, response: response) end |
.time_attributes ⇒ Object
239 240 241 |
# File 'lib/bitly/api/bitlink.rb', line 239 def self.time_attributes [:created_at] end |
Instance Method Details
#click_metrics_by_country(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::ClickMetric::List
Get metrics for a Bitlink by country [‘GET /v4/bitlinks/bitlink/countries`](dev.bitly.com/api-reference/#getMetricsForBitlinkByCountries)
359 360 361 |
# File 'lib/bitly/api/bitlink.rb', line 359 def click_metrics_by_country(unit: nil, units: nil, unit_reference: nil, size: nil) ClickMetric.list_countries_by_bitlink(client: @client, bitlink: id, unit: unit, units: units, unit_reference: unit_reference, size: size) end |
#clicks_summary(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::Bitlink::ClicksSummary
[‘GET /v4/bitlinks/bitlink/clicks/summary`](dev.bitly.com/api-reference/#getClicksSummaryForBitlink)
321 322 323 |
# File 'lib/bitly/api/bitlink.rb', line 321 def clicks_summary(unit: nil, units: nil, unit_reference: nil, size: nil) ClicksSummary.fetch(client: @client, bitlink: id, unit: unit, units: units, unit_reference: unit_reference, size: size) end |
#link_clicks(unit: nil, units: nil, unit_reference: nil, size: nil) ⇒ Bitly::API::Bitlink::LinkClick::List
Get the clicks for the bitlink. [‘GET /v4/bitlinks/bitlink/clicks`](dev.bitly.com/api-reference/#getClicksForBitlink)
341 342 343 |
# File 'lib/bitly/api/bitlink.rb', line 341 def link_clicks(unit: nil, units: nil, unit_reference: nil, size: nil) LinkClick.list(client: @client, bitlink: id, unit: unit, units: units, unit_reference: unit_reference, size: size) end |
#update(archived: nil, tags: nil, created_at: nil, title: nil, deeplinks: nil, created_by: nil, long_url: nil, client_id: nil, custom_bitlinks: nil, link: nil, id: nil, references: nil) ⇒ Object
Update the Bitlink. [‘PATCH /v4/bitlinks/bitlink`](dev.bitly.com/api-reference/#getClicksSummaryForBitlink)
The parameters listed below are from the documentation. Some only work if you have a Bitly Pro account.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/bitly/api/bitlink.rb', line 282 def update( archived: nil, tags: nil, created_at: nil, title: nil, deeplinks: nil, created_by: nil, long_url: nil, client_id: nil, custom_bitlinks: nil, link: nil, id: nil, references: nil ) @response = @client.request( path: "/bitlinks/#{@id}", method: "PATCH", params: { "archived" => archived, "tags" => , "created_at" => created_at, "title" => title, "deeplinks" => deeplinks, "created_by" => created_by, "long_url" =>long_url , "client_id" => client_id, "custom_bitlinks" => custom_bitlinks, "link" => link, "id" => id, "references" => references } ) assign_attributes(@response.body) self end |