Skip to content

Commit f77865e

Browse files
committed
summary tweet, specs
1 parent 32eadd5 commit f77865e

6 files changed

+221
-21
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
source 'https://rubygems.org'
22

3+
ruby '2.2.2'
4+
35
gem 'wkhtmltoimage-binary'
46
gem 'imgkit'
57
gem 'twitter'
8+
gem 'rspec'
9+
gem 'httparty'

Gemfile.lock

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ GEM
33
specs:
44
addressable (2.4.0)
55
buftok (0.2.0)
6+
diff-lcs (1.2.5)
67
domain_name (0.5.20160615)
78
unf (>= 0.0.5, < 1.0.0)
89
equalizer (0.0.10)
@@ -17,12 +18,28 @@ GEM
1718
domain_name (~> 0.5)
1819
http-form_data (1.0.1)
1920
http_parser.rb (0.6.0)
21+
httparty (0.14.0)
22+
multi_xml (>= 0.5.2)
2023
imgkit (1.6.1)
2124
json (1.8.3)
2225
memoizable (0.4.2)
2326
thread_safe (~> 0.3, >= 0.3.1)
27+
multi_xml (0.5.5)
2428
multipart-post (2.0.0)
2529
naught (1.1.0)
30+
rspec (3.5.0)
31+
rspec-core (~> 3.5.0)
32+
rspec-expectations (~> 3.5.0)
33+
rspec-mocks (~> 3.5.0)
34+
rspec-core (3.5.2)
35+
rspec-support (~> 3.5.0)
36+
rspec-expectations (3.5.0)
37+
diff-lcs (>= 1.2.0, < 2.0)
38+
rspec-support (~> 3.5.0)
39+
rspec-mocks (3.5.0)
40+
diff-lcs (>= 1.2.0, < 2.0)
41+
rspec-support (~> 3.5.0)
42+
rspec-support (3.5.0)
2643
simple_oauth (0.3.1)
2744
thread_safe (0.3.5)
2845
twitter (5.16.0)
@@ -45,9 +62,14 @@ PLATFORMS
4562
ruby
4663

4764
DEPENDENCIES
65+
httparty
4866
imgkit
67+
rspec
4968
twitter
5069
wkhtmltoimage-binary
5170

71+
RUBY VERSION
72+
ruby 2.2.2p95
73+
5274
BUNDLED WITH
5375
1.12.5

spec/spec_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RSpec.configure do |config|
2+
config.color = true
3+
end

