t_chatter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 444e14ae8b42aaa85766a23314158ed9d6fb0dd7
4
+ data.tar.gz: 7de15773339d2be2270099b466c733b543eaa056
5
+ SHA512:
6
+ metadata.gz: cb10d13a791e5e7d20e724a194d9ddb88734db0c7263075b178070b1a3bbe8f6f8db482745f5df3327757043ef835a028303929e7e26cb33d1cca506c5660047
7
+ data.tar.gz: 6af4107cc8e98fb12b2473e9d456e47dea2cfeee8fe283e578abac78eb3cc3509be5e06bf64950ff76292f6601860d05d60fd905a5fbd2075c0f2644a176c9b7
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .chatter.yml
11
+ codesnip
12
+ */codesnip
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.7
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in t_chatter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Oreoluwa Akinniranye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # TChatter
2
+
3
+ ## Installation
4
+ Install with:
5
+ ```
6
+ gem install t_chatter
7
+ ```
8
+ May not be useful in your application though, but in case you want to experiment with it
9
+
10
+ ```
11
+ gem t_chatter
12
+ ```
13
+
14
+ ## Setup
15
+ I have written a small setup script which only requires you run:
16
+ ```
17
+ setup_t_chatter
18
+ ```
19
+ It takes you through an interactive console to setup the chatter.
20
+
21
+ All the answers are optional.
22
+ In case you don't want to use the default server the application uses, you can also enter your own server
23
+ Make sure you have a `\receive` route which sends a response in JSON format. The response must contain a
24
+ `message` and `last_message_id` keys.
25
+ And the `\send` route which accepts a post request and returns the `last_message_id` key.
26
+
27
+ ## Usage
28
+ To check it out, simply call type:
29
+ ```
30
+ t_chatter
31
+ ```
32
+ on your terminal and watch the message stream in.
33
+
34
+ If you specified to receive message, you will only see the messages, if to send message you will only be able to send
35
+ However if you specified for both, you can both send an receive messages.
36
+
37
+ Don't worry if you didn't do it at first though. you can always set it up again.
38
+
39
+ ```
40
+ PS: To keep the application as slim and compact as possible, there is no database involved in this application nor the server, everything is stored in an array which puts us at the mercy of the hosting provider and updates. Therefore don't expect much in terms of data persistence.
41
+ ```
42
+
43
+ ## History
44
+
45
+ Thanks for wanting to try out TChatter.
46
+ TChatter is just an experiment and a hack on concurrency in Ruby applications.
47
+ So, I was thinking of something crazy I could do while learning something new and
48
+ I thought of a chat messenger. However, there are many chat messengers out there
49
+ And another could easily be spawned if I was using a web framework, therefore not
50
+ much of a learning.
51
+ I then thought of a chatting application on the terminal hence: TChatter(Terminal Chatter)
52
+ The problem I realized I would have encountered which wasn't much of a problem in
53
+ a web application was concurrency:
54
+ `How do I implement an application that would listen for input from the user as well
55
+ as update almost in realtime the content on another terminal.`
56
+
57
+ ### Solutions
58
+ `The first solution may not always be the best solution`
59
+
60
+ The first solution I thought of was a sort of loop, however this was a blocking solution,
61
+ I could have put it in a Timeout block, however, this still didn't seem to be the best
62
+
63
+ In came EventMachine, which was awesome.
64
+ Due to a very shallow understanding of EventMachine I used some sort of periodic timer which
65
+ which also didnt appear to be the best solution, as I had to send messages on one terminal
66
+ and receive on another.
67
+
68
+ Thread to the rescue
69
+ Then I learnt of the Ruby Thread which was no finally used.
70
+ The Thread seem to be the most effecient as it could do many things at the same time.
71
+
72
+ One major pitfall with Thread is that it may not be stopped even after exiting your
73
+ loop in the application as it 'kinda' freezes my console (I had to depress a key twice
74
+ for the console to respond).
75
+
76
+ The way I found around that was to use the Ruby trap method as in
77
+ ```ruby
78
+ trap("INT") {
79
+ puts "Exiting the chat"
80
+ threads.each{|t|
81
+ t.exit
82
+ }
83
+ }
84
+ ```
85
+ Thanks to [SO(stackoverflow)](http://stackoverflow.com/questions/27642943/kill-all-threads-on-terminate?answertab=votes#tab-top).
86
+
87
+ This is not close to the best application I have and will build, however I seem to be most excited
88
+ about this for now.
89
+
90
+ Let's see where the future takes me.
91
+ [To know how I feel about this you have to listen to this](http://www.youtube.com/watch?v=ZR0v0i63PQ4)
92
+
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andela-oakinniranye/t_chatter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pry"
4
+ require "bundler/setup"
5
+ require "t_chatter"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+ def reload!
10
+ puts "Reloading..."
11
+ files = $LOADED_FEATURES.select { |feat| feat =~ /\/t_chatter\// }
12
+ files.each { |file| load file }
13
+ true
14
+ end
15
+ # reload!
16
+ ARGV.clear
17
+ # Pry.start
18
+
19
+ # (If you use this, don't forget to add pry to your Gemfile!)
20
+ Pry.start
21
+
22
+ # require "irb"
23
+ # IRB.start
24
+ # TChatter::Start.chat
data/bin/setup ADDED
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
8
+
9
+ setup_t_chatter
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "t_chatter"
5
+
6
+ file_root = ENV["HOME"]
7
+ file_name = TChatter::ConfigSetup::CONFIG_FILE
8
+ file_location = "#{file_root}/#{file_name}"
9
+
10
+ unless File.exists? file_location
11
+ puts <<-EOS
12
+ Thanks for installing TChatter.
13
+ A small Ruby application that allows you to chat with a your friends on a particular network
14
+
15
+ This is the last process which basically sets up your application for you.
16
+ Let us begin
17
+ EOS
18
+ else
19
+ print <<-EOS
20
+ You seem to have this application configured already at: #{file_location}
21
+ Do you want to overwrite your old configuration?(Y or N) :
22
+ EOS
23
+ exit if gets.chomp.downcase == 'n'
24
+ end
25
+
26
+ config = Hash.new(nil)
27
+
28
+ print "What is your name?(optional) : "
29
+ config[:user] = gets.chomp.strip
30
+
31
+ puts ""
32
+
33
+ print "Would you like to just send message?(optional) : "
34
+ config[:send] = gets.chomp.strip
35
+
36
+ puts ""
37
+
38
+ print "Would you like to receive messages?(optional) : "
39
+ config[:receive] = gets.chomp.strip
40
+
41
+ print "Enter your URL for the Server?(optional) : "
42
+ config[:host] = gets.chomp.strip
43
+
44
+ true_cmd = {
45
+ 'true' => true,
46
+ 'yes' => true,
47
+ 'y' => true,
48
+ '' => true,
49
+ 'false' => false,
50
+ 'no' => false,
51
+ 'n' => false
52
+ }
53
+
54
+ real_config = Hash.new(nil)
55
+ config.each{ |x, y|
56
+ if [:send, :receive].include? x
57
+ real_config[x] = true_cmd[y]
58
+ real_config[x] = true unless true_cmd.keys.include? y
59
+ else
60
+ real_config[x] = y if !y.blank?
61
+ end
62
+ }
63
+
64
+ real_config[:host] = TChatter::DEFAULT_URL unless real_config[:host]
65
+
66
+ File.open(file_location, 'w') do |f|
67
+ ::YAML.dump real_config, f
68
+ end
69
+
70
+ puts <<-EOS
71
+ Congratulations you have completed the basic setup. The file can be found at: #{file_location}
72
+ EOS
data/bin/t_chatter ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "t_chatter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
15
+ TChatter::Start.chat
@@ -0,0 +1,58 @@
1
+ module TChatter
2
+ class Chat
3
+ attr_reader :connection, :messages, :response, :last_message_id, :user, :config
4
+ # attr_accessor :url
5
+
6
+ def initialize(username, config_class)
7
+ @user = username
8
+ @messages = []
9
+ @last_message_id = 0
10
+ @config = config_class.configuration
11
+ set_url
12
+ connect
13
+ end
14
+
15
+ def set_url
16
+ begin
17
+ @url = URI.parse(@config[:host])
18
+ rescue URI::InvalidURIError
19
+ puts <<-EOS
20
+ You dont seem to have a valid URL in your .chatter.yml file\n
21
+ You may run ``setup_t_chatter`` to set a default url which will be
22
+ used everytime you try to connect. However you are now being connected
23
+ to the global chatter stream
24
+ EOS
25
+ @url = URI.parse(TChatter::DEFAULT_URL)
26
+ end
27
+ end
28
+
29
+ def unknown_url
30
+ puts "The URL doesn't appear to be valid a URL"
31
+ end
32
+
33
+
34
+ def connect
35
+ @connection = Faraday::Connection.new(@url)
36
+ end
37
+
38
+ def send_message(message)
39
+ response = connection.post('/send', {message: message, user: user})
40
+ response = JSON.parse(response.body)
41
+ @last_message_id = response["last_message_id"]
42
+ @last_message = message
43
+ end
44
+
45
+ def receive_message
46
+ sleep 2
47
+ response = connection.get('/receive', { last_message_id: @last_message_id })
48
+ @response = JSON.parse(response.body)
49
+ last_id = @response["last_message_id"]
50
+ unless last_id == last_message_id
51
+ message = @response["message"]
52
+ @last_message_id = @response["last_message_id"]
53
+ @messages << message if message
54
+ puts message
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,32 @@
1
+ module TChatter
2
+ class ConfigSetup
3
+ include Singleton
4
+
5
+ CONFIG_FILE = '.chatter.yml'
6
+ CONFIG_FILE_SOURCES = ['.', '..', ENV["HOME"]]
7
+
8
+ def self.set_configuration
9
+ config_file = nil
10
+ CONFIG_FILE_SOURCES.each{ |file_source|
11
+ config_file = "#{file_source}/#{CONFIG_FILE}"
12
+ break if File.exists? config_file
13
+ }
14
+ if File.exists? config_file
15
+ @@config_data = load_config(config_file)
16
+ else
17
+ @@config_data = Hash.new(nil)
18
+ end
19
+ end
20
+
21
+ def self.load_config(file_path)
22
+ ::YAML.load_file(file_path)
23
+ end
24
+
25
+ def configuration
26
+ @@config_data
27
+ end
28
+
29
+ set_configuration
30
+ end
31
+
32
+ end
@@ -0,0 +1,53 @@
1
+ module TChatter
2
+ class Start
3
+
4
+ def self.chat
5
+ config = ConfigSetup.instance
6
+ user = config.configuration[:user]
7
+
8
+ # host = config[:host]
9
+ unless user
10
+ print "Would you mind telling me your name? > "
11
+ user = gets.chomp
12
+ end
13
+
14
+ chat = Chat.new(user, config)
15
+
16
+ want_to_receive = config.configuration.blank? ? true : config.configuration[:receive]
17
+ want_to_send = config.configuration.blank? ? true : config.configuration[:send]
18
+
19
+ threads = []
20
+
21
+
22
+ if want_to_receive
23
+ #response object
24
+ threads << Thread.new do
25
+ loop{
26
+ chat.receive_message
27
+ }
28
+ end
29
+ end
30
+
31
+ if want_to_send
32
+ #request object
33
+ threads << Thread.new do
34
+ loop{
35
+ message = gets.chomp
36
+ chat.send_message(message) if message
37
+ }
38
+ end
39
+ end
40
+
41
+ trap("INT") {
42
+ puts "Exiting the chat"
43
+ threads.each{|t|
44
+ t.exit
45
+ }
46
+ }
47
+
48
+ threads.each{ |thread|
49
+ thread.join
50
+ }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module TChatter
2
+ VERSION = "0.1.0"
3
+ end
data/lib/t_chatter.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "t_chatter/version"
2
+ # gem 'faraday'
3
+ require 'json'
4
+ require 'faraday'
5
+ require 'yaml'
6
+ require 'singleton'
7
+ require 'uri'
8
+
9
+ module TChatter
10
+ DEFAULT_URL = "https://tchatter.herokuapp.com"
11
+ # Your code goes here...
12
+ end
13
+
14
+ Object.class_eval do
15
+ def blank?
16
+ respond_to?(:empty?) ? !!empty? : !self
17
+ end
18
+ end
19
+
20
+
21
+ APP_ROOT = Pathname.new(File.expand_path('../../', __FILE__))
22
+ Dir[APP_ROOT.join('lib', 't_chatter', '*.rb')].each { |f| require f }
23
+ CS = TChatter::ConfigSetup
24
+ TC = TChatter::Chat
data/t_chatter.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 't_chatter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "t_chatter"
8
+ spec.version = TChatter::VERSION
9
+ spec.authors = ["Oreoluwa Akinniranye"]
10
+ spec.email = ["oreoluwa.akinniranye@andela.com"]
11
+
12
+ spec.summary = %q{Allows you to chat on your terminal}
13
+ spec.description = %q{TChatter allows you to chat with your friends on your terminal. You can extend it further though to chat your friends on Slack and other applications.}
14
+ spec.homepage = "http://github.com/andela-oakinniranye/t_chatter"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = ['t_chatter', 'setup_t_chatter']
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "faraday"
23
+ spec.add_dependency "json"
24
+
25
+ spec.post_install_message = "Thanks for trying out my interesting gem.\nTo complete the installation, run `setup_t_chatter` to create a .chatter.yml configuration file.\nThis allows you to use your own url and set defaults for your version of TChatter"
26
+
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: t_chatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oreoluwa Akinniranye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: TChatter allows you to chat with your friends on your terminal. You can
70
+ extend it further though to chat your friends on Slack and other applications.
71
+ email:
72
+ - oreoluwa.akinniranye@andela.com
73
+ executables:
74
+ - t_chatter
75
+ - setup_t_chatter
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - bin/setup_t_chatter
89
+ - bin/t_chatter
90
+ - lib/t_chatter.rb
91
+ - lib/t_chatter/chat.rb
92
+ - lib/t_chatter/config_setup.rb
93
+ - lib/t_chatter/start.rb
94
+ - lib/t_chatter/version.rb
95
+ - t_chatter.gemspec
96
+ homepage: http://github.com/andela-oakinniranye/t_chatter
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message: |-
101
+ Thanks for trying out my interesting gem.
102
+ To complete the installation, run `setup_t_chatter` to create a .chatter.yml configuration file.
103
+ This allows you to use your own url and set defaults for your version of TChatter
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.8
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Allows you to chat on your terminal
123
+ test_files: []