stream-chat-ruby 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 420f93801b11e854a13b32d09c2c786bd8ecdbca33e05d499879990a6e24a48d
4
+ data.tar.gz: eb27b4294fe5118be8727a7d232b2b1f472b08c32bc5ea645c677bbaf828f1f7
5
+ SHA512:
6
+ metadata.gz: 0e1287264ddb3aaa6fe2202f1a736d014b925155ee8734a98b01563da949df44670797e783531db85bb5ace72a56ec331fd7666213627251878872e4da978ab9
7
+ data.tar.gz: 7ee3baa7e843a526e384e6eb2913a0c0e55fec83b71c8dc3e2ed2c59638b5d04cb9e86d84aceb07bddf429f39ac8907b69adf2b6c98307b5593a581a79b0ea37
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update bundler
4
+ rvm:
5
+ - 2.4.6
6
+ - 2.5.5
7
+ - 2.6.3
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :dev do
6
+ gem "pry"
7
+ gem "pry-doc"
8
+ gem "method_source"
9
+ end
10
+
11
+ group :test do
12
+ gem "faraday"
13
+ gem "rack"
14
+ gem "simplecov"
15
+ end
16
+
data/LICENSE ADDED
@@ -0,0 +1,219 @@
1
+ SOURCE CODE LICENSE AGREEMENT
2
+
3
+ IMPORTANT - READ THIS CAREFULLY BEFORE DOWNLOADING, INSTALLING, USING OR
4
+ ELECTRONICALLY ACCESSING THIS PROPRIETARY PRODUCT.
5
+
6
+ THIS IS A LEGAL AGREEMENT BETWEEN STREAM.IO, INC. (“STREAM.IO”) AND THE
7
+ BUSINESS ENTITY OR PERSON FOR WHOM YOU (“YOU”) ARE ACTING (“CUSTOMER”) AS THE
8
+ LICENSEE OF THE PROPRIETARY SOFTWARE INTO WHICH THIS AGREEMENT HAS BEEN
9
+ INCLUDED (THE “AGREEMENT”). YOU AGREE THAT YOU ARE THE CUSTOMER, OR YOU ARE AN
10
+ EMPLOYEE OR AGENT OF CUSTOMER AND ARE ENTERING INTO THIS AGREEMENT FOR LICENSE
11
+ OF THE SOFTWARE BY CUSTOMER FOR CUSTOMER’S BUSINESS PURPOSES AS DESCRIBED IN
12
+ AND IN ACCORDANCE WITH THIS AGREEMENT. YOU HEREBY AGREE THAT YOU ENTER INTO
13
+ THIS AGREEMENT ON BEHALF OF CUSTOMER AND THAT YOU HAVE THE AUTHORITY TO BIND
14
+ CUSTOMER TO THIS AGREEMENT.
15
+
16
+ STREAM.IO IS WILLING TO LICENSE THE SOFTWARE TO CUSTOMER ONLY ON THE FOLLOWING
17
+ CONDITIONS: (1) YOU ARE A CURRENT CUSTOMER OF STREAM.IO; (2) YOU ARE NOT A
18
+ COMPETITOR OF STREAM.IO; AND (3) THAT YOU ACCEPT ALL THE TERMS IN THIS
19
+ AGREEMENT. BY DOWNLOADING, INSTALLING, CONFIGURING, ACCESSING OR OTHERWISE
20
+ USING THE SOFTWARE, INCLUDING ANY UPDATES, UPGRADES, OR NEWER VERSIONS, YOU
21
+ REPRESENT, WARRANT AND ACKNOWLEDGE THAT (A) CUSTOMER IS A CURRENT CUSTOMER OF
22
+ STREAM.IO; (B) CUSTOMER IS NOT A COMPETITOR OF STREAM.IO; AND THAT (C) YOU HAVE
23
+ READ THIS AGREEMENT, UNDERSTAND THIS AGREEMENT, AND THAT CUSTOMER AGREES TO BE
24
+ BOUND BY ALL THE TERMS OF THIS AGREEMENT.
25
+
26
+ IF YOU DO NOT AGREE TO ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT,
27
+ STREAM.IO IS UNWILLING TO LICENSE THE SOFTWARE TO CUSTOMER, AND THEREFORE, DO
28
+ NOT COMPLETE THE DOWNLOAD PROCESS, ACCESS OR OTHERWISE USE THE SOFTWARE, AND
29
+ CUSTOMER SHOULD IMMEDIATELY RETURN THE SOFTWARE AND CEASE ANY USE OF THE
30
+ SOFTWARE.
31
+
32
+ 1. SOFTWARE. The Stream.io software accompanying this Agreement, may include
33
+ Source Code, Executable Object Code, associated media, printed materials and
34
+ documentation (collectively, the “Software”). The Software also includes any
35
+ updates or upgrades to or new versions of the original Software, if and when
36
+ made available to you by Stream.io. “Source Code” means computer programming
37
+ code in human readable form that is not suitable for machine execution without
38
+ the intervening steps of interpretation or compilation. “Executable Object
39
+ Code" means the computer programming code in any other form than Source Code
40
+ that is not readily perceivable by humans and suitable for machine execution
41
+ without the intervening steps of interpretation or compilation. “Site” means a
42
+ Customer location controlled by Customer. “Authorized User” means any employee
43
+ or contractor of Customer working at the Site, who has signed a written
44
+ confidentiality agreement with Customer or is otherwise bound in writing by
45
+ confidentiality and use obligations at least as restrictive as those imposed
46
+ under this Agreement.
47
+
48
+ 2. LICENSE GRANT. Subject to the terms and conditions of this Agreement, in
49
+ consideration for the representations, warranties, and covenants made by
50
+ Customer in this Agreement, Stream.io grants to Customer, during the term of
51
+ this Agreement, a personal, non-exclusive, non-transferable, non-sublicensable
52
+ license to:
53
+
54
+ a. install and use Software Source Code on password protected computers at a Site,
55
+ restricted to Authorized Users;
56
+
57
+ b. create derivative works, improvements (whether or not patentable), extensions
58
+ and other modifications to the Software Source Code (“Modifications”) to build
59
+ unique scalable newsfeeds, activity streams, and in-app messaging via Stream’s
60
+ application program interface (“API”);
61
+
62
+ c. compile the Software Source Code to create Executable Object Code versions of
63
+ the Software Source Code and Modifications to build such newsfeeds, activity
64
+ streams, and in-app messaging via the API;
65
+
66
+ d. install, execute and use such Executable Object Code versions solely for
67
+ Customer’s internal business use (including development of websites through
68
+ which data generated by Stream services will be streamed (“Apps”));
69
+
70
+ e. use and distribute such Executable Object Code as part of Customer’s Apps; and
71
+
72
+ f. make electronic copies of the Software and Modifications as required for backup
73
+ or archival purposes.
74
+
75
+ 3. RESTRICTIONS. Customer is responsible for all activities that occur in
76
+ connection with the Software. Customer will not, and will not attempt to: (a)
77
+ sublicense or transfer the Software or any Source Code related to the Software
78
+ or any of Customer’s rights under this Agreement, except as otherwise provided
79
+ in this Agreement, (b) use the Software Source Code for the benefit of a third
80
+ party or to operate a service; (c) allow any third party to access or use the
81
+ Software Source Code; (d) sublicense or distribute the Software Source Code or
82
+ any Modifications in Source Code or other derivative works based on any part of
83
+ the Software Source Code; (e) use the Software in any manner that competes with
84
+ Stream.io or its business; or (e) otherwise use the Software in any manner that
85
+ exceeds the scope of use permitted in this Agreement. Customer shall use the
86
+ Software in compliance with any accompanying documentation any laws applicable
87
+ to Customer.
88
+
89
+ 4. OPEN SOURCE. Customer and its Authorized Users shall not use any software or
90
+ software components that are open source in conjunction with the Software
91
+ Source Code or any Modifications in Source Code or in any way that could
92
+ subject the Software to any open source licenses.
93
+
94
+ 5. CONTRACTORS. Under the rights granted to Customer under this Agreement,
95
+ Customer may permit its employees, contractors, and agencies of Customer to
96
+ become Authorized Users to exercise the rights to the Software granted to
97
+ Customer in accordance with this Agreement solely on behalf of Customer to
98
+ provide services to Customer; provided that Customer shall be liable for the
99
+ acts and omissions of all Authorized Users to the extent any of such acts or
100
+ omissions, if performed by Customer, would constitute a breach of, or otherwise
101
+ give rise to liability to Customer under, this Agreement. Customer shall not
102
+ and shall not permit any Authorized User to use the Software except as
103
+ expressly permitted in this Agreement.
104
+
105
+ 6. COMPETITIVE PRODUCT DEVELOPMENT. Customer shall not use the Software in any way
106
+ to engage in the development of products or services which could be reasonably
107
+ construed to provide a complete or partial functional or commercial alternative
108
+ to Stream.io’s products or services (a “Competitive Product”). Customer shall
109
+ ensure that there is no direct or indirect use of, or sharing of, Software
110
+ source code, or other information based upon or derived from the Software to
111
+ develop such products or services. Without derogating from the generality of
112
+ the foregoing, development of Competitive Products shall include having direct
113
+ or indirect access to, supervising, consulting or assisting in the development
114
+ of, or producing any specifications, documentation, object code or source code
115
+ for, all or part of a Competitive Product.
116
+
117
+ 7. LIMITATION ON MODIFICATIONS. Notwithstanding any provision in this Agreement,
118
+ Modifications may only be created and used by Customer as permitted by this
119
+ Agreement and Modification Source Code may not be distributed to third parties.
120
+ Customer will not assert against Stream.io, its affiliates, or their customers,
121
+ direct or indirect, agents and contractors, in any way, any patent rights that
122
+ Customer may obtain relating to any Modifications for Stream.io, its
123
+ affiliates’, or their customers’, direct or indirect, agents’ and contractors’
124
+ manufacture, use, import, offer for sale or sale of any Stream.io products or
125
+ services.
126
+
127
+ 8. DELIVERY AND ACCEPTANCE. The Software will be delivered electronically pursuant
128
+ to Stream.io standard download procedures. The Software is deemed accepted upon
129
+ delivery.
130
+
131
+ 9. IMPLEMENTATION AND SUPPORT. Stream.io has no obligation under this Agreement to
132
+ provide any support or consultation concerning the Software.
133
+
134
+ 10. TERM AND TERMINATION. The term of this Agreement begins when the Software is
135
+ downloaded or accessed and shall continue until terminated. Either party may
136
+ terminate this Agreement upon written notice. This Agreement shall
137
+ automatically terminate if Customer is or becomes a competitor of Stream.io or
138
+ makes or sells any Competitive Products. Upon termination of this Agreement for
139
+ any reason, (a) all rights granted to Customer in this Agreement immediately
140
+ cease to exist, (b) Customer must promptly discontinue all use of the Software
141
+ and return to Stream.io or destroy all copies of the Software in Customer’s
142
+ possession or control. Any continued use of the Software by Customer or attempt
143
+ by Customer to exercise any rights under this Agreement after this Agreement
144
+ has terminated shall be considered copyright infringement and subject Customer
145
+ to applicable remedies for copyright infringement. Sections 2, 5, 6, 8 and 9
146
+ shall survive expiration or termination of this Agreement for any reason.
147
+
148
+ 11. OWNERSHIP. As between the parties, the Software and all worldwide intellectual
149
+ property rights and proprietary rights relating thereto or embodied therein,
150
+ are the exclusive property of Stream.io and its suppliers. Stream.io and its
151
+ suppliers reserve all rights in and to the Software not expressly granted to
152
+ Customer in this Agreement, and no other licenses or rights are granted by
153
+ implication, estoppel or otherwise.
154
+
155
+ 12. WARRANTY DISCLAIMER. USE OF THIS SOFTWARE IS ENTIRELY AT YOURS AND CUSTOMER’S
156
+ OWN RISK. THE SOFTWARE IS PROVIDED “AS IS” WITHOUT ANY WARRANTY OF ANY KIND
157
+ WHATSOEVER. STREAM.IO DOES NOT MAKE, AND HEREBY DISCLAIMS, ANY WARRANTY OF ANY
158
+ KIND, WHETHER EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING WITHOUT
159
+ LIMITATION, THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
160
+ PURPOSE, TITLE, NON-INFRINGEMENT OF THIRD-PARTY RIGHTS, RESULTS, EFFORTS,
161
+ QUALITY OR QUIET ENJOYMENT. STREAM.IO DOES NOT WARRANT THAT THE SOFTWARE IS
162
+ ERROR-FREE, WILL FUNCTION WITHOUT INTERRUPTION, WILL MEET ANY SPECIFIC NEED
163
+ THAT CUSTOMER HAS, THAT ALL DEFECTS WILL BE CORRECTED OR THAT IT IS
164
+ SUFFICIENTLY DOCUMENTED TO BE USABLE BY CUSTOMER. TO THE EXTENT THAT STREAM.IO
165
+ MAY NOT DISCLAIM ANY WARRANTY AS A MATTER OF APPLICABLE LAW, THE SCOPE AND
166
+ DURATION OF SUCH WARRANTY WILL BE THE MINIMUM PERMITTED UNDER SUCH LAW.
167
+ CUSTOMER ACKNOWLEDGES THAT IT HAS RELIED ON NO WARRANTIES OTHER THAN THE
168
+ EXPRESS WARRANTIES IN THIS AGREEMENT.
169
+
170
+ 13. LIMITATION OF LIABILITY. TO THE FULLEST EXTENT PERMISSIBLE BY LAW, STREAM.IO’S
171
+ TOTAL LIABILITY FOR ALL DAMAGES ARISING OUT OF OR RELATED TO THE SOFTWARE OR
172
+ THIS AGREEMENT, WHETHER IN CONTRACT, TORT (INCLUDING NEGLIGENCE) OR OTHERWISE,
173
+ SHALL NOT EXCEED $100. IN NO EVENT WILL STREAM.IO BE LIABLE FOR ANY INDIRECT,
174
+ CONSEQUENTIAL, EXEMPLARY, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES OF ANY KIND
175
+ WHATSOEVER, INCLUDING ANY LOST DATA AND LOST PROFITS, ARISING FROM OR RELATING
176
+ TO THE SOFTWARE EVEN IF STREAM.IO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
177
+ DAMAGES. CUSTOMER ACKNOWLEDGES THAT THIS PROVISION REFLECTS THE AGREED UPON
178
+ ALLOCATION OF RISK FOR THIS AGREEMENT AND THAT STREAM.IO WOULD NOT ENTER INTO
179
+ THIS AGREEMENT WITHOUT THESE LIMITATIONS ON ITS LIABILITY.
180
+
181
+ 14. General. Customer may not assign or transfer this Agreement, by operation of
182
+ law or otherwise, or any of its rights under this Agreement (including the
183
+ license rights granted to Customer) to any third party without Stream.io’s
184
+ prior written consent, which consent will not be unreasonably withheld or
185
+ delayed. Stream.io may assign this Agreement, without consent, including, but
186
+ limited to, affiliate or any successor to all or substantially all its business
187
+ or assets to which this Agreement relates, whether by merger, sale of assets,
188
+ sale of stock, reorganization or otherwise. Any attempted assignment or
189
+ transfer in violation of the foregoing will be null and void. Stream.io shall
190
+ not be liable hereunder by reason of any failure or delay in the performance of
191
+ its obligations hereunder for any cause which is beyond the reasonable control.
192
+ All notices, consents, and approvals under this Agreement must be delivered in
193
+ writing by courier, by electronic mail, or by certified or registered mail,
194
+ (postage prepaid and return receipt requested) to the other party at the
195
+ address set forth in the customer agreement between Stream.io and Customer and
196
+ will be effective upon receipt or when delivery is refused. This Agreement will
197
+ be governed by and interpreted in accordance with the laws of the State of
198
+ Colorado, without reference to its choice of laws rules. The United Nations
199
+ Convention on Contracts for the International Sale of Goods does not apply to
200
+ this Agreement. Any action or proceeding arising from or relating to this
201
+ Agreement shall be brought in a federal or state court in Denver, Colorado, and
202
+ each party irrevocably submits to the jurisdiction and venue of any such court
203
+ in any such action or proceeding. All waivers must be in writing. Any waiver or
204
+ failure to enforce any provision of this Agreement on one occasion will not be
205
+ deemed a waiver of any other provision or of such provision on any other
206
+ occasion. If any provision of this Agreement is unenforceable, such provision
207
+ will be changed and interpreted to accomplish the objectives of such provision
208
+ to the greatest extent possible under applicable law and the remaining
209
+ provisions will continue in full force and effect. Customer shall not violate
210
+ any applicable law, rule or regulation, including those regarding the export of
211
+ technical data. The headings of Sections of this Agreement are for convenience
212
+ and are not to be used in interpreting this Agreement. As used in this
213
+ Agreement, the word “including” means “including but not limited to.” This
214
+ Agreement (including all exhibits and attachments) constitutes the entire
215
+ agreement between the parties regarding the subject hereof and supersedes all
216
+ prior or contemporaneous agreements, understandings and communication, whether
217
+ written or oral. This Agreement may be amended only by a written document
218
+ signed by both parties. The terms of any purchase order or similar document
219
+ submitted by Customer to Stream.io will have no effect.
@@ -0,0 +1,9 @@
1
+ # Submit a pull request
2
+
3
+ ## CLA
4
+
5
+ - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required).
6
+ - [ ] The code changes follow best practices
7
+ - [ ] Code changes are tested (add some information if not applicable)
8
+
9
+ ## Description of the pull request
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # stream-chat-ruby
2
+
3
+ [![Build Status](https://travis-ci.com/GetStream/stream-chat-ruby.svg?branch=master)](https://travis-ci.com/GetStream/stream-chat-ruby) [![Gem Version](https://badge.fury.io/rb/stream-chat-ruby.svg)](http://badge.fury.io/rb/stream-chat-ruby)
4
+
5
+ stream-chat-ruby is the official Ruby client for [Stream chat](https://getstream.io/chat/) a service for building chat applications.
6
+
7
+ You can sign up for a Stream account at https://getstream.io/chat/get_started/.
8
+
9
+ You can use this library to access chat API endpoints server-side. For the
10
+ client-side integrations (web and mobile) have a look at the Javascript, iOS and
11
+ Android SDK libraries (https://getstream.io/chat/).
12
+
13
+ ### Installation
14
+
15
+ stream-chat-ruby supports:
16
+
17
+ - Ruby (2.6, 2.5, 2.4)
18
+
19
+ #### Install
20
+
21
+ ```bash
22
+ gem install stream-chat-ruby
23
+ ```
24
+
25
+ ### Documentation
26
+
27
+ [Official API docs](https://getstream.io/chat/docs/)
28
+
29
+ ### How to build a chat app with Ruby tutorial
30
+
31
+ TODO: add a sample Ruby chat program
32
+
33
+ ### Supported features
34
+
35
+ - Chat channels
36
+ - Messages
37
+ - Chat channel types
38
+ - User management
39
+ - Moderation API
40
+ - Push configuration
41
+ - User devices
42
+ - User search
43
+ - Channel search
44
+
45
+ ### Quickstart
46
+
47
+ ```ruby
48
+ chat = StreamChat::Client.new(api_key='STREAM_KEY', api_secret='STREAM_SECRET')
49
+
50
+ # add a user
51
+ chat.update_user({'id' => 'chuck', 'name' => 'Chuck'})
52
+
53
+ # create a channel about kung-fu
54
+ channel = chat.channel('messaging', 'kung-fu')
55
+ channel.create('chuck')
56
+
57
+ # add a first message to the channel
58
+ channel.send_message({'text' => 'AMA about kung-fu'})
59
+
60
+ ```
61
+
62
+ ### Contributing
63
+
64
+ First, make sure you can run the test suite. Tests are run via rspec
65
+
66
+ ```bash
67
+ STREAM_KEY=my_api_key STREAM_SECRET=my_api_secret bundle exec rake spec
68
+ ```
69
+
70
+ ### Releasing a new version
71
+
72
+ In order to release new version you need to be a maintainer of the library.
73
+
74
+ - Update CHANGELOG
75
+ - Update the version in `lib/stream-chat/version.rb`
76
+ - Commit and push to GitHub
77
+ - Build the gem with `bundle exec rake build`
78
+ - Publish the gem with `bundle exec rake release`
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ # rake spec
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec) { |t| t.verbose = false }
5
+
6
+ # rake console
7
+ task :console do
8
+ require 'pry'
9
+ require 'stream-chat'
10
+ ARGV.clear
11
+ Pry.start
12
+ end
13
+
14
+ task :default => [:spec]
15
+ task :test => [:spec]
16
+
@@ -0,0 +1,123 @@
1
+ module StreamChat
2
+
3
+ class Channel
4
+ attr_reader :id
5
+ attr_reader :channel_type
6
+ attr_reader :custom_data
7
+
8
+ def initialize(client, channel_type, channel_id=nil, custom_data=nil)
9
+ @channel_type = channel_type
10
+ @id = channel_id
11
+ @client = client
12
+ @custom_data = custom_data
13
+ if @custom_data == nil
14
+ @custom_data = {}
15
+ end
16
+ end
17
+
18
+ def url
19
+ if @id == nil
20
+ raise StreamChannelException "channel does not have an id"
21
+ end
22
+ "channels/#{@channel_type}/#{@id}"
23
+ end
24
+
25
+ def send_message(message, user_id)
26
+ payload = {"message": add_user_id(message, user_id)}
27
+ @client.post("#{url}/message", data: payload)
28
+ end
29
+
30
+ def send_event(event, user_id)
31
+ payload = {'event' => add_user_id(event, user_id)}
32
+ @client.post("#{url}/event", data: payload)
33
+ end
34
+
35
+ def send_reaction(message_id, reaction, user_id)
36
+ payload = {"reaction": add_user_id(reaction, user_id)}
37
+ @client.post("messages/#{message_id}/reaction", data: payload)
38
+ end
39
+
40
+ def delete_reaction(message_id, reaction_type, user_id)
41
+ @client.delete(
42
+ "messages/#{message_id}/reaction/#{reaction_type}",
43
+ params: {"user_id": user_id}
44
+ )
45
+ end
46
+
47
+ def create(user_id)
48
+ @custom_data["created_by"] = {"id": user_id}
49
+ query(watch: false, state: false, presence: false)
50
+ end
51
+
52
+ def query(**options)
53
+ payload = {"state": true, "data": @custom_data}.merge(options)
54
+ url = "channels/#{@channel_type}"
55
+ if @id != nil
56
+ url = "#{url}/#{@id}"
57
+ end
58
+
59
+ state = @client.post("#{url}/query", data: payload)
60
+ if @id == nil
61
+ @id = state["channel"]["id"]
62
+ end
63
+ state
64
+ end
65
+
66
+ def update(channel_data, update_message=nil)
67
+ payload = {"data": channel_data, "message": update_message}
68
+ @client.post(url, data: payload)
69
+ end
70
+
71
+ def delete
72
+ @client.delete(url)
73
+ end
74
+
75
+ def truncate
76
+ @client.post("#{url}/truncate")
77
+ end
78
+
79
+ def add_members(user_ids)
80
+ @client.post(url, data: {"add_members": user_ids})
81
+ end
82
+
83
+ def add_moderators(user_ids)
84
+ @client.post(url, data: {"add_moderators": user_ids})
85
+ end
86
+
87
+ def remove_members(user_ids)
88
+ @client.post(url, data: {"remove_members": user_ids})
89
+ end
90
+
91
+ def demote_moderators(user_ids)
92
+ @client.post(url, data: {"demote_moderators": user_ids})
93
+ end
94
+
95
+ def mark_read(user_id, **options)
96
+ payload = add_user_id(options, user_id)
97
+ @client.post("#{url}/read", data: payload)
98
+ end
99
+
100
+ def get_replies(parent_id, **options)
101
+ @client.get("messages/#{parent_id}/replies", params: options)
102
+ end
103
+
104
+ def get_reactions(message_id, **options)
105
+ @client.get("messages/#{message_id}/reactions", params: options)
106
+ end
107
+
108
+ def ban_user(user_id, **options)
109
+ @client.ban_user(user_id, type: @channel_type, id: @id, **options)
110
+ end
111
+
112
+ def unban_user(user_id)
113
+ @client.unban_user(user_id, type: @channel_type, id: @id)
114
+ end
115
+
116
+ private
117
+
118
+ def add_user_id(payload, user_id)
119
+ payload.merge({"user": {"id": user_id}})
120
+ end
121
+
122
+ end
123
+ end
@@ -0,0 +1,270 @@
1
+ # lib/client.rb
2
+ require 'faraday'
3
+ require 'jwt'
4
+ require 'stream-chat/channel'
5
+ require 'stream-chat/errors'
6
+
7
+ module StreamChat
8
+ class Client
9
+
10
+ attr_reader :api_key
11
+ attr_reader :api_secret
12
+ attr_reader :conn
13
+ attr_reader :options
14
+
15
+ # initializes a Stream Chat API Client
16
+ #
17
+ # @param [string] api_key your application api_key
18
+ # @param [string] api_secret your application secret
19
+ # @param [string]
20
+ # @param [hash] options extra options
21
+ #
22
+ # @example initialized the client with a timeout setting
23
+ # StreamChat::Client.new('my_key', 'my_secret', 3.0)
24
+ #
25
+ def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
26
+ @api_key = api_key
27
+ @api_secret = api_secret
28
+ @timeout = timeout
29
+ @options = options
30
+ @base_url = 'https://chat-us-east-1.stream-io-api.com'
31
+ @auth_token = JWT.encode({server: true}, @api_secret, 'HS256')
32
+ @conn = Faraday.new(url: @base_url) do |faraday|
33
+ faraday.options[:open_timeout] = @timeout
34
+ faraday.options[:timeout] = @timeout
35
+ faraday.adapter Faraday.default_adapter
36
+ end
37
+ end
38
+
39
+ def create_token(user_id, exp = nil)
40
+ payload = {user_id: user_id}
41
+ if exp != nil
42
+ payload['exp'] = exp
43
+ end
44
+ JWT.encode(payload, @api_secret, 'HS256')
45
+ end
46
+
47
+ def update_app_settings(**settings)
48
+ patch('app', **settings)
49
+ end
50
+
51
+ def get_app_settings
52
+ get('app')
53
+ end
54
+
55
+ def update_users(users)
56
+ payload = {}
57
+ users.each do |user|
58
+ payload[user[:id]] = user
59
+ end
60
+ post('users', data: {'users': payload})
61
+ end
62
+
63
+ def update_user(user)
64
+ update_users([user])
65
+ end
66
+
67
+ def delete_user(user_id, **options)
68
+ delete("users/#{user_id}", **options)
69
+ end
70
+
71
+ def deactivate_user(user_id, **options)
72
+ post("users/#{user_id}/deactivate", **options)
73
+ end
74
+
75
+ def export_user(user_id, **options)
76
+ get("users/#{user_id}/export", params: options)
77
+ end
78
+
79
+ def ban_user(target_id, **options)
80
+ payload = {'target_user_id': target_id}.merge(options)
81
+ post("moderation/ban", data: payload)
82
+ end
83
+
84
+ def unban_user(target_id, **options)
85
+ params = {'target_user_id': target_id}.merge(options)
86
+ delete("moderation/ban", params: params)
87
+ end
88
+
89
+ def mute_user(target_id, user_id)
90
+ payload = {'target_id': target_id, 'user_id': user_id}
91
+ post('moderation/mute', data: payload)
92
+ end
93
+
94
+ def unmute_user(target_id, user_id)
95
+ payload = {'target_id': target_id, 'user_id': user_id}
96
+ post('moderation/unmute', data: payload)
97
+ end
98
+
99
+ def mark_all_read(user_id)
100
+ payload = {'user': {'id': user_id}}
101
+ post('channels/read', data: payload)
102
+ end
103
+
104
+ def update_message(message)
105
+ if !message.key? 'id'
106
+ raise ArgumentError "message must have an id"
107
+ end
108
+ post("messages/#{message['id']}", data: {'message': message})
109
+ end
110
+
111
+ def delete_message(message_id)
112
+ delete("messages/#{message_id}")
113
+ end
114
+
115
+ def query_users(filter_conditions, sort: nil, **options)
116
+ sort_fields = []
117
+ if sort != nil
118
+ sort.each do |k ,v|
119
+ sort_fields << {"field": k, "direction": v}
120
+ end
121
+ end
122
+ params = options.merge({
123
+ "filter_conditions": filter_conditions,
124
+ "sort": sort_fields
125
+ })
126
+ get("users", params: {"payload": params.to_json})
127
+ end
128
+
129
+ def query_channels(filter_conditions, sort: nil, **options)
130
+ params = {"state": true, "watch": false, "presence": false}
131
+ sort_fields = []
132
+ if sort != nil
133
+ sort.each do |k, v|
134
+ sort_fields << {"field": k, "direction": v}
135
+ end
136
+ end
137
+ params = params.merge(options).merge({
138
+ "filter_conditions": filter_conditions,
139
+ "sort": sort_fields
140
+ })
141
+ get("channels", params: {"payload": params.to_json})
142
+ end
143
+
144
+ def create_channel_type(data)
145
+ if !data.key? "commands" || data["commands"].nil? || data["commands"].empty?
146
+ data["commands"] = ["all"]
147
+ end
148
+ post("channeltypes", data: data)
149
+ end
150
+
151
+ def get_channel_type(channel_type)
152
+ get("channeltypes/#{channel_type}")
153
+ end
154
+
155
+ def list_channel_types
156
+ get("channeltypes")
157
+ end
158
+
159
+ def update_channel_type(channel_type, **options)
160
+ put
161
+ end
162
+
163
+ def delete_channel_type(channel_type)
164
+ delete("channeltypes/#{channel_type}")
165
+ end
166
+
167
+ # Creates a channel instance
168
+ #
169
+ # @param [string] channel_type the channel type
170
+ # @param [string] channel_id the channel identifier
171
+ # @param [hash] data additional channel data
172
+ #
173
+ # @return [StreamChat::Channel]
174
+ #
175
+ def channel(channel_type, channel_id: nil, data: nil)
176
+ StreamChat::Channel.new(self, channel_type, channel_id, data)
177
+ end
178
+
179
+ def add_device(device_id, push_provider, user_id)
180
+ post("devices", data: {
181
+ "id": device_id,
182
+ "push_provider": push_provider,
183
+ "user_id": user_id
184
+ })
185
+ end
186
+
187
+ def delete_device(device_id, user_id)
188
+ delete("devices", params: {"id": device_id, "user_id": user_id})
189
+ end
190
+
191
+ def get_devices(user_id)
192
+ get("devices", params: {"user_id": user_id})
193
+ end
194
+
195
+ def verify_webhook(request_body, x_signature)
196
+ signature = OpenSSL::HMAC.hexdigest("SHA256", @api_key, request_body)
197
+ signature == x_signature
198
+ end
199
+
200
+ def put(relative_url, params: nil, data: nil)
201
+ make_http_request(:put, relative_url, params: params, data: data)
202
+ end
203
+
204
+ def post(relative_url, params: nil, data: nil)
205
+ make_http_request(:post, relative_url, params: params, data: data)
206
+ end
207
+
208
+ def get(relative_url, params: nil)
209
+ make_http_request(:get, relative_url, params: params)
210
+ end
211
+
212
+ def delete(relative_url, params: nil)
213
+ make_http_request(:delete, relative_url, params: params)
214
+ end
215
+
216
+ def patch(relative_url, params: nil, data: nil)
217
+ make_http_request(:patch, relative_url, params: params, data: data)
218
+ end
219
+
220
+ private
221
+
222
+ def get_default_params
223
+ {api_key: @api_key}
224
+ end
225
+
226
+ def get_user_agent
227
+ "stream-ruby-client-#{StreamChat::VERSION}"
228
+ end
229
+
230
+ def get_default_headers
231
+ {
232
+ "Content-Type": "application/json",
233
+ "X-Stream-Client": get_user_agent
234
+ }
235
+ end
236
+
237
+ def parse_response(response)
238
+ begin
239
+ parsed_result = JSON.parse(response.body)
240
+ rescue JSON::ParserError
241
+ raise StreamAPIException.new(response)
242
+ end
243
+ if response.status >= 399
244
+ raise StreamAPIException.new(response)
245
+ end
246
+ return parsed_result
247
+ end
248
+
249
+ def make_http_request(method, relative_url, params: nil, data: nil)
250
+ headers = get_default_headers
251
+ headers['Authorization'] = @auth_token
252
+ headers["stream-auth-type"] = "jwt"
253
+ url = [@base_url, relative_url].join('/')
254
+ params = params != nil ? params : {}
255
+ params = Hash[get_default_params.merge(params).sort_by { |k, v| k.to_s }]
256
+ url = "#{url}?#{URI.encode_www_form(params)}"
257
+ body = data.to_json if %w[patch post put].include? method.to_s
258
+
259
+ response = @conn.run_request(
260
+ method,
261
+ url,
262
+ body,
263
+ headers
264
+ )
265
+ parse_response(response)
266
+ end
267
+
268
+ end
269
+ end
270
+
@@ -0,0 +1,30 @@
1
+ # lib/errors.rb
2
+
3
+ module StreamChat
4
+ class StreamAPIException < StandardError
5
+
6
+ def initialize(response)
7
+ @response = response
8
+ p response
9
+ begin
10
+ parsed_response = JSON.parse(response.body)
11
+ @json_response = true
12
+ @error_code = parsed_response.fetch("data", {})
13
+ .fetch("code", "unknown")
14
+ @error_message = parsed_response.fetch("data", {})
15
+ .fetch("message", "unknown")
16
+ rescue JSON::ParserError
17
+ @json_response = false
18
+ end
19
+ end
20
+
21
+ def message
22
+ if @json_response
23
+ "StreamChat error code #{@error_code}: #{@error_message}"
24
+ else
25
+ "StreamChat error HTTP code: #{@response.status}"
26
+ end
27
+ end
28
+ end
29
+ class StreamChannelException < StandardError; end
30
+ end
@@ -0,0 +1,5 @@
1
+ # lib/version.rb
2
+
3
+ module StreamChat
4
+ VERSION = "0.1.0".freeze
5
+ end
@@ -0,0 +1 @@
1
+ require 'stream-chat/client'
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'stream-chat/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'stream-chat-ruby'
7
+ gem.description = ''
8
+ gem.version = StreamChat::VERSION
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.summary = ''
11
+ gem.email = 'support@getstream.io'
12
+ gem.homepage = 'http://github.com/GetStream/stream-chat-ruby'
13
+ gem.authors = ['Mircea Cosbuc']
14
+ gem.files = Dir.chdir(File.expand_path('..', __FILE__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+
18
+ gem.add_dependency 'faraday'
19
+ gem.add_dependency 'jwt'
20
+ gem.add_dependency 'rake'
21
+ gem.add_dependency 'rspec'
22
+ gem.add_dependency 'simplecov'
23
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stream-chat-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mircea Cosbuc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email: support@getstream.io
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - ".travis.yml"
91
+ - Gemfile
92
+ - LICENSE
93
+ - PULL_REQUEST_TEMPLATE.md
94
+ - README.md
95
+ - Rakefile
96
+ - lib/stream-chat.rb
97
+ - lib/stream-chat/channel.rb
98
+ - lib/stream-chat/client.rb
99
+ - lib/stream-chat/errors.rb
100
+ - lib/stream-chat/version.rb
101
+ - stream-chat.gemspec
102
+ homepage: http://github.com/GetStream/stream-chat-ruby
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.0.3
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: ''
124
+ test_files: []