leancloud-ruby-client 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
1
  # -*- encoding : utf-8 -*-
2
- module AV
3
- class Model < AV::Object
2
+ module LC
3
+ class Model < LC::Object
4
4
 
5
5
  def initialize(data=nil)
6
6
  super(self.class.to_s,data)
7
7
  end
8
8
 
9
9
  def self.find(object_id)
10
- self.new AV::Query.new(self.to_s).eq('objectId',object_id).first
10
+ self.new LC::Query.new(self.to_s).eq('objectId',object_id).first
11
11
  end
12
12
 
13
13
  end
@@ -3,7 +3,7 @@ require 'leancloud/protocol'
3
3
  require 'leancloud/client'
4
4
  require 'leancloud/error'
5
5
 
6
- module AV
6
+ module LC
7
7
 
8
8
  # Represents an individual Parse API object.
9
9
  class Object < Hash
@@ -22,13 +22,13 @@ module AV
22
22
  end
23
23
 
24
24
  def eql?(other)
25
- AV.object_pointer_equality?(self, other)
25
+ LC.object_pointer_equality?(self, other)
26
26
  end
27
27
 
28
28
  alias == eql?
29
29
 
30
30
  def hash
31
- AV.object_pointer_hash(self)
31
+ LC.object_pointer_hash(self)
32
32
  end
33
33
 
34
34
  def uri
@@ -36,7 +36,7 @@ module AV
36
36
  end
37
37
 
38
38
  def pointer
39
- AV::Pointer.new(rest_api_hash) unless new?
39
+ LC::Pointer.new(rest_api_hash) unless new?
40
40
  end
41
41
 
42
42
  # make it easier to deal with the ambiguity of whether you're passed a pointer or object
@@ -67,14 +67,14 @@ module AV
67
67
  body = safe_hash.to_json
68
68
  uri = self.uri
69
69
  uri = uri + '?new=true' if method == :put
70
- data = AV.client.request(uri, method, body)
70
+ data = LC.client.request(uri, method, body)
71
71
 
72
72
  if data
73
73
  # array operations can return mutated view of array which needs to be parsed
74
- parse AV.parse_json(class_name, data)
74
+ parse LC.parse_json(class_name, data)
75
75
  end
76
76
 
77
- if @class_name == AV::Protocol::CLASS_USER
77
+ if @class_name == LC::Protocol::CLASS_USER
78
78
  self.delete("password")
79
79
  self.delete(:username)
80
80
  self.delete(:password)
@@ -93,14 +93,14 @@ module AV
93
93
  elsif value.nil?
94
94
  [key, Protocol::DELETE_OP]
95
95
  else
96
- [key, AV.pointerize_value(value)]
96
+ [key, LC.pointerize_value(value)]
97
97
  end
98
98
  end.compact]
99
99
  end
100
100
 
101
101
  # full REST api representation of object
102
102
  def rest_api_hash
103
- self.merge(AV::Protocol::KEY_CLASS_NAME => class_name)
103
+ self.merge(LC::Protocol::KEY_CLASS_NAME => class_name)
104
104
  end
105
105
 
106
106
  # Handle the addition of Array#to_h in Ruby 2.1
@@ -132,7 +132,7 @@ module AV
132
132
  # values from the API.
133
133
  def refresh
134
134
  if @parse_object_id
135
- data = AV.get @class_name, @parse_object_id
135
+ data = LC.get @class_name, @parse_object_id
136
136
  clear
137
137
  if data
138
138
  parse data
@@ -145,7 +145,7 @@ module AV
145
145
  # Delete the remote Parse API object.
146
146
  def parse_delete
147
147
  if @parse_object_id
148
- response = AV.client.delete self.uri
148
+ response = LC.client.delete self.uri
149
149
  end
150
150
 
151
151
  self.clear
@@ -181,8 +181,8 @@ module AV
181
181
  # return nil
182
182
  #end
183
183
 
