ok fix the API more
This commit is contained in:
parent
8f0b8929ba
commit
8613a03d00
|
|
@ -8,7 +8,7 @@ class ArsesController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@arses = Ars.all
|
@arses = JamRuby::Ars.all
|
||||||
render :json => @arses
|
render :json => @arses
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -18,75 +18,78 @@ class ArsesController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ars = Ars.find_by_id(params[:id])
|
begin
|
||||||
if @ars.nil?
|
# Primary ID lookup
|
||||||
render :json => {error: "Not Found"}, :status => 404
|
@ars = JamRuby::Ars.find_by_id(params[:id])
|
||||||
return
|
|
||||||
end
|
# Explicit secondary lookups if primary ID fails
|
||||||
|
@ars ||= JamRuby::Ars.find_by_id_int(params[:id_int]) if params[:id_int]
|
||||||
|
@ars ||= JamRuby::Ars.find_by_name(params[:name]) if params[:name]
|
||||||
|
|
||||||
allowed = [:password, :username, :active, :beta, :name, :provider, :id_int, :ip, :port, :continent, :country, :city, :subdivision, :latitude, :longitude]
|
if @ars.nil?
|
||||||
|
render :json => {error: "Not Found"}, :status => 404
|
||||||
update_hash = {}
|
return
|
||||||
allowed.each do |attr|
|
end
|
||||||
update_hash[attr] = params[attr] if params.has_key?(attr)
|
|
||||||
end
|
|
||||||
|
|
||||||
if @ars.update_attributes(update_hash, as: :admin)
|
allowed = [:password, :username, :active, :beta, :name, :provider, :id_int, :ip, :port, :continent, :country, :city, :subdivision, :latitude, :longitude]
|
||||||
render :json => @ars, :status => :ok
|
|
||||||
else
|
update_hash = {}
|
||||||
render :json => @ars.errors, :status => :unprocessable_entity
|
allowed.each do |attr|
|
||||||
|
update_hash[attr] = params[attr] if params.has_key?(attr)
|
||||||
|
end
|
||||||
|
|
||||||
|
if @ars.update_attributes(update_hash, as: :admin)
|
||||||
|
render :json => @ars, :status => :ok
|
||||||
|
else
|
||||||
|
render :json => @ars.errors, :status => :unprocessable_entity
|
||||||
|
end
|
||||||
|
rescue => e
|
||||||
|
render :json => {error: e.message, backtrace: e.backtrace.first(5)}, :status => 500
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# create or update a client_artifact row
|
# create or update a client_artifact row
|
||||||
def get_or_create
|
def get_or_create
|
||||||
name = params[:name]
|
begin
|
||||||
provider = params[:provider]
|
name = params[:name]
|
||||||
active = params[:active]
|
provider = params[:provider]
|
||||||
ip = params[:ip]
|
active = params[:active]
|
||||||
username = params[:username]
|
ip = params[:ip]
|
||||||
password = params[:password]
|
username = params[:username]
|
||||||
topology = params[:topology]
|
password = params[:password]
|
||||||
ars_id = params[:ars_id]
|
topology = params[:topology]
|
||||||
puts "TOPOLOGY #{topology}"
|
ars_id = params[:ars_id]
|
||||||
|
|
||||||
if ars_id
|
# Explicit field-based lookups
|
||||||
ars = Ars.find_by_id_int(ars_id)
|
ars = nil
|
||||||
end
|
ars = JamRuby::Ars.find_by_id_int(ars_id) if ars_id
|
||||||
if ars.nil?
|
ars ||= JamRuby::Ars.find_by_name(name) if name
|
||||||
ars = Ars.new
|
|
||||||
ars.name = name
|
|
||||||
ars.id_int = ars_id if !ars_id.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
ars.provider = provider
|
if ars.nil?
|
||||||
ars.active = active
|
ars = JamRuby::Ars.new
|
||||||
ars.ip = ip
|
ars.name = name
|
||||||
ars.password = password
|
|
||||||
ars.username = username
|
|
||||||
if topology
|
|
||||||
ars.city = topology['city']
|
|
||||||
ars.country = topology['country']
|
|
||||||
ars.continent = topology['continent']
|
|
||||||
ars.latitude = topology['latitude']
|
|
||||||
ars.longitude = topology['longitude']
|
|
||||||
ars.subdivision = topology['subdivision']
|
|
||||||
end
|
|
||||||
ars.save
|
|
||||||
|
|
||||||
@ars = ars
|
|
||||||
unless @ars.errors.any?
|
|
||||||
if ars_id.nil?
|
|
||||||
ars.reload
|
|
||||||
ars_id = ars.id_int
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@ars = Ars.find_by_id_int(ars_id)
|
ars.id_int = ars_id if !ars_id.nil?
|
||||||
render :json => {id_int: @ars.id_int, id: @ars.id, name: @ars.name, provider: @ars.provider, active: @ars.active, ip: @ars.ip}, :status => :ok
|
ars.provider = provider
|
||||||
else
|
ars.active = active
|
||||||
response.status = :unprocessable_entity
|
ars.ip = ip
|
||||||
respond_with @ars
|
ars.password = password
|
||||||
end
|
ars.username = username
|
||||||
|
if topology
|
||||||
|
ars.city = topology['city']
|
||||||
|
ars.country = topology['country']
|
||||||
|
ars.continent = topology['continent']
|
||||||
|
ars.latitude = topology['latitude']
|
||||||
|
ars.longitude = topology['longitude']
|
||||||
|
ars.subdivision = topology['subdivision']
|
||||||
|
end
|
||||||
|
ars.save!
|
||||||
|
|
||||||
|
@ars = ars
|
||||||
|
render :json => {id_int: @ars.id_int, id: @ars.id, name: @ars.name, provider: @ars.provider, active: @ars.active, ip: @ars.ip}, :status => :ok
|
||||||
|
rescue => e
|
||||||
|
render :json => {error: e.message, backtrace: e.backtrace.first(5)}, :status => 500
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
|
|
||||||
module JamRuby
|
module JamRuby
|
||||||
class Ars < ActiveRecord::Base
|
class Ars < ActiveRecord::Base
|
||||||
|
|
||||||
attr_accessible :active, :name, :id_int, :ip, as: :admin
|
attr_accessible :password, :username, :active, :beta, :name, :provider, :id_int, :ip, :port, :continent, :country, :city, :subdivision, :latitude, :longitude, as: :admin
|
||||||
|
|
||||||
self.table_name = "arses"
|
self.table_name = "arses"
|
||||||
@@log = Logging.logger[Ars]
|
@@log = Logging.logger[Ars]
|
||||||
|
|
@ -11,4 +10,4 @@ module JamRuby
|
||||||
Ars.where(active: true, beta: beta).where('ip is not NULL').where("ip != ''").all
|
Ars.where(active: true, beta: beta).where('ip is not NULL').where("ip != ''").all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue