ExTwilio v0.7.0 ExTwilio.Parser View Source
A JSON parser tuned specifically for Twilio API responses. Based on Poison's excellent JSON decoder.
Link to this section Summary
Functions
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 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__
Link to this section Types
error()
View Source
error() :: {:error, map(), http_status_code()}
error() :: {:error, map(), http_status_code()}
http_status_code()
View Source
http_status_code() :: number()
http_status_code() :: number()
key()
View Source
key() :: String.t()
key() :: String.t()
metadata()
View Source
metadata() :: map()
metadata() :: map()
parsed_list_response()
View Source
parsed_list_response() :: success_list() | error()
parsed_list_response() :: success_list() | error()
parsed_response() View Source
success()
View Source
success() :: {:ok, map()}
success() :: {:ok, map()}
success_delete()
View Source
success_delete() :: :ok
success_delete() :: :ok
success_list() View Source
Link to this section Functions
parse(response, module)
View Source
parse(HTTPoison.Response.t(), module()) :: success() | error()
parse(HTTPoison.Response.t(), module()) :: success() | error()
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(response, Resource)
{: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"}}
parse_list(response, module, key)
View Source
parse_list(HTTPoison.Response.t(), module(), key()) :: success_list() | error()
parse_list(HTTPoison.Response.t(), module(), key()) :: success_list() | error()
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(json, Resource, "resources")
{:ok, [%Resource{sid: "first"}, %Resource{sid: "second"}], %{"next_page" => 10}}