lean_cloud 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bdcc7d98e55c298d4036e11dbf7f9cd209e969c
4
- data.tar.gz: d696b5333a506609c1de167fbcc8b9f1710fb163
3
+ metadata.gz: 020545fc7a28d1a7a1292f102076cea9d1e22cf7
4
+ data.tar.gz: 40ece99f5a9db440af06602ae1672709e91f4559
5
5
  SHA512:
6
- metadata.gz: e5db0df3d790166bff799638f9e717bf23881734b963ddca826992bc5f09c03e4291984836905ea42eb7575a716c95b194f8c9ba28e2347899a5627c20c826af
7
- data.tar.gz: 991f034528d3a19001f013c81803d26d69e1449fb835cf43df58ed8b542bd3edfc8e425633ea70203ba29d30c548197ba768955aee7c33d57b606eb45a1c8e6c
6
+ metadata.gz: 70fe9d230da9f82aa3604be29716d361709b8c13567ba8405d2b4c4509daf510c86564bbd01f44af3a8cfae6c385880523f03e776648539291b8c4c383644b97
7
+ data.tar.gz: 6843d9561ee16ba62081b238097275625dc86b32d0fa399fa1ff1f888db44ac4b4c32287c9327a1bcc705c16b4524bc1dfb49928167eebca2b8a1a5ed40329f8
data/README.md CHANGED
@@ -2,22 +2,46 @@
2
2
 
3
3
  This project rocks and uses MIT-LICENSE.
4
4
 
5
+
6
+ ## Usage
7
+
8
+ ```
9
+ gem "lean_cloud"
10
+
11
+ LeanCloud::Feedback.create({}) # 用户反馈
12
+ LeanCloud::Function.run(function)
13
+ ....
14
+ ```
15
+ ### 更多请查看 lib/lean_cloud/modules
16
+
5
17
  # 配置
6
18
 
7
19
  ```
8
20
  LeanCloud.configure do
9
- config.app_id = "11f6ad8ec52a2984abaafd7c3b516503785c2072"
10
- config.app_key = "11f6ad8ec52a2984abaafd7c3b516503785c2072"
11
- config.host = "https://leancloud.cn"
12
- config.version = "1.1"
21
+ config.app_id = "11f6ad8ec52a2984abaafd7c3b516503785c2072"
22
+ config.app_key = "11f6ad8ec52a2984abaafd7c3b516503785c2072"
23
+ config.master_key = "11f6ad8ec52a2984abaafd7c3b516503785c2072"
24
+ config.host = "https://leancloud.cn"
25
+ config.version = "1.1"
13
26
  end
14
27
  ```
15
28
 
16
29
  # 在数据管理中添加一个class(GameScore)后:
17
30
 
18
31
  ```
32
+ # the namespace option will generate https://leancloud.cn/1.1/classes/GameScore
19
33
  LeanCloud.register "GameScore", namespace: "classes/GameScore" do
20
34
  only :create, :update, :show, :destroy, :search
35
+
36
+ # GET https://leancloud.cn/1.1/classes/GameScore/sms/:code
37
+ match "sms/:code", via: :get, as: get_code # LeanCloud::GameScore.get_code(code)
38
+ # GET https://leancloud.cn/1.1/sms/:code
39
+ match "sms/:code", via: :get, as: get_code, unscope: true # LeanCloud::GameScore.get_code(code)
40
+ # GET https://leancloud.cn/1.1/classes/GameScore/:id/sms/:code
41
+ match "sms/:code", via: :get, as: get_code, on: :member # LeanCloud::GameScore.get_code(id, code)
42
+
43
+ # GET https://leancloud.cn/1.1/classes/GameScore/result
44
+ route :result
21
45
  end
22
46
  ```
23
47
 