184
- body = {field => AV::Increment.new(amount)}.to_json
185
- data = AV.client.request(self.uri() + '?new=true', :put, body)
184
+ body = {field => LC::Increment.new(amount)}.to_json
185
+ data = LC.client.request(self.uri() + '?new=true', :put, body)
186
186
  parse data
187
187
  self
188
188
  end
@@ -219,7 +219,7 @@ module AV
219
219
  k = k.to_s
220
220
  end
221
221
 
222
- if k != AV::Protocol::KEY_TYPE
222
+ if k != LC::Protocol::KEY_TYPE
223
223
  self[k] = v
224
224
  end
225
225
  end
@@ -233,7 +233,7 @@ module AV
233
233
  if @parse_object_id
234
234
  @op_fields[field] ||= ArrayOp.new(operation, [])
235
235
  raise "only one operation type allowed per array #{field}" if @op_fields[field].operation != operation
236
- @op_fields[field].objects << AV.pointerize_value(value)
236
+ @op_fields[field].objects << LC.pointerize_value(value)
237
237
  end
238
238
 
239
239
  # parse doesn't return column values on initial POST creation so we must maintain them ourselves
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
- module AV
2
+ module LC
3
3
  # A module which encapsulates the specifics of Parse's REST API.
4
4
  module Protocol
5
5
 
@@ -17,18 +17,22 @@ module AV
17
17
 
18
18
  # The HTTP header used for passing your application ID to the
19
19
  # Parse API.
20
- HEADER_APP_ID = "X-AVOSCloud-Application-Id"
20
+ HEADER_APP_ID = "X-LC-Id"
21
+
22
+ # The HTTP header used for passing your API Sign to the
23
+ # Parse API.
24
+ HEADER_API_KEY = "X-LC-Sign"
21
25
 
22
26
  # The HTTP header used for passing your API key to the
23
27
  # Parse API.
24
- HEADER_API_KEY = "X-AVOSCloud-Application-Key"
28
+ #HEADER_API_KEY = "X-LC-Key"
25
29
 
26
30
  # The HTTP header used for passing your API Master key to the
27
31
  # Parse API.
28
- HEADER_MASTER_KEY = "X-AVOSCloud-Master-Key"
32
+ #HEADER_MASTER_KEY = "X-LC-Master"
29
33
 
30
34
  # The HTTP header used for passing your authenticated session
31
- HEADER_SESSION_TOKEN = "X-AVOSCloud-Session-Token"
35
+ HEADER_SESSION_TOKEN = "X-LC-Session"
32
36
 
33
37
  # JSON Keys
34
38
  # ----------------------------------------
@@ -119,6 +123,7 @@ module AV
119
123
  CLASS_INSTALLATION = "_Installation"
120
124
 
121
125
  USER_LOGIN_URI = "/#{VERSION}/login"
126
+ USER_CURRENT_URI = "/#{VERSION}/users/me"
122
127
  PASSWORD_RESET_URI = "/#{VERSION}/requestPasswordReset"
123
128
  SMS_URI = "/#{VERSION}/requestSmsCode"
124
129
 
@@ -2,7 +2,7 @@
2
2
  require 'cgi'
3
3
  require 'leancloud/error'
4
4
 
5
- module AV
5
+ module LC
6
6
  class Push
7
7
  attr_accessor :channels
8
8
  attr_accessor :channel
@@ -40,7 +40,7 @@ module AV
40
40
  body.merge!({ :type => @type }) if @type
41
41
  body.merge!({ :prod => 'dev' }) if not @production
42
42
 
43
- response = AV.client.request uri, :post, body.to_json, nil
43
+ response = LC.client.request uri, :post, body.to_json, nil
44
44
  end
45
45
 
46
46
  end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'cgi'
3
3
 
4
- module AV
4
+ module LC
5
5
 
6
6
  class Query
7
7
  attr_accessor :where
@@ -16,18 +16,18 @@ module AV
16
16
  def self.do_cloud_query(cql, pvalues=[])
17
17
  uri = Protocol.cql_uri
18
18
  query = { "cql" => cql,"pvalues"=> pvalues.to_json }
19
- AV.client.logger.info{"Leancloud query for #{uri} #{query.inspect}"} unless AV.client.quiet
20
- response = AV.client.request uri, :get, nil, query
19
+ LC.client.logger.info{"Leancloud query for #{uri} #{query.inspect}"} unless LC.client.quiet
20
+ response = LC.client.request uri, :get, nil, query
21
21
 
22
22
  if response.is_a?(Hash) && response.has_key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array)
23
23
  class_name = response[Protocol::KEY_CLASS_NAME]
24
- parsed_results = response[Protocol::KEY_RESULTS].map{|o| AV.parse_json(class_name, o)}
24
+ parsed_results = response[Protocol::KEY_RESULTS].map{|o| LC.parse_json(class_name, o)}
25
25
  return {
26
26
  count: response['count'],
27
27
  results: parsed_results
28
28
  }
29
29
  else
30
- raise AVError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}")
30
+ raise LCError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}")
31
31
  end
32
32
  end
33
33
 
@@ -67,12 +67,12 @@ module AV
67
67
  end
68
68
 
69
69
  def eq_pair(field, value)
70
- add_constraint field, AV.pointerize_value(value)
70
+ add_constraint field, LC.pointerize_value(value)
71
71
  self
72
72
  end
73
73
 
74
74
  def not_eq(field, value)
75
- add_constraint field, { "$ne" => AV.pointerize_value(value) }
75
+ add_constraint field, { "$ne" => LC.pointerize_value(value) }
76
76
  self
77
77
  end
78
78
 
@@ -82,42 +82,42 @@ module AV
82
82
  end
83
83
 
84
84
  def less_than(field, value)
85
- add_constraint field, { "$lt" => AV.pointerize_value(value) }
85
+ add_constraint field, { "$lt" => LC.pointerize_value(value) }
86
86
  self
87
87
  end
88
88
 
89
89
  def less_eq(field, value)
90
- add_constraint field, { "$lte" => AV.pointerize_value(value) }
90
+ add_constraint field, { "$lte" => LC.pointerize_value(value) }
91
91
  self
92
92
  end
93
93
 
94
94
  def greater_than(field, value)
95
- add_constraint field, { "$gt" => AV.pointerize_value(value) }
95
+ add_constraint field, { "$gt" => LC.pointerize_value(value) }
96
96
  self
97
97
  end
98
98
 
99
99
  def greater_eq(field, value)
100
- add_constraint field, { "$gte" => AV.pointerize_value(value) }
100
+ add_constraint field, { "$gte" => LC.pointerize_value(value) }
101
101
  self
102
102
  end
103
103
 
104
104
  def value_in(field, values)
105
- add_constraint field, { "$in" => values.map { |v| AV.pointerize_value(v) } }
105
+ add_constraint field, { "$in" => values.map { |v| LC.pointerize_value(v) } }
106
106
  self
107
107
  end
108
108
 
109
109
  def value_not_in(field, values)
110
- add_constraint field, { "$nin" => values.map { |v| AV.pointerize_value(v) } }
110
+ add_constraint field, { "$nin" => values.map { |v| LC.pointerize_value(v) } }
111
111
  self
112
112
  end
113
113
 
114
114
  def contains_all(field, values)
115
- add_constraint field, { "$all" => values.map { |v| AV.pointerize_value(v) } }
115
+ add_constraint field, { "$all" => values.map { |v| LC.pointerize_value(v) } }
116
116
  self
117
117
  end
118
118
 
119
119
  def related_to(field,value)
120
- h = {"object" => AV.pointerize_value(value), "key" => field}
120
+ h = {"object" => LC.pointerize_value(value), "key" => field}
121
121
  add_constraint("$relatedTo", h )
122
122
  end
123
123
 
@@ -127,7 +127,7 @@ module AV
127
127
  end
128
128
 
129
129
  def in_query(field, query=nil)
