code_corps v0.0.1 CodeCorps.GitHub.API.Pagination View Source

Used to parse and build pagination data when fetching multiple pages from the GitHub API

Link to this section Summary

Functions

Adds a page query parameter to an options Keyword list

Parses a collection of response headers and determines the record page count for a GitHub endpoint

From the specified page count, generates a list of integers, 1..count

Link to this section Functions

Link to this function add_page_param(options, page) View Source
add_page_param(Keyword.t(), integer()) :: Keyword.t()

Adds a page query parameter to an options Keyword list.

For HTTPPoison, query parameters go under the params key of the options argument, so this function also ensures the params key is present.

Link to this function retrieve_total_pages(headers) View Source
retrieve_total_pages(list()) :: integer()

Parses a collection of response headers and determines the record page count for a GitHub endpoint.

The response the headers are retrieved from is usually generated using a :head request.

The value of a “Link” header is used to determine the page count.

If the “Link” header is not present in the collection, the count is assumed 1

If the “Link” header is present, we use regex to parse the pagination info from its value.

The format of the header is as follows:

{"Link", '<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=15>; rel="next",
          <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last",
          <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1>; rel="first",
          <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=13>; rel="prev"'

The page count is determind by locating the rel="last" url and extracting the page query parameter from it.

Link to this function to_page_numbers(total) View Source
to_page_numbers(integer()) :: Range.t()

From the specified page count, generates a list of integers, 1..count