zulip_machine 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/examples/annoying_bot.rb +19 -0
- data/lib/zulip_machine.rb +158 -0
- data/lib/zulip_machine/version.rb +3 -0
- data/zulip_machine.gemspec +25 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 843bdc57ae27bf5f5d607c88b90141299eac93c8
|
4
|
+
data.tar.gz: b50e48ec36809e32043833af1a2421053c64df7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ced5c67c9e4778484dc9307edd7d9e6323a44a750a15ca50f82ccf534172257de71e785c713948a3458664565d7dbcc605c99524a9159b95ab1136b97d12a3c9
|
7
|
+
data.tar.gz: 2ae1667d2ee2977f1195a93205e19a4bb0d6ad5691e79f2b7b23fb866b436bc06550783e5ac18c4aa2c8d23fb2ba40212e136ca785f6114ba4b852e90056ea76
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alan O'Donnell
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ZulipMachine
|
2
|
+
|
3
|
+
EventMachine bindings for Zulip's API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'zulip_machine'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install zulip_machine
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Take a look at the `examples` directory.
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'zulip_machine'
|
2
|
+
|
3
|
+
EM.run do
|
4
|
+
email = ENV['EMAIL']
|
5
|
+
api_key = ENV['API_KEY']
|
6
|
+
|
7
|
+
bot = ZulipMachine::Bot.new(email, api_key)
|
8
|
+
bot.start!
|
9
|
+
|
10
|
+
bot.subscribe!(["test-bot"])
|
11
|
+
|
12
|
+
bot.on_private_msg do |from, msg|
|
13
|
+
from.send(msg.upcase)
|
14
|
+
end
|
15
|
+
|
16
|
+
bot.on_stream_msg do |from, convo, msg|
|
17
|
+
convo.send("cool story bro")
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require "zulip_machine/version"
|
2
|
+
|
3
|
+
require 'em-http'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module EM::Deferrable
|
7
|
+
def bind(&f)
|
8
|
+
d = EM::DefaultDeferrable.new
|
9
|
+
callback do |a|
|
10
|
+
f.call(a).callback { |b| d.succeed(b) }.errback { |e| d.fail(e) }
|
11
|
+
end
|
12
|
+
errback { |e| d.fail(e) }
|
13
|
+
d
|
14
|
+
end
|
15
|
+
def map(&f)
|
16
|
+
d = EM::DefaultDeferrable.new
|
17
|
+
callback { |a| d.succeed(f.call(a)) }
|
18
|
+
errback { |e| d.fail(e) }
|
19
|
+
d
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ZulipMachine
|
24
|
+
ENDPOINT = "https://api.zulip.com/v1"
|
25
|
+
|
26
|
+
User = Struct.new(:bot, :deets) do
|
27
|
+
def email
|
28
|
+
deets["email"] || deets["sender_email"] # yuck :/
|
29
|
+
end
|
30
|
+
def send(msg)
|
31
|
+
bot.send_private_msg(email, msg)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Conversation = Struct.new(:bot, :stream, :subject) do
|
36
|
+
def send(msg)
|
37
|
+
bot.send_stream_msg(stream, subject, msg)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Bot
|
42
|
+
def initialize(email, api_key)
|
43
|
+
@email = email
|
44
|
+
@api_key = api_key
|
45
|
+
@private_cbs = []
|
46
|
+
@stream_cbs = []
|
47
|
+
@presence_cbs = []
|
48
|
+
end
|
49
|
+
|
50
|
+
def start!
|
51
|
+
post("/register").callback do |r|
|
52
|
+
fetch_events(r["queue_id"], r["last_event_id"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def send_private_msg(to_whom, msg)
|
57
|
+
post("/messages", type: "private", to: to_whom, content: msg)
|
58
|
+
end
|
59
|
+
|
60
|
+
def send_stream_msg(to, subject, msg)
|
61
|
+
post("/messages", type: "stream", to: to, subject: subject, content: msg)
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_subscriptions
|
65
|
+
get("/users/me/subscriptions")
|
66
|
+
end
|
67
|
+
|
68
|
+
def subscribe!(streams)
|
69
|
+
streams = streams.map { |name| { name: name } }
|
70
|
+
patch("/users/me/subscriptions", add: JSON.unparse(streams))
|
71
|
+
end
|
72
|
+
|
73
|
+
def unsubscribe!(streams)
|
74
|
+
patch("/users/me/subscriptions", subscriptions: JSON.unparse(streams))
|
75
|
+
end
|
76
|
+
|
77
|
+
def on_private_msg(&cb)
|
78
|
+
@private_cbs << cb
|
79
|
+
end
|
80
|
+
def on_stream_msg(&cb)
|
81
|
+
@stream_cbs << cb
|
82
|
+
end
|
83
|
+
def on_presence(&cb)
|
84
|
+
@presence_cbs << cb
|
85
|
+
end
|
86
|
+
|
87
|
+
def fetch_events(q_id, event_id)
|
88
|
+
get("/events", queue_id: q_id, last_event_id: event_id).callback do |r|
|
89
|
+
r["events"].each { |e| handle_event(e) }
|
90
|
+
event_id = r["events"].map { |e| e["id"] }.max
|
91
|
+
fetch_events(q_id, event_id)
|
92
|
+
end.errback do |c|
|
93
|
+
fetch_events(q_id, event_id)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def handle_event(e)
|
98
|
+
case e["type"]
|
99
|
+
when "message"
|
100
|
+
deets = e["message"]
|
101
|
+
case deets["type"]
|
102
|
+
when "private"
|
103
|
+
unless deets["sender_email"] == @email
|
104
|
+
from = User.new(self, deets)
|
105
|
+
msg = deets["content"]
|
106
|
+
@private_cbs.each { |cb| cb.call(from, msg) }
|
107
|
+
end
|
108
|
+
when "stream"
|
109
|
+
unless deets["sender_email"] == @email
|
110
|
+
from = User.new(self, deets)
|
111
|
+
stream = deets["display_recipient"]
|
112
|
+
subject = deets["subject"]
|
113
|
+
convo = Conversation.new(self, stream, subject)
|
114
|
+
msg = deets["content"]
|
115
|
+
@stream_cbs.each { |cb| cb.call(from, convo, msg) }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
when "presence"
|
119
|
+
who = User.new(self, e)
|
120
|
+
presence = e["presence"]
|
121
|
+
@presence_cbs.each { |cb| cb.call(who, presence) }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def get(path, params = nil)
|
126
|
+
req(path).get(
|
127
|
+
query: params,
|
128
|
+
head: auth,
|
129
|
+
inactivity_timeout: 0
|
130
|
+
).map { |c| JSON.parse(c.response) }
|
131
|
+
end
|
132
|
+
|
133
|
+
def post(path, params = nil)
|
134
|
+
req(path).post(
|
135
|
+
body: params,
|
136
|
+
head: auth,
|
137
|
+
inactivity_timeout: 0
|
138
|
+
).map { |c| JSON.parse(c.response) }
|
139
|
+
end
|
140
|
+
|
141
|
+
def patch(path, params = nil)
|
142
|
+
req(path).patch(
|
143
|
+
body: params,
|
144
|
+
head: auth,
|
145
|
+
inactivity_timeout: 0
|
146
|
+
).map { |c| JSON.parse(c.response) }
|
147
|
+
end
|
148
|
+
|
149
|
+
def req(path)
|
150
|
+
EM::HttpRequest.new(ENDPOINT + path)
|
151
|
+
end
|
152
|
+
|
153
|
+
def auth
|
154
|
+
{ authorization: [@email, @api_key] }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'zulip_machine/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zulip_machine"
|
8
|
+
spec.version = ZulipMachine::VERSION
|
9
|
+
spec.authors = ["Alan O'Donnell"]
|
10
|
+
spec.email = ["alan.m.odonnell@gmail.com"]
|
11
|
+
spec.description = "EventMachine bindings for Zulip's API."
|
12
|
+
spec.summary = "EventMachine bindings for Zulip's API."
|
13
|
+
spec.homepage = "https://github.com/happy4crazy/zulip_machine"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "em-http-request", "~> 1.1.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zulip_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alan O'Donnell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: em-http-request
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
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: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: EventMachine bindings for Zulip's API.
|
56
|
+
email:
|
57
|
+
- alan.m.odonnell@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- examples/annoying_bot.rb
|
68
|
+
- lib/zulip_machine.rb
|
69
|
+
- lib/zulip_machine/version.rb
|
70
|
+
- zulip_machine.gemspec
|
71
|
+
homepage: https://github.com/happy4crazy/zulip_machine
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.1.3
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: EventMachine bindings for Zulip's API.
|
95
|
+
test_files: []
|