gittr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 891bc0ea1a2d59ebe24fe96383e2f54e7642c965
4
+ data.tar.gz: f86daba7ffadd9d72cf0252f7aba58cb50961de6
5
+ SHA512:
6
+ metadata.gz: 551dc7554d6d07e7192eacf6f43018733bf01ab75eb55c620ed7745f22dee7b0e183f5d91c41affef587375adb2845c0b9179c0ebca1d412f8c1776d8f1360c4
7
+ data.tar.gz: f4bfab4ff79b139f3d51b49cbbd73b14253921a33db9125db036856f2021836a47808d1dd2e72f8f70fdc8227dc477de986f5faab04c006a24fa5e3182c0d340
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gittr.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tyler Ewing
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Gittr
2
+
3
+ [![Code Climate](https://codeclimate.com/github/zoso10/Gittr/badges/gpa.svg)](https://codeclimate.com/github/zoso10/Gittr) [![Test Coverage](https://codeclimate.com/github/zoso10/Gittr/badges/coverage.svg)](https://codeclimate.com/github/zoso10/Gittr)
4
+ [![Circle CI](https://circleci.com/gh/zoso10/Gittr.svg?style=svg)](https://circleci.com/gh/zoso10/Gittr)
5
+
6
+ Gittr is a Ruby wrapper for the Gitter chat client REST API. Check out [Gitter](https://gitter.im/) if you haven't already!
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'gittr'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install gittr
23
+
24
+ ## Usage
25
+
26
+ Creating a client with your API token.
27
+
28
+ ```ruby
29
+ Gittr.configure do |config|
30
+ config.token = 'abc123'
31
+ end
32
+ client = Gittr::Client.new
33
+ ```
34
+
35
+ The `Gittr::Client` implements all endpoints as described in the [Gitter Documentation](https://developer.gitter.im/docs/rest-api) for the REST API.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it ( https://github.com/zoso10/gittr/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,8 @@
1
+ machine:
2
+ ruby:
3
+ version: 2.0.0
4
+ environment:
5
+ CODECLIMATE_REPO_TOKEN: 7f60dc04d59e21c8022f8924f3b0fdf87195f03435968e89ec0850a79175349c
6
+ dependencies:
7
+ pre:
8
+ - gem install bundler --pre
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gittr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gittr"
8
+ spec.version = Gittr::VERSION
9
+ spec.authors = ["Tyler Ewing"]
10
+ spec.email = ["tewing10@gmail.com"]
11
+ spec.summary = %q{Gitter REST API wrapper}
12
+ spec.description = %q{Gittr is a Ruby wrapper for the Gitter chat client REST API.}
13
+ spec.homepage = "https://github.com/zoso10/Gittr"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = ">= 2.0"
22
+
23
+ spec.add_dependency "httparty"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "vcr"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency "codeclimate-test-reporter"
31
+ spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "pry-byebug"
33
+ end
@@ -0,0 +1,23 @@
1
+ require 'httparty'
2
+
3
+ require 'gittr/version'
4
+ require 'gittr/hash_constructor'
5
+ require 'gittr/room'
6
+ require 'gittr/user'
7
+ require 'gittr/channel'
8
+ require 'gittr/message'
9
+ require 'gittr/organization'
10
+ require 'gittr/repository'
11
+ require 'gittr/client'
12
+
13
+ module Gittr
14
+
15
+ class << self
16
+ attr_accessor :token
17
+
18
+ def configure
19
+ return enum_for(__callee__) unless block_given?
20
+ yield self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Gittr
2
+ class Channel
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :name, :topic, :uri, :one_to_one, :unread_items, :mentions, :last_access_time, :lurk, :url, :github_type, :security, :v
6
+ end
7
+ end
@@ -0,0 +1,95 @@
1
+ module Gittr
2
+ class Client
3
+ include HTTParty
4
+
5
+ base_uri 'https://api.gitter.im/v1/'
6
+
7
+ def initialize(token=Gittr.token)
8
+ @token = token
9
+ @headers = {"Accept" => "application/json", "Authorization" => "Bearer #{@token}", "Content-Type" => "application/json"}
10
+ raise StandardError.new('API Token is missing') unless @token
11
+ end
12
+
13
+ def rooms
14
+ map_get_response('/rooms'){ |room| Room.new(room) }
15
+ end
16
+
17
+ def rooms_users(room_id)
18
+ map_get_response("/rooms/#{room_id}/users"){ |user| User.new(user) }
19
+ end
20
+
21
+ def rooms_channels(room_id)
22
+ map_get_response("/rooms/#{room_id}/channels"){ |channel| Channel.new(channel) }
23
+ end
24
+
25
+ def join_room(uri)
26
+ post_response('/rooms', {uri: uri}){ |room| Room.new(room) }
27
+ end
28
+
29
+ def list_messages(room_id, params={})
30
+ map_get_response("/rooms/#{room_id}/chatMessages", params){ |message| Message.new(message) }
31
+ end
32
+
33
+ def create_message(room_id, text)
34
+ post_response("/rooms/#{room_id}/chatMessages", {}, {text: text}){ |message| Message.new(message) }
35
+ end
36
+
37
+ def update_message(room_id, message_id, text)
38
+ put_response("/rooms/#{room_id}/chatMessages/#{message_id}", {text: text}){ |message| Message.new(message) }
39
+ end
40
+
41
+ def get_user
42
+ # There's only one user in the array, silly...
43
+ get_response("/user"){ |user| User.new(user.first) }
44
+ end
45
+
46
+ def user_rooms(user_id)
47
+ map_get_response("/user/#{user_id}/rooms"){ |room| Room.new(room) }
48
+ end
49
+
50
+ def mark_as_read(user_id, room_id)
51
+ post_response("/user/#{user_id}/rooms/#{room_id}/unreadItems")
52
+ end
53
+
54
+ def user_orgs(user_id)
55
+ map_get_response("/user/#{user_id}/orgs"){ |org| Organization.new(org) }
56
+ end
57
+
58
+ def user_repos(user_id)
59
+ map_get_response("/user/#{user_id}/repos"){ |repo| Repository.new(repo) }
60
+ end
61
+
62
+ def user_channels(user_id)
63
+ map_get_response("/user/#{user_id}/channels"){ |channel| Channel.new(channel) }
64
+ end
65
+
66
+ protected
67
+
68
+ def get_response(uri, params={}, &block)
69
+ response = self.class.get(uri, headers: @headers, query: params)
70
+ response = response.parsed_response
71
+ return response unless block_given?
72
+ yield response
73
+ end
74
+
75
+ def map_get_response(uri, params={}, &block)
76
+ get_response(uri, params).map do |response|
77
+ yield response
78
+ end
79
+ end
80
+
81
+ def post_response(uri, params={}, body='', &block)
82
+ response = self.class.post(uri, headers: @headers, query: params, body: body.to_json)
83
+ response = response.parsed_response
84
+ return response unless block_given?
85
+ yield response
86
+ end
87
+
88
+ def put_response(uri, body='', &block)
89
+ response = self.class.put(uri, headers: @headers, body: body.to_json)
90
+ response = response.parsed_response
91
+ return response unless block_given?
92
+ yield response
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,20 @@
1
+ module Gittr
2
+ module HashConstructor
3
+ def initialize(args={})
4
+ args.each do |key, value|
5
+ add_instance_variable(key) unless respond_to? to_snake_case(key)
6
+ public_send "#{to_snake_case(key)}=", value
7
+ end
8
+ end
9
+
10
+ def to_snake_case(str)
11
+ str.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase
12
+ end
13
+
14
+ protected
15
+
16
+ def add_instance_variable(key)
17
+ self.class.class_eval{ attr_accessor key.to_sym }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module Gittr
2
+ class Message
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :text, :html, :sent, :edited_at, :from_user, :unread, :read_by, :urls, :mentions, :issues, :meta, :v, :error
6
+
7
+ def from_user=(user)
8
+ @from_user = User.new(user)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Gittr
2
+ class Organization
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :name, :avatar_url, :room, :premium
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Gittr
2
+ class Repository
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :name, :description, :uri, :private, :exists, :avatar_url, :room
6
+
7
+ def room=(room)
8
+ @room = Room.new(room)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Gittr
2
+ class Room
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :name, :topic, :uri, :one_to_one, :users, :user_count, :unread_items, :mentions, :last_access_time, :lurk, :url, :github_type, :security, :premium, :noindex, :v
6
+
7
+ def users=(users)
8
+ @users = users.map{ |user| User.new(user) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Gittr
2
+ class User
3
+ include HashConstructor
4
+
5
+ attr_accessor :id, :username, :display_name, :url, :avatar_url_small, :avatar_url_medium, :role
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Gittr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gittr::Channel do
4
+ subject(:channel){ Gittr::Channel.new({"id" => "asdf", "name" => "skriv", "uri" => "zoso10/skriv", "garbage" => "foo"}) }
5
+
6
+ it 'should instantiate from hash' do
7
+ expect(channel.id).to eq('asdf')
8
+ expect(channel.name).to eq('skriv')
9
+ expect(channel.uri).to eq('zoso10/skriv')
10
+ end
11
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gittr::Client do
4
+ subject(:client) { Gittr::Client.new('redacted') }
5
+
6
+ it 'should not raise an exception' do
7
+ expect{
8
+ Gittr.configure{ |config| config.token='asdf'}
9
+ Gittr::Client.new
10
+ }.not_to raise_error
11
+ Gittr.token = nil
12
+ end
13
+
14
+ it 'should return rooms' do
15
+ VCR.use_cassette('rooms') do
16
+ rooms = client.rooms
17
+ expect(rooms.count).to eq(1)
18
+ expect(rooms[0].name).to eq('zoso10/skriv')
19
+ end
20
+ end
21
+
22
+ it 'should return users for room 54b7e136db8155e6700eb569' do
23
+ VCR.use_cassette('rooms_users') do
24
+ users = client.rooms_users('54b7e136db8155e6700eb569')
25
+ expect(users.count).to eq(2)
26
+ expect(users[0].username).to eq('zoso10')
27
+ expect(users[0].display_name).to eq('Tyler Ewing')
28
+ end
29
+ end
30
+
31
+ it 'should return channels for room 54b7e136db8155e6700eb569' do
32
+ VCR.use_cassette('rooms_channels') do
33
+ users = client.rooms_channels('54b7e136db8155e6700eb569')
34
+ expect(users.count).to eq(0)
35
+ end
36
+ end
37
+
38
+ it 'should join room and be admin' do
39
+ VCR.use_cassette('join_room') do
40
+ room = client.join_room('zoso10/gittr')
41
+ expect(room.name).to eq('zoso10/Gittr')
42
+ expect(room.user_count).to eq(1)
43
+ expect(room.users[0].username).to eq('zoso10')
44
+ expect(room.users[0].display_name).to eq('Tyler Ewing')
45
+ expect(room.users[0].role).to eq('admin')
46
+ end
47
+ end
48
+
49
+ it 'should list messages' do
50
+ VCR.use_cassette('list_messages') do
51
+ room_id = '54b7e136db8155e6700eb569'
52
+ messages = client.list_messages(room_id)
53
+ expect(messages).to_not be_nil
54
+ expect(messages.count).to eq(50)
55
+ expect(messages[0].text).to eq('thats pretty cool')
56
+ expect(messages[0].from_user.username).to eq('zoso10')
57
+ end
58
+ end
59
+
60
+ it 'should only list 10 messages' do
61
+ VCR.use_cassette('list_messages', record: :new_episodes) do
62
+ room_id = '54b7e136db8155e6700eb569'
63
+ messages = client.list_messages(room_id, limit: 10)
64
+ expect(messages.count).to eq(10)
65
+ end
66
+ end
67
+
68
+ it 'sends a message' do
69
+ VCR.use_cassette('send_message') do
70
+ room_id = '54b7e136db8155e6700eb569'
71
+ text = 'This is a sample message'
72
+ message = client.create_message(room_id, text)
73
+ expect(message).to_not be_nil
74
+ expect(message.text).to eq(text)
75
+ expect(message.from_user.username).to eq('zoso10')
76
+ end
77
+ end
78
+
79
+ it 'update a message' do
80
+ VCR.use_cassette('update_message') do
81
+ room_id = '54b7e136db8155e6700eb569'
82
+ message_id = '54bd1b4a573df0f92345a00f'
83
+ text = 'This message has been updated'
84
+ message = client.update_message(room_id, message_id, text)
85
+ expect(message).to_not be_nil
86
+ expect(message.error).to eq('You can no longer edit this message')
87
+ end
88
+ end
89
+
90
+ it 'gets the current user' do
91
+ VCR.use_cassette('user') do
92
+ user = client.get_user
93
+ expect(user.id).to eq('54b7df5bdb8155e6700eb552')
94
+ expect(user.username).to eq('zoso10')
95
+ expect(user.display_name).to eq('Tyler Ewing')
96
+ end
97
+ end
98
+
99
+ it 'gets the rooms for the user' do
100
+ VCR.use_cassette('user_rooms') do
101
+ user_id = '54b7df5bdb8155e6700eb552'
102
+ rooms = client.user_rooms(user_id)
103
+ expect(rooms.count).to eq(2)
104
+ expect(rooms[0].name).to eq('zoso10/Gittr')
105
+ expect(rooms[0].uri).to eq('zoso10/Gittr')
106
+ end
107
+ end
108
+
109
+ it 'marks a message as read' do
110
+ VCR.use_cassette('read_message') do
111
+ user_id = '54b7df5bdb8155e6700eb552'
112
+ room_id = '54b7e136db8155e6700eb569'
113
+ something = client.mark_as_read(user_id, room_id)
114
+ expect(something).to_not be_nil
115
+ end
116
+ end
117
+
118
+ it 'gets the orgs for the user' do
119
+ VCR.use_cassette('user_orgs') do
120
+ user_id = '54b7df5bdb8155e6700eb552'
121
+ orgs = client.user_orgs(user_id)
122
+ expect(orgs).to_not be_nil
123
+ expect(orgs[0].name).to eq('thoughtbot')
124
+ expect(orgs[1].name).to eq('secondrotation')
125
+ end
126
+ end
127
+
128
+ it 'gets the repos for the user' do
129
+ VCR.use_cassette('user_repos') do
130
+ user_id = '54b7df5bdb8155e6700eb552'
131
+ repos = client.user_repos(user_id)
132
+ expect(repos).to_not be_nil
133
+ expect(repos.count).to eq(236)
134
+ expect(repos[0].name).to eq('thoughtbot/hound')
135
+ expect(repos[0].uri).to eq('thoughtbot/hound')
136
+ end
137
+ end
138
+
139
+ it 'gets the channels for the user' do
140
+ VCR.use_cassette('user_channels') do
141
+ user_id = '54b7df5bdb8155e6700eb552'
142
+ channels = client.user_channels(user_id)
143
+ expect(channels).to_not be_nil
144
+ expect(channels.count).to eq(0)
145
+ end
146
+ end
147
+ end