130
- query_hash = {AV::Protocol::KEY_CLASS_NAME => query.class_name, "where" => query.where}
130
+ query_hash = {LC::Protocol::KEY_CLASS_NAME => query.class_name, "where" => query.where}
131
131
  add_constraint(field, "$inQuery" => query_hash)
132
132
  self
133
133
  end
@@ -152,26 +152,26 @@ module AV
152
152
 
153
153
  def get
154
154
  uri = Protocol.class_uri @class_name
155
- if @class_name == AV::Protocol::CLASS_USER
155
+ if @class_name == LC::Protocol::CLASS_USER
156
156
  uri = Protocol.user_uri
157
- elsif @class_name == AV::Protocol::CLASS_INSTALLATION
157
+ elsif @class_name == LC::Protocol::CLASS_INSTALLATION
158
158
  uri = Protocol.installation_uri
159
159
  end
160
160
  query = { "where" => where_as_json.to_json }
161
161
  set_order(query)
162
162
  [:count, :limit, :skip, :include].each {|a| merge_attribute(a, query)}
163
- AV.client.logger.info{"Parse query for #{uri} #{query.inspect}"} unless AV.client.quiet
164
- response = AV.client.request uri, :get, nil, query
163
+ LC.client.logger.info{"Parse query for #{uri} #{query.inspect}"} unless LC.client.quiet
164
+ response = LC.client.request uri, :get, nil, query
165
165
 
166
166
  if response.is_a?(Hash) && response.has_key?(Protocol::KEY_RESULTS) && response[Protocol::KEY_RESULTS].is_a?(Array)
167
- parsed_results = response[Protocol::KEY_RESULTS].map{|o| AV.parse_json(class_name, o)}
167
+ parsed_results = response[Protocol::KEY_RESULTS].map{|o| LC.parse_json(class_name, o)}
168
168
  if response.keys.size == 1
169
169
  parsed_results
170
170
  else
171
171
  response.dup.merge(Protocol::KEY_RESULTS => parsed_results)
172
172
  end
173
173
  else
174
- raise AVError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}")
174
+ raise LCError.new("query response not a Hash with #{Protocol::KEY_RESULTS} key: #{response.class} #{response.inspect}")
175
175
  end
176
176
  end
177
177
 
@@ -4,8 +4,14 @@ require 'leancloud/client'
4
4
  require 'leancloud/error'
5
5
  require 'leancloud/object'
6
6
 
7
- module AV
8
- class User < AV::Object
7
+ module LC
8
+ class User < LC::Object
9
+
10
+ def self.become(token)
11
+ response = LC.client._request(uri: LC::Protocol::USER_CURRENT_URI, session_token: token)
12
+ LC.client.session_token = response[LC::Protocol::KEY_USER_SESSION_TOKEN]
13
+ new(response)
14
+ end
9
15
 
10
16
  def self.authenticate(username, password)
11
17
  body = {
@@ -13,21 +19,21 @@ module AV
13
19
  "password" => password
14
20
  }
15
21
 
16
- response = AV.client.request(AV::Protocol::USER_LOGIN_URI, :get, nil, body)
17
- AV.client.session_token = response[AV::Protocol::KEY_USER_SESSION_TOKEN]
22
+ response = LC.client.request(LC::Protocol::USER_LOGIN_URI, :get, nil, body)
23
+ LC.client.session_token = response[LC::Protocol::KEY_USER_SESSION_TOKEN]
18
24
 
19
25
  new(response)
20
26
  end
21
27
 
22
28
  def self.reset_password(email)
23
29
  body = {"email" => email}
24
- AV.client.post(AV::Protocol::PASSWORD_RESET_URI, body.to_json)
30
+ LC.client.post(LC::Protocol::PASSWORD_RESET_URI, body.to_json)
25
31
  end
26
32
 
27
33
  def initialize(data = nil)
28
34
  data["username"] = data[:username] if data[:username]
29
35
  data["password"] = data[:password] if data[:password]
