From 1e0212aafa26e18bf68b03e6d147daf899aac382 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Thu, 13 Nov 2014 18:14:30 -0500 Subject: [PATCH] Share Static Controller Methods * Expose `/api/videos` --- app/controllers/lookbooks_controller.rb | 9 +-- app/controllers/projects_controller.rb | 85 +++++++++++-------------- app/controllers/redis_controller.rb | 22 ++++--- app/controllers/splashes_controller.rb | 7 -- app/controllers/static_controller.rb | 15 +++++ app/controllers/videos_controller.rb | 36 +++++++++++ config/routes.rb | 2 +- spec/models/project_spec.rb | 4 -- spec/models/splash_spec.rb | 37 ----------- spec/requests/splashes_spec.rb | 10 --- spec/requests/videos_spec.rb | 8 +++ spec/support/api/schemas/projects.json | 6 +- spec/support/api/schemas/splashes.json | 23 ------- spec/support/api/schemas/videos.json | 19 ++++++ 14 files changed, 134 insertions(+), 149 deletions(-) delete mode 100644 app/controllers/splashes_controller.rb create mode 100644 app/controllers/static_controller.rb create mode 100644 app/controllers/videos_controller.rb delete mode 100644 spec/models/project_spec.rb delete mode 100644 spec/models/splash_spec.rb delete mode 100644 spec/requests/splashes_spec.rb create mode 100644 spec/requests/videos_spec.rb delete mode 100644 spec/support/api/schemas/splashes.json create mode 100644 spec/support/api/schemas/videos.json diff --git a/app/controllers/lookbooks_controller.rb b/app/controllers/lookbooks_controller.rb index c9684a8..a060f98 100644 --- a/app/controllers/lookbooks_controller.rb +++ b/app/controllers/lookbooks_controller.rb @@ -1,4 +1,4 @@ -class LookbooksController < ApplicationController +class LookbooksController < StaticController def index @lookbooks = build_lookbooks @@ -17,11 +17,4 @@ def build_lookbooks ] }] end - - def image_url(name) - URI.join( - root_url, - self.class.helpers.asset_url(name), - ).to_s - end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index c4e65e1..3b84047 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,4 +1,4 @@ -class ProjectsController < InheritedResources::Base +class ProjectsController < StaticController def index @projects = find_projects @@ -10,69 +10,58 @@ def index def find_projects [{ id: 1, - name: "The Philadelphia Experiment", numeral: "I", slug: "the-philadelphia-experiment", pages: [ - { url: image_url("the-philadelphia-experiment/1.jpg") }, - { url: image_url("the-philadelphia-experiment/2.jpg") }, - { url: image_url("the-philadelphia-experiment/3.jpg") }, - { url: image_url("the-philadelphia-experiment/4.jpg") }, - { url: image_url("the-philadelphia-experiment/5.jpg") }, - { url: image_url("the-philadelphia-experiment/6.jpg") }, - { url: image_url("the-philadelphia-experiment/7.jpg") }, - { url: image_url("the-philadelphia-experiment/8.jpg") }, - { url: image_url("the-philadelphia-experiment/9.jpg") }, - { url: image_url("the-philadelphia-experiment/10.jpg") }, - { url: vimeo_url(61014435), is_video: true }, - { url: image_url("the-philadelphia-experiment/12.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/1.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/2.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/3.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/4.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/5.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/6.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/7.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/8.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/9.jpg") }, + { url: image_url("projects/the-philadelphia-experiment/10.jpg") }, + { url: vimeo_url(61014435), video: true }, + { url: image_url("projects/the-philadelphia-experiment/12.jpg") }, ] }, { id: 2, - name: "Zachary Armstrong", numeral: "II", slug: "zachary-armstrong", pages: [ - { url: image_url("zachary-armstrong/1.jpg") }, - { url: image_url("zachary-armstrong/2.jpg") }, - { url: image_url("zachary-armstrong/3.jpg") }, - { url: image_url("zachary-armstrong/4.jpg") }, - { url: image_url("zachary-armstrong/5.jpg") }, - { url: image_url("zachary-armstrong/6.jpg") }, - { url: image_url("zachary-armstrong/7.jpg") }, - { url: image_url("zachary-armstrong/8.jpg") }, - { url: image_url("zachary-armstrong/9.jpg") }, - { url: vimeo_url(86785302), is_video: true }, - { url: image_url("zachary-armstrong/11.jpg") }, + { url: image_url("projects/zachary-armstrong/1.jpg") }, + { url: image_url("projects/zachary-armstrong/2.jpg") }, + { url: image_url("projects/zachary-armstrong/3.jpg") }, + { url: image_url("projects/zachary-armstrong/4.jpg") }, + { url: image_url("projects/zachary-armstrong/5.jpg") }, + { url: image_url("projects/zachary-armstrong/6.jpg") }, + { url: image_url("projects/zachary-armstrong/7.jpg") }, + { url: image_url("projects/zachary-armstrong/8.jpg") }, + { url: image_url("projects/zachary-armstrong/9.jpg") }, + { url: vimeo_url(86785302), video: true }, + { url: image_url("projects/zachary-armstrong/11.jpg") }, ] }, { id: 3, - name: "Magenta", numeral: "III", slug: "magenta", pages: [ - { url: image_url("magenta/1.jpg") }, - { url: image_url("magenta/2.jpg") }, - { url: image_url("magenta/3.jpg") }, - { url: image_url("magenta/4.jpg") }, - { url: image_url("magenta/5.jpg") }, - { url: vimeo_url(90116548), is_video: true }, - { url: image_url("magenta/7.jpg") }, - { url: image_url("magenta/8.jpg") }, - { url: image_url("magenta/9.jpg") }, - { url: image_url("magenta/10.jpg") }, - { url: image_url("magenta/11.jpg") }, - { url: vimeo_url(93339618), is_video: true }, - { url: image_url("magenta/13.jpg") }, + { url: image_url("projects/magenta/1.jpg") }, + { url: image_url("projects/magenta/2.jpg") }, + { url: image_url("projects/magenta/3.jpg") }, + { url: image_url("projects/magenta/4.jpg") }, + { url: image_url("projects/magenta/5.jpg") }, + { url: vimeo_url(90116548), video: true }, + { url: image_url("projects/magenta/7.jpg") }, + { url: image_url("projects/magenta/8.jpg") }, + { url: image_url("projects/magenta/9.jpg") }, + { url: image_url("projects/magenta/10.jpg") }, + { url: image_url("projects/magenta/11.jpg") }, + { url: vimeo_url(93339618), video: true }, + { url: image_url("projects/magenta/13.jpg") }, ] }] end - - def image_url(name) - self.class.helpers.image_url("projects/#{name}") - end - - def vimeo_url(id) - "http://player.vimeo.com/video/#{id}?title=0&byline=0&portrait=0&color=cfcfcf%22%20width=%22888" - end end diff --git a/app/controllers/redis_controller.rb b/app/controllers/redis_controller.rb index a1c7841..63d7505 100644 --- a/app/controllers/redis_controller.rb +++ b/app/controllers/redis_controller.rb @@ -1,16 +1,18 @@ class RedisController < ApplicationController def index - version = params[:version].presence || "current" + with_redis do |redis| + version = params[:version].presence || "current" - html_key = "index:#{version}" + html_key = "index:#{version}" - html = redis.get(html_key) + html = redis.get(html_key) - raise ActionController::RoutingError, "#{html_key} not found" if html.blank? + raise ActionController::RoutingError, "#{html_key} not found" if html.blank? - insert_csrf_meta(html) + insert_csrf_meta(html) - render text: html + render text: html + end end private @@ -28,13 +30,17 @@ def body_bottom_pos(html) html.index("") end - def redis + def with_redis uri = URI.parse(ENV["REDISTOGO_URL"]) - Redis.new( + redis = Redis.new( host: uri.host, port: uri.port, password: uri.password ) + + yield redis + + redis.quit end end diff --git a/app/controllers/splashes_controller.rb b/app/controllers/splashes_controller.rb deleted file mode 100644 index 046f72e..0000000 --- a/app/controllers/splashes_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class SplashesController < ApplicationController - def index - @splashes = Splash.all - - render json: @splashes, serializer: CachedCollectionSerializer - end -end diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb new file mode 100644 index 0000000..ce28873 --- /dev/null +++ b/app/controllers/static_controller.rb @@ -0,0 +1,15 @@ +class StaticController < ApplicationController + + protected + + def image_url(name) + URI.join( + root_url, + self.class.helpers.asset_url(name), + ).to_s + end + + def vimeo_url(id) + "http://player.vimeo.com/video/#{id}?title=0&byline=0&portrait=0&color=cfcfcf%22%20width=%22888" + end +end diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb new file mode 100644 index 0000000..ad444e2 --- /dev/null +++ b/app/controllers/videos_controller.rb @@ -0,0 +1,36 @@ +class VideosController < StaticController + def index + @videos = find_videos + + render json: { videos: @videos } + end + + private + + def find_videos + [{ + id: 6, + url: vimeo_url(109002548), + }, + { + id: 5, + url: vimeo_url(99653555), + }, + { + id: 4, + url: vimeo_url(84338572), + }, + { + id: 3, + url: vimeo_url(83601495), + }, + { + id: 2, + url: vimeo_url(81305762), + }, + { + id: 1, + url: vimeo_url(73152136), + }] + end +end diff --git a/config/routes.rb b/config/routes.rb index 4c56656..1a07d27 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ resources :posts, only: [:index] resources :projects resources :lookbooks, only: [:index] - resources :splashes, only: [:index] + resources :videos, only: [:index] resources :visuals, only: [:index] end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb deleted file mode 100644 index 41f5358..0000000 --- a/spec/models/project_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -describe Project do - it { should have_one(:banner) } - it { should have_many(:photos) } -end diff --git a/spec/models/splash_spec.rb b/spec/models/splash_spec.rb deleted file mode 100644 index 2d5b9a7..0000000 --- a/spec/models/splash_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -require 'spec_helper' - -describe Splash do - describe '#vimeo_id=' do - it 'sets the #video_url' do - splash = Splash.new(vimeo_id: 1234) - - expect(splash.video_url).to eq '//player.vimeo.com/video/1234?title=0&byline=0&portrait=0&color=ffffff' - end - - context 'when the video_url is set' do - it "doesn't override the #video_url" do - splash = Splash.new(video_url: 'http://thing.com') - - splash.vimeo_id = 1234 - - expect(splash.video_url).to eq 'http://thing.com' - end - end - end - - describe '#vimeo_id' do - it 'comes from the #video_url' do - splash = Splash.new(video_url: '//player.vimeo.com/video/1234?title=0&byline=0&portrait=0&color=ffffff') - - expect(splash.vimeo_id).to eq '1234' - end - - context 'when there is no #video_url' do - it 'returns nil' do - splash = Splash.new - - expect(splash.vimeo_id).to be_nil - end - end - end -end diff --git a/spec/requests/splashes_spec.rb b/spec/requests/splashes_spec.rb deleted file mode 100644 index 0637aed..0000000 --- a/spec/requests/splashes_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -describe "GET /api/splashes" do - it "returns published splashes" do - create(:splash, :published) - - get "/api/splashes", {}, json_header - - expect(response.status).to eq 200 - expect(response).to match_response_schema("splashes") - end -end diff --git a/spec/requests/videos_spec.rb b/spec/requests/videos_spec.rb new file mode 100644 index 0000000..aaace6d --- /dev/null +++ b/spec/requests/videos_spec.rb @@ -0,0 +1,8 @@ +describe "GET /api/videos" do + it "returns published videos" do + get "/api/videos", {}, json_header + + expect(response.status).to eq 200 + expect(response).to match_response_schema("videos") + end +end diff --git a/spec/support/api/schemas/projects.json b/spec/support/api/schemas/projects.json index 6e725e4..80f25d7 100644 --- a/spec/support/api/schemas/projects.json +++ b/spec/support/api/schemas/projects.json @@ -7,19 +7,19 @@ "items": { "required": [ "id", - "name", + "numeral", "pages", "slug" ], "properties" : { "id": { "type": "integer" }, - "name": { "type": "string" }, + "numeral": { "type": "string" }, "slug": { "type": "string" }, "pages": { "type": "array", "items": { "url": { "type": "string" }, - "is_video": { "type": "boolean" } + "video": { "type": "boolean" } } } } diff --git a/spec/support/api/schemas/splashes.json b/spec/support/api/schemas/splashes.json deleted file mode 100644 index 84dae66..0000000 --- a/spec/support/api/schemas/splashes.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "type": "object", - "required": ["splashes"], - "properties": { - "splashes": { - "type": "array", - "items": { - "required": [ - "id", - "name", - "video_url", - "published_at" - ], - "properties" : { - "id": { "type": "integer" }, - "name": { "type": "string" }, - "video_url": { "type": "string" }, - "published_at": { "type": "string", "format": "date-time" } - } - } - } - } -} diff --git a/spec/support/api/schemas/videos.json b/spec/support/api/schemas/videos.json new file mode 100644 index 0000000..ab1c989 --- /dev/null +++ b/spec/support/api/schemas/videos.json @@ -0,0 +1,19 @@ +{ + "type": "object", + "required": ["videos"], + "properties": { + "videos": { + "type": "array", + "items": { + "required": [ + "id", + "url" + ], + "properties" : { + "id": { "type": "integer" }, + "url": { "type": "string" } + } + } + } + } +}