activematrix 0.0.0 → 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.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/{matrix_sdk → active_matrix}/api.rb +18 -22
  3. data/lib/{matrix_sdk → active_matrix}/bot/base.rb +42 -39
  4. data/lib/{matrix_sdk → active_matrix}/bot/main.rb +4 -5
  5. data/lib/active_matrix/bot.rb +7 -0
  6. data/lib/{matrix_sdk → active_matrix}/client.rb +17 -20
  7. data/lib/{matrix_sdk → active_matrix}/errors.rb +4 -4
  8. data/lib/active_matrix/logging.rb +56 -0
  9. data/lib/{matrix_sdk → active_matrix}/mxid.rb +2 -2
  10. data/lib/{matrix_sdk → active_matrix}/protocols/as.rb +1 -1
  11. data/lib/{matrix_sdk → active_matrix}/protocols/cs.rb +6 -8
  12. data/lib/{matrix_sdk → active_matrix}/protocols/is.rb +1 -1
  13. data/lib/{matrix_sdk → active_matrix}/protocols/msc.rb +6 -8
  14. data/lib/{matrix_sdk → active_matrix}/protocols/ss.rb +2 -2
  15. data/lib/active_matrix/railtie.rb +18 -0
  16. data/lib/{matrix_sdk → active_matrix}/response.rb +2 -2
  17. data/lib/{matrix_sdk → active_matrix}/room.rb +21 -25
  18. data/lib/{matrix_sdk → active_matrix}/rooms/space.rb +3 -3
  19. data/lib/{matrix_sdk → active_matrix}/user.rb +13 -15
  20. data/lib/{matrix_sdk → active_matrix}/util/account_data_cache.rb +6 -6
  21. data/lib/{matrix_sdk → active_matrix}/util/events.rb +8 -8
  22. data/lib/{matrix_sdk → active_matrix}/util/extensions.rb +2 -15
  23. data/lib/active_matrix/util/rails_cache_adapter.rb +37 -0
  24. data/lib/{matrix_sdk → active_matrix}/util/state_event_cache.rb +10 -10
  25. data/lib/{matrix_sdk → active_matrix}/util/tinycache.rb +11 -6
  26. data/lib/{matrix_sdk → active_matrix}/util/tinycache_adapter.rb +3 -3
  27. data/lib/{matrix_sdk → active_matrix}/util/uri.rb +4 -4
  28. data/lib/active_matrix/version.rb +5 -0
  29. data/lib/active_matrix.rb +33 -0
  30. metadata +62 -30
  31. data/lib/matrix_sdk/bot.rb +0 -4
  32. data/lib/matrix_sdk/version.rb +0 -5
  33. data/lib/matrix_sdk.rb +0 -75
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # rubocop:disable Metrics/ModuleLength
4
- module MatrixSdk::Protocols::CS
4
+ module ActiveMatrix::Protocols::CS
5
5
  # Gets the available client API versions
6
6
  # @return [Array]
7
7
  #
@@ -12,7 +12,7 @@ module MatrixSdk::Protocols::CS
12
12
  # # => 'latest'
13
13
  def client_api_versions
14
14
  (@client_api_versions ||= request(:get, :client, '/versions')).versions.tap do |vers|
15
- vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
15
+ vers.instance_eval <<-CODE, __FILE__, __LINE__ + 1
16
16
  if !respond_to? :latest
17
17
  def latest
18
18
  last
@@ -32,7 +32,7 @@ module MatrixSdk::Protocols::CS
32
32
  # # => true
33
33
  def client_api_unstable_features
34
34
  (@client_api_versions ||= request(:get, :client, '/versions')).unstable_features.tap do |vers|
35
- vers.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
35
+ vers.instance_eval <<-CODE, __FILE__, __LINE__ + 1
36
36
  def has?(feature)
37
37
  feature = feature.to_s.to_sym unless feature.is_a? Symbol
