rocketchat 0.0.4 → 0.0.5

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: dd58bd7162f1bf9a4b5d6c5a7cf889c670fc3307
4
- data.tar.gz: 39d2f4288d3d42642ec58003e377734c2a5989b9
3
+ metadata.gz: 846bb0b7c4040d10264d89534bfd3d11b173a575
4
+ data.tar.gz: afa6fcb282d0c3f0af6720fff6a92b5fe1b4b043
5
5
  SHA512:
6
- metadata.gz: 25017410a5502a557e9a59887298bf9824b8d6173ab2852ec3c4da05abea15143fc49eff19382abc0f3706c9a6f175474d0c8065d9cc91a827ef3dc63eaf6fba
7
- data.tar.gz: af8b16ad88a70dbb2ec700a63bbdf206307eb5c4637fb4f6abb2c3d2f15e77aaa79422f32cc9cf6a650afcb0f3034234f90f9213456a91dcc2c38a9c661f02ba
6
+ metadata.gz: 7d295d4ff6a5712cb9c547a58c968c40c8d02e57dc28fdc970a84b8960f264a991c1982346ad41b67251e990bed5127f3592fabce8b12c08f9e13e5f49c14428
7
+ data.tar.gz: 35da2385925f779f50f01c248587ec36bff6d19be6edd7b4b52ee9933538003796732ac89c75fa07bf22208415f3340075fc4231f6a9861a95d489d537070cf7
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  Gemfile.lock
2
2
  *.gem
3
+ .bundle
4
+ vendor/bundle
data/.rubocop.yml CHANGED
@@ -7,3 +7,5 @@ Metrics/LineLength:
7
7
 
8
8
  Metrics/MethodLength:
9
9
  Max: 15
10
+ Exclude:
11
+ - "**/*_spec.rb"
data/.travis.yml CHANGED
@@ -8,3 +8,11 @@ rvm:
8
8
 
9
9
  before_install:
10
10
  - gem update bundler
11
+
12
+ install:
13
+ - bundle install --jobs=3 --retry=3
14
+ - gem install rubocop
15
+
16
+ script:
17
+ - rubocop
18
+ - bundle exec rake
data/README.md CHANGED
@@ -25,7 +25,7 @@ And then execute:
25
25
 
26
26
  ## Supported API calls
27
27
 
28
- This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.5.4)
28
+ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.54)
29
29
 
30
30
  #### Miscellaneous information
31
31
  * /api/v1/info
@@ -37,6 +37,9 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
37
37
 
38
38
  ### Users
39
39
  * /api/v1/users.create
40
+ * /api/v1/users.delete
41
+ * /api/v1/users.info
42
+ * /api/v1/users.setAvatar
40
43
  * /api/v1/users.update
41
44
 
42
45
 
@@ -100,6 +103,34 @@ Optional parameters for update are:
100
103
  :username, :email, :password, :name, :active, :roles, :join_default_channels, :require_password_change, :send_welcome_email, :verified, :custom_fields
101
104
 
102
105
 
