Skip to content

Commit

Permalink
Strips protocol from bitlink if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Oct 18, 2022
1 parent 3087248 commit 04189b9
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/bitly/api/bitlink.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require_relative "./base"
require_relative "./list"
require_relative "./bitlink/utils"

module Bitly
module API
Expand Down Expand Up @@ -90,6 +91,7 @@ def self.create(client:, long_url:, domain: nil, group_guid: nil, title: nil, ta
#
# @return [Bitly::API::Bitlink]
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
Expand All @@ -106,6 +108,7 @@ def self.fetch(client:, bitlink:)
#
# @return [Bitly::API::Bitlink]
def self.expand(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
Expand Down Expand Up @@ -241,6 +244,7 @@ def self.time_attributes

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
Expand Down
2 changes: 2 additions & 0 deletions lib/bitly/api/bitlink/clicks_summary.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative "../base"
require_relative "./utils"

module Bitly
module API
Expand All @@ -8,6 +9,7 @@ class ClicksSummary
include Base

def self.fetch(client:, bitlink:, unit: nil, units: nil, unit_reference: nil, size: nil)
bitlink = Utils.normalise_bitlink(bitlink: bitlink)
response = client.request(
path: "/bitlinks/#{bitlink}/clicks/summary",
params: {
Expand Down
4 changes: 3 additions & 1 deletion lib/bitly/api/bitlink/link_click.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true
require "time"
require_relative "../base"
require_relative '../list'
require_relative "../list"
require_relative "./utils"

module Bitly
module API
Expand Down Expand Up @@ -38,6 +39,7 @@ def initialize(items:, response:, units:, unit_reference:, unit:)
#
# @return [Bitly::API::Bitlink::LinkClick::List]
def self.list(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
bitlink = Utils.normalise_bitlink(bitlink: bitlink)
response = client.request(
path: "/bitlinks/#{bitlink}/clicks",
params: {
Expand Down
13 changes: 13 additions & 0 deletions lib/bitly/api/bitlink/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Bitly
module API
class Bitlink
module Utils
def self.normalise_bitlink(bitlink:)
bitlink.gsub(/^https?:\/\//, "")
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/bitly/api/click_metric.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true
require "time"
require_relative "./base"
require_relative './list'
require_relative "./list"
require_relative "./bitlink/utils"

module Bitly
module API
Expand Down Expand Up @@ -88,6 +89,7 @@ def self.list_countries_by_group(client:, group_guid:, unit: nil, units: nil, si
end

def self.list_referrers(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
bitlink = Bitlink::Utils.normalise_bitlink(bitlink: bitlink)
list_metrics(
client: client,
path: "/bitlinks/#{bitlink}/referrers",
Expand All @@ -99,6 +101,7 @@ def self.list_referrers(client:, bitlink:, unit: nil, units: nil, size: nil, uni
end

def self.list_countries_by_bitlink(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
bitlink = Bitlink::Utils.normalise_bitlink(bitlink: bitlink)
list_metrics(
client: client,
path: "/bitlinks/#{bitlink}/countries",
Expand All @@ -110,6 +113,7 @@ def self.list_countries_by_bitlink(client:, bitlink:, unit: nil, units: nil, siz
end

def self.list_referring_domains(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
bitlink = Bitlink::Utils.normalise_bitlink(bitlink: bitlink)
list_metrics(
client: client,
path: "/bitlinks/#{bitlink}/referring_domains",
Expand All @@ -121,6 +125,7 @@ def self.list_referring_domains(client:, bitlink:, unit: nil, units: nil, size:
end

def self.list_referrers_by_domain(client:, bitlink:, unit: nil, units: nil, size: nil, unit_reference: nil)
bitlink = Bitlink::Utils.normalise_bitlink(bitlink: bitlink)
response = client.request(
path: "/bitlinks/#{bitlink}/referrers_by_domains",
params: {
Expand Down
5 changes: 3 additions & 2 deletions spec/bitly/api/bitlink/clicks_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
}

it "requests the summary from the API" do
bitlink = "bit.ly/abc123"
response = Bitly::HTTP::Response.new(
status: "200",
body: clicks_summary_data.to_json,
headers: {}
)
expect(client).to receive(:request)
.with(
path: "/bitlinks/bit.ly/abc123/clicks/summary",
path: "/bitlinks/#{bitlink}/clicks/summary",
params: {
"unit" => nil,
"units" => nil,
Expand All @@ -28,7 +29,7 @@
}
)
.and_return(response)
clicks_summary = Bitly::API::Bitlink::ClicksSummary.fetch(client: client, bitlink: "bit.ly/abc123")
clicks_summary = Bitly::API::Bitlink::ClicksSummary.fetch(client: client, bitlink: "https://#{bitlink}")
expect(clicks_summary.total_clicks).to eq(100)
end
end
2 changes: 1 addition & 1 deletion spec/bitly/api/bitlink/link_click_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
})
.and_return(response)

link_clicks = Bitly::API::Bitlink::LinkClick.list(client: client, bitlink: bitlink_id)
link_clicks = Bitly::API::Bitlink::LinkClick.list(client: client, bitlink: "https://#{bitlink_id}")
expect(link_clicks.unit).to eq(links_list_data["unit"])
expect(link_clicks.units).to eq(links_list_data["units"])
expect(link_clicks.unit_reference).to eq(Time.parse(links_list_data["unit_reference"]))
Expand Down
17 changes: 17 additions & 0 deletions spec/bitly/api/bitlink/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe Bitly::API::Bitlink::Utils do
describe "normalise_bitlink" do
it "removes a leading https protocol from a bitlink" do
expect(Bitly::API::Bitlink::Utils.normalise_bitlink(bitlink: "https://bit.ly/short")).to eq("bit.ly/short")
end

it "removes a leading http protocol from a bitlink" do
expect(Bitly::API::Bitlink::Utils.normalise_bitlink(bitlink: "http://bit.ly/short")).to eq("bit.ly/short")
end

it "returns the same string if there is no leading protocol" do
expect(Bitly::API::Bitlink::Utils.normalise_bitlink(bitlink: "ahttp://bit.ly/short")).to eq("ahttp://bit.ly/short")
end
end
end
40 changes: 40 additions & 0 deletions spec/bitly/api/bitlink_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@
expect(bitlink.long_url).to eq("https://example.com/")
end

it "can fetch a bitlink with protocol" do
response = Bitly::HTTP::Response.new(
status: "200",
body: bitlink_data.to_json,
headers: {}
)
expect(client).to receive(:request)
.with(path: "/bitlinks/#{bitlink_data["id"]}")
.and_return(response)
bitlink = Bitly::API::Bitlink.fetch(client: client, bitlink: "https://bit.ly/2Qj2niP")
expect(bitlink.long_url).to eq("https://example.com/")
end

it "can expand a bitlink to public information" do
response = Bitly::HTTP::Response.new(
status: "200",
Expand All @@ -120,6 +133,33 @@
expect(bitlink.created_at).to eq(Time.parse(public_bitlink_data["created_at"]))
end

it "can expand a bitlink with protocol to public information" do
response = Bitly::HTTP::Response.new(
status: "200",
body: public_bitlink_data.to_json,
headers: {}
)
expect(client).to receive(:request)
.with(path: "/expand", method: "POST", params: { "bitlink_id" => "bit.ly/2Qj2niP" })
.and_return(response)
bitlink = Bitly::API::Bitlink.expand(client: client, bitlink: "https://bit.ly/2Qj2niP")
expect(bitlink.long_url).to eq("https://example.com/")
expect(bitlink.id).to eq("bit.ly/2Qj2niP")
expect(bitlink.link).to eq("http://bit.ly/2Qj2niP")
expect(bitlink.created_at).to eq(Time.parse(public_bitlink_data["created_at"]))
end

describe "creating" do
it "normalises the id under initialisation" do
bitlink_id_without_protocol = bitlink_data["id"]
bitlink = Bitly::API::Bitlink.new(client: client, data: bitlink_data)
expect(bitlink.id).to eq(bitlink_id_without_protocol)
bitlink_data["id"] = "https://#{bitlink_data["id"]}"
bitlink = Bitly::API::Bitlink.new(client: client, data: bitlink_data)
expect(bitlink.id).to eq(bitlink_id_without_protocol)
end
end

describe "with a bitlink" do
let(:bitlink) { Bitly::API::Bitlink.new(client: client, data: bitlink_data) }

Expand Down
8 changes: 4 additions & 4 deletions spec/bitly/api/click_metric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
})
.and_return(response)

click_metrics = Bitly::API::ClickMetric.list_referrers(client: client, bitlink: bitlink)
click_metrics = Bitly::API::ClickMetric.list_referrers(client: client, bitlink: "https://#{bitlink}")
expect(click_metrics.unit).to eq(click_metric_bitlink_referrers['unit'])
expect(click_metrics.units).to eq(click_metric_bitlink_referrers['units'])
expect(click_metrics.unit_reference).to eq(Time.parse(click_metric_bitlink_referrers['unit_reference']))
Expand All @@ -168,7 +168,7 @@
})
.and_return(response)

click_metrics = Bitly::API::ClickMetric.list_countries_by_bitlink(client: client, bitlink: bitlink)
click_metrics = Bitly::API::ClickMetric.list_countries_by_bitlink(client: client, bitlink: "https://#{bitlink}")
expect(click_metrics.unit).to eq(click_metric_data_countries['unit'])
expect(click_metrics.units).to eq(click_metric_data_countries['units'])
expect(click_metrics.unit_reference).to eq(Time.parse(click_metric_data_countries['unit_reference']))
Expand All @@ -193,7 +193,7 @@
})
.and_return(response)

click_metrics = Bitly::API::ClickMetric.list_referring_domains(client: client, bitlink: bitlink)
click_metrics = Bitly::API::ClickMetric.list_referring_domains(client: client, bitlink: "https://#{bitlink}")
expect(click_metrics.unit).to eq(click_metric_bitlink_referring_domains['unit'])
expect(click_metrics.units).to eq(click_metric_bitlink_referring_domains['units'])
expect(click_metrics.unit_reference).to eq(Time.parse(click_metric_bitlink_referring_domains['unit_reference']))
Expand All @@ -218,7 +218,7 @@
})
.and_return(response)

click_metrics = Bitly::API::ClickMetric.list_referrers_by_domain(client: client, bitlink: bitlink)
click_metrics = Bitly::API::ClickMetric.list_referrers_by_domain(client: client, bitlink: "https://#{bitlink}")
expect(click_metrics.unit).to eq(click_metric_bitlink_referrers_by_domain['unit'])
expect(click_metrics.units).to eq(click_metric_bitlink_referrers_by_domain['units'])
expect(click_metrics.unit_reference).to eq(Time.at(click_metric_bitlink_referrers_by_domain['unit_reference']))
Expand Down
2 changes: 1 addition & 1 deletion spec/bitly/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
let(:client) { Bitly::API::Client.new(token: token) }

describe "Bitlink" do
let(:id) { "https://bit.ly/short" }
let(:id) { "bit.ly/short" }

it "shortens a URL" do
long_url = "https://bitly.com"
Expand Down

0 comments on commit 04189b9

Please sign in to comment.