mirror of
https://github.com/ENSL/ensl.org.git
synced 2025-01-14 13:51:26 +00:00
Added server api
This commit is contained in:
parent
ed3bf229c0
commit
03ef45225c
4 changed files with 104 additions and 54 deletions
21
app/controllers/api/v1/servers_controller.rb
Normal file
21
app/controllers/api/v1/servers_controller.rb
Normal 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
|
|
@ -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
|
||||
|
||||
|
|
20
spec/controllers/api/v1/servers_controller_spec.rb
Normal file
20
spec/controllers/api/v1/servers_controller_spec.rb
Normal 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
|
|
@ -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
|
Loading…
Reference in a new issue