Skip to content

Commit

Permalink
switch to jbuilder for simplicity and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
wwahammy committed Jun 26, 2018
1 parent f6741b0 commit 1d4dc3f
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 248 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ gem 'connection_pool'
gem 'foundation-rails', '~> 5'
gem 'httparty'
gem 'haml-rails'
gem 'oj'
gem 'jbuilder'
gem 'jbuilder_cache_multi'
gem 'platform-api'
gem 'puma'
gem 'puma_worker_killer'
gem 'rabl'
gem 'rack-attack'
gem 'rack-cors', require: 'rack/cors'
gem 'rails_autoscale_agent'
Expand Down
12 changes: 7 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ GEM
concurrent-ruby (~> 1.0)
io-like (0.3.0)
jaro_winkler (1.5.1)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jbuilder_cache_multi (0.1.0)
jbuilder (>= 1.5.0, < 3)
jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand All @@ -138,7 +143,6 @@ GEM
multi_xml (0.6.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
oj (3.6.2)
parallel (1.12.1)
parser (2.5.1.0)
ast (~> 2.4.0)
Expand All @@ -156,8 +160,6 @@ GEM
puma_worker_killer (0.1.0)
get_process_mem (~> 0.2)
puma (>= 2.7, < 4)
rabl (0.13.1)
activesupport (>= 2.3.14)
rack (1.6.10)
rack-attack (5.2.0)
rack
Expand Down Expand Up @@ -282,14 +284,14 @@ DEPENDENCIES
foundation-rails (~> 5)
haml-rails
httparty
jbuilder
jbuilder_cache_multi
jquery-rails
oj
pg (= 0.20)
platform-api
pry-rails
puma
puma_worker_killer
rabl
rack-attack
rack-cors
rails (= 4.2.10)
Expand Down
26 changes: 9 additions & 17 deletions app/controllers/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ class MatchesController < BaseApiController

def index
order_by_params
render_template
render 'index.json.jbuilder'
end

def current
@matches = @matches.where(status: 'in progress')
order_by_params
render_template
render 'index.json.jbuilder'
end

def complete
@matches = @matches.where(status: 'completed')
order_by_params
render_template
render 'index.json.jbuilder'
end

def future
@matches = @matches.where(status: 'future')
order_by_params
render_template
render 'index.json.jbuilder'
end

def country
Expand All @@ -35,27 +35,27 @@ def country
end
@matches = @team.matches
order_by_params
render_template
render 'index.json.jbuilder'
end

def today
@matches = Match.today.includes(:match_statistics)
.includes(:home_team).includes(:away_team).includes(:events)
order_by_params
render_template
render 'index.json.jbuilder'
end

def tomorrow
@matches = Match.tomorrow.includes(:match_statistics)
.includes(:home_team).includes(:away_team).includes(:events)
order_by_params
render_template
render 'index.json.jbuilder'
end

def show
@match = Match.find_by!(fifa_id: params[:id])
@matches = Match.where(id: @match.id)
render_template
render 'index.json.jbuilder'
end

private
Expand All @@ -66,14 +66,6 @@ def load_matches
.order('datetime ASC')
end

def render_template
if @details
render 'index.json.rabl'
else
render 'summary.json.rabl'
end
end

def order_by_params
@matches = @matches.order('datetime ASC')
@date = params[:by_date]
Expand Down Expand Up @@ -122,6 +114,6 @@ def order_by_scores
end

def detail_level
@details = params['details'] != 'false'
@summary = params['details'] == 'false'
end
end
10 changes: 5 additions & 5 deletions app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class TeamsController < BaseApiController
def index
@teams = Team.all
render 'team_index.json.rabl'
@teams = Team.all.includes(:group)
render 'index.json.jbuilder'
end

def results
@teams = Team.all
@teams = Team.all.includes(:group)
limit_team_if_requested
render 'team_results.json.rabl'
render 'team_results.json.jbuilder'
end

def group_results
@groups = Group.all
limit_group_if_requested
render 'group_results.json.rabl'
render 'group_results.json.jbuilder'
end

private
Expand Down
78 changes: 78 additions & 0 deletions app/views/matches/_match.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

json.(match, :venue, :location, :status, :time, :fifa_id,
:weather, :attendance, :officials, :stage_name)

json.home_team_country match.home_team&.country
json.away_team_country match.away_team&.country
json.datetime match.datetime&.utc&.iso8601

if match.draw
json.winner 'Draw'
json.winner_code 'Draw'
else
json.winner match.winner&.country
json.winner_code match.winner&.fifa_code
end
json.home_team do
if match.home_team
json.country match.home_team.country
json.code match.home_team.fifa_code
json.goals match.home_team_score
json.penalties match.json_home_team_penalties
else
json.country 'To Be Determined'
json.code 'TBD'
json.team_tbd match.home_team_tbd
end
end
json.away_team do
if match.away_team
json.country match.away_team.country
json.code match.away_team.fifa_code
json.goals match.away_team_score
json.penalties match.json_away_team_penalties
else
json.country 'To Be Determined'
json.code 'TBD'
json.team_tbd match.away_team_tbd
end
end
unless @summary
json.home_team_events do
json.cache! [match.home_team, match.home_team.events], expires_in: @cache_time do
events = match.home_team_events.sort_by { |e| e.time.to_i }
json.array! events do |event|
json.id event.id
json.type_of_event event.type_of_event
json.player event.player
json.time event.time
end
end
end
json.away_team_events do
json.cache! [match.away_team, match.away_team.events], expires_in: @cache_time do
events = match.away_team_events.sort_by { |e| e.time.to_i }
json.array! events do |event|
json.id event.id
json.type_of_event event.type_of_event
json.player event.player
json.time event.time
end
end
end

json.home_team_statistics do
json.cache! match.home_stats, expires_in: @cache_time do
json.partial! '/matches/stats', stats: match.home_stats
end
end

json.away_team_statistics do
json.cache! match.away_stats, expires_in: @cache_time do
json.partial! '/matches/stats', stats: match.away_stats
end
end

json.last_event_update_at match.last_event_update_at&.utc&.iso8601
json.last_score_update_at match.last_score_update_at&.utc&.iso8601
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
object @match_statistic
cache @match_statistic, expires_in: @cache_time
node(:country) { root_object.team.country }
attributes :attempts_on_goal, :on_target, :off_target, :blocked, :woodwork,
json.country stats.team.country
json.(stats, :attempts_on_goal, :on_target, :off_target, :blocked, :woodwork,
:corners, :offsides, :ball_possession, :pass_accuracy, :num_passes,
:passes_completed, :distance_covered, :balls_recovered, :tackles,
:clearances, :yellow_cards, :red_cards, :fouls_committed, :tactics,
:starting_eleven, :substitutes
:starting_eleven, :substitutes)
2 changes: 0 additions & 2 deletions app/views/matches/events.json.rabl

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/matches/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.cache_collection! @matches, expires_in: @cache_time do
json.array! @matches, partial: '/matches/match', as: :match
end
5 changes: 0 additions & 5 deletions app/views/matches/index.json.rabl

This file was deleted.

101 changes: 0 additions & 101 deletions app/views/matches/match.json.rabl

This file was deleted.

Loading

0 comments on commit 1d4dc3f

Please sign in to comment.