Skip to content
This repository has been archived by the owner on Aug 7, 2018. It is now read-only.

Commit

Permalink
#22 RSpec-Tests, twittercontent displayed in the content-area
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerg-Seitz committed Mar 8, 2017
1 parent 4cc875d commit 92562dc
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
6 changes: 5 additions & 1 deletion app/controllers/islands_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class IslandsController < ApplicationController
def index; end
def index
twitter = TwitterService.new
@trend_names = twitter.trend_names
@tweets = twitter.search_tweets('', 5, 'puppy', 'cute')
end
end
44 changes: 16 additions & 28 deletions app/services/twitter_service.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,44 @@

require 'twitter'

APP_CONFIG = YAML.load(File.read('config/settings/development.yml'))
.with_indifferent_access
class TwitterService
APP_CONFIG = YAML.load(File.read('config/settings/development.yml'))
.with_indifferent_access

def initialize
@client = Twitter::REST::Client.new do |config|
@twitter = Twitter::REST::Client.new do |config|
config.consumer_key = APP_CONFIG['consumer_key']
config.consumer_secret = APP_CONFIG['consumer_secret']
config.access_token = APP_CONFIG['access_token']
config.access_token_secret = APP_CONFIG['access_token_secret']
end
end

def tweets_from_user_that_contain(user, number_of_posts = 5, *tags)
@client.search("from:#{user} #{make_hashtags(tags)}", result_type: 'recent')
.take(number_of_posts)
.each do |tweet|
puts tweet.text
end
end

def tweets_containing(*tags)
tweets = @client.search(make_hashtags(tags),
result_type: 'recent',
language: 'en')
tweets.attrs[:statuses].each do |tweet|
"#{tweet[:user][:name]}: #{tweet[:text]}"
end
def search_tweets(user = '', number_of_posts = 5, *tags)
@twitter.search("#{user} #{make_hashtags(tags)}", result_type: 'recent')
.take(number_of_posts)
end

def trend_names
names = []
trends.attrs[:trends].each do |trend|
puts trend[:name]
names << trend[:name]
end
names
end

private

def trends #returns Twitter::Trendresults
@client.trends
def trends # returns Twitter::Trendresults
@twitter.trends
end

def make_hashtags(tags) # takes an array of words
hashtags = []
tags.each do |tag|
hashtags << '#' + tag
hashtags << if tag[0] == '#'
tag
else
'#' + tag
end
end
hashtags.join(', ')
end



end
11 changes: 11 additions & 0 deletions app/views/layouts/_tweet.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1> Wow there is content on this island! </h1>

<% @tweets.each do |tweet| %>
<h3><%= tweet.user.name %></h3><a href="<%= tweet.url %>"> Show source tweet</a>
<p><%= tweet.text %><p>

<% tweet.media.each do |item| %>
<img src="<% item.display_url %>" >
<% end %>
<br>
<% end %>
1 change: 1 addition & 0 deletions app/views/layouts/application.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ html
= render 'layouts/nav'
main<>
== yield
= render "layouts/tweet"
= render 'layouts/footer'
= javascript_include_tag 'application'
4 changes: 4 additions & 0 deletions spec/fixtures/testconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
consumer_key: consumer_key
consumer_secret: consumer_secret
access_token: consumer_secret
access_token_secret: access_token_secret
Empty file added spec/helpers/twitter_helper.rb
Empty file.
60 changes: 39 additions & 21 deletions spec/services/twitter_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,46 @@
require 'twitter'

describe 'TwitterService' do
let(:trend_results) do
Twitter::TrendResults.new(attrs= {trends:[
{ name:"Foo",
url: "http://foo.baz/",
promoted_content:nil,
query: "1",
tweet_volume:1},
{ name:"Bar",
url: "http://bar.baz/",
promoted_content:nil,
query: "1",
tweet_volume:1}]})
let(:service) do
TwitterService.new do |config|
config.consumer_key = APP_CONFIG['consumer_key']
config.consumer_secret = APP_CONFIG['consumer_secret']
config.access_token = APP_CONFIG['access_token']
config.access_token_secret = APP_CONFIG['access_token_secret']
end
end
let(:service) {TwitterService.new do |config|
config.consumer_key = 'consumer_key'
config.consumer_secret = 'consumer_secret'
config.access_token = 'access_token'
config.access_token_secret = 'access_token_secret'
end}

it 'should return the name of the trends' do
expect(service.trends).to receive(trend_results).and_return(trend_results)
expect(service.trend_names).to eq(["Foo","Bar"])
context '#initialize' do # pointless test
it 'creates a new TwitterService with given parameters' do
expect(service.class).to eq(TwitterService)
end
end

context '#trend_names' do
it 'returns the trend names' do
trend_results = Twitter::TrendResults.new(trends:
[{ name: 'Foo',
url: 'http://foo.baz/',
promoted_content: nil,
query: '1',
tweet_volume: 1 },
{ name: 'Bar',
url: 'http://bar.baz/',
promoted_content: nil,
query: '1',
tweet_volume: 1 }])
allow(service).to receive(:trends).and_return(trend_results)
expect(service.trend_names).to eq %w(Foo Bar)
end
end

context 'search_tweets' do
it 'returns a given number of tweets containing specific words ' do
expect(service.search_tweets('', 5, 'puppy', 'cute').first.class)
.to eq(Twitter::Tweet)
expect(service.search_tweets('', 5, 'puppy', 'cute').count).to eq(5)
expect(service.search_tweets('', 5, 'puppy', 'cute').first.text)
.to include('puppy')
end
end
end

0 comments on commit 92562dc

Please sign in to comment.