sendbird 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: 345394b76bf73ff1d473718670ae6093b7e0d659
4
- data.tar.gz: 9ef4b5eb091e0c85f4aef67994c7bab7cb82c9d8
3
+ metadata.gz: 96641c7ad6c001d69d96555bcae0d61fbf6fa3db
4
+ data.tar.gz: 7fc0e43271a581040130ff43e7444151c333b3db
5
5
  SHA512:
6
- metadata.gz: dc88d376b180ddbe99ad6f48ca957843ce80bcdb7a45bcdec25e1fff42aef90f0ed0b8be96743356120bae4d47bb3be47593091e32d44d3da10794dbcd092875
7
- data.tar.gz: 2b5731b29fdcbaa32c33eaa344f97419d49f1c14bc077b680fb65d1f7b5dc728967ddc1c40e8308bf30d246a52ef7240303e5557e18ec522042311ff11551a8f
6
+ metadata.gz: 3dbe8cb40265ff89e2b23ca19875d35860a65d51df3d8b02ba2fe4fe53ff35443cea9b1639cd3e6dea032b1f534bad2764e9be154d6e2a2c7940f1de1ad56431
7
+ data.tar.gz: 4244a7940324e66f5a70746cffff360353c79df2f518493d1a01a10a44bc7ff6701345696c8967725a5568b315a8ed4bd8a5511176c80f8300cc5e9cad576920
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  /config/secrets.yml
11
11
  .ruby-gemset
12
+ .ruby-version
@@ -3,3 +3,5 @@ language: ruby
3
3
  rvm:
4
4
  - 2.3.0
5
5
  before_install: gem install bundler -v 1.13.1
6
+ script:
7
+ - rake
data/README.md CHANGED
@@ -1,28 +1,254 @@
1
1
  # Sendbird
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sendbird`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Sendbird is a ruby Wrapper for the Sendbird [API](https://docs.sendbird.com/platform)
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
10
-
11
7
  ```ruby
12
8
  gem 'sendbird'
