Skip to content

Commit

Permalink
Merge pull request #1 from seekingalpha/users-methods
Browse files Browse the repository at this point in the history
Adds user API methods:
* users.info
* users.delete
* users.setAvatar
  • Loading branch information
abrom authored Apr 25, 2017
2 parents e2173f2 + 059916a commit af2db76
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Gemfile.lock
*.gem
.bundle
vendor/bundle
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Metrics/LineLength:

Metrics/MethodLength:
Max: 15
Exclude:
- "**/*_spec.rb"
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ And then execute:

## Supported API calls

This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.5.4)
This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.54)

#### Miscellaneous information
* /api/v1/info
Expand All @@ -37,6 +37,9 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.

### Users
* /api/v1/users.create
* /api/v1/users.delete
* /api/v1/users.info
* /api/v1/users.setAvatar
* /api/v1/users.update


Expand Down Expand Up @@ -100,6 +103,34 @@ Optional parameters for update are:
:username, :email, :password, :name, :active, :roles, :join_default_channels, :require_password_change, :send_welcome_email, :verified, :custom_fields


To get user info:

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.users.info(username: 'some_username')
```

Either user_id (RocketChat's ID) or username can be used.

Deleting a user can be done with the same options.


To set a user's avatar:

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.users.set_avatar('http://image_url')
```

There is an optional parameter user_id, that works if the setting user is allowed to set other's avatar.


## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/abrom/rocketchat-ruby.
Expand Down
50 changes: 50 additions & 0 deletions lib/rocket_chat/messages/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,56 @@ def update(id, options = {})
RocketChat::User.new response['user']
end

#
# users.delete REST API
# @param [String] user_id Rocket.Chat user id
# @param [String] username Username
# @return [Boolean]
# @raise [HTTPError, StatusError]
#
def delete(user_id: nil, username: nil)
session.request_json(
'/api/v1/users.delete',
method: :post,
body: user_id ? { userId: user_id } : { username: username },
upstreamed_errors: ['error-invalid-user']
)['success']
end

#
# users.info REST API
# @param [String] user_id Rocket.Chat user id
# @param [String] username Username
# @return [User]
# @raise [HTTPError, StatusError]
#
def info(user_id: nil, username: nil)
response = session.request_json(
'/api/v1/users.info',
body: user_id ? { userId: user_id } : { username: username },
upstreamed_errors: ['error-invalid-user']
)

RocketChat::User.new response['user'] if response['success']
end

#
# users.setAvatar REST API
# @param [String] avatar_url URL to use for avatar
# @param [String] user_id user to update (optional)
# @return [Boolean]
# @raise [HTTPError, StatusError]
#
def set_avatar(avatar_url, user_id: nil)
body = { avatarUrl: avatar_url }
body[:userId] = user_id if user_id
session.request_json(
'/api/v1/users.setAvatar',
method: :post,
body: body
)['success']
end

private

attr_reader :session
Expand Down
36 changes: 18 additions & 18 deletions lib/rocket_chat/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def server=(server)

def request_json(path, options = {})
fail_unless_ok = options.delete :fail_unless_ok
upstreamed_errors = Array(options.delete(:upstreamed_errors))

response = request path, options
check_response response, fail_unless_ok

response_json = JSON.parse(response.body)
check_response_json response_json
check_response_json response_json, upstreamed_errors

response_json
end
Expand All @@ -49,11 +50,13 @@ def check_response(response, fail_unless_ok)
raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
end

def check_response_json(response_json)
def check_response_json(response_json, upstreamed_errors)
if response_json.key? 'success'
raise RocketChat::StatusError, response_json['error'] unless response_json['success']
else
raise RocketChat::StatusError, response_json['message'] unless response_json['status'] == 'success'
unless response_json['success'] || upstreamed_errors.include?(response_json['errorType'])
raise RocketChat::StatusError, response_json['error']
end
elsif response_json['status'] != 'success'
raise RocketChat::StatusError, response_json['message']
end
end

Expand Down Expand Up @@ -87,11 +90,16 @@ def create_http(options)

def create_request(path, options)
headers = get_headers(options)

req = Net::HTTP.const_get(options[:method].to_s.capitalize).new(path, headers)

body = options[:body]
add_body(req, body) unless body.nil?

if options[:method] == :post
req = Net::HTTP::Post.new(path, headers)
add_body(req, body) if body
else
uri = path
uri += '?' + URI.encode_www_form(body) if body
req = Net::HTTP::Get.new(uri, headers)
end

req
end
Expand All @@ -101,15 +109,7 @@ def add_body(request, body)
request.body = body.to_json
request.content_type = 'application/json'
else
request.body = url_encode(body)
end
end

def url_encode(body)
if body.is_a?(Hash)
URI.encode_www_form body
else
body.to_s
request.body = body.to_s
end
end
end
Expand Down
Loading

0 comments on commit af2db76

Please sign in to comment.