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

Standardizing API responses

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