-
-
Save joeljuca/bbe5408f19e3afb7372878b4b48a22bc to your computer and use it in GitHub Desktop.
Twitter Client in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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