30
- super(AV::Protocol::CLASS_USER, data)
36
+ super(LC::Protocol::CLASS_USER, data)
31
37
  end
32
38
 
33
39
  def uri
@@ -1,13 +1,13 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
- module AV
3
+ module LC
4
4
 
5
5
  # Parse a JSON representation into a fully instantiated
6
6
  # class. obj can be either a primitive or a Hash of primitives as parsed
7
7
  # by JSON.parse
8
8
  # @param class_name [Object]
9
9
  # @param obj [Object]
10
- def AV.parse_json(class_name, obj)
10
+ def LC.parse_json(class_name, obj)
11
11
  if obj.nil?
12
12
  nil
13
13
 
@@ -22,7 +22,7 @@ module AV
22
22
  if obj.has_key?(Protocol::KEY_TYPE)
23
23
  parse_datatype obj
24
24
  elsif class_name # otherwise it must be a regular object, so deep parse it avoiding re-JSON.parsing raw Strings
25
- AV::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
25
+ LC::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
26
26
  else # plain old hash
27
27
  obj
28
28
  end
@@ -33,49 +33,49 @@ module AV
33
33
  end
34
34
  end
35
35
 
36
- def AV.parse_datatype(obj)
36
+ def LC.parse_datatype(obj)
37
37
  type = obj[Protocol::KEY_TYPE]
38
38
 
39
39
  case type
40
40
  when Protocol::TYPE_POINTER
41
41
  if obj[Protocol::KEY_CREATED_AT]
42
- AV::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
42
+ LC::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
43
43
  else
44
- AV::Pointer.new obj
44
+ LC::Pointer.new obj
45
45
  end
46
46
  when Protocol::TYPE_BYTES
47
- AV::Bytes.new obj
47
+ LC::Bytes.new obj
48
48
  when Protocol::TYPE_DATE
49
- AV::Date.new obj
49
+ LC::Date.new obj
50
50
  when Protocol::TYPE_GEOPOINT
51
- AV::GeoPoint.new obj
51
+ LC::GeoPoint.new obj
52
52
  when Protocol::TYPE_FILE
53
- AV::File.new obj
53
+ LC::File.new obj
54
54
  when Protocol::TYPE_OBJECT # used for relation queries, e.g. "?include=post"
55
- AV::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
55
+ LC::Object.new obj[Protocol::KEY_CLASS_NAME], Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
56
56
  end
57
57
  end
58
58
 
59
- def AV.pointerize_value(obj)
60
- if obj.kind_of?(AV::Object)
59
+ def LC.pointerize_value(obj)
60
+ if obj.kind_of?(LC::Object)
61
61
  p = obj.pointer
62
62
  raise ArgumentError.new("new object used in context requiring pointer #{obj}") unless p
63
63
  p
64
64
  elsif obj.is_a?(Array)
65
65
  obj.map do |v|
66
- AV.pointerize_value(v)
66
+ LC.pointerize_value(v)
67
67
  end
68
68
  elsif obj.is_a?(Hash)
69
69
  Hash[obj.map do |k, v|
70
- [k, AV.pointerize_value(v)]
70
+ [k, LC.pointerize_value(v)]
71
71
  end]
72
72
  else
73
73
  obj
74
74
  end
75
75
  end
76
76
 
77
- def AV.object_pointer_equality?(a, b)
78
- classes = [AV::Object, AV::Pointer]
77
+ def LC.object_pointer_equality?(a, b)
78
+ classes = [LC::Object, LC::Pointer]
79
79
  return false unless classes.any? { |c| a.kind_of?(c) } && classes.any? { |c| b.kind_of?(c) }
80
80
  return true if a.equal?(b)
81
81
  return false if a.new? || b.new?
@@ -83,7 +83,7 @@ module AV
83
83
  a.class_name == b.class_name && a.id == b.id
84
84
  end
85
85
 
86
- def AV.object_pointer_hash(v)
86
+ def LC.object_pointer_hash(v)
87
87
  if v.new?
88
88
  v.object_id
89
89
  else