Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elixir Cookbook

You're reading from   Elixir Cookbook Unleash the full power of programming in Elixir with over 60 incredibly effective recipes

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784397517
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paulo Pereira Paulo Pereira
Author Profile Icon Paulo Pereira
Paulo Pereira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Command Line 2. Data Types and Structures FREE CHAPTER 3. Strings and Binaries 4. Modules and Functions 5. Processes and Nodes 6. OTP – Open Telecom Platform 7. Cowboy and Phoenix 8. Interactions A. Installation and Further Reading 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:

    Getting ready
  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:
    How to do it…
  4. Compile the dependencies by executing mix deps.compile, as shown in the following screenshot:
    How to do it…

    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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime