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