Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Managing dependencies


One of the advantages of OTP (more information on OTP may be found in Chapter 6, OTP – Open Telecom Platform) is modularity, and it is very common to have several applications running as dependencies of one application. An application is a way to achieve modularity; in this context, we call an application something that is known in other programming languages as a library. In this recipe, we will integrate an HTTP client with a new application. We will be using the Hex package manager (http://hex.pm).

Getting ready

  1. Generate a new application with mix new manage_deps:

    > mix new manage_deps
    

    The output is shown in the following screenshot:

  2. Visit https://hex.pm/packages?search=http.

  3. We will choose HTTPoison (https://hex.pm/packages/httpoison).

How to do it…

To add a dependency to our application, we will follow these steps:

  1. Inside the manage_deps application, open mix.exs and edit the file to include HTTPoison as a dependency:

    defp deps do
        [{:httpoison, "~> 0.4"}]
    end
  2. HTTPoison must be started with our system. Add this to the started applications list by including it inside the application function:

    def application do
        [applications: [:logger, :httpoison]]
    end
  3. Save mix.exs and run mix deps.get to fetch the declared dependencies, as shown in this screenshot:

  4. Compile the dependencies by executing mix deps.compile, as shown in the following screenshot:

    Note

    Sometimes, some of the dependencies are Erlang projects, so you may get a prompt asking you to install rebar (rebar is a tool similar to Mix used in Erlang projects). Once you accept to download it, it will be available in your system and you won't have to worry about it anymore.

  5. Start your application with iex –S mix.

  6. Inside the IEx session, check whether HTTPoison is running:

    iex(1)> :application.which_applications
    [{:manage_deps, 'manage_deps', '0.0.1'},
     {:httpoison, '  Yet Another HTTP client for Elixir powered by hackney\n',
      '0.4.2'}, {:hackney, 'simple HTTP client', '0.13.1'}(…)]
    
  7. Get Google's main page using HTTPoison:

    iex(5)> HTTPoison.get("http://www.google.com")
    %HTTPoison.Response{body: "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>302 Moved</TITLE></HEAD><BODY>\n<H1>302 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.pt/?gfe_rd=cr&amp;ei=WFAOVLvQFJSs8wfehYJg\">here</A>.\r\n</BODY></HTML>\r\n",
     headers: %{"Alternate-Protocol" => "80:quic", "Cache-Control" => "private",
       "Content-Length" => "256", "Content-Type" => "text/html; charset=UTF-8",
       "Date" => "Tue, 09 Sep 2014 00:56:56 GMT",
       "Location" => "http://www.google.pt/?gfe_rd=cr&ei=WFAOVLvQFJSs8wfehYJg",
       "Server" => "GFE/2.0"}, status_code: 302}
    

How it works…

Dependencies are preferably added using hex.pm (https://hex.pm/).

Tip

If an application doesn't yet exist in Hex, it is also possible to use a GitHub repository as a source.

To fetch a dependency from GitHub, instead of declaring the dependency with the {:httpoison, "~> 0.4"} format, the following format is used:

{:httpoison, github: " edgurgel/httpoison "} 

The local filesystem may also be configured as a source for dependencies, as follows:

 {:httpotion, path: "path/to/httpotion"}

Once the dependencies are declared inside the mix.exs file, there are Mix tasks to get, compile, and clean them. The dependencies are then fetched, and if these dependencies have more dependencies on themselves, Mix is smart enough to fetch them.

When compiling dependencies, Mix is also capable of figuring out whether any dependent application has its own dependencies and whether they need to be compiled.

Starting IEx with the –S Mix loads the Mix environment inside IEx, and the application becomes accessible.

As shown in the Inspecting your system recipe, it is possible to get a list of running applications and check whether our dependency (and its own dependencies) are running. In the particular case of HTTPoison, automatic start is ensured by adding the atom representing the application name to the list under applications ([applications: [:logger, :httpoison]]).

See also