rocketchat 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 762d05d1acfbb58cc5b20516868c034f7c09903b
4
+ data.tar.gz: c58f16cc9e678a8d31da53419c6f785b8f4a43b6
5
+ SHA512:
6
+ metadata.gz: 03184b8414ab04854cf5b8ff0cda1d445ad006dc099b60bc065e7d2eca6c8a50037010d4ce9668fbb217a7c024d74dfc3e663ab5e9754408f5d0e0436fbd6b31
7
+ data.tar.gz: 1555a4eb6a1c90524dc19712acb4085bb05533703f9757ef71d955b95a984d0f6e9b9d583ffaca732ff66df122a48c24104fe4ef8601c6b9438f89cf60e83b7d
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - "**/*_spec.rb"
4
+
5
+ Metrics/LineLength:
6
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rocketchat.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 int512
4
+ Copyright (c) 2017 Andrew Bromwich
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Rocket.Chat REST API for Ruby
2
+
3
+ This is a gem wrapping the v1 REST API for [Rocket.Chat](https://rocket.chat/).
4
+
5
+ The gem is based on a fork of int2xx9/ruby-rocketchat however that gem implemented v0.1
6
+ of the Rocket.Chat API and it was not forward compatible. Thanks to @int2xx9 for the
7
+ framework on which this gem was based
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rocketchat'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+
22
+ ## Supported API calls
23
+
24
+ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.5.4)
25
+
26
+ #### Miscellaneous information
27
+ * /api/v1/info
28
+
29
+ #### Authentication
30
+ * /api/v1/login
31
+ * /api/v1/logout
32
+
33
+
34
+ ## Usage
35
+
36
+ To get Rocket.Chat version
37
+
38
+ ```ruby
39
+ require 'rocketchat'
40
+
41
+ rocket_server = RocketChat::Server.new('http://your.server.address/')
42
+ info = rocket_server.info
43
+ puts "Rocket.Chat version: #{info.version}"
44
+ ```
45
+
46
+
47
+ To logout from a server:
48
+
49
+ ```ruby
50
+ require 'rocketchat'
51
+
52
+ rocket_server = RocketChat::Server.new('http://your.server.address/')
53
+ session = rocket_server.login('username', 'password')
54
+ # ... use the API ...
55
+ session.logout
56
+ ```
57
+
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abrom/rocketchat-ruby.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rocketchat'
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,6 @@
1
+ module RocketChat
2
+ class Error < StandardError; end
3
+ class HTTPError < Error; end
4
+ class InvalidMethodError < HTTPError; end
5
+ class StatusError < Error; end
6
+ end
@@ -0,0 +1,3 @@
1
+ module RocketChat
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,30 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat Info
4
+ #
5
+ class Info
6
+ # Raw info data
7
+ attr_reader :data
8
+
9
+ #
10
+ # @param [Hash] data Raw version data
11
+ #
12
+ def initialize(data)
13
+ @data = data.dup.freeze
14
+ end
15
+
16
+ # Rocket.Chat version
17
+ def version
18
+ @data['version']
19
+ end
20
+
21
+ def inspect
22
+ format(
23
+ '#<%s:0x%p @version="%s">',
24
+ self.class.name,
25
+ object_id,
26
+ version
27
+ )
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,98 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat HTTP request helper
4
+ #
5
+ module RequestHelper
6
+ DEFAULT_REQUEST_OPTIONS = {
7
+ method: :get,
8
+ body: nil,
9
+ headers: nil,
10
+ ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER,
11
+ ssl_ca_file: nil
12
+ }.freeze
13
+
14
+ # Server URI
15
+ attr_reader :server
16
+
17
+ def server=(server)
18
+ @server = server.is_a?(URI) ? server : URI.parse(server.to_s)
19
+ end
20
+
21
+ def request_json(path, options = {})
22
+ fail_unless_ok = options.delete :fail_unless_ok
23
+ response = request path, options
24
+
25
+ if fail_unless_ok && !response.is_a?(Net::HTTPOK)
26
+ raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
27
+ end
28
+
29
+ response_json = JSON.parse(response.body)
30
+ unless response_json['status'] == 'success'
31
+ raise RocketChat::StatusError, response_json['message']
32
+ end
33
+
34
+ response_json
35
+ end
36
+
37
+ def request(path, options = {})
38
+ options = DEFAULT_REQUEST_OPTIONS.merge(options)
39
+
40
+ raise RocketChat::InvalidMethodError unless %i[get post].include? options[:method]
41
+
42
+ http = create_http(options)
43
+ req = create_request(path, options)
44
+ http.start { http.request(req) }
45
+ end
46
+
47
+ private
48
+
49
+ def get_headers(options)
50
+ headers = options[:headers]
51
+
52
+ token = options.delete :token
53
+ if token
54
+ headers ||= {}
55
+
56
+ headers['X-Auth-Token'] = token.auth_token
57
+ headers['X-User-Id'] = token.user_id
58
+ end
59
+
60
+ return unless headers
61
+ headers = Util.stringify_hash_keys headers
62
+ headers.delete_if { |key, value| key.nil? || value.nil? }
63
+ end
64
+
65
+ def create_http(options)
66
+ http = Net::HTTP.new(server.host, server.port)
67
+
68
+ if server.scheme == 'https'
69
+ http.use_ssl = true
70
+ http.verify_mode = options[:ssl_verify_mode]
71
+ http.ca_file = options[:ssl_ca_file] if options[:ssl_ca_file]
72
+ end
73
+
74
+ http
75
+ end
76
+
77
+ def create_request(path, options)
78
+ headers = get_headers(options)
79
+
80
+ req = Net::HTTP.const_get(options[:method].to_s.capitalize).new(path, headers)
81
+
82
+ body = options[:body]
83
+ req.body = url_encode(body) unless body.nil?
84
+
85
+ req
86
+ end
87
+
88
+ def url_encode(body)
89
+ if body.is_a?(Hash)
90
+ body.map do |key, value|
91
+ "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
92
+ end.join('&')
93
+ else
94
+ body.to_s
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,49 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat Server
4
+ #
5
+ class Server
6
+ include RocketChat::RequestHelper
7
+
8
+ # Server options
9
+ attr_reader :options
10
+
11
+ #
12
+ # @param [URI, String] server Server URI
13
+ # @param [Hash] options Server options
14
+ #
15
+ def initialize(server, options = {})
16
+ self.server = server
17
+ @options = options
18
+ end
19
+
20
+ #
21
+ # Info REST API
22
+ # @return [Info] Rocket.Chat Info
23
+ # @raise [HTTPError, StatusError]
24
+ #
25
+ def info
26
+ response = request_json '/api/v1/info', fail_unless_ok: true
27
+ Info.new response['info']
28
+ end
29
+
30
+ #
31
+ # Login REST API
32
+ # @param [String] username Username
33
+ # @param [String] password Password
34
+ # @return [Session] Rocket.Chat Session
35
+ # @raise [HTTPError, StatusError]
36
+ #
37
+ def login(username, password)
38
+ response = request_json(
39
+ '/api/v1/login',
40
+ method: :post,
41
+ body: {
42
+ username: username,
43
+ password: password
44
+ }
45
+ )
46
+ Session.new self, Token.new(response['data'])
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat Session
4
+ #
5
+ class Session
6
+ # Server
7
+ attr_reader :server
8
+ # Session token
9
+ attr_reader :token
10
+
11
+ #
12
+ # @param [Server] server Server
13
+ # @param [Token] token Session token
14
+ #
15
+ def initialize(server, token)
16
+ @server = server
17
+ @token = token.dup.freeze
18
+ end
19
+
20
+ #
21
+ # logout REST API
22
+ # @return [NilClass]
23
+ # @raise [HTTPError, StatusError]
24
+ #
25
+ def logout
26
+ server.request_json('/api/v1/logout', method: :post, token: token)
27
+ nil
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat Token
4
+ #
5
+ class Token
6
+ # Raw token data
7
+ attr_reader :data
8
+
9
+ #
10
+ # @param [Hash] data Raw token data
11
+ #
12
+ def initialize(data)
13
+ @data = Util.stringify_hash_keys data
14
+ end
15
+
16
+ # Authentication token
17
+ def auth_token
18
+ @data['authToken']
19
+ end
20
+
21
+ # User ID
22
+ def user_id
23
+ @data['userId']
24
+ end
25
+
26
+ def inspect
27
+ format(
28
+ '#<%s:0x%p @auth_token="%s", @user_id="%s">',
29
+ self.class.name,
30
+ object_id,
31
+ auth_token,
32
+ user_id
33
+ )
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ module RocketChat
2
+ #
3
+ # Rocket.Chat generic utility functions
4
+ #
5
+ module Util
6
+ #
7
+ # Stringify symbolized hash keys
8
+ # @param [Hash] hash A string/symbol keyed hash
9
+ # @return Stringified hash
10
+ #
11
+ def stringify_hash_keys(hash)
12
+ newhash = {}
13
+ hash.each do |key, value|
14
+ newhash[key.to_s] = value
15
+ end
16
+ newhash
17
+ end
18
+ module_function :stringify_hash_keys
19
+ end
20
+ end
data/lib/rocketchat.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'rocket_chat/gem_version'
2
+
3
+ require 'json'
4
+
5
+ require 'rocket_chat/error'
6
+ require 'rocket_chat/util'
7
+ require 'rocket_chat/request_helper'
8
+
9
+ require 'rocket_chat/server'
10
+ require 'rocket_chat/session'
11
+ require 'rocket_chat/info'
12
+ require 'rocket_chat/token'
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'rocket_chat/gem_version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'rocketchat'
10
+ spec.version = RocketChat::VERSION
11
+ spec.authors = %w[int512 abrom]
12
+ spec.email = %w[github@int512.net a.bromwich@gmail.com]
13
+
14
+ spec.summary = 'Rocket.Chat REST API v1 for Ruby'
15
+ spec.description = 'Rocket.Chat REST API v1 for Ruby'
16
+ spec.homepage = 'https://github.com/abrom/rocketchat-ruby'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.11'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ spec.add_development_dependency 'webmock', '~> 1.24'
27
+ spec.add_development_dependency 'yard', '~> 0.8.7.6'
28
+ spec.add_development_dependency 'rubocop', '~> 0.48.1'
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocketchat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - int512
8
+ - abrom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-04-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.11'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.11'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.24'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.24'
70
+ - !ruby/object:Gem::Dependency
71
+ name: yard
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: 0.8.7.6
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: 0.8.7.6
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.48.1
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.48.1
98
+ description: Rocket.Chat REST API v1 for Ruby
99
+ email:
100
+ - github@int512.net
101
+ - a.bromwich@gmail.com
102
+ executables:
103
+ - console
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".rubocop.yml"
109
+ - Gemfile
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - lib/rocket_chat/error.rb
115
+ - lib/rocket_chat/gem_version.rb
116
+ - lib/rocket_chat/info.rb
117
+ - lib/rocket_chat/request_helper.rb
118
+ - lib/rocket_chat/server.rb
119
+ - lib/rocket_chat/session.rb
120
+ - lib/rocket_chat/token.rb
121
+ - lib/rocket_chat/util.rb
122
+ - lib/rocketchat.rb
123
+ - rocketchat.gemspec
124
+ homepage: https://github.com/abrom/rocketchat-ruby
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Rocket.Chat REST API v1 for Ruby
148
+ test_files: []