Commit 898db489 authored by John E. Vincent's avatar John E. Vincent

Merge branch 'issue-7'

parents 897b5e3b f899b568
......@@ -62,10 +62,15 @@ module Noah
end
class Configurations
def self.all(options = {})
def self.all(options = {}, short=false)
short_keys = [:format, :created_at, :updated_at]
config_hash = Hash.new
options.empty? ? configs=Configuration.all.sort : configs=Configuration.find(options).sort
configs.each {|x| config_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name} }
if short == false
configs.each {|x| config_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name} }
else
configs.each {|x| config_hash["#{x.name}"] = x.to_hash.select {|k,v| k if short_keys.include?(k)} }
end
config_hash
end
end
......
......@@ -14,15 +14,21 @@ class Noah::App
c = Noah::Configuration.find(:name => configname).first
(halt 404) if c.nil?
content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
#response.headers['Content-Disposition'] = "attachment; filename=#{configname}"
c.body
end
# GET all configurations
get '/configurations/?' do
configs = Noah::Configurations.all.to_hash
params[:short] ||= false
configs = Noah::Configurations.all({},params[:short])
(halt 404) if configs.size == 0
configs.each do |config, values|
u = request.url.gsub(/(\/\?short=true|\?short=true)/,'/'+config)
values.merge!({:location=>u}) if params[:short]
end
configs.to_json
end
# Add configuration object to a custom link hierarchy
put '/configurations/:configname/link' do |configname|
required_params = ["link_name"]
......
......@@ -68,6 +68,17 @@ describe "Using the Configuration Model", :reset_redis => true do
c.has_key?(a.name).should == true
c.has_key?(b.name).should == true
end
it "return all Configurations in short form" do
a = Noah::Configuration.find_or_create(@appconf_string)
b = Noah::Configuration.find_or_create(@appconf_json)
c = Noah::Configurations.all({},true)
c.class.to_s.should == 'Hash'
c.size.should == 2
c.has_key?(a.name).should == true
c.has_key?(b.name).should == true
c.each {|k,v| v.keys.map {|k| k.to_s}.sort.should == ['created_at','format','updated_at']}
end
end
describe "should not" do
......
......@@ -41,6 +41,35 @@ describe "Using the Configuration API", :reset_redis => true, :populate_sample_d
end
end
end
it "all configurations in short form with no trailing slash" do
get '/configurations?short=true'
last_response.should be_ok
response = last_response.should return_json
response.keys.size.should == 3
%w[redis_url json_config yaml_config].each do |c|
response.keys.member?(c).should == true
%w[format location created_at updated_at].each do |ck|
response[c].keys.member?(ck).should == true
end
end
response.each {|k,v| v['location'].should =~ /^http:\/\/.*\/configurations\/#{k}/}
end
it "all configurations in short form with trailing slash" do
get '/configurations/?short=true'
last_response.should be_ok
response = last_response.should return_json
response.keys.size.should == 3
%w[redis_url json_config yaml_config].each do |c|
response.keys.member?(c).should == true
%w[format location created_at updated_at].each do |ck|
response[c].keys.member?(ck).should == true
end
end
response.each {|k,v| v['location'].should =~ /^http:\/\/.*\/configurations\/#{k}/}
end
it "named configuration should work as JSON" do
header "Accept", "application/json"
get '/configurations/redis_url'
......
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