Notes and exercises while reading through Programming Elixir by Dave Thomas.
Let’s stop hacking and get serious.
Yay!
- mix - Elixir build tool
- external dependencies
- ExUnit - testing framework
- libraries, etc.
Excited to make something now! This chapter gives a small taste of OTP as well.
- command line program
- github username, project name, count ---> program ---> issues in table format
- access as HTTP client
- response in JSON
Mix:
- has tasks (like rake)
mix newcreates project skeleton
mix new issues
Parser:
- module Project.CLI (e.g. Issues.CLI) is the command line parser
def runin that module is the main entry point
Namespace:
/lib/_projectname_(e.g. /lib/issues) containscli.ex/lib/issues/cli.ex- '/lib/issues.ex'
OptionParser:
- Use Elixir's provided OptionParser methods
- Includes support for switches (such as --help -h)
Thumbs up!
See issues\test\issues_test.exs for skeleton of an ExUnit test.
work:issues smeade$ mix test
Compiling 1 file (.ex)
....
Finished in 0.03 seconds
4 tests, 0 failures
Pass mix run an Elixir expression.
$ mix run -e 'Issues.CLI.run(["-h"])'
usage: issues <user> <project> [ count | 4 ]
Look for libraries in:
http://elixir-lang.org/docs.htmlhttp://erlang.org/doc/- External dependencies
- package manager (like rubygems, npm, etc.)
https://hex.pm/- Add to
mix.exs
OTP is the framework that manages suites of running applications. The application function configures the contents of these suites.
Instead of calling Library.start, add the library to the suite of applications that is being managed by OTP. You do that by adding it to mix.exs application function.
def application do
[ applications: [ :logger, :httpoison ] ]
endSo far...
def process({user, project, count}) do
Issues.GithubIssues.fetch(user, project)
|> decode_response
|> sort_into_ascending_order
endUse built-in Enum.take.
def process({user, project, count}) do
Issues.GithubIssues.fetch(user, project)
|> decode_response
|> sort_into_ascending_order
|> Enum.take(count)
end def process({user, project, count}) do
Issues.GithubIssues.fetch(user, project)
|> decode_response
|> sort_into_ascending_order
|> Enum.take(count)
|> print_table_for_columns(["number", "created_at", "title"])
endwork:issues smeade$ mix test
.........
Finished in 0.04 seconds
9 tests, 0 failuresMix can package our code, along with its dependencies, into a single file that can be run on any Unix-based platform
- mix can package the app into an executable file
- leverages Erlang's
escript - and
main_moduleescriptconfiguration setting - where main_module is a module containing a
mainfunction.
After adding configuration information and renaming run to main, generate executable like so:
work:issues smeade$ mix escript.build
Compiling 4 files (.ex)
Generated issues app
Generated escript issues with MIX_ENV=dev
- Four levels of messages:
debug,info,warn,error. - compile-time config in
config.exs - run-time config via
Logger.configure
ex_doc- documentation toolearmark- output formatter
Add these dependencies and any additional project information into mix.exs, then:
work:issues smeade$ mix deps.get
Running dependency resolution
Dependency resolution completed
earmark: 1.0.2
ex_doc: 0.14.3
* Getting ex_doc (Hex package)
Checking package (https://repo.hex.pm/tarballs/ex_doc-0.14.3.tar)
Fetched package
* Getting earmark (Hex package)
Checking package (https://repo.hex.pm/tarballs/earmark-1.0.2.tar)
Fetched package
and to generate docs:
work:issues smeade$ mix docs
Docs successfully generated.
View them at "doc/index.html".