spec/tweet_client_spec.rb

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
require 'spec_helper'
2+
require 'rspec/mocks'
3+
require 'rspec/expectations'
4+
require_relative '../tweet_client'
5+
6+
describe '#truncate' do
7+
it 'returns string if it is shorter than truncate_at' do
8+
expect(truncate('My string', 20)).to eq('My string')
9+
end
10+
it 'truncates a long string respecting spaces' do
11+
expect(truncate('One Two Three Four', 10)).to eq('One Two')
12+
end
13+
it 'can handle emoji' do
14+
expect(truncate('👍👏 more text', 10)).to eq '👍👏 more'
15+
end
16+
end
17+
18+
describe '#summary_text' do
19+
it 'renders the text' do
20+
expect(summary_text(['a', 'b']).length).to be <= 115
21+
end
22+
it 'truncates a really long list of makers' do
23+
makers = (1..15).map { |i| "maker#{i}" }
24+
expect(summary_text(makers).length).to be <= 110
25+
expect(summary_text(makers)).to include Time.now.year.to_s
26+
expect(summary_text(makers)).to include '@maker2'
27+
end
28+
end
29+
30+
describe '#run' do
31+
before do
32+
@date = (Date.today - 1).to_s
33+
allow(HTTParty).to receive(:get)
34+
.with("https://ph-tweet-server.herokuapp.com/charts/#{@date}/data")
35+
.and_return(double(body: ENTRY_DATA))
36+
allow_any_instance_of(Twitter::REST::Client)
37+
.to receive(:update_with_media)
38+
allow(IMGKit).to receive(:new)
39+
.with("https://ph-tweet-server.herokuapp.com/charts/#{@date}",
40+
zoom: 2, width: 2048, height: 1024)
41+
.and_return(double(to_file: 'rank_1_img'))
42+
end
43+
it 'initiates twitter client' do
44+
expect(Twitter::REST::Client).to receive(:new).and_call_original
45+
run
46+
end
47+
it 'retrieves entry data from server' do
48+
expect(HTTParty).to receive(:get)
49+
.with("https://ph-tweet-server.herokuapp.com/charts/#{@date}/data")
50+
run
51+
end
52+
it 'sends the summary tweet' do
53+
expect_any_instance_of(Twitter::REST::Client)
54+
.to receive(:update_with_media)
55+
.with(/@RyanKennedy/, 'rank_1_img')
56+
run
57+
end
58+
end
59+
60+
ENTRY_DATA = <<JSON
61+
{
62+
"id":2,
63+
"date":"2016-08-21",
64+
"makers":[
65+
"adrianeholter",
66+
"RyanKennedy",
67+
"nainish",
68+
"seannieuwoudt",
69+
"bevmerriman",
70+
"v_ignatyev",
71+
"photomatt"
72+
],
73+
"hunters":[
74+
"nagra__",
75+
"iWozzy",
76+
"bentossell",
77+
"v_ignatyev",
78+
"arunpattnaik"
79+
],
80+
"posts":[
81+
{
82+
"id":6,
83+
"hunter":"nagra__",
84+
"makers":[
85+
"adrianeholter"
86+
],
87+
"url":"https://www.producthunt.com/tech/html5-speedtest-by-ookla?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Top+Hunts+Daily+%28ID%3A+3237%29",
88+
"rank":1
89+
},
90+
{
91+
"id":10,
92+
"hunter":"arunpattnaik",
93+
"makers":[
94+
"photomatt"
95+
],
96+
"url":"https://www.producthunt.com/tech/get-blog-by-wordpress?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Top+Hunts+Daily+%28ID%3A+3237%29",
97+
"rank":5
98+
},
99+
{
100+
"id":7,
101+
"hunter":"iWozzy",
102+
"makers":[
103+
"RyanKennedy",
104+
"nainish"
105+
],
106+
"url":"https://www.producthunt.com/tech/hyfy?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Top+Hunts+Daily+%28ID%3A+3237%29",
107+
"rank":2
108+
},
109+
{
110+
"id":8,
111+
"hunter":"bentossell",
112+
"makers":[
113+
"seannieuwoudt",
114+
"bevmerriman"
115+
],
116+
"url":"https://www.producthunt.com/tech/arthur-2?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Top+Hunts+Daily+%28ID%3A+3237%29",
117+
"rank":3
118+
},
119+
{
120+
"id":9,
121+
"hunter":"v_ignatyev",
122+
"makers":[
123+
"v_ignatyev"
124+
],
125+
"url":"https://www.producthunt.com/tech/crx-extractor?utm_campaign=producthunt-api\u0026utm_medium=api\u0026utm_source=Application%3A+Top+Hunts+Daily+%28ID%3A+3237%29",
126+
"rank":4
127+
}
128+
]
129+
}
130+
JSON

tweet-client.rb

-21
This file was deleted.

tweet_client.rb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'httparty'
4+
require 'imgkit'
5+
require 'twitter'
6+
7+
# TODO:
8+
# send tweets to makers and hunters
9+
# add hunters to list
10+
# add makers to list
11+
# update Twitter description
12+
13+
ACTIONS = ['👍', '👏']
14+
THINGS = ['🌟', '🏆', '🏅', '💯', '🚀']
15+
DRINKS = ['🍾', '🍻']
16+
17+
# borrowing this from Rails :)
18+
def truncate(str, truncate_at, options = {})
19+
return str unless str.length > truncate_at
20+
21+
options[:separator] ||= ' '
22+
stop = str.rindex(options[:separator], truncate_at) || truncate_at
23+
24+
"#{str[0, stop]}"
25+
end
26+
27+
def summary_text(makers_array)
28+
date_str = (Date.today - 1).strftime("%b %-d, %Y")
29+
handles = makers_array.map{ |m| "@#{m}" }.join(' ')
30+
str = "#TopHunts of #{date_str} on @producthunt #{THINGS.sample(2).join(' ')} " \
31+
"Products by #{handles} #{ACTIONS.sample}"
32+
truncate(str, 110)
33+
end
34+
35+
def run
36+
client = Twitter::REST::Client.new do |config|
37+
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
38+
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
39+
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
40+
config.access_token_secret = ENV['TWITTER_ACCESS_SECRET']
41+
end
42+
43+
date = (Date.today - 1).to_s
44+
response =
45+
HTTParty.get("https://ph-tweet-server.herokuapp.com/charts/#{date}/data")
46+
entry_data = JSON.parse(response.body)
47+
48+
image_kit = IMGKit.new("https://ph-tweet-server.herokuapp.com/charts/#{date}",
49+
zoom: 2, width: 2048, height: 1024)
50+
rank_1_img = image_kit.to_file('rank_1_img.jpg')
51+
52+
# Summary tweet
53+
client.update_with_media(summary_text(entry_data['makers']), rank_1_img)
54+
55+
entry_data['posts'].each do |post|
56+
57+
end
58+
end
59+
60+
if $0 == __FILE__
61+
run
62+
end

0 commit comments

Comments
 (0)