matrix_sdk 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1edf23725115b5d4e4891db5baa88ff1d7850fff891921c0d5f294d74e04d4bc
4
- data.tar.gz: 8e836ac2b7f6758ee89f43f964e6495df332db826d523456563c95c9cc3261ac
3
+ metadata.gz: 6d0b5d000dfb07e1f488a66587a403c087c7056b2cde52cd2ee87e4835972a23
4
+ data.tar.gz: 1fdd6f7a913b42cf8654eeed0b198d14337f47ab7da71baf43c1b2779d752eb5
5
5
  SHA512:
6
- metadata.gz: 3c86db36a677016fb8fcdb9b56a0ba307f6d21d7fccc5fe445557258d339e7ed5a9cc29602ea75f10f3d95e2f77f16c5e9cd9ac4ec4e4314739c61d1c8054cf9
7
- data.tar.gz: fc66623c861bc0d8bb7941330ef9d21661df4a219c872c10b988a328dac6d7d2b0fdfa5a0b5641b145ecc6badf1bbe30ad4dff4d52650aa15046843381d2c9f6
6
+ metadata.gz: 466f80ee4d8b06c1827de39e7526a23e62e654db99c2102db5552492992a58fd6f6a4883d1c90d2a577c345d67312de51d5cbdb0a2bfbcab461278141dffcbc4
7
+ data.tar.gz: 189b5b6243fb574b60ac02036850bfe33f20499b4a88ea9840491892f840fd367167be5233da73e08811d6d8721523bce9027e4907ea6c927a9c70a8c1ed20cc
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
3
+ Exclude:
4
+ - '*.spec'
5
+ - 'Rakefile'
6
+
7
+ # Don't enforce documentation
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Metrics/MethodLength:
12
+ Max: 40
13
+
14
+ Metrics/LineLength:
15
+ Max: 190
16
+
17
+ Style/RescueModifier:
18
+ Enabled: false
19
+
20
+ Style/RegexpLiteral:
21
+ Enabled: false
22
+
23
+ Style/MultilineBlockChain:
24
+ Enabled: false
25
+
26
+ Metrics/AbcSize:
27
+ Enabled: false
28
+
29
+ Metrics/CyclomaticComplexity:
30
+ Enabled: false
31
+
32
+ Metrics/PerceivedComplexity:
33
+ Enabled: false
34
+
35
+ Style/FormatStringToken:
36
+ Enabled: false
37
+
38
+ Naming/AccessorMethodName:
39
+ Enabled: false
data/README.md CHANGED
@@ -3,18 +3,43 @@
3
3
  A Ruby gem for easing the development of software that communicates with servers implementing the Matrix protocol.
4
4
 
5
5
 
6
- ## Usage
6
+ ## Example usage
7
7
 
8
8
  ```ruby
9
+ # Raw API usage
10
+ require 'matrix_sdk'
11
+
9
12
  api = MatrixSdk::Api.new 'https://matrix.org'
10
13
 
11
14
  api.login user: 'example', password: 'notarealpass'
12
15
  api.whoami?
16
+ # => {:user_id=>"@example:matrix.org"}
17
+
18
+ # It's possible to call arbitrary APIs as well
19
+ api.request :get, :federation_v1, '/version'
20
+ # => {:server=>{:version=>"0.28.1", :name=>"Synapse"}}
21
+ ```
22
+
23
+ ```ruby
24
+ # Client wrapper
25
+ require 'matrix_sdk'
26
+
27
+ client = MatrixSdk::Client.new 'https://matrix.org'
28
+ client.login user: 'example', password: 'notarealpass' # no_sync: true
29
+
30
+ client.rooms.count
31
+ # => 5
32
+ hq = client.find_room '#matrix:matrix.org'
33
+ # => #<MatrixSdk::Room:00005592a1161528 @id="!cURbafjkfsMDVwdRDQ:matrix.org" @name="Matrix HQ" @topic="The Official Matrix HQ - please come chat here! | To support Matrix.org development: https://patreon.com/matrixdotorg | Try http://riot.im/app for a glossy web client | Looking for homeserver hosting? Check out https://upcloud.com/matrix!" @canonical_alias="#matrix:matrix.org" @aliases=["#matrix:jda.mn"] @join_rule=:public @guest_access=:can_join @event_history_limit=10>
34
+ hq.guest_access?
35
+ # => true
36
+ hq.send_text "This is an example message - don't actually do this ;)"
37
+ # => {:event_id=>"$123457890abcdef:matrix.org"}
13
38
  ```
