Transformation: Fetch from GitHub

Understand the data transformation chain by fetching data from GitHub.

We'll cover the following

Now, let’s continue down our data transformation chain. Having parsed our arguments, we need to transform them by fetching data from GitHub.

The process function

We’ll extend our run function to call a process function, passing it the value returned from the parse_args function. We could have written this: process(parse_args(argv)).

But to understand this code, we have to read it right to left. We prefer to make the chain more explicit using the Elixir pipe operator:

def run(argv) do argv
  |> parse_args
  |> process
end

Function variants

We need two variants of the process function. One handles the case where the user asked for help and parse_args returned :help. The other handles the case where a user, a project, and a count are returned.

def process(:help) do
  IO.puts """
  usage: issues <user> <project> [ count | #{@default_count} ] 
  """
  System.halt(0)
end
def process({user, project, _count}) do  
  Issues.GithubIssues.fetch(user, project)
end

We can use mix to run our function. Let’s first see if help gets displayed.

$ mix run -e 'Issues.CLI.run(["-h"])'
usage: issues <user> <project> [ count | 4 ]

Get hands-on with 1400+ tech skills courses.