Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Add delete API call #6

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end
_The only difference between `#links` and `#links!` is that the latter version will
raise a new `BranchIO::ErrorApiCallFailed` exception in case of an error._

### `Client#link_info` and `Client#link_info!`: Registers a [new deep linking URL](https://github.com/BranchMetrics/branch-deep-linking-public-api#creating-a-deep-linking-url)
### `Client#link_info` and `Client#link_info!`: Returns information about a [deep linking URL](https://github.com/BranchMetrics/branch-deep-linking-public-api#creating-a-deep-linking-url)

**BEWARE: this method requires the BRANCH_SECRET to be defined**

Expand All @@ -159,7 +159,7 @@ end
**The only difference between `#link_info` and `#link_info!` is that the latter version will
raise a new `BranchIO::ErrorApiCallFailed` exception in case of an error.**

### `Client#update_link` and `Client#update_link!`: Registers a [new deep linking URL](https://github.com/BranchMetrics/branch-deep-linking-public-api#creating-a-deep-linking-url)
### `Client#update_link` and `Client#update_link!`: Updates a [deep linking URL](https://github.com/BranchMetrics/branch-deep-linking-public-api#creating-a-deep-linking-url)

**BEWARE: this method requires the BRANCH_SECRET to be defined**

Expand All @@ -181,6 +181,27 @@ end
**The only difference between `#update_link` and `#update_link!` is that the latter version will
raise a new `BranchIO::ErrorApiCallFailed` exception in case of an error.**

### `Client#delete_link` and `Client#delete_link!`: Deletes a [deep linking URL](https://github.com/BranchMetrics/branch-deep-linking-public-api#creating-a-deep-linking-url)

**BEWARE: this method requires the BRANCH_SECRET to be defined**

This method deletes an existing link and returns the URL and deletion confirmation.

```ruby
# Call the service
res = client.delete_link("https://...")

# Inspect the server response
if res.success?
puts "Successfully deleted link: #{res.link_deleted.to_json['url']}"
else
puts "Error updating link info: #{res.error}"
end
```

**The only difference between `#delete_link` and `#delete_link!` is that the latter version will
raise a new `BranchIO::ErrorApiCallFailed` exception in case of an error.**

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
5 changes: 5 additions & 0 deletions lib/branch_io/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def put(url, data = {})
self.class.put(url, body: body, headers: default_headers)
end

def delete(url, data = {})
body = data.to_json
self.class.delete(url, body: body, headers: default_headers)
end

private

def ensure_branch_secret_defined!
Expand Down
30 changes: 30 additions & 0 deletions lib/branch_io/client/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,36 @@ def link_info(url)
ErrorResponse.new(raw_response)
end
end

def delete_link!(url)
res = delete_link(url)
res.validate!
res
end

def delete_link(url)
ensure_branch_secret_defined!

# Build the request URL
encoded_url = URI.encode_www_form_component(url)
delete_url = "#{LINK_PATH}?url=#{encoded_url}"

# Build the request body
delete_json = {
branch_key: self.branch_key,
branch_secret: self.branch_secret
}

# Call branch.io public API
raw_response = self.delete(delete_url, delete_json)

# Wrap the result in a Response
if raw_response.success?
LinkDeletedResponse.new(raw_response)
else
ErrorResponse.new(raw_response)
end
end
end
end
end
10 changes: 10 additions & 0 deletions lib/branch_io/client/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,15 @@ def link_properties
@link_properties ||= BranchIO::LinkProperties.new(json)
end
end

class LinkDeletedResponse < Response
def success?
json["deleted"] == true
end

def url
json["url"]
end
end
end
end
125 changes: 125 additions & 0 deletions spec/cassettes/branch-io.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,36 @@
expect(props.channel).to eq("retest")
end
end

describe "#delete_link!" do
let!(:url) { client.link(channel: "code", feature: "test", tags: ["test", "test-delete"]).url }

it "calls #delete_link" do
res = double(validate!: true)
expect(client).to receive(:delete_link).and_return(res)
client.delete_link!(url)
end
end

describe "#delete_link" do
let!(:url) { client.link(channel: "code", feature: "test", tags: ["test", "test-delete"]).url }

it "succeeds" do
expect(
client.delete_link(url)
).to be_success
end

it "deletes the link from the server" do
pending unless ENV["BRANCH_KEY"]

# Delete the link
res = client.delete_link(url)

# It should be successful
expect(res).to be_kind_of(BranchIO::Client::LinkDeletedResponse)
expect(res.url).not_to be_nil
end
end
end # /Client
end