Class: Zulip::Client
- Inherits:
-
Object
- Object
- Zulip::Client
- Defined in:
- lib/zulip/client.rb,
lib/zulip/client/version.rb
Constant Summary collapse
- VERSION =
"0.2.1"
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #close_stream ⇒ Object
-
#initialize(site:, username:, api_key:, **options) ⇒ Client
constructor
A new instance of Client.
- #register(event_types: [], narrow: []) ⇒ Object
- #send_message(type: :stream, to: "general", subject: "", content: "") ⇒ Object
- #send_private_message(to:, content:) ⇒ Object
- #send_public_message(to:, subject:, content:) ⇒ Object
- #stream_event(event_types: [], narrow: []) ⇒ Object
- #stream_message(narrow: []) ⇒ Object
- #unregister(queue_id) ⇒ Object
Constructor Details
#initialize(site:, username:, api_key:, **options) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zulip/client.rb', line 15 def initialize(site:, username:, api_key:, **) @site = URI.parse(site) @connection = Faraday.new(@site.to_s, ) do |faraday| faraday.adapter Faraday.default_adapter yield faraday if block_given? end @connection.basic_auth(username, api_key) @running = false @debug = false end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
13 14 15 |
# File 'lib/zulip/client.rb', line 13 def debug @debug end |
Instance Method Details
#close_stream ⇒ Object
127 128 129 130 |
# File 'lib/zulip/client.rb', line 127 def close_stream @running = false @command_writer.write("q") if @command_writer end |
#register(event_types: [], narrow: []) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/zulip/client.rb', line 50 def register(event_types: [], narrow: []) response = @connection.post do |request| request.url("/api/v1/register") params = {} params["event_types"] = JSON.generate(event_types) unless event_types.empty? params["narrow"] = JSON.generate(narrow) unless narrow.empty? request.body = params end if response.success? res = JSON.parse(response.body, symbolize_names: true) [res[:queue_id], res[:last_event_id]] else raise Zulip::ResponseError, reqponse.reason_phrase end end |
#send_message(type: :stream, to: "general", subject: "", content: "") ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/zulip/client.rb', line 26 def (type: :stream, to: "general", subject: "", content: "") @connection.post do |request| request.url("/api/v1/messages") params = { "type" => type.to_s } case type when :private params["to"] = JSON.generate(Array(to)) when :stream params["to"] = to params["subject"] = subject end params["content"] = content request.body = params end end |
#send_private_message(to:, content:) ⇒ Object
46 47 48 |
# File 'lib/zulip/client.rb', line 46 def (to:, content:) (type: :private, to: to, content: content) end |
#send_public_message(to:, subject:, content:) ⇒ Object
42 43 44 |
# File 'lib/zulip/client.rb', line 42 def (to:, subject:, content:) (type: :stream, to: to, subject: subject, content: content) end |
#stream_event(event_types: [], narrow: []) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/zulip/client.rb', line 78 def stream_event(event_types: [], narrow: []) queue_id, last_event_id = register(event_types: event_types, narrow: narrow) response_reader, response_writer = IO.pipe command_reader, @command_writer = IO.pipe @running = true t = Thread.new do loop do break unless @running response = get_events(queue_id: queue_id, last_event_id: last_event_id) response_writer.write(response) end end buf = "" loop do reader, _writer, _exception = IO.select([response_reader, command_reader]) case reader.first when response_reader buf << response_reader.readpartial(1024) begin res = JSON.parse(buf, symbolize_names: true) rescue JSON::ParserError # Ignore error because buf is incomplete JSON next end buf = "" if res[:result] == "success" res[:events].each do |event| last_event_id = event[:id] if event_types.empty? || event_types.include?(event[:type]) yield event end end else raise Zulip::ResponseError, res[:msg] end when command_reader break end end unregister(queue_id) t.join end |
#stream_message(narrow: []) ⇒ Object
121 122 123 124 125 |
# File 'lib/zulip/client.rb', line 121 def (narrow: []) stream_event(event_types: ["message"], narrow: narrow) do |event| yield event[:message] end end |
#unregister(queue_id) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/zulip/client.rb', line 66 def unregister(queue_id) response = @connection.delete do |request| request.url("/api/v1/events") request.body = { "queue_id" => queue_id } end if response.success? JSON.parse(response.body, symbolize_names: true)[:result] == "success" else raise Zulip::ResponseError, reqponse.reason_phrase end end |