mattermost-ruby 0.1.1 → 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
  SHA1:
3
- metadata.gz: 840e068b319f9d5211b3264fb9c9611a5fef1d4e
4
- data.tar.gz: ba85421a1c3b0dfc59b1a4f9c7ab8cb34c94355a
3
+ metadata.gz: 25e25af58c2cddfaec9f104a01d2e175630536b0
4
+ data.tar.gz: e630507758d67fc08881ca1d0ba1ef2dc3dcb44d
5
5
  SHA512:
6
- metadata.gz: 99235fefb90bb3b9964d1f5ceb4969e0d06c915aea3f2e58394441ff201057f4784335b4cfe535c62974629317090eebcad9fb8e749db666ecb6b676c8663816
7
- data.tar.gz: b47b2fe3cb4c41d6c647ab2458d9be5d80610c2c6f1793438793fc9dad0b9cb2944c17efb2f93142034e9f089909c873051120529bfe29c0ea914ab11caddf29
6
+ metadata.gz: bef58638a0e26002b9e4f836bc337b9d25d0eed7e3702d1508af2532cd24e6d37466338945013c1586cf0cc826bbd14e8eb2cf1b2b0cfe17f603573755c2bc92
7
+ data.tar.gz: 67a0da25a8eb63ca465353dca08e7dc1235a36346cc52224ba7b95d710c3b8fbeb3d34382484ff10126faebd155a5c941836a458808d26abf7d047e8432998dd
data/.gitignore CHANGED
@@ -9,4 +9,5 @@
9
9
  /tmp/
10
10
  mattermost-ruby*.gem
11
11
  .DS_Store
12
- test.rb
12
+ test.rb
13
+ /.idea/
data/lib/mattermost.rb CHANGED
@@ -1,4 +1,4 @@
1
- #require "mattermost/version"
1
+ require_relative 'mattermost/version'
2
2
 
3
3
  require 'httparty'
4
4
  require 'pry'
@@ -6,6 +6,7 @@ require 'pry'
6
6
  require_relative 'mattermost/mattermost_object'
7
7
  require_relative 'mattermost/models/base'
8
8
  require_relative 'mattermost/team'
9
+ require_relative 'mattermost/post'
9
10
  require_relative 'mattermost/user'
10
11
  require_relative 'mattermost/admin'
11
12
  require_relative 'mattermost/channel'
@@ -20,14 +21,16 @@ module Mattermost
20
21
 
21
22
  def self.connect(username, password, server, team_name, options = {})
22
23
  self.base_uri server
23
- request = self.post('/users/login',
24
- :body => { :name => team_name, :username => username, :password => password}.to_json )
25
- self.headers "Cookie" => "MMTOKEN=#{request.headers['token']}"
26
- self.headers "X-Requested-With" => 'XMLHttpRequest'
27
- @team = Team.new({:id => request.parsed_response['team_id'], :name => team_name})
28
24
  options[:httparty].each do |k,v|
29
25
  self.send(k, v)
30
26
  end
27
+ request = self.post('/users/login',
28
+ :body => { :login_id => username, :password => password, :token => "" }.to_json )
29
+ self.headers "Cookie" => "MMAUTHTOKEN=#{request.headers['token']}"
30
+ self.headers "X-Requested-With" => 'XMLHttpRequest'
31
+ initial_load = self.get("/users/initial_load")
32
+ team = initial_load.parsed_response['teams'].select { |team| team['name'] == team_name }.first
33
+ @team = Team.new(team)
31
34
  unless options[:preload_user] == false
32
35
  Mattermost::User.all
33
36
  end
@@ -1,35 +1,46 @@
1
1
  module Mattermost
2
2
  class Admin < MattermostObject
3
3
 
4
- def self.get_logs
4
+ # Mattermost puts this under users, but I feel it doesn't belong there
5
+ # since it says more about the logged in user and the system than
6
+ # a user collection itself.
7
+ def self.initial_load
8
+ Mattermost.get("/users/initial_load")
9
+ end
10
+
11
+ def self.logs
5
12
  Mattermost.get("/admin/logs")
6
13
  end
7
14
 
