Module: AV
- Defined in:
- lib/leancloud/push.rb,
lib/leancloud/user.rb,
lib/leancloud/util.rb,
lib/leancloud/batch.rb,
lib/leancloud/cloud.rb,
lib/leancloud/error.rb,
lib/leancloud/model.rb,
lib/leancloud/query.rb,
lib/leancloud/client.rb,
lib/leancloud/object.rb,
lib/leancloud/protocol.rb,
lib/leancloud/datatypes.rb,
lib/leancloud/application.rb,
lib/leancloud/installation.rb
Defined Under Namespace
Modules: Cloud, Protocol Classes: AVError, AVProtocolError, AVProtocolRetry, Application, ArrayOp, Batch, Bytes, Client, Date, File, GeoPoint, Increment, Installation, Model, Object, Pointer, Push, Query, User
Class Method Summary collapse
- .client ⇒ Object
-
.destroy ⇒ Object
Used mostly for testing.
-
.get(class_name, object_id = nil) ⇒ Object
Perform a simple retrieval of a simple object, or all objects of a given class.
-
.init(data = {}, &blk) ⇒ Object
Initialize the singleton instance of Client which is used by all API methods.
-
.init_from_cloud_code(path = "../config/global.json") ⇒ Object
A convenience method for using global.json.
- .object_pointer_equality?(a, b) ⇒ Boolean
- .object_pointer_hash(v) ⇒ Object
- .parse_datatype(obj) ⇒ Object
-
.parse_json(class_name, obj) ⇒ Object
Parse a JSON representation into a fully instantiated class.
- .pointerize_value(obj) ⇒ Object
Class Method Details
.client ⇒ Object
128 129 130 131 |
# File 'lib/leancloud/client.rb', line 128 def client raise AVError, "API not initialized" if !@@client @@client end |
.destroy ⇒ Object
Used mostly for testing. Lets you delete the api key global vars.
123 124 125 126 |
# File 'lib/leancloud/client.rb', line 123 def destroy @@client = nil self end |
.get(class_name, object_id = nil) ⇒ Object
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.
137 138 139 140 141 142 143 144 145 |
# File 'lib/leancloud/client.rb', line 137 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 AVProtocolError => e if e.code == Protocol::ERROR_OBJECT_NOT_FOUND_FOR_GET e. += ": #{class_name}:#{object_id}" end raise end |
.init(data = {}, &blk) ⇒ Object
Initialize the singleton instance of Client which is used by all API methods. AV.init must be called before saving or retrieving any objects.
103 104 105 106 107 108 109 110 |
# File 'lib/leancloud/client.rb', line 103 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") ⇒ Object
A convenience method for using global.json
113 114 115 116 117 118 119 120 |
# File 'lib/leancloud/client.rb', line 113 def init_from_cloud_code(path = "../config/global.json") # warning: toplevel constant File referenced by AV::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) ⇒ Boolean
77 78 79 80 81 82 83 84 |
# File 'lib/leancloud/util.rb', line 77 def AV.object_pointer_equality?(a, b) classes = [AV::Object, AV::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) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/leancloud/util.rb', line 86 def AV.object_pointer_hash(v) if v.new? v.object_id else v.class_name.hash ^ v.id.hash end end |
.parse_datatype(obj) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/leancloud/util.rb', line 36 def AV.parse_datatype(obj) type = obj[Protocol::KEY_TYPE] case type when Protocol::TYPE_POINTER if obj[Protocol::KEY_CREATED_AT] AV::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}] else AV::Pointer.new obj end when Protocol::TYPE_BYTES AV::Bytes.new obj when Protocol::TYPE_DATE AV::Date.new obj when Protocol::TYPE_GEOPOINT AV::GeoPoint.new obj when Protocol::TYPE_FILE AV::File.new obj when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post" AV::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}] end end |
.parse_json(class_name, obj) ⇒ Object
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
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/leancloud/util.rb', line 10 def AV.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 AV::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) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/leancloud/util.rb', line 59 def AV.pointerize_value(obj) if obj.kind_of?(AV::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| AV.pointerize_value(v) end elsif obj.is_a?(Hash) Hash[obj.map do |k, v| [k, AV.pointerize_value(v)] end] else obj end end |