Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct helpers #6

Open
AndrewDryga opened this issue Nov 22, 2016 · 0 comments
Open

Construct helpers #6

AndrewDryga opened this issue Nov 22, 2016 · 0 comments

Comments

@AndrewDryga
Copy link
Member

AndrewDryga commented Nov 22, 2016

+defmodule Gateway.Helpers.Pagination do
 +  @moduledoc """
 +  Functions related to paging
 +  """
 +
 +  def page_info_from(params) do
 +    starting_after = extract_integer(params, "starting_after")
 +    ending_before = extract_integer(params, "ending_before")
 +    limit = extract_integer(params, "limit")
 +
 +    cursors = %Ecto.Paging.Cursors{starting_after: starting_after, ending_before: ending_before}
 +
 +    %Ecto.Paging{limit: limit, cursors: cursors}
 +  end
 +
 +  def extract_integer(map, key) do
 +    case Map.get(map, key) do
 +      nil ->
 +        nil
 +      string ->
 +        case Integer.parse(string) do
 +          {integer, ""} -> integer
 +          _ -> string
 +        end
 +    end
 +  end
 +end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant