Added server api

This commit is contained in:
Chris Blanchard 2015-08-01 16:20:49 +01:00
parent ed3bf229c0
commit 03ef45225c
4 changed files with 104 additions and 54 deletions

View file

@ -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

View file

@ -6,6 +6,7 @@ Ensl::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:show, :index]
resources :servers, only: [:index]
end
end

View file

@ -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

View file

@ -4,5 +4,13 @@ FactoryGirl.define do
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