schatter 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.dirname(__FILE__)+'/../lib'
4
+
5
+ require 'schatter/root_context'
6
+
7
+ Schatter::RootContext.new(*ARGV).push
@@ -0,0 +1,7 @@
1
+ require 'rainbow'
2
+
3
+ module Schatter::Colour
4
+ def c text, colour
5
+ text.to_s.foreground colour
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ require 'schatter/conversation_context'
2
+
3
+ class Schatter::Command::CreateConversation
4
+ attr_reader :usage, :help, :session
5
+
6
+ def initialize session
7
+ @session = session
8
+ @usage = '<name>'
9
+ @help = 'Creates and joins a new conversation'
10
+ end
11
+
12
+ def execute name
13
+ conversation = session.create_conversation name
14
+ Schatter::ConversationContext.new(conversation).push
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ class Schatter::Command::CreateMessage
2
+ attr_reader :usage, :help, :conversation
3
+
4
+ def initialize conversation
5
+ @conversation = conversation
6
+ @usage = 'content'
7
+ @help = 'Creates a new message in the current conversation'
8
+ end
9
+
10
+ def execute content
11
+ conversation.create_message content: content
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require 'schatter/index'
2
+
3
+ class Schatter::Command::DeleteMessage
4
+ include Schatter::Index
5
+
6
+ attr_reader :usage, :help, :conversation
7
+
8
+ def initialize conversation
9
+ @conversation = conversation
10
+ @usage = '<index>'
11
+ @help = 'Deletes the specified message'
12
+ end
13
+
14
+ def execute index
15
+ message = conversation.messages.values[from_index(index)]
16
+ unless message
17
+ puts "invalid index"
18
+ return
19
+ end
20
+ message.destroy
21
+ puts "message #{index} destroyed"
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ class Schatter::Command::InvitePerson
2
+ attr_reader :usage, :help, :conversation
3
+
4
+ def initialize conversation
5
+ @conversation = conversation
6
+ @usage = '<email>'
7
+ @help = 'Invites someone to join a conversation'
8
+ end
9
+
10
+ def execute email
11
+ conversation.create_person email: email
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require 'schatter/conversation_context'
2
+ require 'schatter/index'
3
+
4
+ class Schatter::Command::JoinConversation
5
+ include Schatter::Index
6
+
7
+ attr_reader :usage, :help, :session
8
+
9
+ def initialize session
10
+ @session = session
11
+ @usage = '<index>'
12
+ @help = 'Joins a specified conversation'
13
+ end
14
+
15
+ def execute index
16
+ conversation = session.conversations.values[from_index(index)]
17
+ unless conversation
18
+ puts "invalid index"
19
+ return
20
+ end
21
+ Schatter::ConversationContext.new(conversation).push
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ require 'schatter/colour'
2
+ require 'schatter/index'
3
+
4
+ class Schatter::Command::ListConversations
5
+ include Schatter::Colour
6
+ include Schatter::Index
7
+
8
+ attr_reader :usage, :help, :session
9
+
10
+ def initialize session
11
+ @session = session
12
+ @usage = ''
13
+ @help = 'Lists current conversations'
14
+ end
15
+
16
+ def execute ignored
17
+ session.conversations(true).values.each_with_index do |conversation, index|
18
+ puts description conversation, index
19
+ end
20
+ end
21
+
22
+ def description conversation, index
23
+ [
24
+ c(to_index(index), :yellow),
25
+ c(conversation.formatted_timestamp, :blue),
26
+ c(conversation.name, :magenta),
27
+ ].join ' '
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require 'schatter/colour'
2
+ require 'schatter/index'
3
+
4
+ class Schatter::Command::ListMessages
5
+ include Schatter::Colour
6
+ include Schatter::Index
7
+
8
+ attr_reader :usage, :help, :conversation
9
+
10
+ def initialize conversation
11
+ @conversation = conversation
12
+ @usage = ''
13
+ @help = 'Lists messages for the current conversation'
14
+ end
15
+
16
+ def execute *ignored
17
+ conversation.messages(true).values.each_with_index do |message, index|
18
+ puts description message, index
19
+ end
20
+ end
21
+
22
+ def description message, index
23
+ person = conversation.people[message.person_id]
24
+ parent_index = conversation.messages.keys.index message.parent_id
25
+ email = person ? person.email : '?'
26
+ list = [
27
+ c(to_index(index), :yellow),
28
+ c(message.formatted_timestamp, :blue),
29
+ c(email, :magenta),
30
+ message.content
31
+ ]
32
+ list << c("(reply to #{to_index(parent_index)})", :green) if parent_index
33
+ list.join ' '
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ class Schatter::Command::ListPeople
2
+ attr_reader :usage, :help, :conversation
3
+
4
+ def initialize conversation
5
+ @conversation = conversation
6
+ @usage = ''
7
+ @help = 'Lists people in the current conversation'
8
+ end
9
+
10
+ def execute *ignored
11
+ conversation.people(true).values.each do |person|
12
+ puts "#{person.email}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ require 'schatter/index'
2
+
3
+ class Schatter::Command::ReplyToMessage
4
+ include Schatter::Index
5
+
6
+ attr_reader :usage, :help, :conversation, :context
7
+
8
+ def initialize conversation, context
9
+ @conversation = conversation
10
+ @context = context
11
+ @usage = '<index> <content>'
12
+ @help = 'Replies to the specified message'
13
+ end
14
+
15
+ def execute text
16
+ index, content = context.head_tail text
17
+ message = conversation.messages.values[from_index(index)]
18
+ unless message
19
+ puts "invalid index #{index}"
20
+ return
21
+ end
22
+ conversation.create_message content: content, parent_id: message.uuid
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module Schatter::Command
2
+ def load_command name, *args
3
+ require "schatter/command/#{name}"
4
+ Schatter::Command.const_get(classify name.to_s).new(*args)
5
+ end
6
+
7
+ def classify s
8
+ s.to_s.split('_').map(&:capitalize).join
9
+ end
10
+ end
@@ -0,0 +1,42 @@
1
+ require 'schatter/resource'
2
+ require 'schatter/message'
3
+ require 'schatter/person'
4
+
5
+ class Schatter::Conversation < Schatter::Resource
6
+ def messages reload=false
7
+ @messages = nil if reload
8
+ return @messages if @messages
9
+ @messages = Hash[get(links[:messages])['messages'].map do |resource|
10
+ [resource['uuid'], Schatter::Message.new(resource: resource)]
11
+ end]
12
+ end
13
+
14
+ def new_messages
15
+ params = {}
16
+ params[:message_id] = messages.last.uuid unless messages.empty?
17
+ get(links[:messages], params)['messages'].each do |resource|
18
+ messages[resource['uuid']] = Schatter::Message.new(resource: resource)
19
+ end
20
+ @messages
21
+ end
22
+
23
+ def people reload=false
24
+ @people = nil if reload
25
+ return @people if @people
26
+ @people = Hash[get(links[:people])['people'].map do |resource|
27
+ [resource['uuid'], Schatter::Person.new(resource: resource)]
28
+ end]
29
+ end
30
+
31
+ def create_message params
32
+ post links[:messages], params
33
+ end
34
+
35
+ def create_person params
36
+ post links[:people], params
37
+ end
38
+
39
+ def name
40
+ resource['name']
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ require 'schatter/commands'
2
+ require 'shell_shock/context'
3
+
4
+ class Schatter::ConversationContext
5
+ include Schatter::Command
6
+ include ShellShock::Context
7
+
8
+ def initialize conversation
9
+ @prompt = "#{conversation.name} > "
10
+ @conversation = conversation
11
+ add_command load_command(:list_messages, conversation), "'"
12
+ add_command load_command(:create_message, conversation), 'say'
13
+ add_command load_command(:reply_to_message, conversation, self), 'reply'
14
+ add_command load_command(:delete_message, conversation), 'delete'
15
+ add_command load_command(:invite_person, conversation), 'invite'
16
+ add_command load_command(:list_people, conversation), 'who'
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Schatter::Index
2
+ LETTERS = %w{a b c d e f g h i j}
3
+
4
+ def to_index number
5
+ number.to_s.scan(/./).map{|c| LETTERS[c.to_i]}.join('')
6
+ end
7
+
8
+ def from_index string
9
+ string.scan(/./).map{|c| LETTERS.index c}.join.to_i
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'schatter/resource'
2
+
3
+ class Schatter::Message < Schatter::Resource
4
+ def content
5
+ resource['content']
6
+ end
7
+
8
+ def person_id
9
+ resource['person_id']
10
+ end
11
+
12
+ def parent_id
13
+ resource['parent_id']
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'schatter/resource'
2
+
3
+ class Schatter::Person < Schatter::Resource
4
+ def email
5
+ resource['email']
6
+ end
7
+ end
@@ -0,0 +1,71 @@
1
+ require 'httparty'
2
+ require 'cgi'
3
+
4
+ class Schatter::Resource
5
+ def initialize params
6
+ @url = params[:url]
7
+ @resource = params[:resource]
8
+ @url = links[:self] if @resource
9
+ end
10
+
11
+ def destroy
12
+ delete @url
13
+ end
14
+
15
+ def resource
16
+ return @resource if @resource
17
+ @resource = get @url
18
+ end
19
+
20
+ def links
21
+ return @links if @links
22
+ @links = {}
23
+ resource['_links'].each do |k, v|
24
+ @links[k.to_sym] = v['href']
25
+ end
26
+ puts "#{@links}" if ENV['DEBUG']
27
+ @links
28
+ end
29
+
30
+ def uuid
31
+ resource['uuid']
32
+ end
33
+
34
+ def timestamp
35
+ Time.at resource['timestamp']
36
+ end
37
+
38
+ def formatted_timestamp
39
+ timestamp.strftime "%d/%m/%Y %H:%M:%S"
40
+ end
41
+
42
+ def get url, params={}
43
+ params[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
44
+ full_url = "#{url}?#{params.map{ |k,v| "#{k}=#{CGI.escape v.to_s}" }.join('&')}"
45
+ puts "GET #{full_url}" if ENV['DEBUG']
46
+ response = HTTParty.get full_url,
47
+ headers: {'Accept' => 'application/json'}
48
+ puts response if ENV['DEBUG']
49
+ response
50
+ end
51
+
52
+ def delete url, params={}
53
+ params[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
54
+ full_url = "#{url}?#{params.map{ |k,v| "#{k}=#{CGI.escape v.to_s}" }.join('&')}"
55
+ puts "DELETE #{full_url}" if ENV['DEBUG']
56
+ response = HTTParty.delete full_url,
57
+ headers: {'Accept' => 'application/json'}
58
+ puts response if ENV['DEBUG']
59
+ response
60
+ end
61
+
62
+ def post url, body
63
+ body[:auth_token] = ENV['SCHATTER_AUTH_TOKEN']
64
+ puts "POST #{url} #{body.to_json}" if ENV['DEBUG']
65
+ response = HTTParty.post url,
66
+ headers: {'Accept' => 'application/json', 'Content-Type' => 'application/json'},
67
+ body: body.to_json
68
+ puts response if ENV['DEBUG']
69
+ response
70
+ end
71
+ end
@@ -0,0 +1,22 @@
1
+ require 'schatter/version'
2
+ require 'schatter/session'
3
+ require 'schatter/commands'
4
+ require 'shell_shock/context'
5
+
6
+ class Schatter::RootContext
7
+ include Schatter::Command
8
+ include ShellShock::Context
9
+
10
+ def initialize url=nil
11
+ url = "http://localhost:3000" unless url
12
+ unless ENV['SCHATTER_AUTH_TOKEN']
13
+ puts "Please register at #{url} and set environment variable SCHATTER_AUTH_TOKEN"
14
+ exit 1
15
+ end
16
+ @prompt = "schatter.#{Schatter::VERSION} #{url}> "
17
+ session = Schatter::Session.new url: url
18
+ add_command load_command(:list_conversations, session), "'"
19
+ add_command load_command(:join_conversation, session), 'join'
20
+ add_command load_command(:create_conversation, session), 'create'
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ require 'schatter/resource'
2
+ require 'schatter/conversation'
3
+
4
+ class Schatter::Session < Schatter::Resource
5
+ def conversations reload=false
6
+ @conversations = nil if reload
7
+ return @conversations if @conversations
8
+ @conversations = Hash[get(links[:conversations])['conversations'].map do |resource|
9
+ [resource['uuid'], Schatter::Conversation.new(resource: resource)]
10
+ end]
11
+ end
12
+
13
+ def create_conversation name
14
+ Schatter::Conversation.new post links['conversations'], name: name
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Schatter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'httparty'
22
+ spec.add_dependency 'rainbow'
23
+
22
24
  spec.add_development_dependency 'bundler'
23
25
  spec.add_development_dependency 'rake'
24
26
  end
@@ -0,0 +1,14 @@
1
+ require_relative '../spec_helper'
2
+ require 'schatter/index'
3
+
4
+ describe Schatter::Index do
5
+ include Schatter::Index
6
+
7
+ it 'should translate to string' do
8
+ to_index(123).must_equal 'bcd'
9
+ end
10
+
11
+ it 'should translate to string' do
12
+ from_index('bcd').must_equal 123
13
+ end
14
+ end
@@ -0,0 +1 @@
1
+ require 'minitest/autorun'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-29 00:00:00.000000000 Z
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rainbow
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: bundler
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -62,7 +78,8 @@ dependencies:
62
78
  description: command line interface to schatter
63
79
  email:
64
80
  - mark@ryall.name
65
- executables: []
81
+ executables:
82
+ - schatter
66
83
  extensions: []
67
84
  extra_rdoc_files: []
68
85
  files:
@@ -71,9 +88,30 @@ files:
71
88
  - LICENSE.txt
72
89
  - README.md
73
90
  - Rakefile
74
- - lib/schatter.rb
91
+ - bin/schatter
92
+ - lib/schatter/colour.rb
93
+ - lib/schatter/command/create_conversation.rb
94
+ - lib/schatter/command/create_message.rb
95
+ - lib/schatter/command/delete_message.rb
96
+ - lib/schatter/command/invite_person.rb
97
+ - lib/schatter/command/join_conversation.rb
98
+ - lib/schatter/command/list_conversations.rb
99
+ - lib/schatter/command/list_messages.rb
100
+ - lib/schatter/command/list_people.rb
101
+ - lib/schatter/command/reply_to_message.rb
102
+ - lib/schatter/commands.rb
103
+ - lib/schatter/conversation.rb
104
+ - lib/schatter/conversation_context.rb
105
+ - lib/schatter/index.rb
106
+ - lib/schatter/message.rb
107
+ - lib/schatter/person.rb
108
+ - lib/schatter/resource.rb
109
+ - lib/schatter/root_context.rb
110
+ - lib/schatter/session.rb
75
111
  - lib/schatter/version.rb
76
112
  - schatter.gemspec
113
+ - spec/schatter/index_spec.rb
114
+ - spec/spec_helper.rb
77
115
  homepage: http://github.com/markryall/schatter_gem
78
116
  licenses:
79
117
  - MIT
@@ -89,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
127
  version: '0'
90
128
  segments:
91
129
  - 0
92
- hash: 1114351573189891695
130
+ hash: 1971852999253736068
93
131
  required_rubygems_version: !ruby/object:Gem::Requirement
94
132
  none: false
95
133
  requirements:
@@ -98,11 +136,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
136
  version: '0'
99
137
  segments:
100
138
  - 0
101
- hash: 1114351573189891695
139
+ hash: 1971852999253736068
102
140
  requirements: []
103
141
  rubyforge_project:
104
142
  rubygems_version: 1.8.23
105
143
  signing_key:
106
144
  specification_version: 3
107
145
  summary: api wrapper and command line binary for interaction with schatter rails application
108
- test_files: []
146
+ test_files:
147
+ - spec/schatter/index_spec.rb
148
+ - spec/spec_helper.rb
@@ -1,4 +0,0 @@
1
- require 'schatter/version'
2
-
3
- module Schatter
4
- end