Socrata v1.0.0 Socrata.Client View Source

The Client is the main interface of the library. Using a Client, you can get the records and metadata for a data set.

The Client accepts optional paramaters domain and app_token. These values can either be configured when you call new/3 or in your application’s config/config.exs file:

config :socrata,
  domain: "example.com",
  app_token: "blah blah blah"

For more information about tokens and their use, see the Socrata App Tokens docs.

Link to this section Summary

Functions

Gets the view information (metadata) for a data set

Creates a new Client struct

Link to this section Types

Link to this type t() View Source
t() :: %Socrata.Client{app_token: String.t(), domain: String.t()}

Link to this section Functions

Link to this function get_records(r, query \\ nil, fmt \\ "json", opts \\ []) View Source

Gets the records for a data set.

Options

The opts parameter of the function is passed directly to HTTPoison.get!/3 so you can control the request/response life cycle.

Note: the params key of the options is overwritten by the query struct.

Examples

  iex> # get regular json response
  iex> alias Socrata.{Client, Query}
  iex> client = Client.new("data.cityofchicago.org")
  iex> query = Query.new("yama-9had") |> Query.limit(2)
  iex> %HTTPoison.Response{body: body} = Client.get_records(client, query)
  iex> records = Jason.decode!(body)
  iex> length(records)
  2

  iex> # get csv response
  iex> alias Socrata.{Client, Query}
  iex> client = Client.new("data.cityofchicago.org")
  iex> query = Query.new("yama-9had") |> Query.limit(2)
  iex> %HTTPoison.Response{body: body} = Client.get_records(client, query, "csv")
  iex> {:ok, stream} = StringIO.open(body)
  iex> records = IO.binstream(stream, :line) |> CSV.decode!(headers: true) |> Enum.map(& &1)
  iex> length(records)
  2

  iex> # get tsv response
  iex> alias Socrata.{Client, Query}
  iex> client = Client.new("data.cityofchicago.org")
  iex> query = Query.new("yama-9had") |> Query.limit(2)
  iex> %HTTPoison.Response{body: body} = Client.get_records(client, query, "tsv")
  iex> {:ok, stream} = StringIO.open(body)
  iex> records = IO.binstream(stream, :line) |> CSV.decode!(separator: ?\t, headers: true) |> Enum.map(& &1)
  iex> length(records)
  2

  iex> # get geojson response
  iex> alias Socrata.{Client, Query}
  iex> client = Client.new("data.cityofchicago.org")
  iex> query = Query.new("yama-9had") |> Query.limit(2)
  iex> %HTTPoison.Response{body: body} = Client.get_records(client, query, "geojson")
  iex> %{"crs" => _, "type" => "FeatureCollection", "features" => records} = Jason.decode!(body)
  iex> length(records)
  2

  # get an asynchronous response
  iex> alias Socrata.{Client, Query}
  iex> client = Client.new("data.cityofchicago.org")
  iex> query = Query.new("yama-9had") |> Query.limit(2)
  iex> %HTTPoison.AsyncResponse{id: id} = Client.get_records(client, query, "json", stream_to: self())
  iex> is_reference(id)
  true

Gets the view information (metadata) for a data set.

Options

The opts parameter of the function is passed directly to HTTPoison.get!/3 so you can control the request/response life cycle.

Example

iex> alias Socrata.Client
iex> %HTTPoison.Response{body: body} = Client.new("data.cityofchicago.org") |> Client.get_view("yama-9had")
iex> details = Jason.decode!(body)
iex> Map.keys(details)
["oid", "publicationAppendEnabled", "category", "numberOfComments", "createdAt", "attribution", "hideFromDataJson", "query", "id", "tableAuthor", "rights", "tableId", "attributionLink", "owner", "viewCount", "grants", "downloadCount", "flags", "publicationGroup", "name", "averageRating", "publicationDate", "hideFromCatalog", "provenance", "totalTimesRated", "description", "metadata", "viewLastModified", "rowsUpdatedAt", "rowsUpdatedBy", "viewType", "newBackend", "publicationStage", "tags", "columns"]
Link to this function new(domain \\ nil, app_token \\ nil) View Source

Creates a new Client struct.

Examples

iex> # either no token or relying on app config
iex> alias Socrata.Client
iex> Client.new("data.cityofchicago.org")
%Socrata.Client{domain: "data.cityofchicago.org", app_token: nil}

iex> # explicitly setting up with a token
iex> alias Socrata.Client
iex> Client.new("data.cityofchicago.org", "blah blah blah")
%Socrata.Client{domain: "data.cityofchicago.org", app_token: "blah blah blah"}