Skip to content

Commit

Permalink
Bumped to v0.0.2
Browse files Browse the repository at this point in the history
Added support for me REST API
  • Loading branch information
abrom committed Apr 13, 2017
1 parent 9be1d00 commit 0d796cb
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ Metrics/BlockLength:

Metrics/LineLength:
Max: 120

Metrics/MethodLength:
Max: 12
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![Travis Build Status](http://img.shields.io/travis/abrom/rocketchat-ruby.svg?style=flat)](https://travis-ci.org/abrom/rocketchat-ruby)
[![Code Climate Score](http://img.shields.io/codeclimate/github/abrom/rocketchat-ruby.svg?style=flat)](https://codeclimate.com/github/abrom/rocketchat-ruby)
[![Gem Version](http://img.shields.io/gem/v/rocketchat-ruby.svg?style=flat)](#)

# Rocket.Chat REST API for Ruby

This is a gem wrapping the v1 REST API for [Rocket.Chat](https://rocket.chat/).
Expand Down Expand Up @@ -29,6 +33,7 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
#### Authentication
* /api/v1/login
* /api/v1/logout
* /api/v1/me


## Usage
Expand Down
2 changes: 1 addition & 1 deletion lib/rocket_chat/gem_version.rb
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
4 changes: 2 additions & 2 deletions lib/rocket_chat/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Info
attr_reader :data

#
# @param [Hash] data Raw version data
# @param [Hash] data Raw info data
#
def initialize(data)
@data = data.dup.freeze
end

# Rocket.Chat version
def version
@data['version']
data['version']
end

def inspect
Expand Down
4 changes: 3 additions & 1 deletion lib/rocket_chat/request_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ def server=(server)

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

response = request path, options

if fail_unless_ok && !response.is_a?(Net::HTTPOK)
raise RocketChat::HTTPError, "Invalid http response code: #{response.code}"
end

response_json = JSON.parse(response.body)
unless response_json['status'] == 'success'
unless skip_status_check || response_json['status'] == 'success'
raise RocketChat::StatusError, response_json['message']
end

Expand Down
11 changes: 11 additions & 0 deletions lib/rocket_chat/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,16 @@ def logout
server.request_json('/api/v1/logout', method: :post, token: token)
nil
end

#
# me REST API
# @return [User]
# @raise [HTTPError, StatusError]
#
def me
response = server.request_json('/api/v1/me', method: :get, token: token, skip_status_check: true)
raise RocketChat::StatusError, 'Failed to fetch profile' unless response['success']
User.new response
end
end
end
4 changes: 2 additions & 2 deletions lib/rocket_chat/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def initialize(data)

# Authentication token
def auth_token
@data['authToken']
data['authToken']
end

# User ID
def user_id
@data['userId']
data['userId']
end

def inspect
Expand Down
77 changes: 77 additions & 0 deletions lib/rocket_chat/user.rb
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
1 change: 1 addition & 0 deletions lib/rocketchat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
require 'rocket_chat/session'
require 'rocket_chat/info'
require 'rocket_chat/token'
require 'rocket_chat/user'
55 changes: 55 additions & 0 deletions spec/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0d796cb

Please sign in to comment.