@@ -0,0 +1,4 @@
1
+ LeanCloud::Base.register "AVOSRealtimeGroups", namespace: "classes/AVOSRealtimeGroups" do
2
+ self.settings = { host: "https://cn.avoscloud.com" }
3
+ only :create, :update, :show, :search, :destroy
4
+ end
data/examples/file.rb ADDED
@@ -0,0 +1,6 @@
1
+ # 文件接口
2
+ LeanCloud::Base.register "File", namespace: "files" do
3
+ # https://leancloud.cn/1.1/files/test.png
4
+ # only :create
5
+ match ":filename", via: :post, as: :upload
6
+ end
@@ -0,0 +1,9 @@
1
+ # 文件接口
2
+ LeanCloud::Base.register "LiveFile", namespace: "classes/LiveFile" do
3
+ # https://leancloud.cn/1.1/classes/LiveFile
4
+ only :create, :show, :search, :update, :destroy
5
+
6
+ cattr_accessor :columns
7
+ self.columns = %i(file liveRoom userUUID selected mime_type)
8
+
9
+ end
@@ -0,0 +1,158 @@
1
+ LeanCloud::Base.register "LiveRoom", namespace: "classes/LiveRoom" do
2
+ extend ActiveModel::Naming
3
+ include ActiveModel::Validations
4
+
5
+ only :create, :update, :show, :destroy, :search
6
+
7
+ cattr_accessor :columns, :status_hash
8
+ self.columns = %i(objectId title sourceTitle sourceType sourceId status cover liveTopic liveStartAt liveEndAt hosts avosGroup admins event_id)
9
+ self.status_hash = {open: "开启", closed: "关闭", hidden: "隐藏", ready: "待开启"}
10
+ attr_accessor *columns
11
+
12
+ validates :title, :event_id, :status, :cover, :liveTopic, :liveStartAt, :liveEndAt, :hosts, :admins, presence: true
13
+ validates :status, :inclusion => { :in => status_hash.keys.map(&:to_s) }
14
+
15
+ class << self
16
+
17
+ def primary_key
18
+ :id
19
+ end
20
+
21
+ def find(*args,&block)
22
+ new show(*args, &block)
23
+ end
24
+
25
+ def id
26
+ objectId
27
+ end
28
+
29
+ def create_with_upload(attributes,&block)
30
+ room = new(attributes)
31
+ return room.errors if !room.valid?
32
+
33
+ cover = room.cover
34
+ request = LeanCloud::File.upload(cover.original_filename) do |req|
35
+ req.headers["Content-Type"] = cover.content_type
36
+ req.body = cover.tempfile.read
37
+ end
38
+
39
+ if !request.is_a?(Hash) || !request.key?("objectId")
40
+
41
+ room.errors.add(:base, request)
42
+ return room
43
+ end
44
+
45
+ attributes[:cover] = {
46
+ objectId: request["objectId"],
47
+ className: "_File",
48
+ __type: "Pointer"
49
+ }
50
+
51
+ attributes[:avosGroup] = {
52
+ __type: "Pointer",
53
+ objectId: LeanCloud::AVOSRealtimeGroups.create(m: [])["objectId"],
54
+ "className" => 'AVOSRealtimeGroups'
55
+ }
56
+
57
+ result = create_without_upload(attributes, &block)
58
+
59
+ Rails.logger.info result.to_json
60
+
61
+ if !request.is_a?(Hash) || result.key?("objectId")
62
+ room.errors.add(:base, result)
63
+ room
64
+ else
65
+ room
66
+ end
67
+ end
68
+ alias_method_chain :create, :upload
69
+ end
70
+
71
+ def initialize(attrs={})
72
+ assign_attributes(attrs)
73
+ end
74
+
75
+ def event_id=(val)
76
+ event = Event.find(val)
77
+ self.sourceTitle = event.title
78
+ self.sourceType = event.class.name
79
+ self.sourceId = val
80
+ @event_id = val
81
+ end
82
+
83
+ def event
84
+ @event ||= sourceType.constantize.find(sourceId)
85
+ end
86
+
87
+ def assign_attributes(attrs={})
88
+ attrs.symbolize_keys.slice(*self.class.columns).each do |attribute, value|
89
+ send("#{attribute}=", value)
90
+ end
91
+ end
92
+
93
+ def attributes
94
+ self.class.columns.map {|attribute| [attribute, send(attribute)]}.to_h
95
+ end
96
+
97
+ def hide
98
+ update(status: "hidden")
99
+ end
100
+
101
+ def update(attrs={})
102
+ self.class.update(id, attrs) if id.present?
103
+ end
104
+
105
+ def hosts=(val)
106
+ @hosts = val.to_s.split(',') if val.present?
107
+ end
108
+
109
+ def admins=(val)
110
+ @admins = val.to_s.split(',') if val.present?
111
+ end
112
+
113
+ def save
114
+ id.present? ? update(attributes) : self.class.create(attributes)
115
+ end
116
+
117
+ def destroy
118
+ self.class.destroy(id)
119
+ end
120
+
121
+ def to_key
122
+ [id]
123
+ end
124
+
125
+ def status_name
126
+ STATUS[status.to_sym]
127
+ end
128
+
129
+ def liveStartAt=(val)
130
+ @liveStartAt = {
131
+ __type: "Date",
132
+ "iso" => val
133
+ }
134
+ end
135
+
136
+ def liveEndAt=(val)
137
+ @liveEndAt = {
138
+ __type: "Date",
139
+ "iso" => val
140
+ }
141
+ end
142
+
143
+ # 踢人
144
+ def kick(ids=[])
145
+ update(hosts: {__op: "Remove", objects: ids}) if ids.present?
146
+ end
147
+
148
+ # 查看聊天记录
149
+ def logs
150
+ LeanCloud::Message.logs(convid: avosGroup["objectId"]) if avosGroup.is_a?(Hash).present?
151
+ end
152
+
153
+ # class << self
154
+ # def self.column_names
155
+ # ATTRIBUTES
156
+ # end
157
+ # end
158
+ end
data/lib/lean_cloud.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'lean_cloud/modules'
2
2
  require 'lean_cloud/configuration'
3
+ require 'logger'
3
4
  module LeanCloud
4
5
  class << self
5
6
 
@@ -10,6 +11,14 @@ module LeanCloud
10
11
  def config
11
12
  @config ||= Configuration.instance
12
13
  end
14
+
15
+ def logger
16
+ if defined?(Rails)
17
+ Rails.logger
18
+ else
19
+ Logger.new(STDOUT)
20
+ end
21
+ end
13
22
  end
14
23
 
15
24
  def config
@@ -2,6 +2,7 @@ require 'lean_cloud'
2
2
  require 'lean_cloud/helper'
3
3
  require 'lean_cloud/client'
4
4
  require 'active_support/core_ext/module/attribute_accessors'
5
+ require 'active_support/core_ext/object/blank'
5
6
  require 'active_support/json/decoding'
6
7
  require 'active_support/json/encoding'
7
8
  module LeanCloud
@@ -10,7 +11,7 @@ module LeanCloud
10
11
 
11
12
  class << self
12
13
  def register(klass, options={}, &block)
13
- klass = if !LeanCloud.const_defined?(klass)
14
+ klass = if !LeanCloud.const_defined?(klass, false)
14
15
  LeanCloud.const_set(klass, Class.new(self))
15
16
  else
16
17
  LeanCloud.const_get(klass)
@@ -19,24 +20,38 @@ module LeanCloud
19
20
  klass.class_eval do
20
21
  include Helper
21
22
 
22
- cattr_accessor :routes, :namespace
23
+ cattr_accessor :routes, :namespace, :settings
23
24
 
24
25
  self.routes ||= []
26
+ self.settings = {}
25
27
  self.namespace = options[:namespace]
26
28
 
27
-
28
-
29
29
  class_exec(&block)
30
30
  end
31
31
  end
32
32
 
33
- def client
34
- Client.new(LeanCloud.config).instance
33
+ def client(options={}, &block)
34
+ Client.new(settings).instance(options,&block)
35
35
  end
36
36
 
37
37
  def dispatch(route, *args, &block)
38
38
  options = args.extract_options!
39
- client.send(route.request, route.url(*args), options.to_json, &block)
39
+ data = parse_data(route, options)
40
+ request = client(&route.block).send(route.request, route.url(*args), data, &block)
41
+ LeanCloud.logger.info request.to_json
42
+ parse_body(request)
43
+ end
44
+
45
+ def parse_data(route, data)
46
+ if route.request == :get
47
+ data
48
+ elsif data.present?
49
+ data.to_json
50
+ end
51
+ end
52
+
53
+ def parse_body(request)
54
+ JSON.parse(request.body) rescue request.body
40
55
  end
41
56
  end
42
57
  end
@@ -1,3 +1,4 @@
1
+ require 'lean_cloud/version'
1
2
  require 'active_support/hash_with_indifferent_access'
2
3
  require 'active_support/core_ext/module/delegation'
3
4
  module LeanCloud
@@ -5,23 +6,29 @@ module LeanCloud
5
6
 
6
7
  attr_accessor :options
7
8
 
8
- delegate :app_id, :app_key, :version, :http_adapter, :host, to: :options
9
+ delegate :app_id, :app_key, :master_key, :version, :http_adapter, :host, to: :options
9
10
  delegate :get, :put, :post, :delete, to: :instance
10
11
 
11
12
  def initialize(options)
12
- @options = options
13
+ @options = LeanCloud.config.dup.merge(options)
13
14
  end
14
15
 
15
16
  def adapter
16
17
  http_adapter
17
18
  end
18
19
 
19
- def instance
20
- adapter.new(url, headers: headers)
20
+ def instance(options={}, &block)
21
+ adapter.new(url, headers: headers, &block)
21
22
  end
22
23
 
23
- def headers
24
- {"X-AVOSCloud-Application-Id" => app_id, "X-AVOSCloud-Application-Key" => app_key, 'Content-Type' => 'application/json' }
24
+ def headers(options={})
25
+ {
26
+ "X-AVOSCloud-Application-Id" => app_id,
27
+ "X-AVOSCloud-Application-Key" => app_key,
28
+ "X-AVOSCloud-Master-Key" => master_key,
29
+ 'Content-Type' => 'application/json',
30
+ 'User-Agent' => "LeanCloud SDK Ruby / #{LeanCloud::VERSION}"
31
+ }.merge(options)
25
32
  end
26
33
 
27
34
  def url
@@ -1,6 +1,6 @@
1
1
  require 'faraday'
2
2
  module LeanCloud
3
- class Configuration
3
+ class Configuration < Hash
4
4
  class << self
5
5
  private :new
6
6
  def instance
@@ -9,24 +9,24 @@ module LeanCloud
9
9
  end
10
10
 
11
11
  def initialize
12
- @@options = {
12
+ {
13
13
  :http_adapter => Faraday,
14
14
  :app_id => nil,
15
15
  :app_key => nil
16
- }
16
+ }.each {|k, v| self[k] = v}
17
17
  end
18
18
 
19
19
  def respond_to?(name, include_private = false)
20
- super || @@options.key?(name.to_sym)
20
+ super || key?(name.to_sym)
21
21
  end
22
22
 
23
23
  private
24
24
 
25
25
  def method_missing(name, *args, &blk)
26
26
  if name.to_s =~ /=$/
27
- @@options[$`.to_sym] = args.first
28
- elsif @@options.key?(name)
29
- @@options[name]
27
+ self[$`.to_sym] = args.first
28
+ elsif self.key?(name)
29
+ self[name]
30
30
  else
31
31
  super
32
32
  end
@@ -18,30 +18,20 @@ module LeanCloud
18
18
  end
19
19
  end
20
20
 
21
- def route(name, options={})
22
- add_route(name, options)
21
+ def route(name, options={}, &block)
22
+ add_route(name, options, &block)
23
23
  end
24
24
 
25
- def match(name, options={})
25
+ def match(name, options={}, &block)
26
26
  raise "`as' can't be nil!" if !options[:as]
27
- add_route(options.delete(:as), options, name)
27
+ add_route(options.delete(:as), options, name, &block)
28
28
  end
29
29
 
30
- def add_route(name, options={},match=nil)
30
+ def add_route(name, options={},match=nil, &block)
31
31
  options[:namespace] ||= namespace
32
- routes << Route.new(name, options, match)
33
- end
34
-
35
- def respond_to?(method_id)
36
- routes.any? {|route| route.name.to_s == method_id.to_s}
37
- end
38
-
39
- def method_missing(method_id, *args, &block)
40
- if route=routes.find {|route| route.name.to_s == method_id.to_s}
41
- dispatch(route, *args, &block)
42
- else
43
- super
44
- end
32
+ route = Route.new(name, options, match, &block)
33
+ define_singleton_method(name) {|*args, &block| dispatch(route, *args, &block) }
34
+ routes << route
45
35
  end
46
36
  end
47
37
  end
@@ -2,5 +2,5 @@
2
2
  LeanCloud::Base.register "Function", namespace: "functions" do
3
3
  # /1.1/functions
4
4
  # 调用 Cloud Code 函数
5
- only :create
5
+ match ":name", via: :post, as: :run
6
6
  end
@@ -1,4 +1,4 @@
1
- LeanCloud::Base.register "Message", namespace: "messages" do
1
+ LeanCloud::Base.register "Message", namespace: "rtm/messages" do
2
2
  # /1.1/rtm/messages/logs/
3
3
  # 获得应用聊天记录
4
4
  route :logs
@@ -2,8 +2,8 @@
2
2
  LeanCloud::Base.register "Sms" do
3
3
  # /1.1/requestSmsCode
4
4
  # 请求发送短信验证码
5
- route :requestSmsCode, via: :post
5
+ route :requestSmsCode, via: :post, as: :deliver
6
6
  # /1.1/verifySmsCode/<code>
