From 03ef45225c9df0f1f908c0bf6d38d968d1f68d1d Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 16:20:49 +0100 Subject: [PATCH] Added server api --- app/controllers/api/v1/servers_controller.rb | 21 ++++ config/routes.rb | 97 ++++++++++--------- .../api/v1/servers_controller_spec.rb | 20 ++++ spec/factories/server.rb | 20 ++-- 4 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 app/controllers/api/v1/servers_controller.rb create mode 100644 spec/controllers/api/v1/servers_controller_spec.rb diff --git a/app/controllers/api/v1/servers_controller.rb b/app/controllers/api/v1/servers_controller.rb new file mode 100644 index 0000000..fe5a6cf --- /dev/null +++ b/app/controllers/api/v1/servers_controller.rb @@ -0,0 +1,21 @@ +class Api::V1::ServersController < Api::V1::BaseController + def index + render json: { servers: active_servers } + end + + private + + def active_servers + Server.active.map do |s| + { + id: s.id, + description: s.description, + dns: s.dns, + ip: s.ip, + port: s.port, + password: s.password, + category_id: s.category_id + } + end + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 483f9d2..0cf0a48 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,76 +6,77 @@ Ensl::Application.routes.draw do namespace :api do namespace :v1 do resources :users, only: [:show, :index] + resources :servers, only: [:index] end end - root to: "articles#news_index" + root to: "articles#news_index" - resources :articles do - resources :versions - end + resources :articles do + resources :versions + end match 'contests/del_map' match 'contests/scores' match 'contests/historical', to: "contests#historical" - resources :contests do - get 'current', on: :collection - end + resources :contests do + get 'current', on: :collection + end resources :log_events - resources :categories - resources :options - resources :polls + resources :categories + resources :options + resources :polls match 'comments/quote' - resources :comments - resources :shoutmsgs - resources :teamers - resources :teams - resources :gathers - resources :gatherers - resources :groups - resources :groupers - resources :forumers - resources :topics + resources :comments + resources :shoutmsgs + resources :teamers + resources :teams + resources :gathers + resources :gatherers + resources :groups + resources :groupers + resources :forumers + resources :topics match 'forums/up' match 'forums/down' - resources :forums - resources :users - resources :locks - resources :contesters - resources :contests - resources :challenges - resources :servers - resources :predictions - resources :rounds - resources :matches do |m| + resources :forums + resources :users + resources :locks + resources :contesters + resources :contests + resources :challenges + resources :servers + resources :predictions + resources :rounds + resources :matches do |m| get :admin, to: "matches#admin", on: :collection get :ref, to: "matches#ref" end - resources :maps - resources :logs - resources :log_files - resources :directories + resources :maps + resources :logs + resources :log_files + resources :directories resources :data_files - resources :predictions - resources :weeks - resources :movies - resources :messages - resources :sites - resources :bans - resources :tweets - resources :issues + resources :predictions + resources :weeks + resources :movies + resources :messages + resources :sites + resources :bans + resources :tweets + resources :issues match 'posts/quote' - resources :posts - resources :brackets + resources :posts + resources :brackets match 'about/action' match 'about/staff' @@ -127,10 +128,10 @@ Ensl::Application.routes.draw do match 'votes/create' - match ':controller/:action', requirements: { action: /A-Za-z/ } - match ':controller/:action/:id' - match ':controller/:action/:id.:format' - match ':controller/:action/:id/:id2' + match ':controller/:action', requirements: { action: /A-Za-z/ } + match ':controller/:action/:id' + match ':controller/:action/:id.:format' + match ':controller/:action/:id/:id2' match 'teamers/replace', to: 'teamers#replace', as: 'teamers_replace' end diff --git a/spec/controllers/api/v1/servers_controller_spec.rb b/spec/controllers/api/v1/servers_controller_spec.rb new file mode 100644 index 0000000..8b55eec --- /dev/null +++ b/spec/controllers/api/v1/servers_controller_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Api::V1::ServersController do + before do + request.accept = 'application/json' + end + + describe '#index' do + let!(:server) { create :server, :active } + let!(:inactive_server) { create :server, :inactive } + + it 'returns a list of servers' do + get :index + expect(response).to be_success + expect(json['servers'].length).to eq(1) + json_server = json['servers'][0] + expect(json_server['id']).to eq(server.id) + end + end +end \ No newline at end of file diff --git a/spec/factories/server.rb b/spec/factories/server.rb index 837c9ad..e5cb877 100644 --- a/spec/factories/server.rb +++ b/spec/factories/server.rb @@ -1,8 +1,16 @@ FactoryGirl.define do - factory :server do - sequence(:name) { |n| "ServerName#{n}" } - sequence(:dns) { |n| "DNS#{n}" } - sequence(:ip) { |n| "192.168.#{n % 255}.#{n}" } - sequence(:port) { |n| "#{1000 + n}" } - end + factory :server do + sequence(:name) { |n| "ServerName#{n}" } + sequence(:dns) { |n| "DNS#{n}" } + sequence(:ip) { |n| "192.168.#{n % 255}.#{n}" } + sequence(:port) { |n| "#{1000 + n}" } + + trait :active do + active true + end + + trait :inactive do + active false + end + end end \ No newline at end of file