-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for me REST API
- Loading branch information
Showing
10 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ Metrics/BlockLength: | |
|
||
Metrics/LineLength: | ||
Max: 120 | ||
|
||
Metrics/MethodLength: | ||
Max: 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module RocketChat | ||
VERSION = '0.0.1'.freeze | ||
VERSION = '0.0.2'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module RocketChat | ||
# | ||
# Rocket.Chat User | ||
# | ||
class User | ||
# Raw user data | ||
attr_reader :data | ||
|
||
# | ||
# @param [Hash] data Raw user data | ||
# | ||
def initialize(data) | ||
@data = Util.stringify_hash_keys data | ||
end | ||
|
||
# User ID | ||
def id | ||
data['_id'] | ||
end | ||
|
||
# User name | ||
def name | ||
data['name'] | ||
end | ||
|
||
# User emails | ||
def emails | ||
data['emails'] || [] | ||
end | ||
|
||
# User email | ||
def email | ||
emails.first && emails.first['address'] | ||
end | ||
|
||
# User email verified | ||
def email_verified? | ||
emails.first && emails.first['verified'] | ||
end | ||
|
||
# User status | ||
def status | ||
data['status'] | ||
end | ||
|
||
# User connection status | ||
def status_connection | ||
data['statusConnection'] | ||
end | ||
|
||
# User username | ||
def username | ||
data['username'] | ||
end | ||
|
||
# User UTC offset | ||
def utc_offset | ||
data['utcOffset'] | ||
end | ||
|
||
# User active | ||
def active? | ||
data['active'] | ||
end | ||
|
||
def inspect | ||
format( | ||
'#<%s:0x%p @id="%s" @username="%s" @active="%s">', | ||
self.class.name, | ||
object_id, | ||
id, | ||
username, | ||
active? | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
require 'rocket_chat/session' | ||
require 'rocket_chat/info' | ||
require 'rocket_chat/token' | ||
require 'rocket_chat/user' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,59 @@ | |
end | ||
end | ||
end | ||
|
||
describe '#me' do | ||
before do | ||
# Stubs for /api/v1/me REST API | ||
stub_request(:get, SERVER_URI + '/api/v1/me') | ||
.to_return(body: UNAUTHORIZED_BODY, status: 401) | ||
|
||
stub_request(:get, SERVER_URI + '/api/v1/me') | ||
.with(headers: { 'X-Auth-Token' => AUTH_TOKEN, 'X-User-Id' => USER_ID }) | ||
.to_return( | ||
body: { | ||
_id: USER_ID, | ||
name: 'Example User', | ||
emails: [ | ||
{ | ||
address: '[email protected]', | ||
verified: true | ||
} | ||
], | ||
status: 'online', | ||
statusConnection: 'offline', | ||
username: USERNAME, | ||
utcOffset: 0, | ||
active: true, | ||
success: true | ||
}.to_json, | ||
status: 200 | ||
) | ||
end | ||
|
||
context 'valid session' do | ||
it 'should be success' do | ||
me = session.me | ||
expect(me.id).to eq USER_ID | ||
expect(me.name).to eq 'Example User' | ||
expect(me.email).to eq '[email protected]' | ||
expect(me).to be_email_verified | ||
expect(me.status).to eq 'online' | ||
expect(me.status_connection).to eq 'offline' | ||
expect(me.username).to eq USERNAME | ||
expect(me.utc_offset).to eq 0 | ||
expect(me).to be_active | ||
end | ||
end | ||
|
||
context 'invalid session token' do | ||
let(:token) { RocketChat::Token.new(authToken: nil, userId: nil) } | ||
|
||
it 'should be failure' do | ||
expect do | ||
session.me | ||
end.to raise_error RocketChat::StatusError, 'Failed to fetch profile' | ||
end | ||
end | ||
end | ||
end |