From 0d796cb97a7a680b67f7c36ac6db48836bbb72e9 Mon Sep 17 00:00:00 2001 From: Andrew Bromwich Date: Thu, 13 Apr 2017 22:57:55 +1000 Subject: [PATCH] Bumped to v0.0.2 Added support for me REST API --- .rubocop.yml | 3 ++ README.md | 5 ++ lib/rocket_chat/gem_version.rb | 2 +- lib/rocket_chat/info.rb | 4 +- lib/rocket_chat/request_helper.rb | 4 +- lib/rocket_chat/session.rb | 11 +++++ lib/rocket_chat/token.rb | 4 +- lib/rocket_chat/user.rb | 77 +++++++++++++++++++++++++++++++ lib/rocketchat.rb | 1 + spec/session_spec.rb | 55 ++++++++++++++++++++++ 10 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 lib/rocket_chat/user.rb diff --git a/.rubocop.yml b/.rubocop.yml index 4bfa6a3..7bbdd42 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,3 +4,6 @@ Metrics/BlockLength: Metrics/LineLength: Max: 120 + +Metrics/MethodLength: + Max: 12 diff --git a/README.md b/README.md index ad89ed4..5d480d8 100644 --- a/README.md +++ b/README.md @@ -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/). @@ -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 diff --git a/lib/rocket_chat/gem_version.rb b/lib/rocket_chat/gem_version.rb index 2d260f2..3fdf403 100644 --- a/lib/rocket_chat/gem_version.rb +++ b/lib/rocket_chat/gem_version.rb @@ -1,3 +1,3 @@ module RocketChat - VERSION = '0.0.1'.freeze + VERSION = '0.0.2'.freeze end diff --git a/lib/rocket_chat/info.rb b/lib/rocket_chat/info.rb index 4c80a81..4207f20 100644 --- a/lib/rocket_chat/info.rb +++ b/lib/rocket_chat/info.rb @@ -7,7 +7,7 @@ class Info attr_reader :data # - # @param [Hash] data Raw version data + # @param [Hash] data Raw info data # def initialize(data) @data = data.dup.freeze @@ -15,7 +15,7 @@ def initialize(data) # Rocket.Chat version def version - @data['version'] + data['version'] end def inspect diff --git a/lib/rocket_chat/request_helper.rb b/lib/rocket_chat/request_helper.rb index 83aa7fd..1f9680a 100644 --- a/lib/rocket_chat/request_helper.rb +++ b/lib/rocket_chat/request_helper.rb @@ -22,6 +22,8 @@ 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) @@ -29,7 +31,7 @@ def request_json(path, options = {}) 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 diff --git a/lib/rocket_chat/session.rb b/lib/rocket_chat/session.rb index 6f778a7..98382a8 100644 --- a/lib/rocket_chat/session.rb +++ b/lib/rocket_chat/session.rb @@ -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 diff --git a/lib/rocket_chat/token.rb b/lib/rocket_chat/token.rb index c350d77..db32ecc 100644 --- a/lib/rocket_chat/token.rb +++ b/lib/rocket_chat/token.rb @@ -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 diff --git a/lib/rocket_chat/user.rb b/lib/rocket_chat/user.rb new file mode 100644 index 0000000..d4d32e4 --- /dev/null +++ b/lib/rocket_chat/user.rb @@ -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 diff --git a/lib/rocketchat.rb b/lib/rocketchat.rb index 340acbc..60ebb8c 100644 --- a/lib/rocketchat.rb +++ b/lib/rocketchat.rb @@ -10,3 +10,4 @@ require 'rocket_chat/session' require 'rocket_chat/info' require 'rocket_chat/token' +require 'rocket_chat/user' diff --git a/spec/session_spec.rb b/spec/session_spec.rb index cc2c8df..e0bbd4c 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -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: 'example@example.com', + 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 'example@example.com' + 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