class Esapiserver::Server

Constants

POOL_SIZE
TIMEOUT

Public Instance Methods

findOne(model, id) click to toggle source

Utility method - find one and the sreialize to Ember Friendly JSON

# File lib/esapiserver.rb, line 185
def findOne(model, id)
  result = $db.collection(model).find_one(toBsonId(id))
  jsonArray = []
  if result != nil
    normalizedResult = fromBsonId(result, model).to_json
    hash = JSON.parse(normalizedResult)
    jsonArray << hash[modelName(model)]
    newJson = {modelName(params[:model]) => jsonArray}
  newJson.to_json
  else
    noResults = {modelName(model) => jsonArray}
  noResults.to_json
  end
end
fromBsonId(obj, model) click to toggle source

Extract the BSON id, then replacing the ‘_id’ key with a ‘id’ key

# File lib/esapiserver.rb, line 165
def fromBsonId(obj, model)
  id = obj['_id'].to_s
  obj.delete("_id")
  obj.each{|t| t[1]['id'] = id}
end
modelName(model) click to toggle source

Very crude method to singularize the model name.

# File lib/esapiserver.rb, line 203
def modelName(model)
  #model.chomp("s")
  model.singularize
end
serializeJSON(json, model) click to toggle source

Serialize the Mongo JSON to Ember friendly JSON

# File lib/esapiserver.rb, line 174
def serializeJSON(json, model)
  hash = JSON.parse(json)
  jsonArray = []
  hash.each {|h| jsonArray << h[modelName(params[:model])]}
  newJson = {modelName(params[:model]) => jsonArray}
  newJson.to_json
end
toBsonId(id) click to toggle source

Convert the id to a BSON object id

# File lib/esapiserver.rb, line 158
def toBsonId(id)
  BSON::ObjectId.from_string(id)
end