8
- def self.get_all_audits
15
+ def self.audits
9
16
  Mattermost.get("/admin/audits")
10
17
  end
11
18
 
12
- def self.get_config
19
+ def self.config
13
20
  Mattermost.get("/admin/config")
14
21
  end
15
22
 
16
23
  def self.save_config
24
+ raise NotImplementedError
17
25
  Mattermost.post("/admin/save_config")
18
26
  end
19
27
 
20
28
  def self.test_email
29
+ raise NotImplementedError
21
30
  Mattermost.post("/admin/test_email")
22
31
  end
23
32
 
24
- def self.get_client_config
33
+ def self.client_config
25
34
  Mattermost.get("/admin/client_props")
26
35
  end
27
36
 
28
37
  def self.log_client
38
+ raise NotImplementedError
29
39
  Mattermost.post("/admin/log_client")
30
40
  end
31
41
 
32
- def self.get_analytics(name, id = nil)
42
+ def self.analytics(name, id = nil)
43
+ raise NotImplementedError
33
44
  uri = "/admin/analytics"
34
45
  uri += "/#{id}" if id
35
46
  uri += "/#{name}"
@@ -37,30 +48,37 @@ module Mattermost
37
48
  end
38
49
 
39
50
  def self.save_compliance_report
51
+ raise NotImplementedError
40
52
  Mattermost.post("/admin/save_compliance_report")
41
53
  end
42
54
 
43
- def self.get_compliance_reports
55
+ def self.compliance_reports
56
+ raise NotImplementedError
44
57
  Mattermost.get("/admin/compliance_reports")
45
58
  end
46
59
 
47
60
  def self.download_compliance_report(id)
61
+ raise NotImplementedError
48
62
  Mattermost.get("/admin/download_compliance_report/#{id}")
49
63
  end
50
64
 
51
65
  def self.upload_brand_image
66
+ raise NotImplementedError
52
67
  Mattermost.post("/admin/upload_brand_image")
53
68
  end
54
69
 
55
70
  def self.get_brand_image
71
+ raise NotImplementedError
56
72
  Mattermost.get("/admin/get_brand_image")
57
73
  end
58
74
 
59
75
  def self.admin_reset_mfa
76
+ raise NotImplementedError
60
77
  Mattermost.post("/admin/reset_mfa")
61
78
  end
62
79
 
63
80
  def self.admin_reset_password
81
+ raise NotImplementedError
64
82
  Mattermost.post("/admin/reset_password")
65
83
  end
66
84
 
@@ -5,7 +5,8 @@ module Mattermost
5
5
  ::Channel.new(attributes)
6
6
  end
7
7
 
8
- # Returns all channels for the current team
8
+ # Returns channels for the current team _that the logged in user has joined_
9
+ # call Mattermost::Channel.more to get a list of all the channels (like, actually)
9
10
  #
10
11
  # @param force_refresh [boolean] to recache the channels
11
12
  def self.all(force_refresh = false)
@@ -13,12 +14,19 @@ module Mattermost
13
14
  @channels ||= all_channels
14
15
  end
15
16
 
16
- # Returns "more" channels for the current team
17
+ # Returns all of the channels for the current team
18
+ # Unlike self.all which only returns the channels the user has joined
17
19
  def self.more
18
- Mattermost.get("/channels/more")
20
+ channels = []
21
+ request = Mattermost.get("/teams/#{Mattermost.team.id}/channels/more")
22
+ request.parsed_response['channels'].each do |channel|
23
+ channels << self.new(channel)
24
+ end
25
+ return channels
19
26
  end
20
27
 
21
- # Returns a hash of counts for each *team*
28
+ # Returns a hash of counts
29
+ # This is mostly useless because fetching a channel will return the counts for you.
22
30
  # {"counts"=>{"ps6kdfuk9p8mjx6pkr3krgq3by"=>58334, "yckjbepc4frbmmq9in6tap1dwa"=>32},
23
31
  # "update_times"=>{"ps6kdfuk9p8mjx6pkr3krgq3by"=>1463180151709, "yckjbepc4frbmmq9in6tap1dwa"=>1457216806189}}
24
32
  def self.counts
@@ -33,11 +41,11 @@ module Mattermost
33
41
  protected
