Skip to content

Commit ddf8bba

Browse files
begedinjoshsmith
authored andcommitted
Explanations
1 parent 819a2b2 commit ddf8bba

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

lib/code_corps/github/api/eager_api.ex

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,39 @@ defmodule CodeCorps.GitHub.EagerAPI do
4242
end
4343
end
4444

45-
def extract_total_pages(links_string) do
45+
defp extract_total_pages(links_string) do
46+
# Unfortunately, the paginating info we get from GitHub's responses is not
47+
# suitable for easy extraction.
48+
#
49+
# The information is stored in the following response header:
50+
#
51+
# ```
52+
# {"Link", '<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=15>; rel="next",
53+
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last",
54+
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1>; rel="first",
55+
# <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=13>; rel="prev"'
56+
#
57+
# ```
58+
#
59+
# If the response has no list header, then that means we got all the records
60+
# and there's just that one page.
61+
#
62+
# If the response has a list header, the value will contain at least the
63+
# "last" relation.
64+
#
65+
# Unfortunatly, the only way to parse it is via regex.
4666
links_string
4767
|> String.split(", ")
4868
|> Enum.map(fn link ->
69+
# Searches for `rel=`
4970
rel = Regex.run(~r{rel="([a-z]+)"}, link) |> List.last
71+
# Searches for the following variations:
72+
# ```
73+
# ?page={match}>
74+
# ?page={match}&...
75+
# &page={match}>
76+
# &page={match}&...
77+
# ```
5078
page = Regex.run(~r{[&/?]page=([^>&]+)}, link) |> List.last |> String.to_integer
5179

5280
{rel, page}

0 commit comments

Comments
 (0)