Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Forked from allefgomes/twitter_client.ex
Last active May 23, 2020 02:29
Show Gist options
  • Save joeljuca/bbe5408f19e3afb7372878b4b48a22bc to your computer and use it in GitHub Desktop.
Save joeljuca/bbe5408f19e3afb7372878b4b48a22bc to your computer and use it in GitHub Desktop.
Twitter Client in elixir
defmodule TwitterClient do
use Tesla
@token System.get_env("TWITTER_TOKEN")
plug(Tesla.Middleware.BaseUrl, "https://api.twitter.com")
plug(Tesla.Middleware.DecodeJson)
def get_tweets do
{:ok, response} = make_request()
response.body["statuses"]
|> create_presentations()
end
defp make_request do
get("/1.1/search/tweets.json",
query: [q: "elixir", count: 10],
headers: [
{"Authorization", "Bearer #{@token}"},
{"Accept", "Application/json; Charset=utf-8"}
]
)
end
defp create_presentations(tweets) do
Enum.map(tweets, fn tweet -> print_tweet(tweet) end)
{:ok, "Thank you for watching!"}
end
defp print_tweet(tweet) do
IO.puts("#{tweet["text"]}
- Tweeted by #{tweet["user"]["name"]} at #{tweet["created_at"]}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment