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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/example.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  # -*- encoding : utf-8 -*-
2
- AV.init :application_id => "your_application_id",
2
+ LC.init :application_id => "your_application_id",
3
3
  :api_key => "your_REST_API_Key"
4
4
 
5
- profile = AV::Object.new "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"] = AV::Date.new "1980-12-25"
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 = AV::Object.new cls
18
+ score = LC::Object.new cls
19
19
  score["score"] = i
20
20
  score.save
21
21
  }
22
22
 
23
- AV::Query.new(cls) \
23
+ LC::Query.new(cls) \
24
24
  .greater_eq("score", 10) \
25
25
  .less_eq("score", 20) \
26
26
  .get
27
27
 
28
- AV::Query.new(cls) \
28
+ LC::Query.new(cls) \
29
29
  .value_in("score", [10, 20, 30, 40]) \
30
30
  .get
31
31
 
32
32
  # Pushes
33
- push = AV::Push.new({ "alert"=> "I'm sending this push to all my app users!" })
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.1.1"
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
- AV::Protocol::ERROR_INTERNAL,
15
- AV::Protocol::ERROR_TIMEOUT,
16
- AV::Protocol::ERROR_EXCEEDED_BURST_LIMIT
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'] == AV::Protocol::ERROR_EXCEEDED_BURST_LIMIT
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 AV::AVProtocolError.new(error_hash)
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? ? AV::AVProtocolError : AV::AVProtocolRetry)
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
- require 'faraday/better_retry'
15
- require 'faraday/extended_parse_json'
16
- require 'faraday/get_method_override'
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
- require 'leancloud/object'
24
- require 'leancloud/query'
25
- require 'leancloud/datatypes'
26
- require 'leancloud/util'
27
- require 'leancloud/protocol'
28
- require 'leancloud/user'
29
- require "leancloud/installation"
30
- require 'leancloud/push'
31
- require 'leancloud/cloud'
32
- require 'leancloud/model'
33
- require 'leancloud/batch'
34
- require 'leancloud/application'
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'
@@ -1,7 +1,7 @@
1
- module AV
1
+ module LC
2
2
  class Application
3
3
  def self.config
4
- AV.client.request(AV::Protocol.config_uri)['params']
4
+ LC.client.request(LC::Protocol.config_uri)['params']
5
5
  end
6
6
  end
7
7
  end
@@ -1,10 +1,10 @@
1
1
  # -*- encoding : utf-8 -*-
2
- module AV
2
+ module LC
3
3
  class Batch
4
4
  attr_reader :requests
5
5
  attr_reader :client
6
6
 
7
- def initialize(client = AV.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 = AV::Protocol.class_uri(object.class_name)
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 = AV::Protocol.class_uri(object.class_name, object.id)
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" => AV::Protocol.class_uri(object.class_name, object.id)
41
+ "path" => LC::Protocol.class_uri(object.class_name, object.id)
42
42
  })
43
43
  end
44
44
 
45
45
  def run!
46
- uri = AV::Protocol.batch_request_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
@@ -4,7 +4,7 @@ require 'leancloud/error'
4
4
  require 'leancloud/util'
5
5
 
6
6
  require 'logger'
7
- module AV
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', 'AV::AVProtocolRetry']
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
- # AVProtocolError if the response has an error status code,
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" => 'Parse for Ruby, 0.0',
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 => @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 AV.client to retrieve the client object.
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. AV.init must be called before saving
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 AV::Object::File
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 AVError, "API not initialized" if !@@client
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 AVProtocolError => e
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
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
- module AV
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 = AV.client.post(self.uri, params.to_json)
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
- AV.client.post("/#{Protocol::VERSION}/requestSmsCode", params.to_json)
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
- AV.client.post("/#{Protocol::VERSION}/verifySmsCode/#{code}", params.to_json)
29
+ LC.client.post("/#{Protocol::VERSION}/verifySmsCode/#{code}", params.to_json)
30
30
  end
31
31
  end
32
32
  end
@@ -3,7 +3,7 @@ require 'time'
3
3
  require 'date'
4
4
  require 'base64'
5
5
 
6
- module AV
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
- AV.object_pointer_equality?(self, other)
34
+ LC.object_pointer_equality?(self, other)
35
35
  end
36
36
 
37
37
  alias == eql?
38
38
 
39
39
  def hash
40
- AV.object_pointer_hash(self)
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
- AV.get @class_name, @parse_object_id if @parse_object_id
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 = AV::File.new(:body => "Hello World!", :local_filename => "hello.txt")
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 = AV::Protocol.file_uri(@local_filename)
333
- resp = AV.client.request(uri, :post, @body, nil, @content_type)
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"]
@@ -1,16 +1,16 @@
1
1
  # -*- encoding : utf-8 -*-
2
- module AV
2
+ module LC
3
3
 
4
4
  # Base exception class for errors thrown by the Parse
5
- # client library. AVError will be raised by any
6
- # network operation if AV.init() has not been called.
7
- class AVError < StandardError #Exception ... why? A:http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/
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 AVProtocolError < AVError
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 AVProtocolRetry < AVProtocolError
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 AV
7
- class Installation < AV::Object
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 = AV.client.request(uri, :get, nil, nil)
30
- parse AV.parse_json(nil, response)
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
- AV.client.request uri, method, self.to_json, nil
45
+ LC.client.request uri, method, self.to_json, nil
46
46
  end
47
47
 
48
48
  def rest_api_hash