From 69eb36618be1bc22d8b8fcd3d7d4b0bf05d0e37d Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 2 Jul 2017 11:16:32 -0700 Subject: [PATCH] basic streaming twitter client --- twitter/Gemfile | 5 ++++ twitter/Gemfile.lock | 55 ++++++++++++++++++++++++++++++++++++++++++++ twitter/stream.rb | 35 ++++++++++++++++++++++++++++ twitter/test.rb | 14 +++++++++++ 4 files changed, 109 insertions(+) create mode 100644 twitter/Gemfile create mode 100644 twitter/Gemfile.lock create mode 100644 twitter/stream.rb create mode 100644 twitter/test.rb diff --git a/twitter/Gemfile b/twitter/Gemfile new file mode 100644 index 0000000..519fda3 --- /dev/null +++ b/twitter/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gem 'twitter' +gem 'inifile' +gem 'httparty' diff --git a/twitter/Gemfile.lock b/twitter/Gemfile.lock new file mode 100644 index 0000000..4d5ed7c --- /dev/null +++ b/twitter/Gemfile.lock @@ -0,0 +1,55 @@ +GEM + remote: https://rubygems.org/ + specs: + addressable (2.5.1) + public_suffix (~> 2.0, >= 2.0.2) + buftok (0.2.0) + domain_name (0.5.20160615) + unf (>= 0.0.5, < 1.0.0) + equalizer (0.0.11) + faraday (0.11.0) + multipart-post (>= 1.2, < 3) + http (2.2.2) + addressable (~> 2.3) + http-cookie (~> 1.0) + http-form_data (~> 1.0.1) + http_parser.rb (~> 0.6.0) + http-cookie (1.0.2) + domain_name (~> 0.5) + http-form_data (1.0.1) + http_parser.rb (0.6.0) + httparty (0.14.0) + multi_xml (>= 0.5.2) + inifile (3.0.0) + memoizable (0.4.2) + thread_safe (~> 0.3, >= 0.3.1) + multi_xml (0.6.0) + multipart-post (2.0.0) + naught (1.1.0) + public_suffix (2.0.5) + simple_oauth (0.3.1) + thread_safe (0.3.5) + twitter (6.1.0) + addressable (~> 2.5) + buftok (~> 0.2.0) + equalizer (= 0.0.11) + faraday (~> 0.11.0) + http (~> 2.1) + http_parser.rb (~> 0.6.0) + memoizable (~> 0.4.2) + naught (~> 1.1) + simple_oauth (~> 0.3.1) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.2) + +PLATFORMS + ruby + +DEPENDENCIES + httparty + inifile + twitter + +BUNDLED WITH + 1.14.4 diff --git a/twitter/stream.rb b/twitter/stream.rb new file mode 100644 index 0000000..8f56270 --- /dev/null +++ b/twitter/stream.rb @@ -0,0 +1,35 @@ +Encoding.default_internal = 'UTF-8' +require 'rubygems' +require 'bundler/setup' +require 'json' + +Bundler.require + +$config = IniFile.load('../.env') + +client = Twitter::Streaming::Client.new do |config| + config.consumer_key = $config['global']['TWITTER_CONSUMER_KEY'] + config.consumer_secret = $config['global']['TWITTER_CONSUMER_SECRET'] + config.access_token = $config['global']['TWITTER_ACCESS_TOKEN'] + config.access_token_secret = $config['global']['TWITTER_ACCESS_TOKEN_SECRET'] +end + +client.user do |obj| + case obj + when Twitter::Tweet + puts "Got a tweet" + puts obj.to_h.to_json + + response = HTTParty.post "#{$config['global']['APP_URL']}/twitter/stream", { + body: obj.to_h.to_json, + headers: {'Content-Type' => 'application/json'} + } + when Twitter::DirectMessage + puts "Got a DM" + when Twitter::Streaming::DeletedTweet + puts "Someone deleted a tweet" + when Twitter::Streaming::StallWarning + puts "Falling behind!" + end +end + diff --git a/twitter/test.rb b/twitter/test.rb new file mode 100644 index 0000000..20ebe0e --- /dev/null +++ b/twitter/test.rb @@ -0,0 +1,14 @@ +Encoding.default_internal = 'UTF-8' +require 'rubygems' +require 'bundler/setup' +require 'json' + +Bundler.require + +$config = IniFile.load('../.env') + +response = HTTParty.post "#{$config['global']['APP_URL']}/twitter/stream", { + body: IO.read("tweet.json"), + headers: {'Content-Type' => 'application/json'} +} +puts response