rocketchat 0.0.2 → 0.0.3

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: 9be0e052345a62ea82a06daeaa3c3ec9d109ef5f
4
- data.tar.gz: c7ccbdd37e1705558d97c3f010b86755d012f97e
3
+ metadata.gz: a3c6a81c9b24af32e67ddee2d35a461271507a73
4
+ data.tar.gz: 2adb561c2bbe7474cfdf961dcc8b576bb02f549a
5
5
  SHA512:
6
- metadata.gz: b3f717e2a4455ae459ccba9599074c735f1dd54dd3931135fef6e06ef40f09d50a3c50d707d3eeea58f59c868875deae7569f39ac773943d9d2c021d243bb177
7
- data.tar.gz: 845f9454268a887c7d764ce655a0b89e9c5fa89f50b61e0ca569121f380f215e04585c3b7dcd25521addebff4dc9eadd91e971fdf05f581d78874f8c498272d5
6
+ metadata.gz: 4719b5dce2ba7c22951e6a1848eeb999f3b89ce34eb056f1265b4b21bb688172cf842774f6800b0fef7628842963c035743f8e44b84bc9087c045925d758de38
7
+ data.tar.gz: 066d66b376d3e6529874ea1f397319e433c5b7e1c65206d5b0326456a99834cf6bd7daeefff7fe6b96d19f72fbd83366f534ae1620e9b3417f6031ad4237ace7
data/.rubocop.yml CHANGED
@@ -6,4 +6,4 @@ Metrics/LineLength:
6
6
  Max: 120
7
7
 
8
8
  Metrics/MethodLength:
9
- Max: 12
9
+ Max: 15
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  [![Travis Build Status](http://img.shields.io/travis/abrom/rocketchat-ruby.svg?style=flat)](https://travis-ci.org/abrom/rocketchat-ruby)
2
2
  [![Code Climate Score](http://img.shields.io/codeclimate/github/abrom/rocketchat-ruby.svg?style=flat)](https://codeclimate.com/github/abrom/rocketchat-ruby)
3
- [![Gem Version](http://img.shields.io/gem/v/rocketchat-ruby.svg?style=flat)](#)
3
+ [![Gem Version](http://img.shields.io/gem/v/rocketchat.svg?style=flat)](#)
4
4
 
5
5
  # Rocket.Chat REST API for Ruby
6
6
 
7
7
  This is a gem wrapping the v1 REST API for [Rocket.Chat](https://rocket.chat/).
8
8
 
9
- The gem is based on a fork of int2xx9/ruby-rocketchat however that gem implemented v0.1
10
- of the Rocket.Chat API and it was not forward compatible. Thanks to @int2xx9 for the
9
+ The gem is based on a fork of http://github.com/int2xx9/ruby-rocketchat however that gem implemented v0.1
10
+ of the Rocket.Chat API and it was not forward compatible. Thanks to [@int2xx9](http://github.com/int2xx9) for the
11
11
  framework on which this gem was based
12
12
 
13
13
  ## Installation
@@ -35,6 +35,9 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
35
35
  * /api/v1/logout
36
36
  * /api/v1/me
37
37
 
38
+ ### Users
39
+ * /api/v1/users.create
40
+
38
41
 
39
42
  ## Usage
40
43
 
@@ -1,3 +1,3 @@
1
1
  module RocketChat
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
@@ -0,0 +1,78 @@
1
+ module RocketChat
2
+ module Messages
3
+ #
4
+ # Rocket.Chat User messages
5
+ #
6
+ class User
7
+ #
8
+ # @param [Session] session Session
9
+ #
10
+ def initialize(session)
11
+ @session = session
12
+ end
13
+
14
+ #
15
+ # users.create REST API
16
+ # @param [String] username Username
17
+ # @param [String] email Email
18
+ # @param [String] name Name
19
+ # @param [String] password Password
20
+ # @param [Hash] options Additional options
21
+ # @return [User]
22
+ # @raise [HTTPError, StatusError]
23
+ #
24
+ def create(username, email, name, password, options = {})
25
+ response = session.request_json(
26
+ '/api/v1/users.create',
27
+ method: :post,
28
+ body: {
29
+ username: username,
30
+ email: email,
31
+ name: name,
32
+ password: password
33
+ }.merge(user_option_hash(options))
34
+ )
35
+ RocketChat::User.new response['user']
36
+ end
37
+
38
+ #
39
+ # users.update REST API
40
+ # @param [String] id Rocket.Chat user id
41
+ # @param [String] new_email Email
42
+ # @param [String] new_name Name
43
+ # @return [User]
44
+ # @raise [HTTPError, StatusError]
45
+ #
46
+ def update(id, new_email, new_name, options = {})
47
+ response = session.request_json(
48
+ '/api/v1/users.update',
49
+ method: :post,
50
+ body: {
51
+ userId: id,
52
+ data: {
53
+ email: new_email,
54
+ name: new_name
55
+ }.merge(user_option_hash(options))
56
+ }
57
+ )
58
+ RocketChat::User.new response['user']
59
+ end
60
+
61
+ private
62
+
63
+ attr_reader :session
64
+
65
+ def user_option_hash(options)
66
+ options = Util.slice_hash(
67
+ options, :active, :roles, :join_default_channels, :require_password_change,
68
+ :send_welcome_email, :verified, :custom_fields, :username, :password
69
+ )
70
+ return {} if options.empty?
71
+
72
+ new_hash = {}
73
+ options.each { |key, value| new_hash[Util.camelize(key)] = value }
74
+ new_hash
75
+ end
76
+ end
77
+ end
78
+ end
@@ -22,17 +22,17 @@ module RocketChat
22
22
 
23
23
  def request_json(path, options = {})
24
24
  fail_unless_ok = options.delete :fail_unless_ok
25
- skip_status_check = options.delete :skip_status_check
26
25
 
27
26
  response = request path, options
28
-
29
27
  if fail_unless_ok && !response.is_a?(Net::HTTPOK)
30
28
  raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
31
29
  end
32
30
 
33
31
  response_json = JSON.parse(response.body)
34
- unless skip_status_check || response_json['status'] == 'success'
35
- raise RocketChat::StatusError, response_json['message']
32
+ if response_json.has_key? 'success'
33
+ raise RocketChat::StatusError, response_json['error'] unless response_json['success']
34
+ else
35
+ raise RocketChat::StatusError, response_json['message'] unless response_json['status'] == 'success'
36
36
  end
37
37
 
38
38
  response_json
@@ -84,16 +84,23 @@ module RocketChat
84
84
  req = Net::HTTP.const_get(options[:method].to_s.capitalize).new(path, headers)
85
85
 
86
86
  body = options[:body]
87
- req.body = url_encode(body) unless body.nil?
87
+ add_body(req, body) unless body.nil?
88
88
 
89
89
  req
90
90
  end
91
91
 
92
+ def add_body(request, body)
93
+ if body.is_a? Hash
94
+ request.body = body.to_json
95
+ request.content_type = 'application/json'
96
+ else
97
+ request.body = url_encode(body)
98
+ end
99
+ end
100
+
92
101
  def url_encode(body)
93
102
  if body.is_a?(Hash)
94
- body.map do |key, value|
95
- "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
96
- end.join('&')
103
+ URI.encode_www_form body
97
104
  else
98
105
  body.to_s
99
106
  end
@@ -17,13 +17,17 @@ module RocketChat
17
17
  @token = token.dup.freeze
18
18
  end
19
19
 
20
+ def request_json(path, options = {})
21
+ server.request_json path, options.merge(token: token)
22
+ end
23
+
20
24
  #
21
25
  # logout REST API
22
26
  # @return [NilClass]
23
27
  # @raise [HTTPError, StatusError]
24
28
  #
25
29
  def logout
26
- server.request_json('/api/v1/logout', method: :post, token: token)
30
+ request_json('/api/v1/logout', method: :post)
27
31
  nil
28
32
  end
29
33
 
@@ -33,9 +37,15 @@ module RocketChat
33
37
  # @raise [HTTPError, StatusError]
34
38
  #
35
39
  def me
36
- response = server.request_json('/api/v1/me', method: :get, token: token, skip_status_check: true)
37
- raise RocketChat::StatusError, 'Failed to fetch profile' unless response['success']
38
- User.new response
40
+ User.new request_json('/api/v1/me', method: :get)
41
+ end
42
+
43
+ #
44
+ # User messages proxy
45
+ # @return [Messages::User]
46
+ #
47
+ def users
48
+ @users ||= RocketChat::Messages::User.new(self)
39
49
  end
40
50
  end
41
51
  end
@@ -9,12 +9,37 @@ module RocketChat
9
9
  # @return Stringified hash
10
10
  #
11
11
  def stringify_hash_keys(hash)
12
- newhash = {}
12
+ new_hash = {}
13
+ hash.each { |key, value| new_hash[key.to_s] = value }
14
+ new_hash
15
+ end
16
+ module_function :stringify_hash_keys
17
+
18
+ #
19
+ # Slice keys from hash
20
+ # @param [Hash] hash A hash to slice key/value pairs from
21
+ # @param [Array] *keys The keys to be sliced
22
+ # @return Hash filtered by keys
23
+ #
24
+ def slice_hash(hash, *keys)
25
+ return {} if keys.length.zero?
26
+
27
+ new_hash = {}
13
28
  hash.each do |key, value|
14
- newhash[key.to_s] = value
29
+ new_hash[key] = value if keys.include? key
15
30
  end
16
- newhash
31
+ new_hash
17
32
  end
18
- module_function :stringify_hash_keys
33
+ module_function :slice_hash
34
+
35
+ #
36
+ # Camelize a string or symbol
37
+ # @param [String/Symbol] string A string or symbol
38
+ # @return a camelized string
39
+ #
40
+ def camelize(string)
41
+ string.to_s.gsub(/_([a-z])/) { $1.upcase }
42
+ end
43
+ module_function :camelize
19
44
  end
20
45
  end
data/lib/rocketchat.rb CHANGED
@@ -6,6 +6,8 @@ require 'rocket_chat/error'
6
6
  require 'rocket_chat/util'
7
7
  require 'rocket_chat/request_helper'
8
8
 
9
+ require 'rocket_chat/messages/user'
10
+
9
11
  require 'rocket_chat/server'
10
12
  require 'rocket_chat/session'
11
13
  require 'rocket_chat/info'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocketchat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - int512
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-13 00:00:00.000000000 Z
12
+ date: 2017-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -115,6 +115,7 @@ files:
115
115
  - lib/rocket_chat/error.rb
116
116
  - lib/rocket_chat/gem_version.rb
117
117
  - lib/rocket_chat/info.rb
118
+ - lib/rocket_chat/messages/user.rb
118
119
  - lib/rocket_chat/request_helper.rb
119
120
  - lib/rocket_chat/server.rb
120
121
  - lib/rocket_chat/session.rb