Class: Chronicle::Zulip::Proxy
- Inherits:
-
Object
- Object
- Chronicle::Zulip::Proxy
- Defined in:
- lib/chronicle/zulip/proxy.rb
Instance Method Summary collapse
- #all_private_messages(anchor: 'newest', since: nil, limit: nil) ⇒ Object
-
#initialize(username:, access_token:, realm:) ⇒ Proxy
constructor
A new instance of Proxy.
- #load_messages(anchor:, narrow:) ⇒ Object
- #make_request(endpoint:, params: {}) ⇒ Object
Constructor Details
#initialize(username:, access_token:, realm:) ⇒ Proxy
Returns a new instance of Proxy.
6 7 8 9 10 |
# File 'lib/chronicle/zulip/proxy.rb', line 6 def initialize(username:, access_token:, realm:) @username = username @access_token = access_token @realm = realm end |
Instance Method Details
#all_private_messages(anchor: 'newest', since: nil, limit: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/chronicle/zulip/proxy.rb', line 12 def (anchor: 'newest', since: nil, limit: nil) narrow = '[{"negated":false,"operator":"is","operand":"private"}]' has_more = true count = 0 while has_more response = (anchor: anchor, narrow: narrow) = response[:messages].reverse || [] = .first(limit - count) if limit = .filter { || Time.at([:timestamp]) > since } if since break unless .any? .each do || count += 1 yield end has_more = !response[:found_oldest] anchor = .map { || [:id] }.min - 1 end end |
#load_messages(anchor:, narrow:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/chronicle/zulip/proxy.rb', line 35 def (anchor:, narrow:) params = { narrow: narrow, num_after: '0', num_before: '100', anchor: anchor, apply_markdown: 'false' } make_request(endpoint: 'messages', params: params) end |
#make_request(endpoint:, params: {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/chronicle/zulip/proxy.rb', line 47 def make_request(endpoint:, params: {}) conn = Faraday.new( url: zulip_subdomain, params: params ) do |conn| conn.use Faraday::Response::RaiseError conn.request :authorization, :basic, @username, @access_token end response = conn.get("api/v1/#{endpoint}") JSON.parse(response.body, { symbolize_names: true }) end |