34
42
  def self.all_channels
35
43
  channels = []
36
- request = Mattermost.get("/channels/")
44
+ request = Mattermost.get("/teams/#{Mattermost.team.id}/channels/")
37
45
  request.parsed_response['channels'].each do |channel|
38
46
  channels << self.new(channel)
39
47
  end
40
- channels
48
+ return channels
41
49
  end
42
50
 
43
51
  end
@@ -4,6 +4,18 @@ module Mattermost
4
4
  def initialize
5
5
  end
6
6
 
7
+ # Return an object that matches a given attribute.
8
+ # Return nil if no matches
9
+ def self.find_by(opts = {})
10
+ obj = all.select { |obj| obj.send(opts.keys.first) == opts.values.first }.first
11
+ return obj
12
+ end
13
+
14
+ # Return the user that has an id of @param id
15
+ def self.find(id)
16
+ find_by(:id => id)
17
+ end
18
+
7
19
  class << self
8
20
  def method_missing(method_id, *args) #:nodoc:
9
21
  o = self.new
@@ -14,6 +14,21 @@ class Channel < Base
14
14
  Mattermost.get("/channels/#{self.id}/")
15
15
  end
16
16
 
17
+ # Returns posts for the channel _before_ a given post.id
18
+ #
19
+ # @param before [string] to get posts before a given post id
20
+ def posts(before = nil)
21
+ uri = "/channels/#{self.id}/posts"
22
+ uri += "/#{before}" if before
23
+ uri += "/0/60?_=#{Time.now.to_i}"
24
+ request = Mattermost.get("/channels/#{self.id}/posts/0/60?_=#{Time.now.to_i}")
25
+ response = {}
26
+ request.parsed_response['posts'].each do |_, post|
27
+ response[k] = Post.new(post)
28
+ end
29
+ response
30
+ end
31
+
17
32
  # Get the users in a channel
18
33
  def get_channel_extra_info(member_limit = nil)
19
34
  uri = "/channels/#{self.id}/extra_info"
@@ -48,6 +63,7 @@ class Channel < Base
48
63
  user_id = user.is_a? User ? user.id : user
49
64
  Mattermost.post("/channels/#{self.id}/add", :body => {:user_id => user_id})
50
65
  end
66
+ alias_method :add_user, :add_member
51
67
 
52
68
  # Add a user to a channel
53
69
  # Send a user object or a user_id
@@ -55,6 +71,7 @@ class Channel < Base
55
71
  user_id = user.is_a? User ? user.id : user
56
72
  Mattermost.post("/channels/#{self.id}/remove", :body => {:user_id => user_id})
57
73
  end
74
+ alias_method :remove_user, :remove_member
58
75
 
59
76
  def update_last_viewed_at
60
77
  Mattermost.post("/channels/#{self.id}/update_last_viewed_at")
@@ -68,27 +85,19 @@ class Channel < Base
68
85
  # Update the channel's header. I really hate that they have a specific
69
86
  # method for this.
70
87
  def update_header(header)
71
- Mattermost.post("/channels/update_header", :body => {:channel_id => self.id, :channel_header => header})
88
+ Mattermost.post("/teams/#{Mattermost.team.id}/channels/#{self.id}/update_header", :body => {:channel_id => self.id, :channel_header => header}.to_json)
72
89
  end
73
90
 
74
91
  # Update the channel's purpose. I really hate that they have a specific
75
92
  # method for this.
76
93
  def update_purpose(purpose)
77
- Mattermost.post("/channels/update_purpose", :body => {:channel_id => self.id, :channel_purpose => purpose})
94
+ Mattermost.post("/teams/#{Mattermost.team.id}/channels/#{self.id}/update_purpose", :body => {:channel_id => self.id, :channel_purpose => purpose}.to_json)
78
95
  end
79
96
 
80
97
  def self.default_attributes
81
98
  {:display_name => String, :team_id => String, :type => Integer, :purpose => String}
82
99
  end
83
100
 
84
- def posts
85
- request = Mattermost.get("/channels/#{self.id}/posts/0/60?_=#{Time.now.to_i}")
86
- response = {}
87
- request.parsed_response['posts'].each do |k, v|
88
- response[k] = Post.new(v)
89
- end
90
- response
91
- end
92
101
 
