Commit 293c86ff authored by John E. Vincent's avatar John E. Vincent

converting applications json to hashes

parent 709e6bf3
......@@ -65,23 +65,15 @@ class Noah::App
if app.nil?
halt 404
else
# configs are no longer tied to apps. remove the cascade delete
# configurations = []
# Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete
# r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}"}
r.to_json
end
end
get '/applications/?' do
apps = []
Noah::Application.all.sort.each {|a| apps << a.to_hash}
if apps.empty?
halt 404
else
apps.to_json
end
apps = Noah::Applications.all
(halt 404) if apps.size == 0
apps.to_json
end
end
......@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'configurations')
module Noah
class Application < Model
include Taggable
include Linkable
attribute :name
set :configurations, Configuration
......@@ -14,9 +15,11 @@ module Noah
end
def to_hash
arr = []
self.configurations.sort.each {|c| arr << c.to_hash} if self.configurations.size != 0
super.merge(:name => name, :configurations => arr, :created_at => created_at, :updated_at => updated_at)
cfg_hash = Hash.new
configurations.sort.each do |cfg|
cfg_hash["#{cfg.name}"] = {:format => cfg.to_hash[:format], :body => cfg.to_hash[:body]}
end
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => cfg_hash)
end
class << self
......@@ -37,7 +40,10 @@ module Noah
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
app_hash = Hash.new
options.empty? ? apps=Application.all.sort : apps=Application.find(options).sort
apps.each {|x| app_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name} }
app_hash
end
end
end
......@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'applications')
module Noah
class Configuration < Model
include Taggable
include Linkable
attribute :name
attribute :format
attribute :body
......
......@@ -61,8 +61,8 @@ describe "Using the Application Model", :reset_redis => true do
b = Noah::Application.create(@appdata2)
c = Noah::Applications.all
c.size.should == 2
c.member?(a).should == true
c.member?(b).should == true
c.has_key?(a.name).should == true
c.has_key?(b.name).should == true
end
end
......
......@@ -14,20 +14,23 @@ describe "Using the Application API", :reset_redis => false do
get '/applications'
last_response.should be_ok
response = last_response.should return_json
response.is_a?(Array).should == true
response.is_a?(Hash).should == true
end
it "named application should work" do
get '/applications/rspec_sample_app'
last_response.should be_ok
response = last_response.should return_json
response["id"].should == @a.id
response["name"].should == @a.name
c = response["configurations"].first
c["id"].should == @c.id
c["name"].should == @c.name
c["body"].should == @c.body
c["format"].should == @c.format
response["id"].should == @a.id.to_s
response["name"].should == @a.name
response.has_key?("configurations").should == true
c = response["configurations"]
c.has_key?(@c.name).should == true
p @c
p c
c["#{@c.name}"]["format"].should == "#{@c.format}"
c["#{@c.name}"]["body"].should == "#{@c.body}"
end
it "named configuration for application should work" do
get "/applications/#{@a.name}/#{@c.name}"
......
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