Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Jul 9, 2017
1 parent ff535dc commit 1d8cd87
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 19 deletions.
21 changes: 14 additions & 7 deletions app/helpers/json_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
class JsonRendering
include Rails.application.routes.url_helpers

attr_reader :json
attr_reader :json, :route_prefix

def initialize(json)
def initialize(json, options = {})
options.symbolize_keys!
@json = json
@route_prefix = options[:route_prefix].to_s
@route_prefix += '_' unless @route_prefix.end_with?('_') || @route_prefix.blank?
end

def response(symbol, object)
send(symbol, object)
end

def reps(reps)
Expand All @@ -23,7 +30,7 @@ def office_locations(office_locations)

def rep(rep)
return json.error 'Record not found' if rep.blank?
json.self rep_url(rep.official_id)
json.self send("#{route_prefix}rep_url", rep.official_id)
json.state { state rep.state }
json.district { district rep.district } if rep.district
_rep rep
Expand All @@ -33,18 +40,18 @@ def rep(rep)
end

def state(state)
json.self state_url(state.state_code)
json.self send("#{route_prefix}state_url", state.state_code)
json.extract! state, :state_code, :name, :abbr
end

def district(district)
json.self district_url(district.full_code)
json.self send("#{route_prefix}district_url", district.full_code)
json.extract! district, :full_code, :code, :state_code, :level, :chamber, :name
end

def office_location(office_location)
json.self office_location_url(office_location.office_id)
json.rep rep_url(office_location.official_id)
json.self send("#{route_prefix}office_location_url", office_location.office_id)
json.rep send("#{route_prefix}rep_url", office_location.official_id)
json.extract! office_location, :active, :official_id, :level, :office_id,
:bioguide_id, :state_leg_id, :office_type, :distance, :building,
:address, :suite, :city, :state, :zip, :phone, :fax, :hours,
Expand Down
4 changes: 2 additions & 2 deletions app/views/api/beta/office_locations/index.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

rendering = JsonRendering.new json
rendering = JsonRendering.new json, route_prefix: :api_beta

json.total_records @office_locations.count
json.set! '_links' do
Expand All @@ -9,5 +9,5 @@ json.set! '_links' do
end
end
json.set! 'office_locations' do
rendering.office_locations @office_locations
rendering.response :office_locations, @office_locations
end
4 changes: 2 additions & 2 deletions app/views/api/beta/office_locations/show.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

rendering = JsonRendering.new json
rendering = JsonRendering.new json, route_prefix: :api_beta

rendering.office_location @office_location
rendering.response :office_location, @office_location
4 changes: 2 additions & 2 deletions app/views/api/beta/reps/index.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

rendering = JsonRendering.new json
rendering = JsonRendering.new json, route_prefix: :api_beta

json.total_records @reps.size
json.set! '_links' do
Expand All @@ -9,5 +9,5 @@ json.set! '_links' do
end
end
json.set! 'reps' do
rendering.reps @reps
rendering.response :reps, @reps
end
4 changes: 2 additions & 2 deletions app/views/api/beta/reps/show.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

rendering = JsonRendering.new json
rendering = JsonRendering.new json, route_prefix: :api_beta

rendering.rep @rep
rendering.response :rep, @rep
2 changes: 1 addition & 1 deletion app/views/office_locations/index.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rendering = JsonRendering.new json

rendering.office_locations @office_locations
rendering.response :office_locations, @office_locations
2 changes: 1 addition & 1 deletion app/views/office_locations/show.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rendering = JsonRendering.new json

rendering.office_location @office_location
rendering.response :office_location, @office_location
2 changes: 1 addition & 1 deletion app/views/reps/index.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rendering = JsonRendering.new json

rendering.reps @reps
rendering.response :reps, @reps
2 changes: 1 addition & 1 deletion app/views/reps/show.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

rendering = JsonRendering.new json

rendering.rep @rep
rendering.response :rep, @rep

0 comments on commit 1d8cd87

Please sign in to comment.