mattermost-ruby 0.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2dbd77cb69baccb0f56d2a2b2671ee3e602e5d52
4
+ data.tar.gz: e78806366fcee69bcb32fb63ba09791633d2f415
5
+ SHA512:
6
+ metadata.gz: 5c943f34990584343f93103fd4022fbe2ad3cbf41ae1003d8d87a9827e4de142c23e0bdf612f0030cef4b849ccc48d81ffebdc55decef21bf3875d87e5ee1e4b
7
+ data.tar.gz: 9de2faac62461b67d860c814139b306b3d80d6d28dd09faf9ed09470ab4aa53eab55e9ffc8c004a424e0d7431c1fd40a4af0024a96486024a1f7db717d61e3fc
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'httparty'
4
+ gem 'pry'
@@ -0,0 +1,50 @@
1
+ # Mattermost-Ruby
2
+
3
+ An ActiveRecord-inspired API client for Mattermost
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mattermost'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mattermost
20
+
21
+ ## Configuration
22
+
23
+ ```
24
+ options = {:httparty => {:debug_output => $stdout} } # pass defaults to httparty
25
+
26
+ Mattermost.connect("apiuser", "apipassword", "https://mattermost.example.com/api/v1", "default_team_name", options)
27
+
28
+ users = Mattermost::User.all
29
+
30
+ users.last.class #User
31
+
32
+ users.last.reset_password("newpassword")
33
+
34
+ users.last.id # abcdefgh12345678
35
+
36
+ Mattermost::User.find("abcdefgh12345678")
37
+
38
+ Mattermost::User.find_by(:email => "me@example.com") # <#User ...>
39
+ ```
40
+
41
+ ## Todo
42
+
43
+ Documentation.
44
+
45
+ Tests.
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshmn/mattermost-ruby.
50
+
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :spec
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList['spec/lib/mattermost/*_spec.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mattermost"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ #require "mattermost/version"
2
+
3
+ require 'httparty'
4
+ require 'pry'
5
+
6
+ require_relative 'mattermost/mattermost_object'
7
+ require_relative 'mattermost/models/base'
8
+ require_relative 'mattermost/team'
9
+ require_relative 'mattermost/user'
10
+ require_relative 'mattermost/admin'
11
+ require_relative 'mattermost/channel'
12
+
13
+ module Mattermost
14
+
15
+ include HTTParty
16
+
17
+ attr_accessor :team
18
+
19
+ class NotImplementedError < StandardError; end
20
+
21
+ def self.connect(username, password, server, team_name, options = {})
22
+ 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
+ options[:httparty].each do |k,v|
29
+ self.send(k, v)
30
+ end
31
+ unless options[:preload_user] == false
32
+ Mattermost::User.all
33
+ end
34
+ end
35
+
36
+ def self.team
37
+ @team
38
+ end
39
+
40
+ end
@@ -0,0 +1,68 @@
1
+ module Mattermost
2
+ class Admin < MattermostObject
3
+
4
+ def self.get_logs
5
+ Mattermost.get("/admin/logs")
6
+ end
7
+
8
+ def self.get_all_audits
9
+ Mattermost.get("/admin/audits")
10
+ end
11
+
12
+ def self.get_config
13
+ Mattermost.get("/admin/config")
14
+ end
15
+
16
+ def self.save_config
17
+ Mattermost.post("/admin/save_config")
18
+ end
19
+
20
+ def self.test_email
21
+ Mattermost.post("/admin/test_email")
22
+ end
23
+
24
+ def self.get_client_config
25
+ Mattermost.get("/admin/client_props")
26
+ end
27
+
28
+ def self.log_client
29
+ Mattermost.post("/admin/log_client")
30
+ end
31
+
32
+ def self.get_analytics(name, id = nil)
33
+ uri = "/admin/analytics"
34
+ uri += "/#{id}" if id
35
+ uri += "/#{name}"
36
+ Mattermost.get(uri)
37
+ end
38
+
39
+ def self.save_compliance_report
40
+ Mattermost.post("/admin/save_compliance_report")
41
+ end
42
+
43
+ def self.get_compliance_reports
44
+ Mattermost.get("/admin/compliance_reports")
45
+ end
46
+
47
+ def self.download_compliance_report(id)
48
+ Mattermost.get("/admin/download_compliance_report/#{id}")
49
+ end
50
+
51
+ def self.upload_brand_image
52
+ Mattermost.post("/admin/upload_brand_image")
53
+ end
54
+
55
+ def self.get_brand_image
56
+ Mattermost.get("/admin/get_brand_image")
57
+ end
58
+
59
+ def self.admin_reset_mfa
60
+ Mattermost.post("/admin/reset_mfa")
61
+ end
62
+
63
+ def self.admin_reset_password
64
+ Mattermost.post("/admin/reset_password")
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,44 @@
1
+ module Mattermost
2
+ class Channel < MattermostObject
3
+
4
+ def self.new(attributes = {})
5
+ ::Channel.new(attributes)
6
+ end
7
+
8
+ # Returns all channels for the current team
9
+ #
10
+ # @param force_refresh [boolean] to recache the channels
11
+ def self.all(force_refresh = false)
12
+ @channels = nil if force_refresh
13
+ @channels ||= all_channels
14
+ end
15
+
16
+ # Returns "more" channels for the current team
17
+ def self.more
18
+ Mattermost.get("/channels/more")
19
+ end
20
+
21
+ # Returns a hash of counts for each *team*
22
+ # {"counts"=>{"ps6kdfuk9p8mjx6pkr3krgq3by"=>58334, "yckjbepc4frbmmq9in6tap1dwa"=>32},
23
+ # "update_times"=>{"ps6kdfuk9p8mjx6pkr3krgq3by"=>1463180151709, "yckjbepc4frbmmq9in6tap1dwa"=>1457216806189}}
24
+ def self.counts
25
+ Mattermost.get("/channels/counts")
26
+ end
27
+
28
+ def self.create_direct(attributes = {})
29
+ raise NotImplementedError
30
+ Mattermost.post("/channels/create_direct")
31
+ end
32
+
33
+ protected
34
+ def self.all_channels
35
+ channels = []
36
+ request = Mattermost.get("/channels/")
37
+ request.parsed_response['channels'].each do |channel|
38
+ channels << self.new(channel)
39
+ end
40
+ channels
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ module Mattermost
2
+ class MattermostObject #:nodoc: all
3
+
4
+ def initialize
5
+ end
6
+
7
+ class << self
8
+ def method_missing(method_id, *args) #:nodoc:
9
+ o = self.new
10
+ o.send(method_id, *args)
11
+ rescue HTTParty::UnsupportedURIScheme
12
+ raise "You must set Mattermost.connect before calling #{self.inspect}##{method_id}"
13
+ end
14
+ end
15
+
16
+ def connected?
17
+ self.class.base_uri
18
+ end
19
+
20
+ def self.before_method(*names)
21
+ names.each do |name|
22
+ m = name # public_class_method(name)
23
+ define_method(name) do |*args, &block|
24
+ yield
25
+ m.bind(self).(*args, &block)
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ class Base
2
+
3
+ def initialize(attrs)
4
+ attrs.each do |k,v|
5
+ singleton_class.class_eval { attr_accessor "#{k}" }
6
+ instance_variable_set "@#{k.to_sym}", v
7
+ end
8
+ end
9
+
10
+ def attributes
11
+ return self.class.default_attributes unless defined?(@id)
12
+ hash = {}
13
+ self.instance_variables.each do |v|
14
+ hash[v.to_s.gsub('@', '').to_sym] = instance_variable_get(v)
15
+ end
16
+ hash
17
+ end
18
+
19
+ def last
20
+ eval("Mattermost::#{self.class}").all.last
21
+ end
22
+
23
+ def first
24
+ eval("Mattermost::#{self.class}").all.first
25
+ end
26
+
27
+ def to_json
28
+ attributes.to_json
29
+ end
30
+
31
+ end
32
+
33
+ require_relative 'team'
34
+ require_relative 'user'
35
+ require_relative 'channel'
36
+ require_relative 'post'
@@ -0,0 +1,98 @@
1
+ class Channel < Base
2
+
3
+ def save
4
+ defined?(self.id) ? update : create_channel
5
+ end
6
+
7
+ # Delete a channel
8
+ def delete
9
+ Mattermost.post("/channels/#{self.id}/delete")
10
+ end
11
+
12
+ # Get channel info
13
+ def info
14
+ Mattermost.get("/channels/#{self.id}/")
15
+ end
16
+
17
+ # Get the users in a channel
18
+ def get_channel_extra_info(member_limit = nil)
19
+ uri = "/channels/#{self.id}/extra_info"
20
+ uri += "/#{member_limit}" if member_limit
21
+ request = Mattermost.get(uri)
22
+ response = {}
23
+ response['id'] = request.parsed_response['id']
24
+ response['member_count'] = request.parsed_response['member_count']
25
+ response['members'] = []
26
+ request.parsed_response['members'].each do |user|
27
+ response['members'] << User.new(user)
28
+ end
29
+ response
30
+ end
31
+ alias_method :users, :get_channel_extra_info
32
+
33
+ # Join the channel with your authenticated user
34
+ # If you're looking to add a user to a channel, see #add_member
35
+ def join
36
+ Mattermost.post("/channels/#{self.id}/join")
37
+ end
38
+
39
+ # Leave the channel with your authenticated user
40
+ # If you're looking to remove a user from a channel, see #remove_member
41
+ def leave
42
+ Mattermost.post("/channels/#{self.id}/leave")
43
+ end
44
+
45
+ # Add a user to a channel
46
+ # Send a user object or a user_id
47
+ def add_member(user)
48
+ user_id = user.is_a? User ? user.id : user
49
+ Mattermost.post("/channels/#{self.id}/add", :body => {:user_id => user_id})
50
+ end
51
+
52
+ # Add a user to a channel
53
+ # Send a user object or a user_id
54
+ def remove_member(user)
55
+ user_id = user.is_a? User ? user.id : user
56
+ Mattermost.post("/channels/#{self.id}/remove", :body => {:user_id => user_id})
57
+ end
58
+
59
+ def update_last_viewed_at
60
+ Mattermost.post("/channels/#{self.id}/update_last_viewed_at")
61
+ end
62
+
63
+ def update_attributes(attributes = {})
64
+ raise NotImplementedError
65
+ Mattermost.post("/channels/update")
66
+ end
67
+
68
+ # Update the channel's header. I really hate that they have a specific
69
+ # method for this.
70
+ def update_header(header)
71
+ Mattermost.post("/channels/update_header", :body => {:channel_id => self.id, :channel_header => header})
72
+ end
73
+
74
+ # Update the channel's purpose. I really hate that they have a specific
75
+ # method for this.
76
+ def update_purpose(purpose)
77
+ Mattermost.post("/channels/update_purpose", :body => {:channel_id => self.id, :channel_purpose => purpose})
78
+ end
79
+
80
+ def self.default_attributes
81
+ {:display_name => String, :team_id => String, :type => Integer, :purpose => String}
82
+ end
83
+
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
+
93
+ protected
94
+ def create_channel
95
+ `curl '#{Mattermost.base_uri}/channels/create' -H 'Cookie: MMTOKEN=#{Mattermost.headers['Cookie'].split("=")[1]};' -H 'X-Requested-With: XMLHttpRequest' --data-binary '{"display_name":"#{self.display_name}","name":"#{self.display_name.gsub(" ", '-')}","team_id":"#{Mattermost.team.id}","purpose":"#{self.purpose}.","type":"#{self.type}"}' --compressed`
96
+ end
97
+
98
+ end
@@ -0,0 +1,9 @@
1
+ class Post < Base
2
+ attr_accessor :user
3
+
4
+ def initialize(attributes)
5
+ super(attributes)
6
+ @user = Mattermost::User.find(attributes['user_id'])
7
+ end
8
+
9
+ end
@@ -0,0 +1,6 @@
1
+ class Team < Base
2
+
3
+ def add_user(user)
4
+ Mattermost.post("/teams/#{Mattermost.team.id}/add_user_to_team", :body => { :user_id => user.id })
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ class User < Base
2
+
3
+ def reset_password(new_password)
4
+ payload = {:name => Mattermost.team.name, :user_id => self.id, :new_password => new_password}
5
+ Mattermost.post("/users/reset_password", :body => payload.to_json)
6
+ end
7
+
8
+ def status
9
+ Mattermost::User.status([self.id])[self.id]
10
+ end
11
+
12
+ def preferences
13
+ Mattermost.get("/preferences")
14
+ end
15
+
16
+ end
@@ -0,0 +1,25 @@
1
+ module Mattermost
2
+ class Team < MattermostObject
3
+
4
+ def self.new(attributes = {})
5
+ ::Team.new(attributes)
6
+ end
7
+
8
+ def self.all(force_refresh = false)
9
+ @teams = nil if force_refresh
10
+ @teams ||= all_teams
11
+ end
12
+
13
+ protected
14
+ def self.all_teams
15
+ teams = []
16
+ request = Mattermost.get("/teams/all")
17
+ request.parsed_response.values.each do |team|
18
+ teams << self.new(team)
19
+ end
20
+ teams
21
+ end
22
+
23
+ end
24
+ end
25
+
@@ -0,0 +1,47 @@
1
+ module Mattermost
2
+ class User < MattermostObject
3
+
4
+ def self.new(attributes = {})
5
+ ::User.new(attributes)
6
+ end
7
+
8
+ def self.all(force_refresh = false)
9
+ @users = nil if force_refresh
10
+ @users ||= all_users
11
+ end
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
+ # Return the status of users
26
+ # Statuses are "offline", "away", or "online"
27
+ def self.status(user_ids = [])
28
+ Mattermost.post("/users/status", :body => user_ids.to_json)
29
+ end
30
+
31
+ # Returns the user that was used for authentication on Mattermost.connect
32
+ def self.me
33
+ request = Mattermost.get("/users/me")
34
+ self.new(request.parsed_response)
35
+ end
36
+
37
+ protected
38
+ def self.all_users
39
+ users = []
40
+ request = Mattermost.get("/users/profiles")
41
+ request.parsed_response.values.each do |user|
42
+ users << self.new(user)
43
+ end
44
+ users
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Mattermost
2
+ VERSION = "0.1.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mattermost/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mattermost-ruby'
8
+ spec.version = Mattermost::VERSION
9
+ spec.authors = ['Josh Brody']
10
+ spec.email = ['josh@josh.mn']
11
+ spec.platform = Gem::Platform::RUBY
12
+ spec.licenses = ['MIT']
13
+ spec.authors = ['Josh Brody']
14
+ spec.summary = %q{An ActiveModel-inspired API client for Mattermost}
15
+ spec.description = %q{An ActiveModel-inspired API client for Mattermost}
16
+ spec.homepage = 'https://github.com/joshmn/mattermost-ruby'
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mattermost-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: An ActiveModel-inspired API client for Mattermost
42
+ email:
43
+ - josh@josh.mn
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - lib/mattermost.rb
55
+ - lib/mattermost/admin.rb
56
+ - lib/mattermost/channel.rb
57
+ - lib/mattermost/mattermost_object.rb
58
+ - lib/mattermost/models/base.rb
59
+ - lib/mattermost/models/channel.rb
60
+ - lib/mattermost/models/post.rb
61
+ - lib/mattermost/models/team.rb
62
+ - lib/mattermost/models/user.rb
63
+ - lib/mattermost/team.rb
64
+ - lib/mattermost/user.rb
65
+ - lib/mattermost/version.rb
66
+ - mattermost.gemspec
67
+ homepage: https://github.com/joshmn/mattermost-ruby
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ allowed_push_host: https://rubygems.org
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: An ActiveModel-inspired API client for Mattermost
92
+ test_files: []