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.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/README.md +144 -116
- data/VERSION +1 -1
- data/example.rb +7 -7
- data/leancloud-ruby-client.gemspec +1 -3
- data/lib/faraday/extended_parse_json.rb +6 -6
- data/lib/leancloud-ruby-client.rb +15 -15
- data/lib/leancloud/application.rb +2 -2
- data/lib/leancloud/batch.rb +6 -6
- data/lib/leancloud/client.rb +39 -14
- data/lib/leancloud/cloud.rb +4 -4
- data/lib/leancloud/datatypes.rb +7 -7
- data/lib/leancloud/error.rb +6 -6
- data/lib/leancloud/installation.rb +5 -5
- data/lib/leancloud/model.rb +3 -3
- data/lib/leancloud/object.rb +15 -15
- data/lib/leancloud/protocol.rb +10 -5
- data/lib/leancloud/push.rb +2 -2
- data/lib/leancloud/query.rb +22 -22
- data/lib/leancloud/user.rb +12 -6
- data/lib/leancloud/util.rb +18 -18
- data/test/helper.rb +5 -7
- data/test/test_batch.rb +18 -18
- data/test/test_client.rb +17 -17
- data/test/test_cloud.rb +7 -7
- data/test/test_datatypes.rb +23 -23
- data/test/test_file.rb +5 -5
- data/test/test_init.rb +4 -4
- data/test/test_init_from_cloud_code.rb +2 -2
- data/test/test_installation.rb +6 -6
- data/test/test_model.rb +4 -4
- data/test/test_object.rb +45 -45
- data/test/test_push.rb +7 -7
- data/test/test_query.rb +39 -39
- data/test/test_throttle.rb +1 -1
- data/test/test_user.rb +9 -9
- metadata +23 -24
- data/Gemfile.lock +0 -86
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/example.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
|
2
|
+
LC.init :application_id => "your_application_id",
|
3
3
|
:api_key => "your_REST_API_Key"
|
4
4
|
|
5
|
-
profile =
|
5
|
+
profile = LC::Object.new "Profile"
|
6
6
|
profile["first_name"] = "John"
|
7
7
|
profile["last_name"] = "Doe"
|
8
8
|
profile["username"] = "jdoe"
|
9
9
|
profile["email_address"] = "jdoe@fubar.com"
|
10
|
-
profile["birthday"] =
|
10
|
+
profile["birthday"] = LC::Date.new "1980-12-25"
|
11
11
|
profile.save
|
12
12
|
|
13
13
|
profile.increment "login_count"
|
@@ -15,21 +15,21 @@ profile.increment "login_count"
|
|
15
15
|
# Queries
|
16
16
|
cls = "GameScore"
|
17
17
|
(1..100).each { |i|
|
18
|
-
score =
|
18
|
+
score = LC::Object.new cls
|
19
19
|
score["score"] = i
|
20
20
|
score.save
|
21
21
|
}
|
22
22
|
|
23
|
-
|
23
|
+
LC::Query.new(cls) \
|
24
24
|
.greater_eq("score", 10) \
|
25
25
|
.less_eq("score", 20) \
|
26
26
|
.get
|
27
27
|
|
28
|
-
|
28
|
+
LC::Query.new(cls) \
|
29
29
|
.value_in("score", [10, 20, 30, 40]) \
|
30
30
|
.get
|
31
31
|
|
32
32
|
# Pushes
|
33
|
-
push =
|
33
|
+
push = LC::Push.new({ "alert"=> "I'm sending this push to all my app users!" })
|
34
34
|
push.save
|
35
35
|
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "leancloud-ruby-client"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alan deLevie", "Adam Alpern", "Dennis Zhuang"]
|
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".travis.yml",
|
21
21
|
"Gemfile",
|
22
|
-
"Gemfile.lock",
|
23
22
|
"LICENSE.txt",
|
24
23
|
"README.md",
|
25
24
|
"Rakefile",
|
@@ -163,4 +162,3 @@ Gem::Specification.new do |s|
|
|
163
162
|
s.add_dependency(%q<pry>, [">= 0"])
|
164
163
|
end
|
165
164
|
end
|
166
|
-
|
@@ -11,18 +11,18 @@ module Faraday
|
|
11
11
|
data = parse(env[:body]) || {} rescue {}
|
12
12
|
|
13
13
|
array_codes = [
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
LC::Protocol::ERROR_INTERNAL,
|
15
|
+
LC::Protocol::ERROR_TIMEOUT,
|
16
|
+
LC::Protocol::ERROR_EXCEEDED_BURST_LIMIT
|
17
17
|
]
|
18
18
|
error_hash = { "error" => "HTTP Status #{env[:status]} Body #{env[:body]}", "http_status_code" => env[:status] }.merge(data)
|
19
19
|
if data['code'] && array_codes.include?(data['code'])
|
20
|
-
sleep 60 if data['code'] ==
|
20
|
+
sleep 60 if data['code'] == LC::Protocol::ERROR_EXCEEDED_BURST_LIMIT
|
21
21
|
raise exception(env).new(error_hash.merge(data))
|
22
22
|
elsif env[:status] >= 500
|
23
23
|
raise exception(env).new(error_hash.merge(data))
|
24
24
|
end
|
25
|
-
raise
|
25
|
+
raise LC::LCProtocolError.new(error_hash)
|
26
26
|
else
|
27
27
|
data = parse(env[:body]) || {}
|
28
28
|
|
@@ -32,7 +32,7 @@ module Faraday
|
|
32
32
|
|
33
33
|
def exception env
|
34
34
|
# decide to retry or not
|
35
|
-
(env[:retries].to_i.zero? ?
|
35
|
+
(env[:retries].to_i.zero? ? LC::LCProtocolError : LC::LCProtocolRetry)
|
36
36
|
end
|
37
37
|
|
38
38
|
end
|
@@ -11,24 +11,24 @@ require 'bundler/setup'
|
|
11
11
|
|
12
12
|
require 'faraday'
|
13
13
|
require 'faraday_middleware'
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
require_relative 'faraday/better_retry'
|
15
|
+
require_relative 'faraday/extended_parse_json'
|
16
|
+
require_relative 'faraday/get_method_override'
|
17
17
|
require 'date'
|
18
18
|
require 'cgi'
|
19
19
|
|
20
20
|
cwd = Pathname(__FILE__).dirname
|
21
21
|
$:.unshift(cwd.to_s) unless $:.include?(cwd.to_s) || $:.include?(cwd.expand_path.to_s)
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
23
|
+
require_relative 'leancloud/object'
|
24
|
+
require_relative 'leancloud/query'
|
25
|
+
require_relative 'leancloud/datatypes'
|
26
|
+
require_relative 'leancloud/util'
|
27
|
+
require_relative 'leancloud/protocol'
|
28
|
+
require_relative 'leancloud/user'
|
29
|
+
require_relative "leancloud/installation"
|
30
|
+
require_relative 'leancloud/push'
|
31
|
+
require_relative 'leancloud/cloud'
|
32
|
+
require_relative 'leancloud/model'
|
33
|
+
require_relative 'leancloud/batch'
|
34
|
+
require_relative 'leancloud/application'
|
data/lib/leancloud/batch.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
module
|
2
|
+
module LC
|
3
3
|
class Batch
|
4
4
|
attr_reader :requests
|
5
5
|
attr_reader :client
|
6
6
|
|
7
|
-
def initialize(client =
|
7
|
+
def initialize(client = LC.client)
|
8
8
|
@client = client
|
9
9
|
@requests ||= []
|
10
10
|
end
|
@@ -15,7 +15,7 @@ module AV
|
|
15
15
|
|
16
16
|
def create_object(object)
|
17
17
|
method = "POST"
|
18
|
-
path =
|
18
|
+
path = LC::Protocol.class_uri(object.class_name)
|
19
19
|
body = object.safe_hash
|
20
20
|
add_request({
|
21
21
|
"method" => method,
|
@@ -26,7 +26,7 @@ module AV
|
|
26
26
|
|
27
27
|
def update_object(object)
|
28
28
|
method = "PUT"
|
29
|
-
path =
|
29
|
+
path = LC::Protocol.class_uri(object.class_name, object.id)
|
30
30
|
body = object.safe_hash
|
31
31
|
add_request({
|
32
32
|
"method" => method,
|
@@ -38,12 +38,12 @@ module AV
|
|
38
38
|
def delete_object(object)
|
39
39
|
add_request({
|
40
40
|
"method" => "DELETE",
|
41
|
-
"path" =>
|
41
|
+
"path" => LC::Protocol.class_uri(object.class_name, object.id)
|
42
42
|
})
|
43
43
|
end
|
44
44
|
|
45
45
|
def run!
|
46
|
-
uri =
|
46
|
+
uri = LC::Protocol.batch_request_uri
|
47
47
|
body = {:requests => @requests}.to_json
|
48
48
|
@client.request(uri, :post, body)
|
49
49
|
end
|
data/lib/leancloud/client.rb
CHANGED
@@ -4,7 +4,7 @@ require 'leancloud/error'
|
|
4
4
|
require 'leancloud/util'
|
5
5
|
|
6
6
|
require 'logger'
|
7
|
-
module
|
7
|
+
module LC
|
8
8
|
|
9
9
|
# A class which encapsulates the HTTPS communication with the Parse
|
10
10
|
# API server.
|
@@ -40,7 +40,7 @@ module AV
|
|
40
40
|
max: @max_retries,
|
41
41
|
logger: @logger,
|
42
42
|
interval: 0.5,
|
43
|
-
exceptions: ['Faraday::Error::TimeoutError', 'Faraday::Error::ParsingError', '
|
43
|
+
exceptions: ['Faraday::Error::TimeoutError', 'Faraday::Error::ParsingError', 'LC::LCProtocolRetry']
|
44
44
|
c.use Faraday::ExtendedParseJson
|
45
45
|
|
46
46
|
c.response :logger, @logger unless @quiet
|
@@ -49,21 +49,46 @@ module AV
|
|
49
49
|
yield(c) if block_given?
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
def get_sign(time = Time.now)
|
53
|
+
m = !!@master_key
|
54
|
+
if m
|
55
|
+
m = ",master"
|
56
|
+
else
|
57
|
+
m = ""
|
58
|
+
end
|
59
|
+
k = @master_key || @api_key
|
60
|
+
t = (time.to_f * 1000).to_i.to_s
|
61
|
+
md5 = Digest::MD5.hexdigest t+k
|
62
|
+
sign = "#{md5},#{t}#{m}"
|
63
|
+
end
|
53
64
|
# Perform an HTTP request for the given uri and method
|
54
65
|
# with common basic response handling. Will raise a
|
55
|
-
#
|
66
|
+
# LCProtocolError if the response has an error status code,
|
56
67
|
# and will return the parsed JSON body on success, if there is one.
|
57
|
-
def request(uri, method = :get, body = nil, query = nil, content_type = nil)
|
68
|
+
def request(uri, method = :get, body = nil, query = nil, content_type = nil, session_token = nil)
|
69
|
+
headers = {}
|
70
|
+
{
|
71
|
+
"Content-Type" => content_type || 'application/json',
|
72
|
+
"User-Agent" => 'leancloud-ruby-client, 0.0',
|
73
|
+
Protocol::HEADER_APP_ID => @application_id,
|
74
|
+
Protocol::HEADER_API_KEY => get_sign,
|
75
|
+
Protocol::HEADER_SESSION_TOKEN => session_token || @session_token,
|
76
|
+
}.each do |key, value|
|
77
|
+
headers[key] = value if value
|
78
|
+
end
|
79
|
+
|
80
|
+
@session.send(method, uri, query || body || {}, headers).body
|
81
|
+
end
|
82
|
+
def _request(uri: "", method: :get, body: nil, query: nil, content_type: nil, session_token: nil)
|
83
|
+
raise ArgumentError, "wrong number of arguments (given 0, expected 1..6)" if !(uri && uri != "")
|
58
84
|
headers = {}
|
59
85
|
|
60
86
|
{
|
61
87
|
"Content-Type" => content_type || 'application/json',
|
62
|
-
"User-Agent" => '
|
63
|
-
Protocol::HEADER_MASTER_KEY => @master_key,
|
88
|
+
"User-Agent" => 'leancloud-ruby-client, 0.0',
|
64
89
|
Protocol::HEADER_APP_ID => @application_id,
|
65
|
-
Protocol::HEADER_API_KEY =>
|
66
|
-
Protocol::HEADER_SESSION_TOKEN => @session_token,
|
90
|
+
Protocol::HEADER_API_KEY => get_sign,
|
91
|
+
Protocol::HEADER_SESSION_TOKEN => session_token || @session_token,
|
67
92
|
}.each do |key, value|
|
68
93
|
headers[key] = value if value
|
69
94
|
end
|
@@ -94,11 +119,11 @@ module AV
|
|
94
119
|
# ------------------------------------------------------------
|
95
120
|
class << self
|
96
121
|
# A singleton client for use by methods in Object.
|
97
|
-
# Always use
|
122
|
+
# Always use LC.client to retrieve the client object.
|
98
123
|
@client = nil
|
99
124
|
|
100
125
|
# Initialize the singleton instance of Client which is used
|
101
|
-
# by all API methods.
|
126
|
+
# by all API methods. LC.init must be called before saving
|
102
127
|
# or retrieving any objects.
|
103
128
|
def init(data = {}, &blk)
|
104
129
|
defaults = {:application_id => ENV["LC_APPLICATION_ID"], :api_key => ENV["LC_APPLICATION_KEY"]}
|
@@ -111,7 +136,7 @@ module AV
|
|
111
136
|
|
112
137
|
# A convenience method for using global.json
|
113
138
|
def init_from_cloud_code(path = "../config/global.json")
|
114
|
-
# warning: toplevel constant File referenced by
|
139
|
+
# warning: toplevel constant File referenced by LC::Object::File
|
115
140
|
global = JSON.parse(Object::File.open(path).read)
|
116
141
|
application_name = global["applications"]["_default"]["link"]
|
117
142
|
application_id = global["applications"][application_name]["applicationId"]
|
@@ -126,7 +151,7 @@ module AV
|
|
126
151
|
end
|
127
152
|
|
128
153
|
def client
|
129
|
-
raise
|
154
|
+
raise LCError, "API not initialized" if !@@client
|
130
155
|
@@client
|
131
156
|
end
|
132
157
|
|
@@ -137,7 +162,7 @@ module AV
|
|
137
162
|
def get(class_name, object_id = nil)
|
138
163
|
data = self.client.get( Protocol.class_uri(class_name, object_id) )
|
139
164
|
self.parse_json class_name, data
|
140
|
-
rescue
|
165
|
+
rescue LCProtocolError => e
|
141
166
|
if e.code == Protocol::ERROR_OBJECT_NOT_FOUND_FOR_GET
|
142
167
|
e.message += ": #{class_name}:#{object_id}"
|
143
168
|
end
|
data/lib/leancloud/cloud.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
module
|
2
|
+
module LC
|
3
3
|
module Cloud
|
4
4
|
|
5
5
|
class Function
|
@@ -14,19 +14,19 @@ module AV
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def call(params={})
|
17
|
-
response =
|
17
|
+
response = LC.client.post(self.uri, params.to_json)
|
18
18
|
result = response["result"]
|
19
19
|
result
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.request_sms(params)
|
24
|
-
|
24
|
+
LC.client.post("/#{Protocol::VERSION}/requestSmsCode", params.to_json)
|
25
25
|
end
|
26
26
|
|
27
27
|
def self.verify_sms_code(phone, code)
|
28
28
|
params = { mobilePhoneNumber: phone}
|
29
|
-
|
29
|
+
LC.client.post("/#{Protocol::VERSION}/verifySmsCode/#{code}", params.to_json)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
data/lib/leancloud/datatypes.rb
CHANGED
@@ -3,7 +3,7 @@ require 'time'
|
|
3
3
|
require 'date'
|
4
4
|
require 'base64'
|
5
5
|
|
6
|
-
module
|
6
|
+
module LC
|
7
7
|
|
8
8
|
# Pointer
|
9
9
|
# ------------------------------------------------------------
|
@@ -31,13 +31,13 @@ module AV
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def eql?(other)
|
34
|
-
|
34
|
+
LC.object_pointer_equality?(self, other)
|
35
35
|
end
|
36
36
|
|
37
37
|
alias == eql?
|
38
38
|
|
39
39
|
def hash
|
40
|
-
|
40
|
+
LC.object_pointer_hash(self)
|
41
41
|
end
|
42
42
|
|
43
43
|
def new?
|
@@ -59,7 +59,7 @@ module AV
|
|
59
59
|
|
60
60
|
# Retrieve the Parse object referenced by this pointer.
|
61
61
|
def get
|
62
|
-
|
62
|
+
LC.get @class_name, @parse_object_id if @parse_object_id
|
63
63
|
end
|
64
64
|
|
65
65
|
def to_s
|
@@ -295,7 +295,7 @@ module AV
|
|
295
295
|
|
296
296
|
# File
|
297
297
|
# ------------------------------------------------------------
|
298
|
-
# tf =
|
298
|
+
# tf = LC::File.new(:body => "Hello World!", :local_filename => "hello.txt")
|
299
299
|
# tf.save
|
300
300
|
class File
|
301
301
|
# '{"avatar": {"__type":"File", "name":"profile.png", "url"=>"http://files.parse.com/blah/profile.png"}}'
|
@@ -329,8 +329,8 @@ module AV
|
|
329
329
|
end
|
330
330
|
|
331
331
|
def save
|
332
|
-
uri =
|
333
|
-
resp =
|
332
|
+
uri = LC::Protocol.file_uri(@local_filename)
|
333
|
+
resp = LC.client.request(uri, :post, @body, nil, @content_type)
|
334
334
|
@parse_filename = resp["name"]
|
335
335
|
@url = resp["url"]
|
336
336
|
@id = resp["id"]
|
data/lib/leancloud/error.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
module
|
2
|
+
module LC
|
3
3
|
|
4
4
|
# Base exception class for errors thrown by the Parse
|
5
|
-
# client library.
|
6
|
-
# network operation if
|
7
|
-
class
|
5
|
+
# client library. LCError will be raised by any
|
6
|
+
# network operation if LC.init() has not been called.
|
7
|
+
class LCError < StandardError #Exception ... why? A:http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/
|
8
8
|
end
|
9
9
|
|
10
10
|
# An exception class raised when the REST API returns an error.
|
11
11
|
# The error code and message will be parsed out of the HTTP response,
|
12
12
|
# which is also included in the response attribute.
|
13
|
-
class
|
13
|
+
class LCProtocolError < LCError
|
14
14
|
attr_accessor :code
|
15
15
|
attr_accessor :error
|
16
16
|
attr_accessor :response
|
@@ -36,7 +36,7 @@ module AV
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
class
|
39
|
+
class LCProtocolRetry < LCProtocolError
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
@@ -3,8 +3,8 @@ require 'leancloud/client'
|
|
3
3
|
require 'leancloud/error'
|
4
4
|
require 'leancloud/object'
|
5
5
|
|
6
|
-
module
|
7
|
-
class Installation <
|
6
|
+
module LC
|
7
|
+
class Installation < LC::Object
|
8
8
|
UPDATABLE_FIELDS = {
|
9
9
|
badge: 'badge',
|
10
10
|
channels: 'channels',
|
@@ -26,8 +26,8 @@ module AV
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def get
|
29
|
-
if response =
|
30
|
-
parse
|
29
|
+
if response = LC.client.request(uri, :get, nil, nil)
|
30
|
+
parse LC.parse_json(nil, response)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -42,7 +42,7 @@ module AV
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def save
|
45
|
-
|
45
|
+
LC.client.request uri, method, self.to_json, nil
|
46
46
|
end
|
47
47
|
|
48
48
|
def rest_api_hash
|