|
| 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 |
0 commit comments