Skip to content

Commit

Permalink
Merge pull request #12 from braiden-vasco/development
Browse files Browse the repository at this point in the history
Version 0.1.0
  • Loading branch information
Braiden Vasco committed Jun 20, 2015
2 parents 653e0df + 16f118a commit fa3b6a7
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ build/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset
/Gemfile.lock
/.ruby-version
/.ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AllCops:
Exclude:
- vendor/**/*

Style/AndOr:
Enabled: false

Style/TrailingComma:
Enabled: false
10 changes: 10 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'coveralls'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter,
]

SimpleCov.start do
add_filter '/spec/'
end
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: ruby

rvm:
- 2.0.0

script: bundle exec rake

before_install:
- gem update --system

services:
- redis-server
5 changes: 5 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--protected
--private
--markup markdown
-
LICENSE
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Change Log

## [v0.1.0](https://github.com/braiden-vasco/lita-gitter/tree/v0.1.0) (2015-06-20)

**Merged pull requests:**

- Add instructions [\#11](https://github.com/braiden-vasco/lita-gitter/pull/11) ([braiden-vasco](https://github.com/braiden-vasco))

- Basic implementation [\#10](https://github.com/braiden-vasco/lita-gitter/pull/10) ([braiden-vasco](https://github.com/braiden-vasco))

- Add changelog generator [\#9](https://github.com/braiden-vasco/lita-gitter/pull/9) ([braiden-vasco](https://github.com/braiden-vasco))

- Initialize Lita adapter [\#8](https://github.com/braiden-vasco/lita-gitter/pull/8) ([braiden-vasco](https://github.com/braiden-vasco))

- Use YARD [\#7](https://github.com/braiden-vasco/lita-gitter/pull/7) ([braiden-vasco](https://github.com/braiden-vasco))

- Configure Coveralls [\#6](https://github.com/braiden-vasco/lita-gitter/pull/6) ([braiden-vasco](https://github.com/braiden-vasco))

- Configure Travis CI [\#5](https://github.com/braiden-vasco/lita-gitter/pull/5) ([braiden-vasco](https://github.com/braiden-vasco))

- Add README [\#4](https://github.com/braiden-vasco/lita-gitter/pull/4) ([braiden-vasco](https://github.com/braiden-vasco))

- Configure testing [\#3](https://github.com/braiden-vasco/lita-gitter/pull/3) ([braiden-vasco](https://github.com/braiden-vasco))

- Configure linting [\#2](https://github.com/braiden-vasco/lita-gitter/pull/2) ([braiden-vasco](https://github.com/braiden-vasco))

- Initialize gem [\#1](https://github.com/braiden-vasco/lita-gitter/pull/1) ([braiden-vasco](https://github.com/braiden-vasco))



\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
16 changes: 16 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in lita-gitter.gemspec
gemspec

gem 'github_changelog_generator', '~> 1.6.1'

gem 'yard', '~> 0.8.7.6', require: false
gem 'redcarpet', require: false

gem 'rubocop', '~> 0.31.0', require: false

gem 'rspec', '~> 3.3.0', require: false

gem 'simplecov', '~> 0.10.0', require: false
gem 'coveralls'
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Lita::Adapters::Gitter
======================

[![Gem Version](https://badge.fury.io/rb/lita-gitter.svg)](http://badge.fury.io/rb/lita-gitter)
[![Build Status](https://travis-ci.org/braiden-vasco/lita-gitter.svg)](https://travis-ci.org/braiden-vasco/lita-gitter)
[![Coverage Status](https://coveralls.io/repos/braiden-vasco/lita-gitter/badge.svg)](https://coveralls.io/r/braiden-vasco/lita-gitter)

[Gitter](https://gitter.im) adapter for the [Lita](https://lita.io) chat bot.

Usage
-----

At first, see the documentation for Lita: https://docs.lita.io/

### Installation

Add **lita-gitter** to your Lita instance's Gemfile:

```ruby
gem 'lita-gitter', '~> 0.1.0'
```

### Preparation

Go to https://developer.gitter.im/apps, sign in if you are not already
signed in, and remember your token.

Then go to https://gitter.im/api/v1/rooms, find needed room by it's name
and remember room ID which precedes room name.

### Configuration

#### Required attributes

- `token` (String) - Secret token of Gitter user
- `room_id` (String) - Room ID

#### Example

This is an example `lita_config.rb` file:

```ruby
Lita.configure do |config|
config.robot.name = 'Lita'
config.robot.mention_name = 'lita'

config.robot.adapter = :gitter

config.adapters.gitter.token = 'LpMMyGbceCbUNl4ldRHfzjzb9a48F5WZYbgtBWoi'
config.adapters.gitter.room_id = 'Q5cjBQ9BwrNdK0JcicI9AYbL'
end
```
34 changes: 34 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rubygems'

gemspec = Gem::Specification.load('lita-gitter.gemspec')

github_user, github_project =
gemspec.homepage.scan(%r{^https://github\.com/([^/]+)/([^/]+)/?$})[0]

DEFAULT_EXCLUDE_LABELS = 'duplicate,question,invalid,wontfix'

require 'bundler/gem_tasks'

task default: [:spec, :lint]

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new

task lint: [:rubocop]

require 'rubocop/rake_task'
RuboCop::RakeTask.new

require 'yard'
YARD::Rake::YardocTask.new

desc 'Generate changelog'
task :changelog, [:token] do |_t, args|
cmd = 'github_changelog_generator'
cmd << " -u #{github_user}"
cmd << " -p #{github_project}"
cmd << " -t #{args[:token]}" if args[:token]
cmd << " --exclude-labels version,#{DEFAULT_EXCLUDE_LABELS}"

sh cmd
end
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'lita/gitter'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start
7 changes: 7 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
9 changes: 9 additions & 0 deletions lib/lita-gitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# rubocop:disable Style/FileName

require 'lita'

Lita.load_locales Dir[File.expand_path(
File.join('..', '..', 'locales', '*.yml'), __FILE__
)]

require 'lita/adapters/gitter'
126 changes: 126 additions & 0 deletions lib/lita/adapters/gitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
require 'em-http'
require 'json'
require 'net/http'

##
# Lita module.
#
module Lita
##
# Lita adapters module.
#
module Adapters
##
# Gitter adapter for the Lita chat bot.
#
class Gitter < Adapter
config :token, type: String, required: true
config :room_id, type: String, required: true

# The main loop. Listens for incoming messages,
# creates {Lita::Message} objects from them,
# and dispatches them to the robot.
#
def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
stream_url =
"https://stream.gitter.im/v1/rooms/#{config.room_id}/chatMessages"

http = EventMachine::HttpRequest.new(
stream_url,
keepalive: true,
connect_timeout: 0,
inactivity_timeout: 0,
)

EventMachine.run do
request = http.get(
head: {
'Accept' => 'application/json',
'Authorization' => "Bearer #{config.token}",
}
)

buffer = ''

request.stream do |chunk|
body = buffer + chunk

unless body.strip.empty?
begin
response = JSON.parse(body)
body = ''

text = response['text']
from_id = response['fromUser']['id']
room_id = config.room_id

get_message(text, from_id, room_id)

rescue JSON::ParserError
buffer = body
end
end
end
end
end

# Sends one or more messages to a user or room.
#
# @param target [Lita::Source] The user or room to send messages to.
# @param messages [Array<String>] An array of messages to send.
#
def send_messages(target, messages)
messages.reject(&:empty?).each do |message|
send_message(target, message)
end
end

protected

# Handle new message
#
# @param text [String] Message text.
# @param from_id [String] ID of user who sent this message.
# @param room_id [String] Room ID.
#
def get_message(text, from_id, room_id)
user = User.new(from_id)
source = Source.new(user: user, room: room_id)
message = Message.new(robot, text, source)

return if from_id == @user_id

message.command!
robot.receive(message)
end

# Sends one message to a user or room.
#
# @param target [Lita::Source] The user or room to send message to.
# @param text [String] Messages to send.
#
def send_message(_target, text) # rubocop:disable AbcSize, MethodLength
url = "https://api.gitter.im/v1/rooms/#{config.room_id}/chatMessages"
uri = URI.parse(url)

Net::HTTP.start(
uri.host,
uri.port,
use_ssl: true,
verify_mode: OpenSSL::SSL::VERIFY_NONE,
) do |http|
request = Net::HTTP::Post.new(uri.path)
request.add_field('Content-Type', 'application/json')
request.add_field('Accept', 'application/json')
request.add_field('Authorization', "Bearer #{config.token}")
request.body = { 'text' => text }.to_json
response = http.request(request)

@user_id = JSON.parse(response.body)['fromUser']['id']
end
end
end

Lita.register_adapter(:gitter, Gitter)
end
end
30 changes: 30 additions & 0 deletions lita-gitter.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8

Gem::Specification.new do |spec|
spec.name = 'lita-gitter'
spec.version = '0.1.0'
spec.authors = ['Braiden Vasco']
spec.email = ['braiden-vasco@mailtor.net']

spec.summary = 'Gitter adapter for the Lita chat bot'
spec.description = 'Gitter adapter for the Lita chat bot.'
spec.homepage = 'https://github.com/braiden-vasco/lita-gitter'
spec.license = 'MIT'

spec.respond_to?(:metadata) and
spec.metadata['lita_plugin_type'] = 'adapter'

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end

spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'

spec.add_runtime_dependency 'lita', '>= 4.4'
spec.add_runtime_dependency 'em-http-request', '~> 1.1'
end
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
lita:
adapters:
gitter:
2 changes: 2 additions & 0 deletions spec/lita/adapters/gitter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
describe Lita::Adapters::Gitter, lita: true do
end
Loading

0 comments on commit fa3b6a7

Please sign in to comment.