Permalink
Browse files

Use crack gem to parse JSON

Change-Id: Icb1e70f8714b2ed56ce11f7cecad7908976daa64
  • Loading branch information...
1 parent 151fe06 commit 83bb4e696676eaf6967dbe0cfe4ca5eb994035a0 Jennifer Hickey committed Dec 8, 2011
Showing with 48 additions and 55 deletions.
  1. +1 −1 cfruntime/cf-runtime.gemspec
  2. +4 −4 cfruntime/lib/cfruntime/properties.rb
  3. +43 −50 cfruntime/spec/spec_helper.rb
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.extra_rdoc_files = ["LICENSE"]
- s.add_dependency "json_pure", "~> 1.6.1"
+ s.add_dependency "crack", "~> 0.3.1"
s.add_development_dependency "rake", "~> 0.9.2"
s.add_development_dependency "rcov", "~> 0.9.10"
@@ -2,7 +2,7 @@
module CFRuntime
- require 'json/pure'
+ require 'crack/json'
require 'uri'
class CloudApp
@@ -23,7 +23,7 @@ def port
def service_props(service_name)
registered_svcs = {}
if ENV['VCAP_SERVICES']
- svcs = JSON.parse(ENV['VCAP_SERVICES'])
+ svcs = Crack::JSON.parse(ENV['VCAP_SERVICES'])
else
svcs = {}
end
@@ -81,7 +81,7 @@ def service_props(service_name)
def service_names
service_names = []
- JSON.parse(ENV['VCAP_SERVICES']).each do |key,list|
+ Crack::JSON.parse(ENV['VCAP_SERVICES']).each do |key,list|
list.each do |svc|
service_names << svc["name"]
end
@@ -91,7 +91,7 @@ def service_names
def service_names_of_type(type)
service_names = []
- JSON.parse(ENV['VCAP_SERVICES']).each do |key,list|
+ Crack::JSON.parse(ENV['VCAP_SERVICES']).each do |key,list|
label, version = key.split('-')
list.each do |svc|
if label == type
@@ -9,7 +9,7 @@
require 'bundler/setup'
require 'rubygems'
require 'rspec'
-require 'json/pure'
+
module CFRuntime
module Test
@@ -26,27 +26,37 @@ def self.port
end
def with_vcap_application
- vcap_app = {"instance_id"=>"#{secure_uuid}",
- "instance_index"=>0,
- "name"=>"tr_env",
- "uris"=>["tr_env.cloudfoundry.com"],
- "users"=>["trisberg@vmware.com"],
- "version"=>"#{secure_uuid}",
- "start"=>"2011-11-05 13:29:32 +0000",
- "runtime"=>"ruby19",
- "state_timestamp"=>1320499772,
- "port"=>SOME_PORT,
- "limits"=>{"fds"=>256,"mem"=>134217728,"disk"=>2147483648},
- "host"=>"#{SOME_SERVER}"}
- ENV['VCAP_APPLICATION'] = JSON vcap_app
+ vcap_app = '{"instance_id":"#{secure_uuid}",
+ "instance_index": 0,
+ "name":"tr_env",
+ "uris":["tr_env.cloudfoundry.com"],
+ "users":["trisberg@vmware.com"],
+ "version":"#{secure_uuid}",
+ "start":"2011-11-05 13:29:32 +0000",
+ "runtime":"ruby19",
+ "state_timestamp":1320499772,
+ "port":SOME_PORT,
+ "limits":{"fds":256,"mem":134217728,"disk":2147483648},
+ "host":"#{SOME_SERVER}"}'
+ ENV['VCAP_APPLICATION'] = vcap_app
ENV['VCAP_APP_HOST'] = SOME_SERVER
ENV['VCAP_APP_PORT'] = "#{SOME_PORT}"
yield
end
def with_vcap_services(services)
- # with_vcap_application
- ENV['VCAP_SERVICES'] = JSON services
+ vcap_svcs = '{'
+ services.each {|type, svcs|
+ vcap_svcs = vcap_svcs + '"' + type + '":['
+ svcs.each do |svc|
+ vcap_svcs = vcap_svcs + svc + ','
+ end
+ vcap_svcs.chomp!(',')
+ vcap_svcs = vcap_svcs + '],'
+ }
+ vcap_svcs.chomp!(',')
+ vcap_svcs = vcap_svcs + '}'
+ ENV['VCAP_SERVICES'] = vcap_svcs
yield
end
@@ -68,27 +78,18 @@ def rabbit_version
end
def create_mongo_service(name)
- vcap_svc = create_service(name, "mongodb", mongo_version)
- vcap_svc["credentials"]["db"] = "db"
- vcap_svc
+ create_service(name, "mongodb", mongo_version,"db")
end
def create_redis_service(name)
create_service(name, "redis", redis_version)
end
def create_rabbit_service(name, vhost=nil)
- {"name"=>"#{name}",
- "label"=>"rabbitmq-#{rabbit_version}",
- "plan"=>"free",
- "tags"=>["rabbitmq","rabbitmq-#{rabbit_version}"],
- "credentials"=>{
- "hostname"=>"#{SOME_SERVER}",
- "port"=>25046,
- "user"=>"#{secure_uuid}",
- "pass"=>"#{secure_uuid}",
- "vhost"=>"#{vhost}"}
- }
+ "{\"name\":\"#{name}\",\"label\":\"rabbitmq-#{rabbit_version}\"," +
+ "\"plan\":\"free\",\"tags\":[\"rabbitmq\",\"rabbitmq-#{rabbit_version}\"]," +
+ "\"credentials\":{\"hostname\":\"#{SOME_SERVER}\",\"port\":25046,\"user\":\"#{secure_uuid}\"," +
+ "\"pass\":\"#{secure_uuid}\",\"vhost\":\"#{vhost}\"}}"
end
def create_rabbit_srs_service(name, vhost=nil)
@@ -97,28 +98,20 @@ def create_rabbit_srs_service(name, vhost=nil)
else
url = "amqp://#{secure_uuid}:#{secure_uuid}@#{SOME_SERVER}:25046"
end
- {"name"=>"#{name}",
- "label"=>"rabbitmq-#{rabbit_version}",
- "plan"=>"free",
- "tags"=>["rabbitmq","rabbitmq-#{rabbit_version}"],
- "credentials"=>{
- "url"=>url}
- }
+ "{\"name\":\"#{name}\",\"label\":\"rabbitmq-#{rabbit_version}\"," +
+ "\"plan\":\"free\",\"tags\":[\"rabbitmq\",\"rabbitmq-#{rabbit_version}\"]," +
+ "\"credentials\":{\"url\":\"#{url}\"}}"
end
- def create_service(name, type, version)
- {"name"=>"#{name}",
- "label"=>"#{type}-#{version}",
- "plan"=>"free",
- "tags"=>["#{type}","#{type}-#{version}"],
- "credentials"=>{
- "hostname"=>"#{SOME_SERVER}",
- "host"=>"#{SOME_SERVER}",
- "port"=>25046,
- "username"=>"#{secure_uuid}",
- "password"=>"#{secure_uuid}",
- "name"=>"#{secure_uuid}"}
- }
+ def create_service(name, type, version, db=nil)
+ svc = "{\"name\":\"#{name}\",\"label\":\"#{type}-#{version}\",\"plan\":\"free\"," +
+ "\"tags\":[\"#{type}\",\"#{type}-#{version}\"],\"credentials\":{\"hostname\":\"#{SOME_SERVER}\"," +
+ "\"host\":\"#{SOME_SERVER}\",\"port\":25046,\"username\":\"#{secure_uuid}\",\"password\":\"#{secure_uuid}\"," +
+ "\"name\":\"#{secure_uuid}\""
+ if db
+ svc = svc + ", \"db\":\"#{db}\""
+ end
+ svc = svc + '}}'
end
end
end

0 comments on commit 83bb4e6

Please sign in to comment.