Commit af0b589b authored by John E. Vincent's avatar John E. Vincent

Standardizing API responses

parent e8c7e089
source "http://rubygems.org"
gem "sinatra", "1.1.2"
gem "sinatra-reloader", "0.5.0"
gem "sinatra-namespace", "0.6.1"
gem "ohm", "0.1.3"
gem "ohm-contrib", "0.1.0"
gem "haml", "3.0.25"
gem "sinatra", "= 1.1.2"
gem "sinatra-namespace", "= 0.6.1"
gem "ohm", "= 0.1.3"
gem "ohm-contrib", "= 0.1.0"
gem "haml", "= 3.0.25"
group :development do
gem "sinatra-reloader", "= 0.5.0"
end
platforms :mri do
gem "yajl-ruby", "= 0.7.9", :require => "yajl"
end
platforms :jruby do
gem "json-jruby", "= 1.4.6", :require => "json"
end
......@@ -3,6 +3,9 @@ GEM
specs:
backports (1.18.2)
haml (3.0.25)
json (1.4.6-java)
json-jruby (1.4.6-java)
json (= 1.4.6)
monkey-lib (0.5.4)
backports
nest (1.1.0)
......@@ -29,14 +32,18 @@ GEM
monkey-lib (~> 0.5.0)
sinatra (~> 1.0)
tilt (1.2.1)
yajl-ruby (0.7.9)
PLATFORMS
java
ruby
DEPENDENCIES
haml (= 3.0.25)
json-jruby (= 1.4.6)
ohm (= 0.1.3)
ohm-contrib (= 0.1.0)
sinatra (= 1.1.2)
sinatra-namespace (= 0.6.1)
sinatra-reloader (= 0.5.0)
yajl-ruby (= 0.7.9)
require 'sinatra/reloader' if development?
require 'ohm'
require 'json'
begin
require 'yajl'
rescue LoadError
require 'json'
end
require 'haml'
require File.join(File.dirname(__FILE__), 'config/db')
......@@ -168,16 +172,18 @@ namespace "/a" do
end
put '/:appname/?' do |appname|
app = Application.find(:name => appname).first ||= Application.create(:name => appname)
required_params = ["name"]
data = JSON.parse(request.body.read)
app.name = data['name']
if data.keys.sort == required_params.sort && data['name'] == appname
app = Application.find_or_create(:name => appname)
else
raise "Missing or invalid parameters"
end
if app.valid?
if app.save
r = {"result" => "success","id" => "#{app.id}"}
r.to_json
else
raise "#{app.errors}"
end
action = app.is_new? ? "create" : "update"
app.save
r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name }
r.to_json
else
raise "#{app.errors}"
end
......@@ -257,8 +263,10 @@ namespace '/c' do
data = JSON.parse(request.body.read)
data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
if config.valid?
action = config.is_new? ? "create" : "update"
dependency_action = app.is_new? ? "created" : "updated"
config.save
r = {"result" => "success","id" => "#{config.id}"}
r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
r.to_json
else
raise "#{config.errors}"
......
require "bundler/setup"
require 'ohm'
require 'json'
begin
require 'yajl'
rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'models')
......
......@@ -25,6 +25,10 @@ class Host < Ohm::Model
super.merge(:name => name, :status => status, :updated_at => updated_at, :services => arr)
end
def is_new?
self.created_at == self.updated_at
end
class << self
def find_or_create(opts = {})
begin
......@@ -67,6 +71,10 @@ class Service < Ohm::Model
def to_hash
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => Host[host_id].name)
end
def is_new?
self.created_at == self.updated_at
end
end
class Configuration < Ohm::Model
......@@ -78,6 +86,7 @@ class Configuration < Ohm::Model
attribute :name
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :name
......@@ -93,10 +102,18 @@ class Configuration < Ohm::Model
super.merge(:name => name, :format => format, :body => body, :update_at => updated_at, :application => Application[application_id].name)
end
def is_new?
self.created_at == self.updated_at
end
class << self
def find_or_create(opts={})
begin
find(opts).first.nil? ? (conf = create(opts)) : (conf = find(opts).first)
if find(opts).first.nil?
conf = create(opts)
else
conf = find(opts).first
end
rescue Exception => e
e.message
end
......@@ -126,6 +143,10 @@ class Application < Ohm::Model
super.merge(:name => name, :updated_at => updated_at, :configurations => arr)
end
def is_new?
self.created_at == self.updated_at
end
class << self
def find_or_create(opts = {})
begin
......
require 'ohm'
require 'json'
begin
require 'yajl'
rescue LoadError
require 'json'
end
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'models')
......
<%= {:status => "success"}.merge(api_call_results).to_json %>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment