Elixir foundationSort Order

Elixir: Implementing Public API for Retrieving HTML - [025]

We need to make our process of retrieving the html of a page simpler. In this post we will abstract away the inner-workings of our rendering engine and expose a simple api for the rendering process.

Simple Public API

We will use the Botiful.Render module as our public api.

defmodule Botiful.Render do  alias Botiful.Render.Engine  def html(url) do    {:ok, pid} = Engine.start_link(url)    {:ok, wait_for_html(pid)}  end  defp wait_for_html(pid) do    case Engine.html(pid) do      :incomplete ->        wait_for_html(pid)      html when is_binary(html) ->        # Engine.stop(pid)        html    end  endend

We implement 2 functions the html function is the public function that calls the private function wait_for_html. The private function will call itself until the page is completed. We also call Engine.stop once the page is rendered.

We can simply use the api as shown below.

Botiful.Render.html("https://www.artellectual.com")

Next we'll need to implement the termination strategy for our Render Engine.