14
39
 
15
40
  ## Contributing
16
41
 
17
- Bug reports and pull requests are welcome on GitHub at https://github.com/ananace/ruby-matrix-sdk.
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ananace/ruby-matrix-sdk
18
43
 
19
44
 
20
45
  ## License
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'io/console'
4
+ require 'matrix_sdk'
5
+
6
+ class SimpleClient < MatrixSdk::Client
7
+ def initialize(hs_url)
8
+ super hs_url, sync_filter_limit: 10
9
+
10
+ @pls = {}
11
+ end
12
+
13
+ def add_listener(room)
14
+ room.on_event { |ev| on_message(room, ev) }
15
+ end
16
+
17
+ def run
18
+ start_listener_thread
19
+ end
20
+
21
+ private
22
+
23
+ def get_user_level(room, mxid)
24
+ levels = @pls[room.id] ||= api.get_power_levels(room.id)[:users]
25
+ levels[mxid.to_sym]
26
+ end
27
+
28
+ def on_message(room, event)
29
+ puts "Event: #{ev}"
30
+ case ev[:type]
31
+ when 'm.room.member'
32
+ puts "#{Time.now.strftime '%H:%M'} #{event[:content][:displayname]} joined." if event['membership'] == 'join'
33
+ when 'm.room.message'
34
+ user = get_user event[:sender]
35
+ admin_level = get_user_level(room, user.id)
36
+ prefix = (admin_level >= 100 ? '@' : (admin_level >= 50 ? '+' : ' '))
37
+ if %w[m.text m.notice].include? event[:content][:msgtype]
38
+ puts "#{Time.now.strftime '%H:%M'} <#{prefix}#{user.display_name}> #{event[:content][:body]}"
39
+ elsif event[:content][:msgtype] == 'm.emote'
40
+ puts "#{Time.now.strftime '%H:%M'} *#{prefix}#{user.display_name} #{event[:content][:body]}"
41
+ else
42
+ puts "#{Time.now.strftime '%H:%M'} <#{prefix}#{user.display_name}> [#{event[:content][:msgtype]}] #{event[:content][:body]} - #{api.get_download_url event[:content][:url]}"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ if $PROGRAM_NAME == __FILE__
49
+ raise "Usage: #{$PROGRAM_NAME} [-d] homeserver_url room_id_or_alias" unless ARGV.length >= 2
50
+ begin
51
+ if ARGV.first == '-d'
52
+ MatrixSdk.debug!
53
+ ARGV.shift
54
+ end
55
+
56
+ client = SimpleClient.new ARGV.first
57
+
58
+ print 'Username: '
59
+ user = STDIN.gets.strip
60
+ puts 'Password: '
61
+ password = STDIN.noecho(&:gets).strip
62
+
63
+ puts 'Logging in...'
64
+ client.login(user, password, sync_timeout: 5)
65
+
66
+ puts 'Finding room...'
67
+ room = client.find_room(ARGV.last)
68
+ room ||= begin
69
+ puts 'Joining room...'
70
+ client.join_room(ARGV.last)
71
+ end
72
+
73
+ client.add_listener(room)
74
+
75
+ puts 'Starting listener'
76
+ client.run
77
+
78
+ puts 'Entering main loop'
79
+ loop do
80
+ print '> '
81
+ msg = STDIN.gets.strip
82
+ break if msg.start_with? '/quit'
83
+
84
+ if msg.start_with? '/me'
85
+ room.send_emote msg.gsub(/\/me\s*/, '')
86
+ else
87
+ room.send_text msg
88
+ end
89
+ end
90
+ ensure
91
+ client.logout if client
92
+ end
93
+ end