38
38
  fetch(feature, nil)
@@ -68,9 +68,7 @@ module MatrixSdk::Protocols::CS
68
68
  # @see https://matrix.org/docs/spec/client_server/latest.html#get-matrix-client-r0-sync
69
69
  # For more information on the parameters and what they mean
70
70
  def sync(timeout: 30.0, **params)
71
- query = params.select do |k, _v|
72
- %i[since filter full_state set_presence].include? k
73
- end
71
+ query = params.slice(:since, :filter, :full_state, :set_presence)
74
72
 
75
73
  query[:timeout] = (timeout * 1000).to_i if timeout
76
74
  query[:timeout] = params.delete(:timeout_ms).to_i if params.key? :timeout_ms
@@ -198,7 +196,7 @@ module MatrixSdk::Protocols::CS
198
196
 
199
197
  data = {
200
198
  type: login_type,
201
- initial_device_display_name: params.delete(:initial_device_display_name) { MatrixSdk::Api::USER_AGENT }
199
+ initial_device_display_name: params.delete(:initial_device_display_name) { ActiveMatrix::Api::USER_AGENT }
202
200
  }.merge params
203
201
  data[:device_id] = device_id if device_id
204
202
 
@@ -785,7 +783,7 @@ module MatrixSdk::Protocols::CS
785
783
  state_type = ERB::Util.url_encode state_type.to_s
786
784
  key = ERB::Util.url_encode key.to_s
787
785
 
