sparks 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 ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # Sparks
2
+ ### a tiny campfire library
3
+
4
+ So it turns out that Tinder requires nine gems nowadays, including EventMachine and the Twitter streaming API client gem, and that is too much for me. So, (strongly inspired by Aaron Patterson's A Bot), I wrote this extremely tiny Campfire client API. Share and enjoy.
5
+
6
+ ### Known Issues
7
+
8
+ The `watch` method tends to succumb to network issues after a few hours, and is probably not suitable for building a long-running bot that listens to a room. This library is fantastic for anything that says things into a room based on other ruby code, though.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module Sparks
2
+ VERSION = "0.0.1"
3
+ end
data/lib/sparks.rb ADDED
@@ -0,0 +1,119 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/https'
4
+
5
+ # sparks, a tiny Campfire library
6
+
7
+ # Usage:
8
+ # c = Sparks::Campfire.new('subdomain', 'abc123')
9
+ # r = c.room_named "Room Name"
10
+ # r.say "hi there"
11
+ # r.paste "class Foo\nend"
12
+
13
+ # Authors:
14
+ # Andre Arko <andre@arko.net>
15
+ # Aaron Patterson <aaron.patterson@gmail.com>
16
+
17
+ module Sparks
18
+ class Room
19
+ attr_accessor :id
20
+
21
+ def initialize api, name, id
22
+ @api = api
23
+ @name = name
24
+ @id = id
25
+ end
26
+
27
+ def method_missing method, *args, &block
28
+ if @api.respond_to? method
29
+ args.unshift(@id)
30
+ @api.send method, *args, &block
31
+ end
32
+ end
33
+ end
34
+
35
+ class Campfire
36
+ attr_reader :uri, :token, :pass
37
+
38
+ def initialize subdomain, token
39
+ @uri = URI.parse("https://#{subdomain}.campfirenow.com")
40
+ @token = token
41
+ @pass = 'x'
42
+
43
+ @http = Net::HTTP.new(uri.host, uri.port)
44
+ @http.use_ssl = true
45
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
46
+ end
47
+
48
+ def room_named name
49
+ r = rooms.find{|r| r["name"] == name }
50
+ r ? Room.new(self, name, r["id"]) : nil
51
+ end
52
+
53
+ def rooms
54
+ @http.start do |http|
55
+ req = Net::HTTP::Get.new "/rooms.json"
56
+ req['Content-Type'] = 'application/json'
57
+ req.basic_auth token, pass
58
+ begin
59
+ JSON.parse(http.request(req).body)["rooms"]
60
+ rescue JSON::ParserError
61
+ {}
62
+ end
63
+ end
64
+ end
65
+
66
+ def post room_id, message, type = nil
67
+ data = {'body' => message}
68
+ data.merge!('type' => type) if type
69
+ json = JSON.generate('message' => data)
70
+
71
+ @http.start do |http|
72
+ req = Net::HTTP::Post.new "/room/#{room_id}/speak.json"
73
+ req['Content-Type'] = 'application/json'
74
+ req.basic_auth token, pass
75
+ http.request(req, json)
76
+ end
77
+ end
78
+
79
+ def speak room_id, message
80
+ post room_id, message, 'TextMessage'
81
+ end
82
+ alias_method :say, :speak
83
+
84
+ def paste room_id, message
85
+ post room_id, message, 'PasteMessage'
86
+ end
87
+
88
+ def join room_id
89
+ @http.start do |http|
90
+ req = Net::HTTP::Post.new "/room/#{room_id}/join.xml"
91
+ req.basic_auth token, pass
92
+ http.request(req)
93
+ end
94
+ end
95
+
96
+ def watch room_id
97
+ uri = URI.parse('https://streaming.campfirenow.com')
98
+
99
+ x = Net::HTTP.new(uri.host, uri.port)
100
+ x.use_ssl = true
101
+ x.verify_mode = OpenSSL::SSL::VERIFY_NONE
102
+
103
+ x.start do |http|
104
+ req = Net::HTTP::Get.new "/room/#{room_id}/live.json"
105
+ req.basic_auth token, pass
106
+ http.request(req) do |res|
107
+ res.read_body do |chunk|
108
+ unless chunk.strip.empty?
109
+ chunk.split("\r").each do |message|
110
+ yield JSON.parse(message)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ end
119
+ end
data/sparks.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sparks/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sparks"
7
+ s.version = Sparks::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["André Arko"]
10
+ s.email = ["andre@arko.net"]
11
+ s.homepage = "http://github.com/indirect/sparks"
12
+ s.summary = %q{A tiny Campfire client API}
13
+ s.description = %q{A tiny Campfire client API that only uses the standard library}
14
+
15
+ s.rubyforge_project = "sparks"
16
+ s.add_development_dependency "bundler", "~>1.0"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sparks
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Andr\xC3\xA9 Arko"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-15 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 1
32
+ - 0
33
+ version: "1.0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: A tiny Campfire client API that only uses the standard library
37
+ email:
38
+ - andre@arko.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - README.md
48
+ - Rakefile
49
+ - lib/sparks.rb
50
+ - lib/sparks/version.rb
51
+ - sparks.gemspec
52
+ has_rdoc: true
53
+ homepage: http://github.com/indirect/sparks
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project: sparks
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A tiny Campfire client API
86
+ test_files: []
87
+