anchor.diff:  -type client_name()     :: string().
   -type stream_arg()      :: {string(), string()}.
   -type capability()      :: {client_incoming, client_name()}
  -                           | {client_outgoing, application_sid()}
  -                           | {event_stream, [stream_arg()]}.
  +                         | {client_outgoing, application_sid(), [{string(), string()}]}
  +                         | {event_stream, [stream_arg()]}.
   -type capability_opt()  :: {expires_after, integer()}.
   
   %% @doc Generates a twilio capabilities token.
  
positive.code:     {Megaseconds, Seconds, _Microseconds} = erlang:now(),
      (Megaseconds * 1000000) + Seconds + ExpiresAfter.
  
  %% @doc Formats a capability as a scope string.
  capability_to_scope_string({client_incoming, ClientName}, _ClientName) ->
      build_scope_string("client", "incoming", [{"clientName", ClientName}]);
  capability_to_scope_string({client_outgoing, ApplicationSID}, ClientName) ->
      case ClientName of
          undefined ->
              Params = [];
          _ ->
              Params = [{"clientName", ClientName}]
      end,
      build_scope_string("client", "outgoing", [{"appSid", ApplicationSID} | Params]);
  capability_to_scope_string({event_stream, Params}, _ClientName) ->
      build_scope_string("stream", "subscribe", [{"path", "/2010-04-01/Events"} | Params]).
      
  %% @doc Builds a scope string of the form `scope:service:privilege?key=value&key2=value2'.
  
negative.code:         capability_to_scope_string({client_incoming, "CLIENT<"}, "asdfasdf")),
      ?assertEqual(
          "scope:client:outgoing?appSid=1234&clientName=CLIENT%3c", 
          capability_to_scope_string({client_outgoing, "1234", []}, "CLIENT<")),
      ?assertEqual(
          "scope:client:outgoing?appSid=1234&clientName=CLIENT%3c", 
          capability_to_scope_string({client_outgoing, "1234", []}, "CLIENT<")),
      ?assertEqual(
          "scope:client:outgoing?appSid=1234",
          capability_to_scope_string({client_outgoing, "1234", []}, undefined)),
      ?assertEqual(
          "scope:client:outgoing?appSid=1234&appParams=name%3dryan%26this%3dthat",
          capability_to_scope_string({client_outgoing, "1234",
                  [{"name", "ryan"}, {"this", "that"}]}, undefined)),
      ?assertEqual(
          "scope:stream:subscribe?path=%2f2010-04-01%2fEvents",
          capability_to_scope_string({event_stream, []}, undefined)),
      ?assertEqual(
          "scope:stream:subscribe?path=%2f2010-04-01%2fEvents&params=channel%3d5",
          capability_to_scope_string({event_stream, [{"channel", "5"}]}, undefined)),
      ok.
  
  