Class: MatrixDBus::Matrix
- Inherits:
-
Object
- Object
- MatrixDBus::Matrix
- Defined in:
- lib/matrix_dbus/matrix.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#network ⇒ Object
readonly
Returns the value of attribute network.
Instance Method Summary collapse
- #bind(func) ⇒ Object
- #check_login ⇒ Object
- #error(res) ⇒ Object
- #init_batch ⇒ Object
-
#initialize(username: '', password: '', host: 'https://matrix.org:8448') ⇒ Matrix
constructor
A new instance of Matrix.
- #load_batch ⇒ Object
- #load_token ⇒ Object
- #login ⇒ Object
- #quit ⇒ Object
- #run ⇒ Object
- #save_batch ⇒ Object
- #save_token ⇒ Object
- #sync ⇒ Object
Constructor Details
#initialize(username: '', password: '', host: 'https://matrix.org:8448') ⇒ Matrix
Returns a new instance of Matrix.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/matrix_dbus/matrix.rb', line 6 def initialize(username: '', password: '', host: 'https://matrix.org:8448') @username = username @password = password @run = true @event_method = [] @config = File.join(ENV['HOME'], '.config', 'matrix-qq') @network = Network.new(host + '/_matrix/client/r0', method(:error)) load_token load_batch end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
4 5 6 |
# File 'lib/matrix_dbus/matrix.rb', line 4 def access_token @access_token end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
4 5 6 |
# File 'lib/matrix_dbus/matrix.rb', line 4 def json @json end |
#network ⇒ Object (readonly)
Returns the value of attribute network.
4 5 6 |
# File 'lib/matrix_dbus/matrix.rb', line 4 def network @network end |
Instance Method Details
#bind(func) ⇒ Object
17 18 19 |
# File 'lib/matrix_dbus/matrix.rb', line 17 def bind(func) @event_method << func end |
#check_login ⇒ Object
53 54 55 56 57 58 |
# File 'lib/matrix_dbus/matrix.rb', line 53 def check_login json = @network.get '/login' raise "can't login" unless json['flows'].find do |f| f['type'] == 'm.login.password' end end |
#error(res) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/matrix_dbus/matrix.rb', line 90 def error(res) json = JSON.parse res.body return json unless json.key? 'error' case json['errcode'] when 'M_UNKNOWN_TOKEN' then login else raise json['error'] end end |
#init_batch ⇒ Object
73 74 75 76 77 |
# File 'lib/matrix_dbus/matrix.rb', line 73 def init_batch json = get "/sync?access_token=#{@access_token}" @next_batch = json['next_batch'] save_batch end |
#load_batch ⇒ Object
46 47 48 49 50 51 |
# File 'lib/matrix_dbus/matrix.rb', line 46 def load_batch sync unless File.exist? File.join(@config, 'batch') File.open File.join(@config, 'batch') do |f| @next_batch = f.gets.chomp end end |
#load_token ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/matrix_dbus/matrix.rb', line 31 def load_token login unless File.exist? File.join @config, 'token' login unless File.size? File.join @config, 'token' File.open File.join(@config, 'token'), 'r', 0o600 do |f| @access_token = f.gets.chomp end end |
#login ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/matrix_dbus/matrix.rb', line 60 def login check_login raise 'Unknow username' if @username == '' raise 'Unknow password' if @password == '' json = @network.post \ '/login', type: 'm.login.password', user: @username, password: @password @access_token = json['access_token'] save_token end |
#quit ⇒ Object
99 100 101 102 |
# File 'lib/matrix_dbus/matrix.rb', line 99 def quit @run = false save_batch end |
#run ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/matrix_dbus/matrix.rb', line 104 def run while @run sync save_batch @event_method.each { |func| func.call self } sleep 1 end end |
#save_batch ⇒ Object
39 40 41 42 43 44 |
# File 'lib/matrix_dbus/matrix.rb', line 39 def save_batch puts @next_batch if $DEBUG File.open File.join(@config, 'batch'), 'w' do |f| f.puts @next_batch end end |
#save_token ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/matrix_dbus/matrix.rb', line 21 def save_token unless File.directory? @config File.delete @config if File.exist? @config Dir.mkdir @config end File.open File.join(@config, 'token'), 'w', 0o600 do |f| f.puts @access_token end end |
#sync ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/matrix_dbus/matrix.rb', line 79 def sync init_batch if @next_batch.nil? @json = @network.get \ '/sync', since: @next_batch, timeout: 10_000, access_token: @access_token @next_batch = @json['next_batch'] save_batch end |