Twittex.Client.Base

A behaviour module for implementing your own Twitter client.

It implements the GenServer behaviour, and keeps the authentication state during the entire process livetime. If the server dies, and is part of a supervisor tree, it will restart with the same token.

See get/4 and post/5 for more detailed informations.

Example

To create a client, create a new module and use Twittex.Client.Base as follow:

defmodule TwitterBot do
  use Twittex.Client.Base

  def search(term, options \ []) do
    get "/search/tweets.json?" <> URI.encode_query(Dict.merge(%{q: term}, options))
  end
end

This client works as a singleton and can be added to a supervisor tree:

worker(TwitterBot, [])

And this is how you may use it:

iex> TwitterBot.search "#myelixirstatus", count: 3
{:ok, %{}}

Summary

Functions

Issues a GET request to the given url

Same as get/4 but raises HTTPoison.Error if an error occurs during the request

Issues a POST request to the given url

Same as post/5 but raises HTTPoison.Error if an error occurs during the request

Starts the process linked to the current process

Functions

get(pid, url, headers \\ [], options \\ [])

Specs

get(pid, String.t, Twittex.API.headers, Keyword.t) ::
  {:ok, %{}} |
  {:error, HTTPoison.Error.t}

Issues a GET request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See Twitter.API.request/5 for more detailed information.

get!(pid, url, headers \\ [], options \\ [])

Specs

get!(pid, String.t, Twittex.API.headers, Keyword.t) :: %{}

Same as get/4 but raises HTTPoison.Error if an error occurs during the request.

post(pid, url, body \\ [], headers \\ [], options \\ [])

Specs

post(pid, String.t, binary, Twittex.API.headers, Keyword.t) ::
  {:ok, %{}} |
  {:error, HTTPoison.Error.t}

Issues a POST request to the given url.

Returns {:ok, response} if the request is successful, {:error, reason} otherwise.

See Twitter.API.request/5 for more detailed information.

post!(pid, url, body, headers \\ [], options \\ [])

Specs

post!(pid, String.t, binary, Twittex.API.headers, Keyword.t) :: %{}

Same as post/5 but raises HTTPoison.Error if an error occurs during the request.

start_link(options \\ [])

Specs

start_link(Keyword.t) :: GenServer.on_start

Starts the process linked to the current process.

Options

  • :username - Twitter username or email address
  • :password - Twitter password

Other options are passed to GenServer._start_link/1.