# frozen_string_literal: true module NeetoCommonsBackend module Services class CustomDomain attr_reader :domain_name, :default_url def initialize(domain_name) @domain_name = domain_name @default_url = "https://api.heroku.com/apps/#{Rails.application.secrets.heroku[:app_name]}/domains" end def create params = { hostname: domain_name, sni_endpoint: Rails.application.secrets.heroku[:certificate_name] } uri = URI(default_url) req = Net::HTTP::Post.new(uri) make_api_request(req, params, uri) end def delete uri = URI("#{default_url}/#{domain_name}") req = Net::HTTP::Delete.new(uri) make_api_request(req, nil, uri) end private def make_api_request(req, params, uri) response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| req.body = JSON[params] if params req["Content-Type"] = "application/json" req["Accept"] = "application/vnd.heroku+json; version=3" req["Authorization"] = "Bearer #{Rails.application.secrets.heroku[:auth_token]}" http.request(req) end JSON.parse(response.body) end end end end