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

#22 Twitter service #53

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ bower.json

# yarn
yarn-error.log

config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
config/settings/development.yml
21 changes: 10 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ source 'https://rubygems.org'

gem 'better_errors'
gem 'binding_of_caller'
gem 'byebug'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use Puma as the app server
gem 'puma', '~> 3.0'

gem 'npm-pipeline-rails'
gem 'slim'
gem 'sass-rails'
gem 'bourbon'
gem 'byebug'
gem 'config'
gem 'neat'
gem 'normalize-rails'
gem 'npm-pipeline-rails'
gem 'puma', '~> 3.0'
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 All @@ -23,6 +22,7 @@ end
group :development, :test do
gem 'pry'
gem 'pry-rails'
gem 'rspec-rails'
end

group :development do
Expand All @@ -33,4 +33,3 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'web-console'
end
gem 'rspec-rails', group: [:development, :test]
45 changes: 44 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.0)
public_suffix (~> 2.0, >= 2.0.2)
arel (7.1.4)
ast (2.3.0)
better_errors (2.1.1)
Expand All @@ -49,6 +51,7 @@ GEM
bourbon (4.3.2)
sass (~> 3.4)
thor (~> 0.19)
buftok (0.2.0)
builder (3.2.3)
byebug (9.0.6)
codecov (0.1.8)
Expand All @@ -57,13 +60,31 @@ GEM
url
coderay (1.1.1)
concurrent-ruby (1.0.4)
config (1.4.0)
activesupport (>= 3.0)
deep_merge (~> 1.1.1)
debug_inspector (0.0.2)
deep_merge (1.1.1)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.20170223)
unf (>= 0.0.5, < 1.0.0)
equalizer (0.0.11)
erubis (2.7.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
ffi (1.9.14)
globalid (0.3.7)
activesupport (>= 4.1.0)
http (2.2.1)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 1.0.1)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (1.0.1)
http_parser.rb (0.6.0)
i18n (0.8.0)
json (2.0.2)
listen (3.0.8)
Expand All @@ -73,12 +94,16 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
multipart-post (2.0.0)
naught (1.1.0)
neat (1.8.0)
sass (>= 3.3)
thor (~> 0.19)
Expand All @@ -98,6 +123,7 @@ GEM
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
public_suffix (2.0.5)
puma (3.6.0)
rack (2.0.1)
rack-test (0.6.3)
Expand Down Expand Up @@ -162,6 +188,7 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
simple_oauth (0.3.1)
simplecov (0.12.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
Expand All @@ -187,8 +214,21 @@ GEM
thor (0.19.4)
thread_safe (0.3.5)
tilt (2.0.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)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
unicode-display_width (1.1.1)
url (0.3.2)
web-console (3.3.1)
Expand All @@ -209,6 +249,7 @@ DEPENDENCIES
bourbon
byebug
codecov
config
listen (~> 3.0.5)
neat
normalize-rails
Expand All @@ -221,10 +262,12 @@ DEPENDENCIES
rspec-rails
rubocop
sass-rails
simplecov
slim
spring
spring-watcher-listen (~> 2.0.0)
twitter
web-console

BUNDLED WITH
1.13.7
1.14.6
25 changes: 25 additions & 0 deletions app/controllers/api/oceans_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Api::OceansController < ApplicationController
protect_from_forgery with: :null_session
before_filter :render_bad_request, unless: :limit_valid?

def index
oceans = Array.new(number_of_oceans) do
OceanService.new(Random.rand(1..5)).call
end
render json: oceans
end

private

def number_of_oceans
@number_of_oceans ||= params.fetch(:limit, 4).to_i
end

def render_bad_request
render nothing: true, status: :bad_request
end

def limit_valid?
number_of_oceans.between?(0, 20)
end
end
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: 44 additions & 0 deletions app/services/twitter_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'twitter'
APP_CONFIG = YAML.load(File.read('config/settings/development.yml'))
.with_indifferent_access
class TwitterService
def initialize
@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 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|
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 << 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'
36 changes: 36 additions & 0 deletions config/initializers/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Config.setup do |config|
# Name of the constant exposing loaded settings
config.const_name = 'Settings'

# Ability to remove elements of the array set in earlier loaded settings file. For example value: '--'.
#
# config.knockout_prefix = nil

# Overwrite arrays found in previously loaded settings file. When set to `false`, arrays will be merged.
#
# config.overwrite_arrays = true

# Load environment variables from the `ENV` object and override any settings defined in files.
#
# config.use_env = false

# Define ENV variable prefix deciding which variables to load into config.
#
# config.env_prefix = 'Settings'

# What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well
# with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where
# using dots in variable names might not be allowed (eg. Bash).
#
# config.env_separator = '.'

# Ability to process variables names:
# * nil - no change
# * :downcase - convert to lower case
#
# config.env_converter = :downcase

# Parse numeric values as integers instead of strings.
#
# config.env_parse_values = true
end
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Rails.application.routes.draw do
resources :islands, only: :index
root 'islands#index'
resources :islands, only: :index
resources :test_oceans, only: :index
namespace :api, defaults: { format: 'json' } do
resources :oceans, only: :index
end
end
Empty file added config/settings.yml
Empty file.
Empty file added config/settings/production.yml
Empty file.
Empty file added config/settings/test.yml
Empty file.
31 changes: 31 additions & 0 deletions spec/controllers/api/oceans_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe Api::OceansController do
let(:json_response) { JSON.parse(response.body) }
let(:oceans) { json_response }
let(:ocean_service) { instance_double(OceanService, call: 'ocean') }

it 'renders 4 oceans, when there are no additional parameters' do
get :index
expect(response).to be_success
expect(oceans.size).to be 4
end

it 'renders 0 oceans' do
get :index, limit: '0'
expect(response).to be_success
expect(oceans.size).to be
end

it 'calls the OceanService with 4 as limit' do
expect(OceanService)
.to(receive(:new).exactly(4).times.and_return(ocean_service))
get :index, limit: '4'
expect(oceans).to eq %w(ocean ocean ocean ocean)
end

it 'calls the ocean with forbidden parameters (< 0 | > 20)' do
get :index, limit: '21'
expect(response).to be_bad_request
end
end
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.
Loading