module LC

Public Class Methods

client() click to toggle source
# File lib/leancloud/client.rb, line 152
def client
  raise LCError, "API not initialized" if !@@client
  @@client
end
destroy() click to toggle source

Used mostly for testing. Lets you delete the api key global vars.

# File lib/leancloud/client.rb, line 147
def destroy
  @@client = nil
  self
end
get(class_name, object_id = nil) click to toggle source

Perform a simple retrieval of a simple object, or all objects of a given class. If object_id is supplied, a single object will be retrieved. If object_id is not supplied, then all objects of the given class will be retrieved and returned in an Array.

# File lib/leancloud/client.rb, line 161
def get(class_name, object_id = nil)
  data = self.client.get( Protocol.class_uri(class_name, object_id) )
  self.parse_json class_name, data
rescue LCProtocolError => e
  if e.code == Protocol::ERROR_OBJECT_NOT_FOUND_FOR_GET
    e.message += ": #{class_name}:#{object_id}"
  end
  raise
end
init(data = {}, &blk) click to toggle source

Initialize the singleton instance of Client which is used by all API methods. LC.init must be called before saving or retrieving any objects.

# File lib/leancloud/client.rb, line 127
def init(data = {}, &blk)
  defaults = {:application_id => ENV["LC_APPLICATION_ID"], :api_key => ENV["LC_APPLICATION_KEY"]}
  defaults.merge!(data)

  # use less permissive key if both are specified
  defaults[:master_key] = ENV["PARSE_MASTER_API_KEY"] unless data[:master_key] || defaults[:api_key]
  @@client = Client.new(defaults, &blk)
end
init_from_cloud_code(path = "../config/global.json") click to toggle source

A convenience method for using global.json

# File lib/leancloud/client.rb, line 137
def init_from_cloud_code(path = "../config/global.json")
  # warning: toplevel constant File referenced by LC::Object::File
  global = JSON.parse(Object::File.open(path).read)
  application_name  = global["applications"]["_default"]["link"]
  application_id    = global["applications"][application_name]["applicationId"]
  master_key        = global["applications"][application_name]["masterKey"]
  self.init(:application_id => application_id, :master_key => master_key)
end
object_pointer_equality?(a, b) click to toggle source
# File lib/leancloud/util.rb, line 76
def LC.object_pointer_equality?(a, b)
  classes = [LC::Object, LC::Pointer]
  return false unless classes.any? { |c| a.kind_of?(c) } && classes.any? { |c| b.kind_of?(c) }
  return true if a.equal?(b)
  return false if a.new? || b.new?

  a.class_name == b.class_name && a.id == b.id
end
object_pointer_hash(v) click to toggle source
# File lib/leancloud/util.rb, line 85
def LC.object_pointer_hash(v)
  if v.new?
    v.object_id
  else
    v.class_name.hash ^ v.id.hash
  end
end
parse_datatype(obj) click to toggle source
# File lib/leancloud/util.rb, line 35
def LC.parse_datatype(obj)
  type = obj[Protocol::KEY_TYPE]

  case type
    when Protocol::TYPE_POINTER
      if obj[Protocol::KEY_CREATED_AT]
        LC::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
      else
        LC::Pointer.new obj
      end
    when Protocol::TYPE_BYTES
      LC::Bytes.new obj
    when Protocol::TYPE_DATE
      LC::Date.new obj
    when Protocol::TYPE_GEOPOINT
      LC::GeoPoint.new obj
    when Protocol::TYPE_FILE
      LC::File.new obj
    when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post"
      LC::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
  end
end
parse_json(class_name, obj) click to toggle source

Parse a JSON representation into a fully instantiated class. obj can be either a primitive or a Hash of primitives as parsed by JSON.parse @param class_name [Object] @param obj [Object]

# File lib/leancloud/util.rb, line 9
def LC.parse_json(class_name, obj)
  if obj.nil?
    nil

  # Array
  elsif obj.is_a? Array
    obj.collect { |o| parse_json(class_name, o) }

  # Hash
  elsif obj.is_a? Hash

    # If it's a datatype hash
    if obj.has_key?(Protocol::KEY_TYPE)
      parse_datatype obj
    elsif class_name # otherwise it must be a regular object, so deep parse it avoiding re-JSON.parsing raw Strings
      LC::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
    else # plain old hash
      obj
    end

  # primitive
  else
    obj
  end
end
pointerize_value(obj) click to toggle source
# File lib/leancloud/util.rb, line 58
def LC.pointerize_value(obj)
  if obj.kind_of?(LC::Object)
    p = obj.pointer
    raise ArgumentError.new("new object used in context requiring pointer #{obj}") unless p
    p
  elsif obj.is_a?(Array)
    obj.map do |v|
      LC.pointerize_value(v)
    end
  elsif obj.is_a?(Hash)
    Hash[obj.map do |k, v|
      [k, LC.pointerize_value(v)]
    end]
  else
    obj
  end
end