106
+ To get user info:
107
+
108
+ ```ruby
109
+ require 'rocketchat'
110
+
111
+ rocket_server = RocketChat::Server.new('http://your.server.address/')
112
+ session = rocket_server.login('username', 'password')
113
+ session.users.info(username: 'some_username')
114
+ ```
115
+
116
+ Either user_id (RocketChat's ID) or username can be used.
117
+
118
+ Deleting a user can be done with the same options.
119
+
120
+
121
+ To set a user's avatar:
122
+
123
+ ```ruby
124
+ require 'rocketchat'
125
+
126
+ rocket_server = RocketChat::Server.new('http://your.server.address/')
127
+ session = rocket_server.login('username', 'password')
128
+ session.users.set_avatar('http://image_url')
129
+ ```
130
+
131
+ There is an optional parameter user_id, that works if the setting user is allowed to set other's avatar.
132
+
133
+
103
134
  ## Contributing
104
135
 
105
136
  Bug reports and pull requests are welcome on GitHub at https://github.com/abrom/rocketchat-ruby.
@@ -1,3 +1,3 @@
1
1
  module RocketChat
2
- VERSION = '0.0.4'.freeze
2
+ VERSION = '0.0.5'.freeze
3
3
  end
@@ -54,6 +54,56 @@ module RocketChat
54
54
  RocketChat::User.new response['user']
55
55
  end
56
56
 
57
+ #
58
+ # users.delete REST API
59
+ # @param [String] user_id Rocket.Chat user id
60
+ # @param [String] username Username
61
+ # @return [Boolean]
62
+ # @raise [HTTPError, StatusError]
63
+ #
64
+ def delete(user_id: nil, username: nil)
65
+ session.request_json(
66
+ '/api/v1/users.delete',
67
+ method: :post,
68
+ body: user_id ? { userId: user_id } : { username: username },
69
+ upstreamed_errors: ['error-invalid-user']
70
+ )['success']
71
+ end
72
+
73
+ #
74
+ # users.info REST API
75
+ # @param [String] user_id Rocket.Chat user id
76
+ # @param [String] username Username
77
+ # @return [User]
78
+ # @raise [HTTPError, StatusError]
79
+ #
80
+ def info(user_id: nil, username: nil)
81
+ response = session.request_json(
82
+ '/api/v1/users.info',
83
+ body: user_id ? { userId: user_id } : { username: username },
84
+ upstreamed_errors: ['error-invalid-user']
85
+ )
86
+
87
+ RocketChat::User.new response['user'] if response['success']
88
+ end
89
+
90
+ #
91
+ # users.setAvatar REST API
92
+ # @param [String] avatar_url URL to use for avatar
93
+ # @param [String] user_id user to update (optional)
94
+ # @return [Boolean]
95
+ # @raise [HTTPError, StatusError]
96
+ #
97
+ def set_avatar(avatar_url, user_id: nil)
98
+ body = { avatarUrl: avatar_url }
99
+ body[:userId] = user_id if user_id
100
+ session.request_json(
101
+ '/api/v1/users.setAvatar',
102
+ method: :post,
103
+ body: body
104
+ )['success']
105
+ end
106
+
57
107
  private
58
108
 
59
109
  attr_reader :session
@@ -61,7 +111,7 @@ module RocketChat
61
111
  def user_option_hash(options, include_personal_fields = false)
62
112
  args = [options, :active, :roles, :join_default_channels, :require_password_change,
63
113
  :send_welcome_email, :verified, :custom_fields]
64
- args += [:username, :email, :name, :password] if include_personal_fields
114
+ args += %i[username email name password] if include_personal_fields
65
115
 
66
116
  options = Util.slice_hash(*args)
67
117
  return {} if options.empty?
@@ -22,18 +22,13 @@ module RocketChat
22
22
 
23
23
  def request_json(path, options = {})
24
24
  fail_unless_ok = options.delete :fail_unless_ok
25
+ upstreamed_errors = Array(options.delete(:upstreamed_errors))
25
26
 
26
27
  response = request path, options
27
- if fail_unless_ok && !response.is_a?(Net::HTTPOK)
28
- raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
29
- end
28
+ check_response response, fail_unless_ok
30
29
 
31
30
  response_json = JSON.parse(response.body)
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
- end
31
+ check_response_json response_json, upstreamed_errors
37
32
 
38
33
  response_json
39
34
  end
@@ -50,6 +45,21 @@ module RocketChat
50
45
 
51
46
  private
52
47
 
48
+ def check_response(response, fail_unless_ok)
49
+ return unless fail_unless_ok && !response.is_a?(Net::HTTPOK)
50
+ raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
51
+ end
52
+
53
+ def check_response_json(response_json, upstreamed_errors)
54
+ if response_json.key? 'success'
55
+ unless response_json['success'] || upstreamed_errors.include?(response_json['errorType'])
56
+ raise RocketChat::StatusError, response_json['error']
57
+ end
58
+ elsif response_json['status'] != 'success'
59
+ raise RocketChat::StatusError, response_json['message']
60
+ end
61
+ end
62
+
53
63
  def get_headers(options)
54
64
  headers = options[:headers]
55
65
 
@@ -80,11 +90,16 @@ module RocketChat
80
90
 
81
91
  def create_request(path, options)
82
92
  headers = get_headers(options)
83
-
84
- req = Net::HTTP.const_get(options[:method].to_s.capitalize).new(path, headers)
85
-
86
93
  body = options[:body]
87
- add_body(req, body) unless body.nil?
94
+
95
+ if options[:method] == :post
96
+ req = Net::HTTP::Post.new(path, headers)
97
+ add_body(req, body) if body
98
+ else
99
+ uri = path
100
+ uri += '?' + URI.encode_www_form(body) if body
101
+ req = Net::HTTP::Get.new(uri, headers)
102
+ end
88
103
 
89
104
  req
90
105
  end
@@ -94,15 +109,7 @@ module RocketChat
94
109
  request.body = body.to_json
95
110
  request.content_type = 'application/json'
96
111
  else
97
- request.body = url_encode(body)
98
- end
99
- end
100
-
101
- def url_encode(body)
102
- if body.is_a?(Hash)
103
- URI.encode_www_form body
104
- else
105
- body.to_s
112
+ request.body = body.to_s
106
113
  end
107
114
  end
108
115
  end
@@ -38,7 +38,7 @@ module RocketChat
38
38
  # @return a camelized string
39
39
  #
40
40
  def camelize(string)
41
- string.to_s.gsub(/_([a-z])/) { $1.upcase }
41
+ string.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
42
42
  end
43
43
  module_function :camelize
44
44
  end
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.4
4
+ version: 0.0.5
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-19 00:00:00.000000000 Z
12
+ date: 2017-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler