convore 0.0.0

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,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in convore.gemspec
4
+ gemspec
@@ -0,0 +1,42 @@
1
+ Convore API
2
+ ===========
3
+
4
+ Quick implementation of the Convore.com API in ruby.
5
+
6
+ Usage
7
+ -----
8
+
9
+ require 'convore'
10
+
11
+ @client = Convore::Client.new
12
+ @client.username = 'user'
13
+ @client.password = 'pass'
14
+
15
+ # listen will fork a thread that listens to the long polling /api/live.json
16
+ # the poll function will check if the thread exited (i.e. a response was received)
17
+ # process the response and relaunch the thread
18
+
19
+ @client.listen
20
+
21
+ while true do
22
+ @client.poll # optionally takes a float for how long to wait for the poll thread to exit, defaults to 0.05
23
+
24
+ while m = @client.stream.pop do
25
+ case m
26
+ when Convore::Topic then
27
+ puts "#{m.group.name}: Topic '#{m.name}' created by #{m.user.username}"
28
+ when Convore::Message then
29
+ puts "#{m.group.name}: [##{m.topic.name}] <#{m.user.username}> #{m.text}"
30
+ when Convore::Star then
31
+ puts "* #{m.user.username} has #{m.star? ? 'stared' : 'unstared'} '#{m.message.text}' by #{m.message.user.username}"
32
+ else
33
+ puts "UNKNOWN MESSAGE: #{m.inspect}"
34
+ end
35
+ end
36
+
37
+ sleep(1)
38
+ end
39
+
40
+ All models are quick hacks - Group and Topic have #find, which fetches a given ID via the Rest API.
41
+
42
+ I wrote this to implement it in an rbot plugin ..watch out for that, coming soon (or maybe not)
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "convore/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "convore"
7
+ s.version = Convore::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Marian Rudzynski"]
10
+ s.email = ["mr@impaled.org"]
11
+ s.homepage = ""
12
+ s.summary = %q{A hasty convore.com API implementation in ruby}
13
+ s.description = %q{}
14
+
15
+ # s.rubyforge_project = "convore"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,17 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module Convore
5
+ end
6
+
7
+ require 'convore/version'
8
+
9
+ require 'convore/api'
10
+ require 'convore/base'
11
+ require 'convore/user'
12
+ require 'convore/group'
13
+ require 'convore/topic'
14
+ require 'convore/message'
15
+ require 'convore/star'
16
+ require 'convore/client'
17
+
@@ -0,0 +1,22 @@
1
+ # TODO: Establish link to client connect, to use their username/password?
2
+
3
+ module Convore
4
+ class API
5
+ class << self
6
+ def cache
7
+ @@cache ||= {}
8
+ end
9
+
10
+ def get(api_uri, username=nil, password=nil)
11
+ response = cache[api_uri] ? cache[api_uri] :
12
+ Net::HTTP.start('convore.com', 443, nil, nil, nil, nil, {:use_ssl => true}) {|http|
13
+ req = Net::HTTP::Get.new(api_uri)
14
+ req.basic_auth(username, password) if username && password
15
+ req.set_content_type('application/json')
16
+ http.request(req)
17
+ }
18
+ cache[api_uri] = response
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,66 @@
1
+ module Convore
2
+ class Base
3
+ attr_accessor :attributes
4
+
5
+ def initialize
6
+ @attributes ||= {}
7
+ end
8
+
9
+ def method_missing(method, *args)
10
+ if method.match(/\=/)
11
+ @attributes[method.to_s.gsub(/\=$/, '').to_sym] = args.first
12
+ return true
13
+ else
14
+ return @attributes[method] if @attributes[method]
15
+ end
16
+ raise NoMethodError.new("no such method: #{method}")
17
+ end
18
+
19
+ class << self
20
+
21
+ def get_class(key)
22
+ case key
23
+ when 'message' then Message
24
+ when 'user', 'creator', 'mentioned_user' then User
25
+ when 'group' then Group
26
+ when 'topic' then Topic
27
+ end
28
+ end
29
+
30
+ def from_json(json)
31
+ return nil if !json
32
+
33
+ object = new
34
+ json.each {|key, val|
35
+ key_class = get_class(key)
36
+
37
+ value = if val.is_a?(Hash) && key_class
38
+ key_class.from_json(json[key])
39
+ elsif val.is_a?(Numeric) && key_class && key_class.api
40
+ key_class.find(val)
41
+ else
42
+ val
43
+ end
44
+
45
+ object.send("#{key}=", value)
46
+ }
47
+ object
48
+ end
49
+
50
+ def api
51
+ nil
52
+ end
53
+
54
+ def find(rid)
55
+ return false if !api || !rid.is_a?(Numeric)
56
+
57
+ api_uri = "/api/#{api[-1] == 's' ? api : "#{api}s"}/#{rid}.json"
58
+
59
+ response = Convore::API.get(api_uri)
60
+
61
+ json = JSON.parse(response.body)
62
+ from_json(json[api])
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,54 @@
1
+ require 'yaml'
2
+ module Convore
3
+ class Client
4
+ attr_accessor :thread, :stream, :cursor, :username, :password
5
+
6
+ def initialize
7
+ @stream ||= []
8
+ @ts ||= Time.now.to_f
9
+ end
10
+
11
+ def listen
12
+ raise Exception.new("username and password need to be set to listen to /live") if !username || !password
13
+
14
+ @thread = Thread.fork {
15
+ Net::HTTP.start('convore.com', 443, nil, nil, nil, nil, {:use_ssl => true}) {|http|
16
+ req = Net::HTTP::Get.new("/api/live.json?cursor=#{@cursor if @cursor}")
17
+ req.basic_auth(username, password)
18
+ req.set_content_type('application/json')
19
+ response = http.request(req)
20
+
21
+ Thread.current[:response] = response.body
22
+ }
23
+ }
24
+ end
25
+
26
+ def process_response(response)
27
+ json = JSON.parse(response)
28
+
29
+ if json['messages']
30
+ json['messages'].each {|msg|
31
+ if msg['_ts'] && @ts < msg['_ts']
32
+ case msg['kind']
33
+ when 'message' then
34
+ stream.unshift(Message.from_json(msg))
35
+ when 'topic' then
36
+ stream.unshift(Topic.from_json(msg))
37
+ when 'star', 'unstar' then
38
+ stream.unshift(Star.from_json(msg))
39
+ end
40
+ @ts = msg['_ts'] if msg['_ts']
41
+ @cursor = msg['_id'] if msg['_id']
42
+ end
43
+ }
44
+ end
45
+ end
46
+
47
+ def poll(wait = 0.05)
48
+ if t = @thread.join(wait)
49
+ listen unless @thread.alive?
50
+ process_response(t[:response])
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,7 @@
1
+ module Convore
2
+ class Group < Base
3
+ def self.api
4
+ 'group'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Convore
2
+ class Mention < Base
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module Convore
2
+ class Message < Base
3
+ def text
4
+ message
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Convore
2
+ class Star < Base
3
+ class << self
4
+ def from_json(json)
5
+ object = new
6
+ object.user = User.from_json(json['user'])
7
+ object.message = Message.from_json(json['star']['message'])
8
+ object.star = json['kind'] == 'star' ? true : false
9
+ object
10
+ end
11
+ end
12
+
13
+ def star?
14
+ star
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module Convore
2
+ class Topic < Base
3
+ def self.api
4
+ 'topic'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module Convore
2
+ class User < Base
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Convore
2
+ VERSION = "0.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: convore
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Marian Rudzynski
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-15 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: ""
22
+ email:
23
+ - mr@impaled.org
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.md
34
+ - Rakefile
35
+ - convore.gemspec
36
+ - lib/convore.rb
37
+ - lib/convore/api.rb
38
+ - lib/convore/base.rb
39
+ - lib/convore/client.rb
40
+ - lib/convore/group.rb
41
+ - lib/convore/mention.rb
42
+ - lib/convore/message.rb
43
+ - lib/convore/star.rb
44
+ - lib/convore/topic.rb
45
+ - lib/convore/user.rb
46
+ - lib/convore/version.rb
47
+ has_rdoc: true
48
+ homepage: ""
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A hasty convore.com API implementation in ruby
79
+ test_files: []
80
+