13
9
  ```
14
10
 
15
- And then execute:
11
+ ## Requirements
16
12
 
17
- $ bundle
13
+ You must provide:
14
+ - Your list of applications, been the Applications name and its Api Key.
15
+ - Your username and password.
16
+ You can find your applications list [here](https://dashboard.sendbird.com).
17
+ By setting a default, will set the api_key in the request where need, if not you will have to provide a app parameter to every method call.
18
18
 
19
- Or install it yourself as:
20
19
 
21
- $ gem install sendbird
20
+ Rails way:
21
+ ```ruby
22
+ Sendbird.config do |c|
23
+ c.applications = {app_name: 'API_KEY'}
24
+ c.user = 'username'
25
+ c.password = 'password'
26
+ c.default_app = 'app_name'
27
+ end
28
+ ```
29
+
30
+ Ruby way:
31
+ ```ruby
32
+ Sendbird.applications = {app_name: 'API_KEY'}
33
+ Sendbird.user = 'username'
34
+ Sendbird.password = 'password'
35
+ Sendbird.default_app = 'app_name'
36
+ ```
22
37
 
23
38
  ## Usage
24
39
 
25
- TODO: Write usage instructions here
40
+ ## Api
41
+
42
+ There are two types of classes inside the Wrapper the one ending with Api, like `Sendbird::UserApi` or `Sendbird::MessageApi`, this classes act like simple wrapper for the Sendbird api.
43
+
44
+ The Sendbird Api is quite big, to understand how to work with this gem, just remenber that every event from the api map to a class method from that Api class.
45
+
46
+ Example:
47
+ ```ruby
48
+ body = {
49
+ "user_id" => "string",
50
+ "nickname" => "string",
51
+ "profile_url" => "string",
52
+ "issue_access_token" => "boolean"
53
+ }
54
+ Sendbird::UserApi.create(body)
55
+ ```
56
+
57
+ All methods that accept a variables in the uri, they will argument in order for the method, and all request arguments or parameters, are the last arguments as hash.
58
+
59
+ Example for event [Message send](https://docs.sendbird.com/platform#messages):
60
+ ```ruby
61
+ request_body = {
62
+ "message_type" => "MESG",
63
+ "user_id" => "string",
64
+ "message" => "string",
65
+ "data" => "string",
66
+ "mark_as_read" => "boolean"
67
+ }
68
+ Sendbird::MessageApi.send('group_channels', 'group_channels_url', request_body)
69
+ ```
70
+
71
+ Example for event [User list](https://docs.sendbird.com/platform#messages):
72
+ ```ruby
73
+ request_parameters = {
74
+ "token" => "string",
75
+ "limit" => "int",
76
+ "user_ids" => "string"
77
+ }
78
+ Sendbird::UserApi.list(request_parameters)
79
+ ```
80
+
81
+ Example for sending a message to a different app:
82
+ ```ruby
83
+ request_parameters = {
84
+ "token" => "string",
85
+ "limit" => "int",
86
+ "user_ids" => "string",
87
+ "app" => 'string' # This app has to be in the configuration
88
+ }
89
+ Sendbird::UserApi.list(request_parameters)
90
+ ```
91
+
92
+ All methods return a `Sendbird::Response` with have the next methods:
93
+ #### body
94
+ Returns the parsed body of the request
95
+ #### status
96
+ Returns the status code of the request
97
+ #### error_code
98
+ Returns the error_code from the request, if their is one
99
+ #### error_message
100
+ Returns the error_message from the request, if their is one
101
+
102
+ ## List of all classes and it's class methods.
103
+
104
+ ### Sendbird::UserApi
105
+ ```ruby
106
+ view(user_id)
107
+ create(body)
108
+ list(params={})
109
+ update(user_id, body)
110
+ unread_count(user_id)
111
+ activate(user_id, body)
112
+ block(user_id, body)
113
+ unblock(user_id, unblock_user_id)
114
+ block_list(user_id, params={})
115
+ mark_as_read_all(user_id)
116
+ register_gcm_token(user_id, token)
117
+ register_apns_token(user_id, token)
118
+ unregister_gcm_token(user_id, token)
119
+ unregister_apns_token(user_id, token)
120
+ unregister_all_device_token(user_id)
121
+ push_preferences(user_id)
122
+ update_push_preferences(user_id, body)
123
+ delete_push_preferences(user_id)
124
+ ```
125
+
126
+ ### Sendbird::OpenChannelApi
127
+ ```ruby
128
+ view(channel_url, params={})
129
+ create(body={})
130
+ list(params={})
131
+ destroy(channel_url)
132
+ update(channel_url, body)
133
+ participants(channel_url, params)
134
+ freeze(channel_url, body)
135
+ ban_user(channel_url, body)
136
+ ban_list(channel_url, params={})
137
+ ban_update(channel_url, user_id, body)
138
+ ban_delete(channel_url, user_id)
139
+ ban_view(channel_url, user_id)
140
+ mute(channel_url, body)
141
+ mute_list(channel_url, params={})
142
+ mute_delete(channel_url, user_id)
143
+ mute_view(channel_url, user_id)
144
+ ```
145
+
146
+ ### Sendbird::GroupChannelApi
147
+ ```ruby
148
+ create(body)
149
+ list(params={})
150
+ update(channel_url, body)
151
+ destroy(channel_url)
152
+ view(channel_url, params={})
153
+ members(channel_url, params={})
154
+ is_member?(channel_url, user_id)
155
+ invite(channel_url, body)
156
+ hide(channel_url, body)
157
+ leave(channel_url, body)
158
+ ```
159
+
160
+ ### Sendbird::MetaCounterApi && Sendbird::MetaDataApi
161
+ ```ruby
162
+ create(channel_type, channel_url, body)
163
+ view(channel_type, channel_url, params={})
164
+ view_by_key(channel_type, channel_url, key)
165
+ update(channel_type, channel_url, body)
166
+ update_by_key(channel_type, channel_url, key, body)
167
+ destroy(channel_type, channel_url)
168
+ destroy_by_key(channel_type, channel_url, key)
169
+ ```
170
+
171
+ ### Sendbird::ApplicationApi
172
+ ```ruby
173
+ create(body)
174
+ list(params={})
175
+ destroy_all
176
+ destroy
177
+ profanaty(body={})
178
+ ccu
179
+ mau(params={})
180
+ dau(params={})
181
+ daily_message_count(params={})
182
+ gcm_push_configuration
183
+ apns_push_configuration
184
+ ```
185
+
186
+ ## Interface
187
+
188
+ This is the second type of class inside the gem, acts as interface.
189
+
190
+ Currently only supporting `User`, in the future I will add the rest of interfaces.
191
+
192
+ There are three ways to interact with the user interface:
193
+
194
+ #### Block
195
+ ```ruby
196
+ Sendbird::User.new('testing_user_interface_1') do |u|
197
+ u.nickname('Yolo')
198
+ u.profile_url('udbue')
199
+ u.timezone('Europe/London')
200
+ u.request!
201
+ end
202
+ ```
203
+
204
+ #### Chain Methods
205
+ ```ruby
206
+ user = Sendbird::User.new('testing_user_interface_1')
207
+ user.nickname('Yolo').profile_url('udbue').timezone('Europe/London').request!
208
+ ```
209
+
210
+ #### Simple methods
211
+ ```ruby
212
+ user = Sendbird::User.new('testing_user_interface_1')
213
+ user.nickname='Yolo'
214
+ user.profile_url=('udbue')
215
+ user.timezone=('Europe/London')
216
+ user.request!
217
+ ```
218
+
219
+ This interface provide you with multiple methods to work with the Sendbird Api:
220
+
221
+ ```ruby
222
+ user_information=(user_information={})
223
+ nickname=(nickname)
224
+ profile_url=(profile_url)
225
+ issue_access_token=(issue_access_token)
226
+ push_preferences=(push_preferences={})
227
+ timezone=(timezone)
228
+ start_hour=(start_hour)
229
+ end_hour=(end_hour)
230
+ start_min=(start_min)
231
+ end_min=(end_min)
232
+ activate
233
+ deactivate
234
+ mark_as_read_all
235
+ register_gcm_token(token)
236
+ register_apns_token(token)
237
+ unregister_gcm_token(token)
238
+ unregister_apns_token(token)
239
+ unregister_all_device_token
240
+ ```
241
+
242
+ The interface will store all the pending requests called before the `request!`, when triggering the `request!` method will optimize how many request, have to be done, you don't have to worry about that.
243
+
244
+ The User interface also provide some attributes: `gcm_tokens` and `apns_tokens`, they will store the different token this user have, so by triggering any of the `register_*` methods will store or remove from this attributes.
245
+
246
+ The user interface also provide some getter methods that will execute the request instantly, this methods return the info response body. This methods are:
247
+ ```ruby
248
+ get_user
249
+ get_unread_count
250
+ get_push_preferences
251
+ ```
26
252
 
27
253
  ## Development
28
254
 
@@ -38,4 +264,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
38
264
  ## License
39
265
 
40
266
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -1 +1,6 @@
1
- api_key:
1
+ applications:
2
+ App_name: 'api_key'
3
+ App_name: 'api_key'
4
+ user:
5
+ password:
6
+ default_app:
@@ -1,14 +1,19 @@
1
1
  require "sendbird/client"
2
2
  require "sendbird/configuration"
3
+ require "sendbird/invalid_request"
3
4
  require "sendbird/response"
5
+ require "sendbird/request_handler"
6
+ require "sendbird/application_api"
4
7
  require "sendbird/user_api"
5
8
  require "sendbird/user"
6
- require "sendbird/open_channel"
9
+ require "sendbird/open_channel_api"
10
+ require "sendbird/group_channel_api"
7
11
  require "sendbird/group_channel"
12
+ require "sendbird/message_api"
8
13
  require "sendbird/message"
9
14
  require "sendbird/meta_base"
10
- require "sendbird/meta_data"
11
- require "sendbird/meta_counter"
15
+ require "sendbird/meta_data_api"
16
+ require "sendbird/meta_counter_api"
12
17
  require "sendbird/version"
13
18
 
14
19
  module Sendbird
@@ -0,0 +1,57 @@
1
+ module Sendbird
2
+ class ApplicationApi
3
+ extend Client
4
+ ENDPOINT = 'applications'.freeze
5
+
6
+ class << self
7
+ def create(body)
8
+ post_http_basic(path: build_url, body: body)
9
+ end
10
+
11
+ def list(params={})
12
+ get_http_basic(path: build_url, params: params)
13
+ end
14
+
15
+ # Right now this endpoint is failing in there site
16
+ # def view
17
+ # get(path: build_url)
18
+ # end
19
+
20
+ def destroy_all
21
+ delete_http_basic(path: build_url)
22
+ end
23
+
24
+ def destroy
25
+ delete(path: build_url)
26
+ end
27
+
28
+ def profanaty(body={})
29
+ put(path: build_url('profanity'), body: body)
30
+ end
31
+
32
+ def ccu
33
+ get(path: build_url('ccu'))
34
+ end
35
+
36
+ def mau(params={})
37
+ get(path: build_url('mau'), params: params)
38
+ end
39
+
40
+ def dau(params={})
41
+ get(path: build_url('dau'), params: params)
42
+ end
43
+
44
+ def daily_message_count(params={})
45
+ get(path: build_url('daily_count'), params: params)
46
+ end
47
+
48
+ def gcm_push_configuration
49
+ get(path: build_url('push', 'gcm'))
50
+ end
51
+
52
+ def apns_push_configuration
53
+ get(path: build_url('push', 'apns'))
54
+ end
55
+ end
56
+ end
57
+ end
@@ -3,12 +3,24 @@ require 'faraday'
3
3
  module Sendbird
4
4
  module Client
5
5
  class ApiKeyMissingError < StandardError; end
6
+ class HttpBasicMissing < StandardError; end
7
+ class NotValidApplication < StandardError; end
8
+
6
9
  PUBLIC_METHODS = [:get, :post, :put, :delete]
7
10
 
8
11
  PUBLIC_METHODS.each do |method|
9
12
  define_method(method) do |path: , params: nil , body: nil|
10
- fail ApiKeyMissingError.new(api_key_message) if Sendbird.api_key.nil?
11
- response = request(method: method, path: path, params: params, body: body)
13
+ params, body, app = get_app_from_params_or_body(params, body)
14
+ fail ApiKeyMissingError.new(api_key_message) unless api_key(app)
15
+ response = api_token_request(method: method, path: path, params: params, body: body)
16
+ Response.new(response.status, response.body)
17
+ end
18
+ end
19
+
20
+ PUBLIC_METHODS.each do |method|
21
+ define_method("#{method}_http_basic") do |path: , params: nil , body: nil|
22
+ fail HttpBasicMissing.new(http_basic_message) if sendbird_user.nil? || sendbird_password.nil?
23
+ response = http_basic_request(method: method, path: path, params: params, body: body)
12
24
  Response.new(response.status, response.body)
13
25
  end
14
26
  end
@@ -22,7 +34,33 @@ module Sendbird
22
34
  end
23
35
  end
24
36
 
37
+
25
38
  private
39
+
40
+ def get_app_from_params_or_body(params, body)
41
+ app = if params && params.has_key?(:app)
42
+ params.delete(:app)
43
+ elsif body && body.has_key?(:app)
44
+ body.delete(:app)
45
+ else
46
+ nil
47
+ end
48
+ [params, body, app]
49
+ end
50
+
51
+ def api_key(app)
52
+ if app
53
+ if api_key = Sendbird.applications[app]
54
+ @api_key = api_key
55
+ else
56
+ fail NotValidApplication.new(invalid_application_message(app))
57
+ end
58
+ else
59
+ @api_key = Sendbird.applications[Sendbird.default_app]
60
+ end
61
+ @api_key
62
+ end
63
+
26
64
  def conn
27
65
  @conn ||= Faraday.new(url: Sendbird::Configuration::SENDBIRD_ENDPOINT) do |c|
28
66
  c.request :url_encoded
@@ -30,17 +68,49 @@ module Sendbird
30
68
  end
31
69
  end
32
70
 
33
- def request(method:, path:, params:, body:)
71
+ def http_basic_conn
72
+ @http_basic_conn ||= Faraday.new(url: Sendbird::Configuration::SENDBIRD_ENDPOINT) do |c|
73
+ c.request :url_encoded
74
+ c.adapter Faraday.default_adapter
75
+ c.basic_auth(sendbird_user, sendbird_password)
76
+ end
77
+ end
78
+
79
+ def sendbird_user
80
+ Sendbird.user
81
+ end
82
+
83
+ def sendbird_password
84
+ Sendbird.password
85
+ end
86
+
87
+ def api_token_request(method:, path:, params:, body:)
34
88
  conn.send(method) do |req|
35
89
  req.url path, params
36
- req.headers['Api-Token'] = Sendbird.api_key
90
+ req.headers['Api-Token'] = @api_key
91
+ req.headers['Content-Type'] = 'application/json, charset=utf8'
92
+ req.body = body.to_json if body
93
+ end
94
+ end
95
+
96
+ def http_basic_request(method:, path:, params:, body:)
97
+ http_basic_conn.send(method) do |req|
98
+ req.url path, params
37
99
  req.headers['Content-Type'] = 'application/json, charset=utf8'
38
100
  req.body = body.to_json if body
39
101
  end
40
102
  end
41
103
 
42
104
  def api_key_message
43
- 'Please set up your api key'
105
+ 'Plase set up your applications and default_app'
106
+ end
107
+
108
+ def http_basic_message
109
+ 'Please set up you http basic information to be able to execute this requets'
110
+ end
111
+
112
+ def invalid_application_message(app)
113
+ "Application name (#{app}) not found in the configuration, please check your configuration"
44
114
  end
45
115
  end
46
116
  end
@@ -1,6 +1,6 @@
1
1
  module Sendbird
2
2
  module Configuration
3
- PUBLIC_METHODS = [:api_key]
3
+ PUBLIC_METHODS = [:applications, :user, :password, :default_app]
4
4
 
5
5
  SENDBIRD_ENDPOINT = 'https://api.sendbird.com/v3/'
6
6
 
@@ -1,48 +1,52 @@
1
1
  module Sendbird
2
2
  class GroupChannel
3
- extend Client
4
- ENDPOINT = 'group_channels'.freeze
5
-
6
- class << self
7
- def create(body)
8
- post(path: build_url, body: body)
9
- end
10
-
11
- def list(params={})
12
- get(path: build_url, params: params)
13
- end
14
-
15
- def update(channel_url, body)
16
- put(path: build_url(channel_url), body: body)
17
- end
18
-
19
- def destroy(channel_url)
20
- delete(path: build_url(channel_url))
21
- end
3
+ class MissingUserId < StandardError; end
4
+ include RequestHandler
5
+
6
+ api_class GroupChannelApi
7
+ default_arg :channel_url
8
+
9
+ attr_reader :channel_url, :user_id, :pending_requests
10
+ def initialize(channel_url, user_id = :missing_user_id)
11
+ @channel_url = channel_url
12
+ @user_id = user_id
13
+ @pending_requests = {}
14
+ yield(self) if block_given?
15
+ end
22
16
 
23
- def view(channel_url, params={})
24
- get(path: build_url(channel_url), params: params)
25
- end
17
+ def name=(name)
18
+ merge_arguments(:update, {name: name})
19
+ self
20
+ end
26
21
 
27
- def members(channel_url, params={})
28
- get(path: build_url(channel_url, 'members'), params: params)
29
- end
22
+ def cover_url=(cover_url)
23
+ merge_arguments(:update, {cover_url: cover_url})
24
+ self
25
+ end
30
26
 
31
- def is_member?(channel_url, user_id)
32
- get(path: build_url(channel_url, 'members', user_id))
33
- end
27
+ def data=(data)
28
+ merge_arguments(:update, {data: data})
29
+ self
30
+ end
34
31
 
35
- def invite(channel_url, body)
36
- post(path: build_url(channel_url, 'invite'), body: body)
37
- end
32
+ def is_distinct=(is_distinct)
33
+ merge_arguments(:update, {is_distinct: is_distinct})
34
+ self
35
+ end
38
36
 
39
- def hide(channel_url, body)
40
- put(path: build_url(channel_url, 'hide'), body: body)
41
- end
37
+ def send_message(type, data)
38
+ message.send(type, data)
39
+ end
42
40
 
43
- def leave(channel_url, body)
44
- put(path: build_url(channel_url, 'leave'), body: body)
41
+ def message
42
+ return @message if defined?(@message)
43
+ if user_id == :missing_user_id
44
+ raise MissingUserId, 'Please provide an user_id at initialize to been able to use send_message'
45
45
  end
46
+ @message ||= Message.new(user_id, channel_url, 'group_channels')
46
47
  end
48
+
49
+ private
50
+ attr_writer :pending_requests
47
51
  end
48
52
  end
@@ -0,0 +1,48 @@
1
+ module Sendbird
2
+ class GroupChannelApi
3
+ extend Client
4
+ ENDPOINT = 'group_channels'.freeze
5
+
6
+ class << self
7
+ def create(body)
8
+ post(path: build_url, body: body)
9
+ end
10
+
11
+ def list(params={})
12
+ get(path: build_url, params: params)
13
+ end
14
+
15
+ def update(channel_url, body)
16
+ put(path: build_url(channel_url), body: body)
17
+ end
18
+
19
+ def destroy(channel_url)
20
+ delete(path: build_url(channel_url))
21
+ end
22
+
23
+ def view(channel_url, params={})
24
+ get(path: build_url(channel_url), params: params)
25
+ end
26
+
27
+ def members(channel_url, params={})
28
+ get(path: build_url(channel_url, 'members'), params: params)
29
+ end
30
+
31
+ def is_member?(channel_url, user_id)
32
+ get(path: build_url(channel_url, 'members', user_id))
33
+ end
34
+
35
+ def invite(channel_url, body)
36
+ post(path: build_url(channel_url, 'invite'), body: body)
37
+ end
38
+
39
+ def hide(channel_url, body)
40
+ put(path: build_url(channel_url, 'hide'), body: body)
41
+ end
42
+
43
+ def leave(channel_url, body)
44
+ put(path: build_url(channel_url, 'leave'), body: body)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,4 @@
1
+ module Sendbird
2
+ class InvalidRequest < StandardError
3
+ end
4
+ end
@@ -1,42 +1,44 @@
1
1
  module Sendbird
2
2
  class Message
3
- extend Client
3
+ class InvalidMessageType < StandardError; end
4
+ attr_reader :user_id, :channel_url, :channel_type
4
5
 
5
- class << self
6
- def send(channel_type, channel_url, body)
7
- post(path: build_url(channel_type, channel_url, 'messages'), body: body)
8
- end
9
-
10
- # Get messages function can be called only
11
- # from Park or Enterprise plan.
12
- def list(channel_type, channel_url, params)
13
- get(path: build_url(channel_type, channel_url, 'messages'), params: params)
14
- end
15
-
16
- def view(channel_type, channel_url, message_id)
17
- get(path: build_url(channel_type, channel_url, 'messages', message_id))
18
- end
19
-
20
- def destroy(channel_type, channel_url, message_id)
21
- delete(path: build_url(channel_type, channel_url, 'messages', message_id))
22
- end
23
-
24
- def mark_as_read(channel_type, channel_url, body)
25
- put(path: build_url(channel_type, channel_url, 'messages', 'mark_as_read'), body: body)
26
- end
6
+ def initialize(user_id, channel_url, channel_type)
7
+ @user_id = user_id
8
+ @channel_url = channel_url
9
+ @channel_type = channel_type
10
+ end
27
11
 
28
- def count(channel_type, channel_url)
29
- get(path: build_url(channel_type, channel_url, 'messages', 'total_count'))
30
- end
12
+ def send(type, data)
13
+ MessageApi.send(channel_type, channel_url, message_body(type).merge(data))
14
+ self
15
+ end
31
16
 
32
- def unread_count(channel_type, channel_url, params)
33
- get(path: build_url(channel_type, channel_url, 'messages', 'unread_count'), params: params)
17
+ private
18
+ def message_type(type)
19
+ case type
20
+ when :text then 'MESG'
21
+ when :file then 'FILE'
22
+ when :admin then 'ADMM'
23
+ else
24
+ raise InvalidMessageType, "Please provide a valid message type, valid types are: [:text, :file, :admin]"
34
25
  end
35
26
  end
36
27
 
37
- def self.build_url(*args)
38
- args_dup = args.dup
39
- args_dup.join('/')
28
+ def message_body(type)
29
+ case type
30
+ when :text, :file
31
+ {
32
+ "message_type": message_type(type),
33
+ "user_id": user_id
34
+ }
35
+ when :admin
36
+ {
37
+ "message_type": message_type(type),
38
+ }
39
+ else
40
+ raise InvalidMessageType, "Please provide a valid message type, valid types are: [:text, :file, :admin]"
41
+ end
40
42
  end
41
43
  end
42
44
  end
@@ -0,0 +1,42 @@
1
+ module Sendbird
2
+ class MessageApi
3
+ extend Client
4
+
5
+ class << self
6
+ def send(channel_type, channel_url, body)
7
+ post(path: build_url(channel_type, channel_url, 'messages'), body: body)
8
+ end
9
+
10
+ # Get messages function can be called only
11
+ # from Park or Enterprise plan.
12
+ def list(channel_type, channel_url, params)
13
+ get(path: build_url(channel_type, channel_url, 'messages'), params: params)
14
+ end
15
+
16
+ def view(channel_type, channel_url, message_id)
17
+ get(path: build_url(channel_type, channel_url, 'messages', message_id))
18
+ end
19
+
20
+ def destroy(channel_type, channel_url, message_id)
21
+ delete(path: build_url(channel_type, channel_url, 'messages', message_id))
22
+ end
23
+
24
+ def mark_as_read(channel_type, channel_url, body)
25
+ put(path: build_url(channel_type, channel_url, 'messages', 'mark_as_read'), body: body)
26
+ end
27
+
28
+ def count(channel_type, channel_url)
29
+ get(path: build_url(channel_type, channel_url, 'messages', 'total_count'))
30
+ end
31
+
32
+ def unread_count(channel_type, channel_url, params)
33
+ get(path: build_url(channel_type, channel_url, 'messages', 'unread_count'), params: params)
34
+ end
35
+ end
36
+
37
+ def self.build_url(*args)
38
+ args_dup = args.dup
39
+ args_dup.join('/')
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module Sendbird
2
- class MetaCounter < MetaBase
2
+ class MetaCounterApi < MetaBase
3
3
  ENDPOINT = 'metacounter'.freeze
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  module Sendbird
2
- class MetaData < MetaBase
2
+ class MetaDataApi < MetaBase
3
3
  ENDPOINT = 'metadata'.freeze
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  module Sendbird
2
- class OpenChannel
2
+ class OpenChannelApi
3
3
  extend Client
4
4
  ENDPOINT = 'open_channels'.freeze
5
5
 
@@ -0,0 +1,41 @@
1
+ require_relative "./request_handler/request"
2
+ require_relative "./request_handler/request_merger"
3
+
4
+ module Sendbird
5
+ module RequestHandler
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ def request!
11
+ Request.new(pending_requests, api_class, default_arg).execute
12
+ rescue InvalidRequest => e
13
+ raise e
14
+ ensure
15
+ self.pending_requests = {}
16
+ self
17
+ end
18
+
19
+ def merge_arguments(method, args, callback=nil)
20
+ self.pending_requests = RequestMerger.call(pending_requests, method, args, callback)
21
+ end
22
+
23
+ def default_arg
24
+ self.send(self.class.instance_variable_get(:@default_arg))
25
+ end
26
+
27
+ def api_class
28
+ self.class.instance_variable_get(:@api_class)
29
+ end
30
+
31
+ module ClassMethods
32
+ def api_class(api_class)
33
+ @api_class = api_class
34
+ end
35
+
36
+ def default_arg(method)
37
+ @default_arg = method
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ module Sendbird
2
+ module RequestHandler
3
+ class Request
4
+ attr_reader :pending_requests, :api_class, :default_argument
5
+ def initialize(pending_requests, api_class, default_argument)
6
+ @pending_requests = pending_requests
7
+ @api_class = api_class
8
+ @default_argument = default_argument
9
+ end
10
+
11
+ def execute
12
+ pending_requests.each do |method, params|
13
+ response = case params[:args]
14
+ when Array
15
+ if params[:args].any?
16
+ api_class.send(method, default_argument, *params[:args])
17
+ else
18
+ api_class.send(method, default_argument)
19
+ end
20
+ when Hash
21
+ api_class.send(method, default_argument, params[:args])
22
+ end
23
+ handle_response_status(response, method, params[:args])
24
+ execute_callbacks(params[:callback], response)
25
+ end
26
+ end
27
+
28
+ def handle_response_status(response, method, arguments)
29
+ if response.status != 200
30
+ raise InvalidRequest, "#{response.error_message} executing #{method} with arguments: #{arguments}"
31
+ end
32
+ end
33
+
34
+ def execute_callbacks(callbacks, response)
35
+ callbacks.each do |cb|
36
+ cb.call(response)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ module Sendbird
2
+ module RequestHandler
3
+ class RequestMerger
4
+ def self.call(pending_requests, method, args, callback)
5
+ new(pending_requests, method, args, callback).merge
6
+ end
7
+
8
+ attr_reader :pending_requests, :method, :args, :callback
9
+ def initialize(pending_requests, method, args, callback)
10
+ @pending_requests = pending_requests
11
+ @method = method
12
+ @args = args
13
+ @callback = callback
14
+ end
15
+
16
+ def merge
17
+ pending_requests[method] ||= {}
18
+ case args
19
+ when Hash
20
+ merge_hash_arguments(args)
21
+ else
22
+ merge_rest_of_arguments(args)
23
+ end
24
+ append_callbacks(args)
25
+ pending_requests
26
+ end
27
+
28
+ def merge_hash_arguments(args)
29
+ pending_requests[method][:args] ||= {}
30
+ pending_requests[method][:args] = pending_requests[method][:args].merge(args)
31
+ end
32
+
33
+ def merge_rest_of_arguments(args)
34
+ pending_requests[method][:args] ||= []
35
+ pending_requests[method][:args] = pending_requests[method][:args] << args if args
36
+ end
37
+
38
+ def append_callbacks(args)
39
+ pending_requests[method][:callback] ||= []
40
+ pending_requests[method][:callback] = pending_requests[method][:callback] << callback if callback
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,56 +1,178 @@
1
1
  module Sendbird
2
- class User < Struct.new(:user_id, :nickname, :profile_url, :access_token, :last_seen_at, :is_online)
3
- def self.create_or_initialize(params)
4
- case params
5
- when Response
6
- new(*params.body.map{|x| x.last})
7
- when Hash
8
- response = UserApi.show(params[:user_id])
9
- if response.status == 200
10
- new(*response.body.map{|x| x.last})
11
- else
12
- new_user = UserApi.create(params)
13
- new(*new_user.body.map{|x| x.last})
14
- end
2
+ class User
3
+ include RequestHandler
4
+
5
+ api_class UserApi
6
+ default_arg :user_id
7
+
8
+ attr_reader :user_id, :pending_requests, :gcm_tokens, :apns_tokens
9
+ def initialize(user_id)
10
+ @user_id = user_id
11
+ @gcm_tokens = []
12
+ @apns_tokens = []
13
+ @pending_requests = {}
14
+ yield(self) if block_given?
15
+ end
16
+
17
+ def in_sync?
18
+ pending_requests.empty?
19
+ end
20
+
21
+ #Getters
22
+ def get_user
23
+ response = UserApi.view(user_id)
24
+ if response.status == 200
25
+ response.body
26
+ else
27
+ raise InvalidRequest.new(
28
+ error_message(
29
+ response.error_message,
30
+ __method__,
31
+ user_id
32
+ )
33
+ )
34
+ end
35
+ end
36
+
37
+ def get_unread_count
38
+ response = UserApi.unread_count(user_id)
39
+ if response.status == 200
40
+ response.body['unread_count']
41
+ else
42
+ raise InvalidRequest.new(
43
+ error_message(
44
+ response.error_message,
45
+ __method__,
46
+ user_id
47
+ )
48
+ )
15
49
  end
16
50
  end
17
51
 
18
- private_class_method :new
52
+ def get_push_preferences
53
+ response = UserApi.push_preferences(user_id)
54
+ if response.status == 200
55
+ response.body
56
+ else
57
+ raise InvalidRequest.new(
58
+ error_message(
59
+ response.error_message,
60
+ __method__,
61
+ user_id
62
+ )
63
+ )
64
+ end
65
+ end
19
66
 
20
- def view
21
- UserApi.show(user_id)
67
+ # Setters
68
+ def user_information=(user_information={})
69
+ merge_arguments(:update, user_information)
70
+ self
22
71
  end
72
+ alias_method :user_information, :user_information=
23
73
 
24
- def update(body)
25
- UserApi.update(user_id, body)
74
+ def nickname=(nickname)
75
+ merge_arguments(:update, {nickname: nickname})
76
+ self
26
77
  end
78
+ alias_method :nickname, :nickname=
27
79
 
28
- def unread_count
29
- UserApi.unread_count(user_id)
80
+ def profile_url=(profile_url)
81
+ merge_arguments(:update, {profile_url: profile_url})
82
+ self
30
83
  end
84
+ alias_method :profile_url, :profile_url=
31
85
 
32
- def mark_as_read_all
33
- UserApi.mark_as_read_all(user_id)
86
+ def issue_access_token=(issue_access_token)
87
+ merge_arguments(:update, {issue_access_token: issue_access_token})
88
+ self
89
+ end
90
+ alias_method :issue_access_token, :issue_access_token=
91
+
92
+ def activate
93
+ merge_arguments(:activate, { activate: true })
94
+ self
95
+ end
96
+
97
+ def deactivate
98
+ merge_arguments(:activate, { activate: false })
99
+ self
100
+ end
101
+
102
+ def push_preferences=(push_preferences={})
103
+ merge_arguments(:update_push_preferences, push_preferences)
104
+ self
34
105
  end
106
+ alias_method :push_preferences, :push_preferences=
107
+
108
+ def timezone=(timezone)
109
+ merge_arguments(:update_push_preferences, { timezone: timezone })
110
+ self
111
+ end
112
+ alias_method :timezone, :timezone=
113
+
114
+ def start_hour=(start_hour)
115
+ merge_arguments(:update_push_preferences, { start_hour: start_hour })
116
+ self
117
+ end
118
+ alias_method :start_hour, :start_hour=
119
+
120
+ def end_hour=(end_hour)
121
+ merge_arguments(:update_push_preferences, { end_hour: end_hour })
122
+ self
123
+ end
124
+ alias_method :end_hour, :end_hour=
125
+
126
+ def start_min=(start_min)
127
+ merge_arguments(:update_push_preferences, { start_min: start_min })
128
+ self
129
+ end
130
+ alias_method :start_min, :start_min=
131
+
132
+ def end_min=(end_min)
133
+ merge_arguments(:update_push_preferences, { end_min: end_min })
134
+ self
135
+ end
136
+ alias_method :end_min, :end_min=
35
137
 
36
138
  def register_gcm_token(token)
37
- UserApi.register_gcm_token(user_id, token)
139
+ merge_arguments(:register_gcm_token, token, ->(response){ self.gcm_tokens << response.body['token'] })
140
+ self
38
141
  end
39
142
 
40
143
  def register_apns_token(token)
41
- UserApi.register_apns_token(user_id, token)
144
+ merge_arguments(:register_apns_token, token, ->(response){ self.apns_tokens << response.body['token'] })
145
+ self
42
146
  end
43
147
 
44
148
  def unregister_gcm_token(token)
45
- UserApi.unregister_gcm_token(user_id, token)
149
+ merge_arguments(:unregister_gcm_token, token, ->(response){ self.gcm_tokens.delete(response.body['token'])})
150
+ self
46
151
  end
47
152
 
48
153
  def unregister_apns_token(token)
49
- UserApi.unregister_apns_token(user_id, token)
154
+ merge_arguments(:unregister_apns_token, token, ->(response){ self.apns_tokens.delete(response.body['token'])})
155
+ self
50
156
  end
51
157
 
52
158
  def unregister_all_device_token
53
- UserApi.unregister_all_device_token(user_id)
159
+ merge_arguments(:unregister_all_device_token, nil, ->(response) { self.apns_tokens = []; self.gcm_tokens = []; })
160
+ self
161
+ end
162
+
163
+ def mark_as_read_all
164
+ merge_arguments(:mark_as_read_all, {})
165
+ self
166
+ end
167
+
168
+ def group_channel(channel_url)
169
+ GroupChannel.new(channel_url, user_id)
170
+ end
171
+
172
+ private
173
+ attr_writer :pending_requests, :apns_tokens, :gcm_tokens
174
+ def error_message(error_message, method_name, args)
175
+ "Invalid request for User with user_id: #{user_id}, error message: #{error_message} the request method was #{method_name} with args: #{args}"
54
176
  end
55
177
  end
56
178
  end
@@ -4,7 +4,7 @@ module Sendbird
4
4
  ENDPOINT = 'users'.freeze
5
5
 
6
6
  class << self
7
- def show(user_id)
7
+ def view(user_id)
8
8
  get(path: build_url(user_id))
9
9
  end
10
10
 
@@ -1,3 +1,3 @@
1
1
  module Sendbird
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: sendbird
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
  - GustavoCaso
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-17 00:00:00.000000000 Z
11
+ date: 2016-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,14 +128,21 @@ files:
128
128
  - bin/setup
129
129
  - config/secrets.example.yml
130
130
  - lib/sendbird.rb
131
+ - lib/sendbird/application_api.rb
131
132
  - lib/sendbird/client.rb
132
133
  - lib/sendbird/configuration.rb
133
134
  - lib/sendbird/group_channel.rb
135
+ - lib/sendbird/group_channel_api.rb
136
+ - lib/sendbird/invalid_request.rb
134
137
  - lib/sendbird/message.rb
138
+ - lib/sendbird/message_api.rb
135
139
  - lib/sendbird/meta_base.rb
136
- - lib/sendbird/meta_counter.rb
137
- - lib/sendbird/meta_data.rb
138
- - lib/sendbird/open_channel.rb
140
+ - lib/sendbird/meta_counter_api.rb
141
+ - lib/sendbird/meta_data_api.rb
142
+ - lib/sendbird/open_channel_api.rb
143
+ - lib/sendbird/request_handler.rb
144
+ - lib/sendbird/request_handler/request.rb
145
+ - lib/sendbird/request_handler/request_merger.rb
139
146
  - lib/sendbird/response.rb
140
147
  - lib/sendbird/user.rb
141
148
  - lib/sendbird/user_api.rb