93
102
  protected
94
103
  def create_channel
@@ -3,7 +3,9 @@ class Post < Base
3
3
 
4
4
  def initialize(attributes)
5
5
  super(attributes)
6
- @user = Mattermost::User.find(attributes['user_id'])
6
+ if attributes['user_id']
7
+ @user = Mattermost::User.find(attributes['user_id'])
8
+ end
7
9
  end
8
10
 
9
11
  end
@@ -1,7 +1,7 @@
1
1
  class Team < Base
2
2
 
3
3
  def add_user(user)
4
- Mattermost.post("/teams/#{Mattermost.team.id}/add_user_to_team", :body => { :user_id => user.id })
4
+ Mattermost.post("/teams/#{Mattermost.team.id}/add_user_to_team", :body => { :user_id => user.id }.to_json )
5
5
  end
6
6
 
7
7
  end
@@ -0,0 +1,19 @@
1
+ module Mattermost
2
+ class Post < MattermostObject
3
+
4
+ def self.new(attributes = {})
5
+ ::Post.new(attributes)
6
+ end
7
+
8
+ def self.search(terms)
9
+ request = Mattermost.post("/teams/#{Mattermost.team.id}/posts/search", :body => { :terms => terms }.to_json)
10
+ response = []
11
+ request.parsed_response['posts'].each do |_, post|
12
+ response << self.new(post)
13
+ end
14
+ response
15
+ end
16
+
17
+ end
18
+ end
19
+
@@ -10,22 +10,10 @@ module Mattermost
10
10
  @users ||= all_users
11
11
  end
12
12
 
13
- # Return a user that matches a given attribute.
14
- # Common pairs are :email => email, or :username => username
15
- # Return nil if not no matches
16
- def self.find_by(opts = {})
17
- all.select { |user| user.send(opts.keys.first) == opts.values.first }.first
18
- end
19
-
20
- # Return the user that has an id of @param id
21
- def self.find(id)
22
- find_by(:id => id)
23
- end
24
-
25
13
  # Return the status of users
26
14
  # Statuses are "offline", "away", or "online"
27
15
  def self.status(user_ids = [])
28
- Mattermost.post("/users/status", :body => user_ids.to_json)
16
+ request = Mattermost.post("/users/status", :body => user_ids.to_json)
29
17
  end
30
18
 
31
19
  # Returns the user that was used for authentication on Mattermost.connect
@@ -34,10 +22,18 @@ module Mattermost
34
22
  self.new(request.parsed_response)
35
23
  end
36
24
 
25
+ # Create a user from the team's invite.
26
+ # Returns a user
27
+ def self.create_from_invite(email, password, username)
28
+ request = Mattermost.post("/users/create?d=&iid=#{Mattermost.team.invite_id}",
29
+ :body => {:allow_marketing => true, :email => email, :password => password, :username => username}.to_json)
30
+ self.new(request.parsed_response)
31
+ end
32
+
37
33
  protected
38
34
  def self.all_users
39
35
  users = []
40
- request = Mattermost.get("/users/profiles")
36
+ request = Mattermost.get("/users/profiles/#{Mattermost.team.id}")
41
37
  request.parsed_response.values.each do |user|
42
38
  users << self.new(user)
43
39
  end
@@ -1,3 +1,3 @@
1
1
  module Mattermost
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattermost-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Brody
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-14 00:00:00.000000000 Z
11
+ date: 2016-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ files:
60
60
  - lib/mattermost/models/post.rb
61
61
  - lib/mattermost/models/team.rb
62
62
  - lib/mattermost/models/user.rb
63
+ - lib/mattermost/post.rb
63
64
  - lib/mattermost/team.rb
64
65
  - lib/mattermost/user.rb
65
66
  - lib/mattermost/version.rb
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  version: '0'
86
87
  requirements: []
87
88
  rubyforge_project:
88
- rubygems_version: 2.4.3
89
+ rubygems_version: 2.5.1
89
90
  signing_key:
90
91
  specification_version: 4
91
92
  summary: An ActiveModel-inspired API client for Mattermost