ExTwilio.Parser

A JSON parser tuned specifically for Twilio API responses. Based on Poison's excellent JSON decoder.

Source

Summary

parse(module, response)

Parse a response expected to contain a single resource. If you pass in a module as the first argument, the JSON will be parsed into that module's __struct__

parse_list(module, response, key)

Parse a response expected to contain multiple resources. If you pass in a module as the first argument, the JSON will be parsed into that module's __struct__

Types

metadata :: %{}

http_status_code :: number

response :: %{body: String.t, status_code: number}

success :: {:ok, [%{}]}

success_list :: {:ok, [%{}], metadata}

Functions

parse(module, response)

Specs:

Parse a response expected to contain a single resource. If you pass in a module as the first argument, the JSON will be parsed into that module's __struct__.

Examples

Given you have a module named Resource, defined like this:

defmodule Resource do
  defstruct sid: nil
end

You can parse JSON into that module's struct like so:

iex> response = %{body: "{ \"sid\": \"AD34123\" }", status_code: 200}
...> ExTwilio.Parser.parse(Resource, response)
{:ok, %Resource{sid: "AD34123"}}

You can also parse into a regular map if you want.

iex> response = %{body: "{ \"sid\": \"AD34123\" }", status_code: 200}
...> ExTwilio.Parser.parse(%{}, response)
{:ok, %{"sid" => "AD34123"}}
Source
parse_list(module, response, key)

Specs:

Parse a response expected to contain multiple resources. If you pass in a module as the first argument, the JSON will be parsed into that module's __struct__.

Examples

Given you have a module named Resource, defined like this:

defmodule Resource do
  defstruct sid: nil
end

And the JSON you are parsing looks like this:

{
  "resources": [{
    "sid": "first"
  }, {
    "sid": "second"
  }],
  "next_page": 10
}

You can parse the the JSON like this:

ExTwilio.Parser.parse_list(Resource, json, "resources")
{:ok, [%Resource{sid: "first"}, %Resource{sid: "second"}], %{"next_page" => 10}}
Source