flint 0.0.1
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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +10 -0
- data/flint.gemspec +28 -0
- data/lib/flint.rb +12 -0
- data/lib/flint/client.rb +87 -0
- data/lib/flint/dsl.rb +24 -0
- data/lib/flint/version.rb +3 -0
- data/spec/client_spec.rb +52 -0
- data/spec/dsl_spec.rb +32 -0
- data/spec/spec_helper.rb +8 -0
- metadata +131 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/flint.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "flint/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "flint"
|
7
|
+
s.version = Flint::VERSION
|
8
|
+
s.authors = ["vanstee"]
|
9
|
+
s.email = ["vanstee@highgroove.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Flint is a simple Campfire client heavily inspired by the blather gem}
|
12
|
+
s.description = %q{Flint is a simple Campfire client heavily inspired by the blather gem}
|
13
|
+
|
14
|
+
s.rubyforge_project = "flint"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "em-http-request"
|
22
|
+
s.add_dependency "active_support"
|
23
|
+
s.add_dependency "json"
|
24
|
+
|
25
|
+
s.add_development_dependency "bundler"
|
26
|
+
s.add_development_dependency "rake"
|
27
|
+
s.add_development_dependency "rspec"
|
28
|
+
end
|
data/lib/flint.rb
ADDED
data/lib/flint/client.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
module Flint
|
2
|
+
class Client < EventMachine::HttpClient
|
3
|
+
attr_accessor :connection
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@state = :initializing
|
7
|
+
@handlers = {}
|
8
|
+
@token = nil
|
9
|
+
@account = nil
|
10
|
+
@room_id = nil
|
11
|
+
@buffer = ""
|
12
|
+
end
|
13
|
+
|
14
|
+
def register_handler(type, gaurd, &block)
|
15
|
+
@handlers[type] ||= []
|
16
|
+
@handlers[type] << [gaurd, block]
|
17
|
+
end
|
18
|
+
|
19
|
+
def setup(token, account, room_id)
|
20
|
+
@token = token
|
21
|
+
@account = account
|
22
|
+
@room_id = room_id
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def run
|
27
|
+
trap(:INT) { EventMachine.stop }
|
28
|
+
trap(:TERM) { EventMachine.stop }
|
29
|
+
|
30
|
+
EventMachine.run { join }
|
31
|
+
|
32
|
+
@state = :setup
|
33
|
+
end
|
34
|
+
|
35
|
+
def join
|
36
|
+
headers = { "authorization" => [@token, "X"] }
|
37
|
+
@connection = EventMachine::HttpRequest.new("https://#{@account}.campfirenow.com/room/#{@room_id}/join.json").post(:head => headers)
|
38
|
+
@connection.headers { |headers| listen }
|
39
|
+
end
|
40
|
+
|
41
|
+
def listen
|
42
|
+
headers = { "authorization" => [@token, "X"] }
|
43
|
+
@connection = EventMachine::HttpRequest.new("https://streaming.campfirenow.com/room/#{@room_id}/live.json").get(:head => headers)
|
44
|
+
@connection.headers { |headers| handle_ready }
|
45
|
+
@connection.stream { |chunk| process(chunk) unless chunk.blank? }
|
46
|
+
end
|
47
|
+
|
48
|
+
def say(message)
|
49
|
+
return if @state == :initializing
|
50
|
+
|
51
|
+
headers = { "authorization" => [@token, "X"] }
|
52
|
+
message = { :message => message }
|
53
|
+
EventMachine::HttpRequest.new("https://#{@account}.campfirenow.com/room/#{@room_id}/speak.json").post(:head => headers, :query => message)
|
54
|
+
end
|
55
|
+
|
56
|
+
def handle_ready
|
57
|
+
Array(@handlers[:ready]).each { |_, block| block.call }
|
58
|
+
@state = :ready
|
59
|
+
end
|
60
|
+
|
61
|
+
def process(chunk)
|
62
|
+
@buffer += chunk
|
63
|
+
while line = @buffer.slice!(/.*\r/)
|
64
|
+
message = JSON.parse(line).with_indifferent_access rescue nil
|
65
|
+
handle_message(message) if message
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def handle_message(message)
|
70
|
+
Array(@handlers[:message]).each do |guard, block|
|
71
|
+
block.call(message) if guarded?(guard, message)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def guarded?(guard, message)
|
76
|
+
guard.all? do |attribute, test|
|
77
|
+
case test
|
78
|
+
when String then message[attribute] == test
|
79
|
+
when Regexp then message[attribute] =~ test
|
80
|
+
when Array then test.include?(message[attribute])
|
81
|
+
when Proc then test.call(message[attribute])
|
82
|
+
else false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/flint/dsl.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Flint
|
2
|
+
module DSL
|
3
|
+
def client
|
4
|
+
@client ||= Client.new
|
5
|
+
end
|
6
|
+
module_function :client
|
7
|
+
|
8
|
+
def setup(token, account, room_id)
|
9
|
+
client.setup(token, account, room_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def ready(&block)
|
13
|
+
client.register_handler(:ready, nil, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def message(gaurd, &block)
|
17
|
+
client.register_handler(:message, gaurd, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def say(message)
|
21
|
+
client.say(message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Flint::Client do
|
4
|
+
before do
|
5
|
+
@client = Flint::Client.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'can be setup' do
|
9
|
+
@client.setup('token', 'account', 'room').should == @client
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#initialize" do
|
13
|
+
it 'sets the state to :initializing' do
|
14
|
+
@client.state.should == :initializing
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#message' do
|
19
|
+
before do
|
20
|
+
@client = Flint::Client.new
|
21
|
+
@response = mock()
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'can be a hash with string match' do
|
25
|
+
@response.should_receive(:call)
|
26
|
+
@client.register_handler(:message, :body => "Taco party?") { @response.call }
|
27
|
+
|
28
|
+
@client.handle_message(:body => "Taco party?")
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'can be a hash with a regexp' do
|
32
|
+
@response.should_receive(:call)
|
33
|
+
@client.register_handler(:message, :body => /tacos/) { @response.call }
|
34
|
+
|
35
|
+
@client.handle_message(:body => "So we\'re going to eat tacos for lunch 4 days in a row")
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'can be a hash with an array' do
|
39
|
+
@response.should_receive(:call)
|
40
|
+
@client.register_handler(:message, :body => ["Taqueria del Sol", "Taco Bell", "Del Taco"]) { @response.call }
|
41
|
+
|
42
|
+
@client.handle_message(:body => "Taqueria del Sol")
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'can be a hash with a lambda' do
|
46
|
+
@response.should_receive(:call)
|
47
|
+
@client.register_handler(:message, :body => lambda { |body| body.length == 30 }) { @response.call }
|
48
|
+
|
49
|
+
@client.handle_message(:body => "What\'s the blue plate special?")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/dsl_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Flint::DSL do
|
4
|
+
before do
|
5
|
+
@client = Flint::Client.new
|
6
|
+
@dsl = Class.new { include Flint::DSL }.new
|
7
|
+
Flint::Client.stub!(:new).and_return(@client)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'wraps the setup' do
|
11
|
+
args = ['token', 'account', 'room']
|
12
|
+
@client.should_receive(:setup).with(*args)
|
13
|
+
@dsl.setup(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'provides a helper for ready state' do
|
17
|
+
@client.should_receive(:register_handler).with(:ready, nil)
|
18
|
+
@dsl.ready
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'sets up handlers' do
|
22
|
+
guard = {:from => 'taco@lover.com', :body => 'I love tacos!'}
|
23
|
+
@client.should_receive(:register_handler).with(:message, guard)
|
24
|
+
@dsl.message(guard)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'provides a "say" helper' do
|
28
|
+
message = "Can we get some cheese dip over here?"
|
29
|
+
@client.should_receive(:say).with(message)
|
30
|
+
@dsl.say(message)
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flint
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- vanstee
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: em-http-request
|
16
|
+
requirement: &70100277458420 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70100277458420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: active_support
|
27
|
+
requirement: &70100277458000 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70100277458000
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: json
|
38
|
+
requirement: &70100277457400 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70100277457400
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: &70100277456720 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70100277456720
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rake
|
60
|
+
requirement: &70100277456300 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70100277456300
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: &70100277445440 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70100277445440
|
80
|
+
description: Flint is a simple Campfire client heavily inspired by the blather gem
|
81
|
+
email:
|
82
|
+
- vanstee@highgroove.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- Rakefile
|
90
|
+
- flint.gemspec
|
91
|
+
- lib/flint.rb
|
92
|
+
- lib/flint/client.rb
|
93
|
+
- lib/flint/dsl.rb
|
94
|
+
- lib/flint/version.rb
|
95
|
+
- spec/client_spec.rb
|
96
|
+
- spec/dsl_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -3766259367202404661
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: -3766259367202404661
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project: flint
|
124
|
+
rubygems_version: 1.8.6
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Flint is a simple Campfire client heavily inspired by the blather gem
|
128
|
+
test_files:
|
129
|
+
- spec/client_spec.rb
|
130
|
+
- spec/dsl_spec.rb
|
131
|
+
- spec/spec_helper.rb
|