Skip to content

Commit

Permalink
add jurisdictions/1 and people_from/1 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Mar 14, 2019
1 parent 6f0cbfd commit 705eeaf
Showing 1 changed file with 73 additions and 13 deletions.
86 changes: 73 additions & 13 deletions lib/open_states.ex
@@ -1,8 +1,11 @@
defmodule OpenStates do
@moduledoc """
Documentation for OpenStates.
An Elixir client for the OpenStates GraphQL API.
"""

@type response :: {:ok, Neuron.Response.t()} | {:error, response :: term}
@type args :: [atom | String.t()]

@doc """
Returns the API endpoint URL.
Expand Down Expand Up @@ -46,19 +49,20 @@ defmodule OpenStates do
{:ok, response} =
OpenStates.query(\"\"\"
{jurisdictions {
edges {
node {
id
{
jurisdictions {
edges {
node {
id
}
}
}
}}
}
\"\"\")
#=> {:ok, %Neuron.Response{body: %{ ... }, headers: [ ... ], status_code: 200}}
"""
@spec query(query_string :: String.t()) ::
{:ok, Neuron.Response.t()} | {:error, response :: term}
@spec query(query_string :: String.t()) :: response
def query(query_string) do
Neuron.query(query_string, _variables = %{}, url: url(), headers: headers())
end
Expand All @@ -69,15 +73,71 @@ defmodule OpenStates do
## Example
~q\"\"\"
{jurisdictions {
edges {
node {
id
{
jurisdictions {
edges {
node {
id
}
}
}
}}
}
\"\"\"
#=> {:ok, %Neuron.Response{body: %{ ... }, headers: [ ... ], status_code: 200}}
"""
@spec sigil_q(query_string :: String.t(), list) :: response
def sigil_q(query_string, []), do: query(query_string)

@doc """
Fetches the jurisdiction objects from the API.
"""
@spec jurisdictions() :: response
def jurisdictions, do: jurisdictions(attrs: [:id, :name])

@spec jurisdictions(attrs: args) :: response
def jurisdictions(attrs: attrs) do
attrs = Enum.join(attrs, "\n")

~q"""
{
jurisdictions {
edges {
node {
#{attrs}
}
}
}
}
"""
end

@doc """
Fetches legislators from a particular jurisdiction and organization.
"""
def people_from(opts) do
name = Keyword.get(opts, :name)
id = Keyword.get(opts, :id)
classification = Keyword.get(opts, :classification)
attrs = Keyword.get(opts, :attrs)

~q"""
{
jurisdiction(name: "#{name}", id: "#{id}") {
name
organizations(first: 1, classification: "#{classification}") {
edges {
node {
name
currentMemberships {
person {
#{Enum.join(attrs, "\n")}
}
}
}
}
}
}
}
"""
end
end

0 comments on commit 705eeaf

Please sign in to comment.