Skip to content

Instantly share code, notes, and snippets.

@cilim
Created October 15, 2019 12:32
Show Gist options
  • Save cilim/feb5d415bbd381ef7607962722238708 to your computer and use it in GitHub Desktop.
Save cilim/feb5d415bbd381ef7607962722238708 to your computer and use it in GitHub Desktop.
class BranchLinkAdapter
BRANCH_IO_API_URL = 'https://api.branch.io/v1/url'.freeze
def initialize(token, desktop_url)
@token = token
@key = Rails.application.secrets.branchio_key
@desktop_url = desktop_url
end
def fetch
JSON.parse(fetch_url.body)['url'] if fetch_url.code == 200
end
private
def headers
{ 'content_type' => 'application/json', 'accept' => 'application/json' }
end
def payload
# set type: 1, for one time links
{
branch_key: @key,
data: {
token: @token,
'$desktop_url' => @desktop_url
}
}.to_json
end
def fetch_url
@fetch_url ||= RestClient.post(BRANCH_IO_API_URL, payload, headers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment