Skip to content

Commit cbd49db

Browse files
author
Chris Rees
committed
Poll Twitter using ExTwitter and the Streaming API
1 parent aa29ac9 commit cbd49db

14 files changed

+207
-9
lines changed

apps/zapdos/.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps
9+
10+
# Where 3rd-party dependencies like ExDoc output generated docs.
11+
/doc
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez

apps/zapdos/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Zapdos
2+
3+
**TODO: Add description**
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `zapdos` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[{:zapdos, "~> 0.1.0"}]
13+
end
14+
```
15+
16+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
17+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
18+
be found at [https://hexdocs.pm/zapdos](https://hexdocs.pm/zapdos).
19+

apps/zapdos/config/config.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# 3rd-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure for your application as:
12+
#
13+
# config :zapdos, key: :value
14+
#
15+
# And access this configuration in your application as:
16+
#
17+
# Application.get_env(:zapdos, :key)
18+
#
19+
# Or configure a 3rd-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env}.exs"
31+
32+
config :extwitter, :oauth, [
33+
consumer_key: "",
34+
consumer_secret: "",
35+
access_token: "",
36+
access_token_secret: ""
37+
]

apps/zapdos/lib/zapdos.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Zapdos do
2+
alias Zapdos.ColorParsing
3+
4+
@moduledoc """
5+
Documentation for Zapdos.
6+
"""
7+
8+
def get_tweets(query) do
9+
ExTwitter.stream_filter(track: query)
10+
|> Stream.map(fn(tweet) -> tweet.text end)
11+
|> Stream.map(fn(text) -> ColorParsing.get_color(text) |> ColorParsing.hex_to_number |> IO.puts end)
12+
|> Enum.to_list
13+
end
14+
15+
end

apps/zapdos/lib/zapdos/application.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule Zapdos.Application do
2+
# See http://elixir-lang.org/docs/stable/elixir/Application.html
3+
# for more information on OTP Applications
4+
@moduledoc false
5+
6+
use Application
7+
8+
def start(_type, _args) do
9+
import Supervisor.Spec, warn: false
10+
11+
# Define workers and child supervisors to be supervised
12+
children = [
13+
]
14+
15+
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
16+
# for other strategies and supported options
17+
opts = [strategy: :one_for_one, name: Zapdos.Supervisor]
18+
Supervisor.start_link(children, opts)
19+
end
20+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Zapdos.ColorParsing do
2+
def get_color("Color: #" <> <<raw_hex::bytes-size(6)>> <> _), do: raw_hex
3+
4+
def get_color(_), do: "FFFFFF"
5+
6+
def parse_rgb_hex(rgb_value) do
7+
[r1, r2, g1, g2, b1, b2] = rgb_value |> String.codepoints |> Enum.take(-6)
8+
{hex_to_number(r1 <> r2), hex_to_number(g1 <> g2), hex_to_number(b1 <> b2)}
9+
end
10+
11+
def hex_to_number(hex_value) do
12+
hex_value |> String.upcase |> Base.decode16! |> :binary.decode_unsigned
13+
end
14+
end
15+

apps/zapdos/lib/zapdos/pluck.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule Zapdos.Pluck do
2+
use GenServer
3+
4+
def start_link do
5+
GenServer.start_link(__MODULE__, %{})
6+
end
7+
8+
def init(state) do
9+
schedule_work() # Schedule work to be performed at some point
10+
{:ok, state}
11+
end
12+
13+
def handle_info(:work, state) do
14+
# Do the work you desire here
15+
Zapdos.get_tweets("#cmm_storm", 1)
16+
|> IO.inspect
17+
18+
schedule_work() # Reschedule once more
19+
{:noreply, state}
20+
end
21+
22+
defp schedule_work() do
23+
Process.send_after(self(), :work, 15 * 1000)
24+
end
25+
end

apps/zapdos/mix.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule Zapdos.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[app: :zapdos,
6+
version: "0.1.0",
7+
build_path: "../../_build",
8+
config_path: "../../config/config.exs",
9+
deps_path: "../../deps",
10+
lockfile: "../../mix.lock",
11+
elixir: "~> 1.3",
12+
build_embedded: Mix.env == :prod,
13+
start_permanent: Mix.env == :prod,
14+
deps: deps()]
15+
end
16+
17+
# Configuration for the OTP application
18+
#
19+
# Type "mix help compile.app" for more information
20+
def application do
21+
# Specify extra applications you'll use from Erlang/Elixir
22+
[extra_applications: [:logger],
23+
mod: {Zapdos.Application, []}]
24+
end
25+
26+
# Dependencies can be Hex packages:
27+
#
28+
# {:my_dep, "~> 0.3.0"}
29+
#
30+
# Or git/path repositories:
31+
#
32+
# {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
33+
#
34+
# Type "mix help deps" for more examples and options
35+
defp deps do
36+
[
37+
{:extwitter, "~> 0.8"},
38+
]
39+
end
40+
end

apps/zapdos/test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

apps/zapdos/test/zapdos_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule ZapdosTest do
2+
use ExUnit.Case
3+
doctest Zapdos
4+
5+
test "the truth" do
6+
assert 1 + 1 == 2
7+
end
8+
end

apps/zeus/lib/zeus.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ defmodule Zeus do
1818
# for other strategies and supported options
1919
opts = [strategy: :one_for_one, name: Zeus.Supervisor]
2020
Supervisor.start_link(children, opts)
21+
22+
:timer.apply_interval(:timer.seconds(15), Zapdos, :get_tweet("#cmm_storm", 1))
23+
|> IO.puts
2124
end
2225

2326
# Tell Phoenix to update the endpoint configuration

apps/zeus/lib/zeus/color_parsing.ex

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
defmodule Zeus.ColorParsing do
22

3-
def parse_rgb_hex(rgb_value) do
4-
[r1, r2, g1, g2, b1, b2] = rgb_value |> String.codepoints |> Enum.take(-6)
5-
{hex_to_number(r1 <> r2), hex_to_number(g1 <> g2), hex_to_number(b1 <> b2)}
6-
end
7-
8-
def hex_to_number(hex_value) do
9-
hex_value |> String.upcase |> Base.decode16! |> :binary.decode_unsigned
10-
end
113
end

apps/zeus/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ defmodule Zeus.Mixfile do
3939
{:phoenix_live_reload, "~> 1.0", only: :dev},
4040
{:gettext, "~> 0.11"},
4141
{:cowboy, "~> 1.0"},
42-
{:lightning, in_umbrella: true}]
42+
{:lightning, in_umbrella: true},
43+
{:zapdos, in_umbrella: true}]
4344
end
4445

4546
def aliases do

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
33
"distillery": {:hex, :distillery, "1.1.2", "4cf32ecc70ca7eecca9e52e111edf320bd78011050825863cd8bc7ffee686c5d", [:mix], []},
44
"elixir_make": {:hex, :elixir_make, "0.3.0", "285147fa943806eee82f6680b7b446b5569bcf3ee8328fa0a7c200ffc44fbaba", [:mix], []},
5+
"extwitter": {:hex, :extwitter, "0.8.2", "1b1b6842c6541ddfa468f1589b54c02a3b278b5ae74d6bed260655394d8cc7be", [:mix], [{:oauther, "~> 1.1", [hex: :oauther, optional: false]}, {:poison, "~> 2.0", [hex: :poison, optional: false]}]},
56
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
67
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], []},
78
"mime": {:hex, :mime, "1.0.1", "05c393850524767d13a53627df71beeebb016205eb43bfbd92d14d24ec7a1b51", [:mix], []},
@@ -14,6 +15,7 @@
1415
"nerves_toolchain_arm_unknown_linux_gnueabihf": {:hex, :nerves_toolchain_arm_unknown_linux_gnueabihf, "0.9.0", "5a1bca8c46776ad24c358ab58800ed470f91a3e294ac6eb8ffda0041954781e1", [:mix], [{:nerves, "~> 0.4.0", [hex: :nerves, optional: false]}, {:nerves_toolchain_ctng, "~> 0.8.0", [hex: :nerves_toolchain_ctng, optional: false]}]},
1516
"nerves_toolchain_ctng": {:hex, :nerves_toolchain_ctng, "0.8.0", "6dff7ed51e1711c5f4da3d559bc528a8265e3dd950dda95f4d6832aed9dbe320", [:mix], []},
1617
"nerves_wpa_supplicant": {:hex, :nerves_wpa_supplicant, "0.2.3", "2666b21bf0868f0d3b127930c3bacf59b178a0d015d308ae3985dcb7c1595870", [:make, :mix], [{:elixir_make, "~> 0.3", [hex: :elixir_make, optional: false]}]},
18+
"oauther": {:hex, :oauther, "1.1.0", "c9a56e200507ce64e069f5273143db0cddd7904cd5d1fe46920388a48f22441b", [:mix], []},
1719
"phoenix": {:hex, :phoenix, "1.2.1", "6dc592249ab73c67575769765b66ad164ad25d83defa3492dc6ae269bd2a68ab", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.1", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
1820
"phoenix_html": {:hex, :phoenix_html, "2.9.3", "1b5a2122cbf743aa242f54dced8a4f1cc778b8bd304f4b4c0043a6250c58e258", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
1921
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.8", "4333f9c74190f485a74866beff2f9304f069d53f047f5fbb0fb8d1ee4c495f73", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]},

0 commit comments

Comments
 (0)