How to create a new Mix project?

Hello am a begginer to Elixir.So I need to create a mix project which I can name it “news”.I need to add a dependency and write a function called “fetch_news” that uses HTTPoison to fetch a blog post from a link maybe like " google.com".
How do I go about it

At first in your terminal, run this command mix new news, it creates an elixir project which has mix file.

For example, if you go to your new project path and run ls command you can see like this

README.md   deps      lib       mix.exs   mix.lock  test

If you consider you have a lib folder, and you have a mix file. In mix file you can add your project dependencies and in lib folder you can create your module file with ex format.

mix.exs

for adding deps please go to hex.pm site and search you can see the lib HTTPoison has a page

You can see the right side mix.exs copy the value {:httpoison, "~> 1.8"} out it into deps in mix.exs

like

  defp deps do
    [
      {:httpoison, "~> 1.8"}
    ]
  end

After that in your project current working directory run this command mix deps.get, it gets the lib from hex site and after that run mix deps.compile. Now you are ready to start coding in your practical project.

For editor, I prefer to suggest you Vscode, and open lib directory. You can see there is a file which is same to your project name .ex open and edit it.

For example:

defmodule Hello do
  def test do
    "This is test one"
  end
end

you can create functions under a module, and the function name should be downcase and the module is clear if you see the top code

Now create a function with any name you want like fetch_news and after that open the lib github: GitHub - edgurgel/httpoison: Yet Another HTTP client for Elixir powered by hackney

See the documents:

defmodule Hello do
  def test do
    "This is test one"
  end
  
  def fetch_news(url) do
    HTTPoison.get!(url)
  end
end

Finally, you can call the function like this in your terminal Hello.fetch_news("https://google.com")

It should be noted that elixir is a programming language which should be compiled, hence after you create your function, run recompile in your iex


When you use iex, actually you create a elixir console session. there are diffrent ways to deploy elixir applications, but this way I talked about it is dev mode

9 Likes

Thanks so after setting up all the environment ,the project requires me to first of all write a function that converts a given string input from small to uppercase ,I have tried using an anagram is it the correct way to go about it? Or should I create a differrent module for that.
Secondly ,am required to uee the library https://github.com/edgurgel/httpoison to fetch the url https://jsonplaceholder.typicode.com/todos/1 and print out the todo title on the console.
So am asking how am I able to print to the console using iex,is it possible.Thanks

Hey @codeInfinity can you show the resources you’re reading to learn Elixir? These sorts of questions are would generally be covered in one of the Elixir books or guides.

Who is this project for?

1 Like

@benwilson512 Its a learning project sir.Am learning online from Elixir school website.
Just require a clue about that or if possible I would appreciate if you would reccomend resources.Thanks

I’d start with Programming Elixir if you have experience coding in another language.

1 Like