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 authored and Jörg Seitz committed Apr 9, 2017
1 parent 9e0b50b commit b190039
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
gem 'sass-rails'
gem 'slim'
gem 'twitter'
gem 'simplecov'

group :test do
gem 'codecov', require: false
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ DEPENDENCIES
rspec-rails
rubocop
sass-rails
simplecov
slim
spring
spring-watcher-listen (~> 2.0.0)
twitter
web-console

BUNDLED WITH
1.14.3
1.14.6
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
45 changes: 20 additions & 25 deletions app/services/twitter_service.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@

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
def search_tweets(user = '', number_of_posts = 5, *tags)
@twitter.search("#{user} #{make_hashtags(tags)}", result_type: 'recent')
.take(number_of_posts)
end

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

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

private

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
Expand Down
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.
45 changes: 40 additions & 5 deletions spec/services/twitter_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
require 'pry'
require 'rails_helper'
require 'twitter'

describe 'TwitterService' do
let(:APP_CONFIG) { {"consumer_key"=>"con_key",
"consumer_secret"=>"con_secret",
"access_token"=>"access_tk",
"access_token_secret"=>"access_secret_tk"}}
let(:service) { TwitterService.new}
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

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 b190039

Please sign in to comment.