788
- request(:get, client_api_latest, "/rooms/#{room_id}/state/#{state_type}#{key.empty? ? nil : "/#{key}"}", query: query)
786
+ request(:get, client_api_latest, "/rooms/#{room_id}/state/#{state_type}#{"/#{key}" unless key.empty?}", query: query)
789
787
  end
790
788
 
791
789
  # Retrieves all current state objects from a room
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk::Protocols::IS
3
+ module ActiveMatrix::Protocols::IS
4
4
  def identity_status
5
5
  request(:get, :identity_api_v1, '/')
6
6
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Preliminary support for unmerged MSCs (Matrix Spec Changes)
4
- module MatrixSdk::Protocols::MSC
4
+ module ActiveMatrix::Protocols::MSC
5
5
  def refresh_mscs
6
6
  @msc = {}
7
7
  end
@@ -9,12 +9,12 @@ module MatrixSdk::Protocols::MSC
9
9
  # Check if there's support for MSC2108 - Sync over Server Sent Events
10
10
  def msc2108?
11
11
  @msc ||= {}
12
- @msc[2108] ||= \
12
+ @msc[2108] ||=
13
13
  begin
14
14
  request(:get, :client_r0, '/sync/sse', skip_auth: true, headers: { accept: 'text/event-stream' })
15
- rescue MatrixSdk::MatrixNotAuthorizedError # Returns 401 if implemented
15
+ rescue ActiveMatrix::MatrixNotAuthorizedError # Returns 401 if implemented
16
16
  true
17
- rescue MatrixSdk::MatrixRequestError
17
+ rescue ActiveMatrix::MatrixRequestError
18
18
  false
19
19
  end
20
20
  rescue StandardError => e
@@ -41,9 +41,7 @@ module MatrixSdk::Protocols::MSC
41
41
  unless on_data.is_a?(Proc) && on_data.arity == 2
42
42
  raise 'Needs to be logged in' unless access_token # TODO: Better error
43
43
 
44
- query = params.select do |k, _v|
45
- %i[filter full_state set_presence].include? k
46
- end
44
+ query = params.slice(:filter, :full_state, :set_presence)
47
45
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)
48
46
 
49
47
  req = Net::HTTP::Get.new(homeserver.dup.tap do |u|
@@ -125,7 +123,7 @@ module MatrixSdk::Protocols::MSC
125
123
 
126
124
  if %w[sync sync_error].include? event
127
125
  data = JSON.parse(data, symbolize_names: true)
128
- yield((MatrixSdk::Response.new self, data), event: event, id: id)
126
+ yield((ActiveMatrix::Response.new self, data), event: event, id: id)
129
127
  elsif event
130
128
  logger.info "MSC2108 : #{stream_id} : Received unknown event '#{event}'; #{data}"
131
129
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk::Protocols::SS
3
+ module ActiveMatrix::Protocols::SS
4
4
  # Gets the server version
5
5
  def server_version
6
- MatrixSdk::Response.new(self, request(:get, :federation_v1, '/version').server).tap do |resp|
6
+ ActiveMatrix::Response.new(self, request(:get, :federation_v1, '/version').server).tap do |resp|
7
7
  resp.instance_eval <<-'CODE', __FILE__, __LINE__ + 1
8
8
  def to_s
9
9
  "#{name} #{version}"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module ActiveMatrix
6
+ class Railtie < Rails::Railtie
7
+ initializer 'activematrix.configure_rails_initialization' do
8
+ # Configure Rails.logger as the default logger
9
+ ActiveMatrix.logger = Rails.logger
10
+ end
11
+
12
+ initializer 'activematrix.configure_cache' do
13
+ # Rails cache adapter is automatically used when Rails is detected
14
+ require 'active_matrix/util/rails_cache_adapter'
15
+ ActiveMatrix::Util::Tinycache.adapter = ActiveMatrix::Util::RailsCacheAdapter
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk
3
+ module ActiveMatrix
4
4
  # An usability wrapper for API responses as an extended [Hash]
5
5
  # All results can be read as both hash keys and as read-only methods on the key
6
6
  #
@@ -39,7 +39,7 @@ module MatrixSdk
39
39
  data.instance_variable_set(:@api, api)
40
40
 
41
41
  data.select { |_k, v| v.is_a? Hash }
42
- .each { |_v, v| Response.new api, v }
42
+ .each_value { |v| Response.new api, v }
43
43
 
44
44
  data
45
45
  end
@@ -1,15 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'matrix_sdk'
4
- require 'matrix_sdk/util/events'
5
- require 'matrix_sdk/util/tinycache'
6
-
7
- module MatrixSdk
3
+ module ActiveMatrix
8
4
  # A class for tracking the information about a room on Matrix
9
5
  class Room
10
- extend MatrixSdk::Extensions
11
- extend MatrixSdk::Util::Tinycache
12
- include MatrixSdk::Logging
6
+ extend ActiveMatrix::Extensions
7
+ extend ActiveMatrix::Util::Tinycache
8
+ include ActiveMatrix::Logging
13
9
 
14
10
  # @!attribute [rw] event_history_limit
15
11
  # @return [Fixnum] the limit of events to keep in the event log
@@ -34,8 +30,8 @@ module MatrixSdk
34
30
  cached :joined_members, cache_level: :all, expires_in: 60 * 60
35
31
 
36
32
  # Only cache unfiltered requests for aliases and members
37
- cached :aliases, unless: proc { |args| args.any? }, cache_level: :all, expires_in: 60 * 60
38
- cached :all_members, unless: proc { |args| args.any? }, cache_level: :all, expires_in: 60 * 60
33
+ cached :aliases, unless: proc { |_, args| args.any? }, cache_level: :all, expires_in: 60 * 60
34
+ cached :all_members, unless: proc { |_, args| args.any? }, cache_level: :all, expires_in: 60 * 60
39
35
 
40
36
  alias room_id id
41
37
  alias members joined_members
@@ -90,7 +86,7 @@ module MatrixSdk
90
86
  data.each do |k, v|
91
87
  next if %i[client].include? k
92
88
 
93
- if respond_to?("#{k}_cached?".to_sym) && send("#{k}_cached?".to_sym)
89
+ if respond_to?(:"#{k}_cached?") && send(:"#{k}_cached?")
94
90
  tinycache_adapter.write(k, v)
95
91
  elsif instance_variable_defined? "@#{k}"
96
92
  instance_variable_set("@#{k}", v)
@@ -178,7 +174,7 @@ module MatrixSdk
178
174
  # @return [String, nil] the canonical alias of the room
179
175
  def canonical_alias
180
176
  get_state('m.room.canonical_alias')[:alias]
181
- rescue MatrixSdk::MatrixNotFoundError
177
+ rescue ActiveMatrix::MatrixNotFoundError
182
178
  nil
183
179
  end
184
180
 
@@ -292,9 +288,9 @@ module MatrixSdk
292
288
  end
293
289
 
294
290
  def room_state
295
- return MatrixSdk::Util::StateEventCache.new self if client.cache == :none
291
+ return ActiveMatrix::Util::StateEventCache.new self if client.cache == :none
296
292
 
297
- @room_state ||= MatrixSdk::Util::StateEventCache.new self
293
+ @room_state ||= ActiveMatrix::Util::StateEventCache.new self
298
294
  end
299
295
 
300
296
  # Gets a state object in the room
@@ -525,7 +521,7 @@ module MatrixSdk
525
521
  # @param user_id [String,User] the MXID of the user
526
522
  # @return [Boolean] wether the action succeeded
527
523
  def invite_user(user_id)
528
- user_id = user_id.id if user_id.is_a? MatrixSdk::User
524
+ user_id = user_id.id if user_id.is_a? ActiveMatrix::User
529
525
  client.api.invite_user(id, user_id)
530
526
  true
531
527
  end
@@ -536,7 +532,7 @@ module MatrixSdk
536
532
  # @param reason [String] the reason for the kick
537
533
  # @return [Boolean] wether the action succeeded
538
534
  def kick_user(user_id, reason = '')
539
- user_id = user_id.id if user_id.is_a? MatrixSdk::User
535
+ user_id = user_id.id if user_id.is_a? ActiveMatrix::User
540
536
  client.api.kick_user(id, user_id, reason: reason)
541
537
  true
542
538
  end
@@ -547,7 +543,7 @@ module MatrixSdk
547
543
  # @param reason [String] the reason for the ban
548
544
  # @return [Boolean] wether the action succeeded
549
545
  def ban_user(user_id, reason = '')
550
- user_id = user_id.id if user_id.is_a? MatrixSdk::User
546
+ user_id = user_id.id if user_id.is_a? ActiveMatrix::User
551
547
  client.api.ban_user(id, user_id, reason: reason)
552
548
  true
553
549
  end
@@ -557,7 +553,7 @@ module MatrixSdk
557
553
  # @param user_id [String,User] the MXID of the user
558
554
  # @return [Boolean] wether the action succeeded
559
555
  def unban_user(user_id)
560
- user_id = user_id.id if user_id.is_a? MatrixSdk::User
556
+ user_id = user_id.id if user_id.is_a? ActiveMatrix::User
561
557
  client.api.unban_user(id, user_id)
562
558
  true
563
559
  end
@@ -572,9 +568,9 @@ module MatrixSdk
572
568
  end
573
569
 
574
570
  def account_data
575
- return MatrixSdk::Util::AccountDataCache.new client, room: self if client.cache == :none
571
+ return ActiveMatrix::Util::AccountDataCache.new client, room: self if client.cache == :none
576
572
 
577
- @account_data ||= MatrixSdk::Util::AccountDataCache.new client, room: self
573
+ @account_data ||= ActiveMatrix::Util::AccountDataCache.new client, room: self
578
574
  end
579
575
 
580
576
  # Retrieves a custom entry from the room-specific account data
@@ -640,7 +636,7 @@ module MatrixSdk
640
636
  # @return [Boolean,nil] True if the room is a space
641
637
  def space?
642
638
  room_type == 'm.space'
643
- rescue MatrixSdk::MatrixForbiddenError, MatrixSdk::MatrixNotFoundError
639
+ rescue ActiveMatrix::MatrixForbiddenError, ActiveMatrix::MatrixNotFoundError
644
640
  nil
645
641
  end
646
642
 
@@ -990,10 +986,10 @@ module MatrixSdk
990
986
 
991
987
  def ensure_room_handlers
992
988
  client.instance_variable_get(:@room_handlers)[id] ||= {
993
- account_data: MatrixSdk::EventHandlerArray.new,
994
- event: MatrixSdk::EventHandlerArray.new,
995
- state_event: MatrixSdk::EventHandlerArray.new,
996
- ephemeral_event: MatrixSdk::EventHandlerArray.new
989
+ account_data: ActiveMatrix::EventHandlerArray.new,
990
+ event: ActiveMatrix::EventHandlerArray.new,
991
+ state_event: ActiveMatrix::EventHandlerArray.new,
992
+ ephemeral_event: ActiveMatrix::EventHandlerArray.new
997
993
  }
998
994
  end
999
995
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk::Rooms
4
- class Space < MatrixSdk::Room
3
+ module ActiveMatrix::Rooms
4
+ class Space < ActiveMatrix::Room
5
5
  TYPE = 'm.space'
6
6
 
7
7
  def tree(suggested_only: nil, max_rooms: nil)
@@ -26,7 +26,7 @@ module MatrixSdk::Rooms
26
26
 
27
27
  # Inject available room information
28
28
  r.each do |k, v|
29
- if room.respond_to?("#{k}_cached?".to_sym) && send("#{k}_cached?".to_sym)
29
+ if room.respond_to?(:"#{k}_cached?") && send(:"#{k}_cached?")
30
30
  room.send(:tinycache_adapter).write(k, v)
31
31
  elsif room.instance_variable_defined? "@#{k}"
32
32
  room.instance_variable_set("@#{k}", v)
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'matrix_sdk'
4
-
5
- module MatrixSdk
3
+ module ActiveMatrix
6
4
  # A class for tracking information about a user on Matrix
7
5
  class User
8
- extend MatrixSdk::Extensions
6
+ extend ActiveMatrix::Extensions
9
7
 
10
8
  attr_reader :id, :client
11
9
  alias user_id :id
@@ -35,13 +33,13 @@ module MatrixSdk
35
33
  end
36
34
 
37
35
  # @return [String] the display name
38
- # @see MatrixSdk::Protocols::CS#get_display_name
36
+ # @see ActiveMatrix::Protocols::CS#get_display_name
39
37
  def display_name
40
38
  @display_name ||= client.api.get_display_name(id)[:displayname]
41
39
  end
42
40
 
43
41
  # @param name [String] the display name to set
44
- # @see MatrixSdk::Protocols::CS#set_display_name
42
+ # @see ActiveMatrix::Protocols::CS#set_display_name
45
43
  def display_name=(name)
46
44
  client.api.set_display_name(id, name)
47
45
  @display_name = name
@@ -55,7 +53,7 @@ module MatrixSdk
55
53
 
56
54
  # Gets the avatar for the user
57
55
  #
58
- # @see MatrixSdk::Protocols::CS#get_avatar_url
56
+ # @see ActiveMatrix::Protocols::CS#get_avatar_url
59
57
  def avatar_url
60
58
  @avatar_url ||= client.api.get_avatar_url(id)[:avatar_url]
61
59
  end
@@ -67,8 +65,8 @@ module MatrixSdk
67
65
  #
68
66
  # @param url [String,URI::MXC] the new avatar URL
69
67
  # @note Requires a mxc:// URL, check example on
70
- # {MatrixSdk::Protocols::CS#set_avatar_url} for how this can be done
71
- # @see MatrixSdk::Protocols::CS#set_avatar_url
68
+ # {ActiveMatrix::Protocols::CS#set_avatar_url} for how this can be done
69
+ # @see ActiveMatrix::Protocols::CS#set_avatar_url
72
70
  def avatar_url=(url)
73
71
  client.api.set_avatar_url(id, url)
74
72
  @avatar_url = url
@@ -93,7 +91,7 @@ module MatrixSdk
93
91
  # Get the user's current presence status
94
92
  #
95
93
  # @return [Symbol] One of :online, :offline, :unavailable
96
- # @see MatrixSdk::Protocols::CS#get_presence_status
94
+ # @see ActiveMatrix::Protocols::CS#get_presence_status
97
95
  # @note This information is not cached in the abstraction layer
98
96
  def presence
99
97
  raw_presence[:presence]&.to_sym
@@ -103,7 +101,7 @@ module MatrixSdk
103
101
  # Should be one of :online, :offline, or :unavailable
104
102
  #
105
103
  # @param new_presence [:online,:offline,:unavailable] The new presence status to set
106
- # @see MatrixSdk::Protocols::CS#set_presence_status
104
+ # @see ActiveMatrix::Protocols::CS#set_presence_status
107
105
  def presence=(new_presence)
108
106
  raise ArgumentError, 'Presence must be one of :online, :offline, :unavailable' unless %i[online offline unavailable].include?(presence)
109
107
 
@@ -118,7 +116,7 @@ module MatrixSdk
118
116
 
119
117
  # Gets the user-specified status message - if any
120
118
  #
121
- # @see MatrixSdk::Protocols::CS#get_presence_status
119
+ # @see ActiveMatrix::Protocols::CS#get_presence_status
122
120
  # @note This information is not cached in the abstraction layer
123
121
  def status_msg
124
122
  raw_presence[:status_msg]
@@ -127,7 +125,7 @@ module MatrixSdk
127
125
  # Sets the user-specified status message
128
126
  #
129
127
  # @param message [String,nil] The message to set, or nil for no message
130
- # @see MatrixSdk::Protocols::CS#set_presence_status
128
+ # @see ActiveMatrix::Protocols::CS#set_presence_status
131
129
  def status_msg=(message)
132
130
  client.api.set_presence_status(id, presence, message: message)
133
131
  end
@@ -135,7 +133,7 @@ module MatrixSdk
135
133
  # Gets the last time the user was active at, from the server's side
136
134
  #
137
135
  # @return [Time] when the user was last active
138
- # @see MatrixSdk::Protocols::CS#get_presence_status
136
+ # @see ActiveMatrix::Protocols::CS#get_presence_status
139
137
  # @note This information is not cached in the abstraction layer
140
138
  def last_active
141
139
  since = raw_presence[:last_active_ago]
@@ -147,7 +145,7 @@ module MatrixSdk
147
145
  # Gets a direct message room with the user if one exists
148
146
  #
149
147
  # @return [Room,nil] A direct message room if one exists
150
- # @see MatrixSdk::Client#direct_room
148
+ # @see ActiveMatrix::Client#direct_room
151
149
  def direct_room
152
150
  client.direct_room(id)
153
151
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk::Util
3
+ module ActiveMatrix::Util
4
4
  class AccountDataCache
5
- extend MatrixSdk::Extensions
6
- extend MatrixSdk::Util::Tinycache
5
+ extend ActiveMatrix::Extensions
6
+ extend ActiveMatrix::Util::Tinycache
7
7
  include Enumerable
8
8
 
9
9
  attr_reader :client, :room
@@ -13,7 +13,7 @@ module MatrixSdk::Util
13
13
  ignore_inspect :client, :room, :tinycache_adapter
14
14
 
15
15
  def initialize(client, room: nil, cache_time: 1 * 60 * 60, **_params)
16
- raise ArgumentError, 'Must be given a Client instance' unless client.is_a? MatrixSdk::Client
16
+ raise ArgumentError, 'Must be given a Client instance' unless client.is_a? ActiveMatrix::Client
17
17
 
18
18
  @client = client
19
19
  @cache_time = cache_time
@@ -21,7 +21,7 @@ module MatrixSdk::Util
21
21
  return unless room
22
22
 
23
23
  @room = room
24
- @room = client.ensure_room room unless @room.is_a? MatrixSdk::Room
24
+ @room = client.ensure_room room unless @room.is_a? ActiveMatrix::Room
25
25
  end
26
26
 
27
27
  def reload!
@@ -73,7 +73,7 @@ module MatrixSdk::Util
73
73
  else
74
74
  client.api.get_account_data(client.mxid, key)
75
75
  end
76
- rescue MatrixSdk::MatrixNotFoundError
76
+ rescue ActiveMatrix::MatrixNotFoundError
77
77
  {}
78
78
  end
79
79
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk
3
+ module ActiveMatrix
4
4
  class EventHandlerArray < Hash
5
- include MatrixSdk::Logging
5
+ include ActiveMatrix::Logging
6
6
  attr_accessor :reraise_exceptions
7
7
 
8
- def initialize(*args)
8
+ def initialize(*)
9
9
  @reraise_exceptions = false
10
10
 
11
- super(*args)
11
+ super
12
12
  end
13
13
 
14
14
  def add_handler(filter = nil, id = nil, &block)
@@ -32,7 +32,7 @@ module MatrixSdk
32
32
  end
33
33
 
34
34
  class Event
35
- extend MatrixSdk::Extensions
35
+ extend ActiveMatrix::Extensions
36
36
 
37
37
  attr_writer :handled
38
38
 
@@ -57,7 +57,7 @@ module MatrixSdk
57
57
 
58
58
  def initialize(error, source)
59
59
  @error = error
60
- super source
60
+ super(source)
61
61
  end
62
62
 
63
63
  def source
@@ -74,7 +74,7 @@ module MatrixSdk
74
74
  def initialize(sender, event = nil, filter = nil)
75
75
  @event = event
76
76
  @filter = filter || @event[:type]
77
- super sender
77
+ super(sender)
78
78
  end
79
79
 
80
80
  def matches?(filter, filter_override = nil)
@@ -93,7 +93,7 @@ module MatrixSdk
93
93
  end
94
94
 
95
95
  def to_s
96
- "#{event[:type]}: #{event.reject { |k, _v| k == :type }.to_json}"
96
+ "#{event[:type]}: #{event.except(:type).to_json}"
97
97
  end
98
98
 
99
99
  def method_missing(method, *args)
@@ -8,7 +8,7 @@ unless Object.respond_to? :yield_self
8
8
  end
9
9
  end
10
10
 
11
- module MatrixSdk
11
+ module ActiveMatrix
12
12
  module Extensions
13
13
  def events(*symbols)
14
14
  module_name = "#{name}Events"
@@ -21,7 +21,7 @@ module MatrixSdk
21
21
  name = sym.to_s
22
22
 
23
23
  initializers << "
24
- @on_#{name} = MatrixSdk::EventHandlerArray.new
24
+ @on_#{name} = ActiveMatrix::EventHandlerArray.new
25
25
  "
26
26
  readers << ":on_#{name}"
27
27
  methods << "
@@ -69,17 +69,4 @@ module MatrixSdk
69
69
  *, __FILE__, __LINE__ - 14
70
70
  end
71
71
  end
72
-
73
- module Logging
74
- def logger
75
- return MatrixSdk.logger if MatrixSdk.global_logger?
76
- return @logger if instance_variable_defined?(:@logger) && @logger
77
-
78
- ::Logging.logger[self]
79
- end
80
-
81
- def logger=(logger)
82
- @logger = logger
83
- end
84
- end
85
72
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveMatrix
4
+ module Util
5
+ class RailsCacheAdapter
6
+ attr_accessor :client
7
+
8
+ def initialize
9
+ @cache = ::Rails.cache
10
+ end
11
+
12
+ def read(key, _options = {})
13
+ @cache.read(key)
14
+ end
15
+
16
+ def write(key, value, expires_in: nil)
17
+ @cache.write(key, value, expires_in: expires_in)
18
+ end
19
+
20
+ def exist?(key)
21
+ @cache.exist?(key)
22
+ end
23
+
24
+ def delete(key)
25
+ @cache.delete(key)
26
+ end
27
+
28
+ def clear
29
+ @cache.clear
30
+ end
31
+
32
+ def cleanup
33
+ # Rails.cache handles its own cleanup
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module MatrixSdk::Util
3
+ module ActiveMatrix::Util
4
4
  class StateEventCache
5
- extend MatrixSdk::Extensions
6
- extend MatrixSdk::Util::Tinycache
5
+ extend ActiveMatrix::Extensions
6
+ extend ActiveMatrix::Util::Tinycache
7
7
  include Enumerable
8
8
 
9
9
  attr_reader :room
@@ -13,7 +13,7 @@ module MatrixSdk::Util
13
13
  ignore_inspect :client, :room, :tinycache_adapter
14
14
 
15
15
  def initialize(room, cache_time: 30 * 60, **_params)
16
- raise ArgumentError, 'Must be given a Room instance' unless room.is_a? MatrixSdk::Room
16
+ raise ArgumentError, 'Must be given a Room instance' unless room.is_a? ActiveMatrix::Room
17
17
 
18
18
  @room = room
19
19
  @cache_time = cache_time
@@ -46,11 +46,11 @@ module MatrixSdk::Util
46
46
  end
47
47
 
48
48
  def key?(type, key = nil)
49
- keys.key?("#{type}#{key ? "|#{key}" : ''}")
49
+ keys.key?("#{type}#{"|#{key}" if key}")
50
50
  end
51
51
 
52
52
  def expire(type, key = nil)
53
- tinycache_adapter.expire("#{type}#{key ? "|#{key}" : ''}")
53
+ tinycache_adapter.expire("#{type}#{"|#{key}" if key}")
54
54
  end
55
55
 
56
56
  def each(live: false)
@@ -71,14 +71,14 @@ module MatrixSdk::Util
71
71
  def delete(type, key = nil)
72
72
  type = type.to_s unless type.is_a? String
73
73
  client.api.set_room_state(room.id, type, {}, **{ state_key: key }.compact)
74
- tinycache_adapter.delete("#{type}#{key ? "|#{key}" : ''}")
74
+ tinycache_adapter.delete("#{type}#{"|#{key}" if key}")
75
75
  end
76
76
 
77
77
  def [](type, key = nil)
78
78
  type = type.to_s unless type.is_a? String
79
- tinycache_adapter.fetch("#{type}#{key ? "|#{key}" : ''}", expires_in: @cache_time) do
79
+ tinycache_adapter.fetch("#{type}#{"|#{key}" if key}", expires_in: @cache_time) do
80
80
  client.api.get_room_state(room.id, type, **{ key: key }.compact)
81
- rescue MatrixSdk::MatrixNotFoundError
81
+ rescue ActiveMatrix::MatrixNotFoundError
82
82
  {}
83
83
  end
84
84
  end
@@ -86,7 +86,7 @@ module MatrixSdk::Util
86
86
  def []=(type, key = nil, value) # rubocop:disable Style/OptionalArguments Not possible to put optional last
87
87
  type = type.to_s unless type.is_a? String
88
88
  client.api.set_room_state(room.id, type, value, **{ state_key: key }.compact)
89
- tinycache_adapter.write("#{type}#{key ? "|#{key}" : ''}", value)
89
+ tinycache_adapter.write("#{type}#{"|#{key}" if key}", value)
90
90
  end
91
91
  end
92
92
  end