zulip-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +11 -0
- data/lib/zulip/client.rb +139 -0
- data/lib/zulip/client/version.rb +5 -0
- data/lib/zulip/error.rb +4 -0
- data/zulip-client.gemspec +29 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2156d1fd144e2ea7808de4108f0973e638a28db4
|
4
|
+
data.tar.gz: 72a36e82a6d60df942a29424ae9b8c4bd863049b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0cd3179c5dfe9505d1a47b58154e290fcb91998d43664de65e7f185a6451edf881e89701b7b757f05ef0f54033427309da8ebfe28cc42681c94888fe6cb6593c
|
7
|
+
data.tar.gz: d41cfdf494f5f40b9773feee043d3848ffb2cae8ee22471cd114952bc536805129aa6d108fef973902dc88991977021b1d853590dcfd2a9fd42256fd1f253f2e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 okkez
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Zulip::Client
|
2
|
+
|
3
|
+
[Zulip](https://zulip.org/) client for [Ruby](https://www.ruby-lang.org/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'zulip-client'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install zulip-client
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Send message to stream:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
client = Zulip::Client.new(site: "https://zulip.example.com/",
|
27
|
+
username: "test-bot@zulip.example.com",
|
28
|
+
api_token: "xxxxxxxxxxxxx")
|
29
|
+
client.send_message(type: :stream, to: "general", subject: "projects", content: "Hello, Zulip!")
|
30
|
+
```
|
31
|
+
|
32
|
+
Send private message to users:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
client.send_message(type: :stream, to: "user@zulip.example.com", content: "Hello, Zulip!")
|
36
|
+
client.send_message(type: :stream, to: ["user1@zulip.example.com", "user2@zulip.example.com"], content: "Hello, Zulip!")
|
37
|
+
```
|
38
|
+
|
39
|
+
Receive all events:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
client.each_event do |event|
|
43
|
+
p event
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Receive message event:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
client.each_message do |event|
|
51
|
+
p event
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/okkez/zulip-client.
|
58
|
+
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
63
|
+
|
data/Rakefile
ADDED
data/lib/zulip/client.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require "json"
|
2
|
+
require "uri"
|
3
|
+
require "faraday"
|
4
|
+
require "typhoeus"
|
5
|
+
require "typhoeus/adapters/faraday"
|
6
|
+
|
7
|
+
require "zulip/error"
|
8
|
+
|
9
|
+
Faraday.default_adapter = :typhoeus
|
10
|
+
|
11
|
+
module Zulip
|
12
|
+
class Client
|
13
|
+
attr_accessor :debug
|
14
|
+
|
15
|
+
def initialize(site:, username:, api_key:, **options)
|
16
|
+
@site = URI.parse(site)
|
17
|
+
@connection = Faraday.new(@site.to_s, options) do |faraday|
|
18
|
+
faraday.adapter Faraday.default_adapter
|
19
|
+
yield faraday if block_given?
|
20
|
+
end
|
21
|
+
@connection.basic_auth(username, api_key)
|
22
|
+
@running = false
|
23
|
+
@debug = false
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_message(type: :stream, to: "general", subject: "", content: "")
|
27
|
+
@connection.post do |request|
|
28
|
+
request.url("/api/v1/messages")
|
29
|
+
params = { "type" => type.to_s }
|
30
|
+
case type
|
31
|
+
when :private
|
32
|
+
params["to"] = JSON.generate(Array(to))
|
33
|
+
when :stream
|
34
|
+
params["to"] = to
|
35
|
+
params["subject"] = subject
|
36
|
+
end
|
37
|
+
params["content"] = content
|
38
|
+
request.body = params
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def register(event_types: [], narrow: [])
|
43
|
+
response = @connection.post do |request|
|
44
|
+
request.url("/api/v1/register")
|
45
|
+
params = {}
|
46
|
+
params["event_types"] = JSON.generate(event_types) unless event_types.empty?
|
47
|
+
params["narrow"] = JSON.generate(narrow) unless narrow.empty?
|
48
|
+
request.body = params
|
49
|
+
end
|
50
|
+
if response.success?
|
51
|
+
res = JSON.parse(response.body, symbolize_names: true)
|
52
|
+
[res[:queue_id], res[:last_event_id]]
|
53
|
+
else
|
54
|
+
raise Zulip::ResponseError, reqponse.reason_phrase
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def unregister(queue_id)
|
59
|
+
response = @connection.delete do |request|
|
60
|
+
request.url("/api/v1/events")
|
61
|
+
request.body = { "queue_id" => queue_id }
|
62
|
+
end
|
63
|
+
if response.success?
|
64
|
+
JSON.parse(response.body, symbolize_names: true)[:result] == "success"
|
65
|
+
else
|
66
|
+
raise Zulip::ResponseError, reqponse.reason_phrase
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def stream_event(event_types: [], narrow: [])
|
71
|
+
queue_id, last_event_id = register(event_types: event_types, narrow: narrow)
|
72
|
+
response_reader, response_writer = IO.pipe
|
73
|
+
command_reader, @command_writer = IO.pipe
|
74
|
+
@running = true
|
75
|
+
t = Thread.new do
|
76
|
+
loop do
|
77
|
+
break unless @running
|
78
|
+
response = get_events(queue_id: queue_id, last_event_id: last_event_id)
|
79
|
+
response_writer.write(response)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
buf = ""
|
83
|
+
loop do
|
84
|
+
reader, _writer, _exception = IO.select([response_reader, command_reader])
|
85
|
+
case reader.first
|
86
|
+
when response_reader
|
87
|
+
buf << response_reader.readpartial(1024)
|
88
|
+
begin
|
89
|
+
res = JSON.parse(buf, symbolize_names: true)
|
90
|
+
rescue JSON::ParserError => ex
|
91
|
+
warn("#{ex.class}:#{ex.message}")
|
92
|
+
next
|
93
|
+
end
|
94
|
+
buf = ""
|
95
|
+
if res[:result] == "success"
|
96
|
+
res[:events].each do |event|
|
97
|
+
last_event_id = event[:id]
|
98
|
+
if event_types.empty? || event_types.include?(event[:type])
|
99
|
+
yield event
|
100
|
+
end
|
101
|
+
end
|
102
|
+
else
|
103
|
+
raise Zulip::ResponseError, res[:msg]
|
104
|
+
end
|
105
|
+
when command_reader
|
106
|
+
break
|
107
|
+
end
|
108
|
+
end
|
109
|
+
unregister(queue_id)
|
110
|
+
t.join
|
111
|
+
end
|
112
|
+
|
113
|
+
def stream_message(narrow: [])
|
114
|
+
stream_event(event_types: ["message"], narrow: narrow) do |event|
|
115
|
+
yield event[:message]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def close_stream
|
120
|
+
@running = false
|
121
|
+
@command_writer.write("q") if @command_writer
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def get_events(queue_id:, last_event_id:)
|
127
|
+
response = @connection.get do |request|
|
128
|
+
request.url("/api/v1/events")
|
129
|
+
request.params["queue_id"] = queue_id
|
130
|
+
request.params["last_event_id"] = last_event_id
|
131
|
+
end
|
132
|
+
if response.success?
|
133
|
+
response.body
|
134
|
+
else
|
135
|
+
raise Zulip::ResponseError, response.reason_phrase
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
data/lib/zulip/error.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "zulip/client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "zulip-client"
|
7
|
+
spec.version = Zulip::Client::VERSION
|
8
|
+
spec.authors = ["okkez"]
|
9
|
+
spec.email = ["okkez000@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Zulip client for Ruby"
|
12
|
+
spec.description = "Zulip client for Ruby"
|
13
|
+
spec.homepage = "https://github.com/okkez/zulip-client-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "bin"
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "faraday", ">= 0.11.0"
|
24
|
+
spec.add_runtime_dependency "typhoeus", "~> 1.1.0"
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
27
|
+
spec.add_development_dependency "test-unit", ">= 3.2.0"
|
28
|
+
spec.add_development_dependency "webmock"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zulip-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- okkez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-04 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.11.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.11.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: typhoeus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.2.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.2.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Zulip client for Ruby
|
98
|
+
email:
|
99
|
+
- okkez000@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/zulip/client.rb
|
110
|
+
- lib/zulip/client/version.rb
|
111
|
+
- lib/zulip/error.rb
|
112
|
+
- zulip-client.gemspec
|
113
|
+
homepage: https://github.com/okkez/zulip-client-ruby
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.6.11
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Zulip client for Ruby
|
137
|
+
test_files: []
|