7
7
  # 验证短信验证码
8
- match "verifySmsCode/:code", via: :post, as: :verify_sms_code
8
+ match "verifySmsCode/:code", via: :post, as: :verify
9
9
  end
@@ -1,8 +1,8 @@
1
1
  LeanCloud::Base.register "Stats",namespace: "stats" do
2
2
  # /1.1/stats/appinfo
3
3
  # 获取应用的基本信息
4
- route :appinfo, via: :post
4
+ route :appinfo, via: :get
5
5
  # /1.1/stats/appmetrics
6
6
  # 获取应用的具体统计数据
7
- route :appmetrics, via: :post
7
+ route :appmetrics, via: :get
8
8
  end
@@ -3,19 +3,19 @@ LeanCloud::Base.register "User", namespace: "users" do
3
3
  # /1.1/login GET 用户登录
4
4
  route :login, via: :get, unscope: true
5
5
  # /1.1/users/<objectId>/updatePassword PUT 更新密码,要求输入旧密码。
6
- route :updatePassword, via: :put, on: :member
6
+ route :updatePassword, via: :put, on: :member, unscope: true
7
7
  # /1.1/requestPasswordReset POST 请求密码重设
8
- route :requestPasswordReset, via: :post
8
+ route :requestPasswordReset, via: :post, unscope: true
9
9
  # /1.1/requestEmailVerify POST 请求验证用户邮箱
10
- route :requestEmailVerify, via: :post
10
+ route :requestEmailVerify, via: :post, unscope: true
11
11
  # /1.1/requestMobilePhoneVerify POST 请求发送用户手机号码验证短信
12
- route :requestMobilePhoneVerify, via: :post
12
+ route :requestMobilePhoneVerify, via: :post, unscope: true
13
13
  # /1.1/verifyMobilePhone/<code> POST 使用 "验证码" 验证用户手机号码
14
- match "verifyMobilePhone/:code", via: :post, as: :verify_mobile_phone
14
+ match "verifyMobilePhone/:code", via: :post, as: :verify_mobile_phone, unscope: true
15
15
  # /1.1/requestLoginSmsCode POST 请求发送手机号码登录短信。
16
- route :requestLoginSmsCode, via: :post
16
+ route :requestLoginSmsCode, via: :post, unscope: true
17
17
  # /1.1/requestPasswordResetBySmsCode POST 请求发送手机短信验证码重置用户密码。
18
- route :requestPasswordResetBySmsCode, via: :post
18
+ route :requestPasswordResetBySmsCode, via: :post, unscope: true
19
19
  # /1.1/resetPasswordBySmsCode/<code> PUT 验证手机短信验证码并重置密码。
20
- match "resetPasswordBySmsCode/:code", via: :put, as: :reset_password_by_sms_code
20
+ match "resetPasswordBySmsCode/:code", via: :put, as: :reset_password_by_sms_code, unscope: true
21
21
  end
@@ -1,18 +1,19 @@
1
1
  module LeanCloud
2
2
  class Route
3
- attr_accessor :name, :request, :match, :options
4
- def initialize(name, options, match=false)
3
+ attr_accessor :name, :request, :match, :options, :block
4
+ def initialize(name, options, match=false, &block)
5
5
  options[:on] ||= :collection
6
6
  @name = name
7
7
  @request = options[:via] ||= :get
8
8
  @match = match
9
9
  @options = options
10
+ @block = block
10
11
  end
11
12
 
12
13
  def url(*args)
13
14
  namespace = options[:namespace] if !options[:unscope]
14
15
  id = args.shift if options[:on].to_sym == :member
15
- path = !match ? name.to_s : match.sub(/(:\w+)/, args[0]) if !options[:root]
16
+ path = !match ? name.to_s : match.sub(/(:\w+)/, args[0].to_s) if !options[:root]
16
17
  [namespace, id, path].compact.join('/')
17
18
  end
18
19
  end
@@ -1,3 +1,3 @@
1
1
  module LeanCloud
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lean_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - plusrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-21 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -78,6 +78,10 @@ files:
78
78
  - MIT-LICENSE
79
79
  - README.md
80
80
  - Rakefile
81
+ - examples/AVOSRealtimeGroups.rb
82
+ - examples/file.rb
83
+ - examples/live_file.rb
84
+ - examples/live_room.rb
81
85
  - lean_cloud.gemspec
82
86
  - lib/lean_cloud.rb
83
87
  - lib/lean_cloud/base.rb