Twittex v0.2.3 Twittex.Client.Base

A behaviour module for implementing your own Twitter client.

It implements the GenServer behaviour, authenticates when starting and keeps the authentication token in it state during the entire process livetime.

Example

To create your own 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(Keyword.merge(%{q: term}, options))
  end
end

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

Supervisor.start_link([TwittexBot.child_spec], strategy: :one_for_one)

And here’s how you may use it:

TwitterBot.search "#myelixirstatus", count: 3

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

Same as stage/6 but raises HTTPoison.Error if an error occurs during the request

Starts the process as part of a supervisor tree

Macros

Generates a singleton Twitter client

Functions

get(pid, url, headers \\ [], options \\ [])
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 Twittex.API.request/5 for more detailed information.

get!(pid, url, headers \\ [], options \\ [])
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 \\ [])
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 Twittex.API.request/5 for more detailed information.

post!(pid, url, body, headers \\ [], options \\ [])
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.

stage(pid, method, url, body \\ [], headers \\ [], options \\ [])

Streams data from the given url.

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

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

Same as stage/6 but raises HTTPoison.Error if an error occurs during the request.

start_link(options \\ [])
start_link(Keyword.t) :: GenServer.on_start

Starts the process as part of a supervisor tree.

Options

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

Further options are passed to GenServer.start_link/1.

Macros

__using__(options)

Generates a singleton Twitter client.

Options

  • :pool - Use pool of clients (default: false)

It generates get/3, post/4, stage/5 and their ! counterparts so you don’t have to care about authentication. Here’s, a very basic example:

defmodule TwitterBot do
  use Twittex.Client.Base

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

Note that the generated child_spec/1 helper function can be used to start the client as part of a supervisor tree.