class Sparkpost::SuppressionService < ApplicationService
  def initialize(action, email)
    @action = action
    @email = email
    @url = 'https://api.sparkpost.com/api/v1/suppression-list'
    @header = {
      Accept: :json,
      'Context-Type' => :json,
      Authorization: Rails.application.secrets.sparkpost[:api_key]
    }
  end

  def call
    case @action
    when :remove
      RestClient.delete("#{@url}/#{@email}", @header)
    when :get
      JSON.parse(RestClient.get("#{@url}/#{@email}", @header))
    end
  rescue RestClient::NotFound
    false
  end
end
