trumpet-trumpet 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/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +50 -0
- data/VERSION.yml +4 -0
- data/lib/core_extensions.rb +153 -0
- data/lib/http.rb +68 -0
- data/lib/trumpet/channel.rb +40 -0
- data/lib/trumpet/exceptions.rb +29 -0
- data/lib/trumpet/listener.rb +37 -0
- data/lib/trumpet/message.rb +34 -0
- data/lib/trumpet/receiver.rb +51 -0
- data/lib/trumpet/sieve.rb +102 -0
- data/lib/trumpet/transmitter.rb +43 -0
- data/lib/trumpet/trumpet_resource.rb +7 -0
- data/lib/trumpet/user.rb +20 -0
- data/lib/trumpet.rb +29 -0
- data/spec/channel_spec.rb +5 -0
- data/spec/spec_helper.rb +11 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Michael Taras
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "trumpet"
|
8
|
+
gem.summary = "The official trumpet gem"
|
9
|
+
gem.email = "wtf@trumpet.io"
|
10
|
+
gem.homepage = "http://github.com/trumpet/trumpet"
|
11
|
+
gem.authors = ["Michael Taras"]
|
12
|
+
gem.add_dependency 'resourceful'
|
13
|
+
gem.add_dependency 'json'
|
14
|
+
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
if File.exist?('VERSION.yml')
|
39
|
+
config = YAML.load(File.read('VERSION.yml'))
|
40
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "trumpet #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
50
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
# Copyright (c) 2008 Sam Smoot.
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
class Object #:nodoc:
|
25
|
+
# @return <TrueClass, FalseClass>
|
26
|
+
#
|
27
|
+
# @example [].blank? #=> true
|
28
|
+
# @example [1].blank? #=> false
|
29
|
+
# @example [nil].blank? #=> false
|
30
|
+
#
|
31
|
+
# Returns true if the object is nil or empty (if applicable)
|
32
|
+
def blank?
|
33
|
+
nil? || (respond_to?(:empty?) && empty?)
|
34
|
+
end unless method_defined?(:blank?)
|
35
|
+
end # class Object
|
36
|
+
|
37
|
+
class Numeric #:nodoc:
|
38
|
+
# @return <TrueClass, FalseClass>
|
39
|
+
#
|
40
|
+
# Numerics can't be blank
|
41
|
+
def blank?
|
42
|
+
false
|
43
|
+
end unless method_defined?(:blank?)
|
44
|
+
end # class Numeric
|
45
|
+
|
46
|
+
class NilClass #:nodoc:
|
47
|
+
# @return <TrueClass, FalseClass>
|
48
|
+
#
|
49
|
+
# Nils are always blank
|
50
|
+
def blank?
|
51
|
+
true
|
52
|
+
end unless method_defined?(:blank?)
|
53
|
+
end # class NilClass
|
54
|
+
|
55
|
+
class TrueClass #:nodoc:
|
56
|
+
# @return <TrueClass, FalseClass>
|
57
|
+
#
|
58
|
+
# True is not blank.
|
59
|
+
def blank?
|
60
|
+
false
|
61
|
+
end unless method_defined?(:blank?)
|
62
|
+
end # class TrueClass
|
63
|
+
|
64
|
+
class FalseClass #:nodoc:
|
65
|
+
# False is always blank.
|
66
|
+
def blank?
|
67
|
+
true
|
68
|
+
end unless method_defined?(:blank?)
|
69
|
+
end # class FalseClass
|
70
|
+
|
71
|
+
class String #:nodoc:
|
72
|
+
# @example "".blank? #=> true
|
73
|
+
# @example " ".blank? #=> true
|
74
|
+
# @example " hey ho ".blank? #=> false
|
75
|
+
#
|
76
|
+
# @return <TrueClass, FalseClass>
|
77
|
+
#
|
78
|
+
# Strips out whitespace then tests if the string is empty.
|
79
|
+
def blank?
|
80
|
+
strip.empty?
|
81
|
+
end unless method_defined?(:blank?)
|
82
|
+
|
83
|
+
def snake_case
|
84
|
+
return self.downcase if self =~ /^[A-Z]+$/
|
85
|
+
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
|
86
|
+
return $+.downcase
|
87
|
+
end unless method_defined?(:snake_case)
|
88
|
+
end # class String
|
89
|
+
|
90
|
+
class Hash #:nodoc:
|
91
|
+
# @return <String> This hash as a query string
|
92
|
+
#
|
93
|
+
# @example
|
94
|
+
# { :name => "Bob",
|
95
|
+
# :address => {
|
96
|
+
# :street => '111 Ruby Ave.',
|
97
|
+
# :city => 'Ruby Central',
|
98
|
+
# :phones => ['111-111-1111', '222-222-2222']
|
99
|
+
# }
|
100
|
+
# }.to_params
|
101
|
+
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
|
102
|
+
def to_params
|
103
|
+
params = self.map { |k,v| normalize_param(k,v) }.join
|
104
|
+
params.chop! # trailing &
|
105
|
+
params
|
106
|
+
end
|
107
|
+
|
108
|
+
# @param key<Object> The key for the param.
|
109
|
+
# @param value<Object> The value for the param.
|
110
|
+
#
|
111
|
+
# @return <String> This key value pair as a param
|
112
|
+
#
|
113
|
+
# @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
|
114
|
+
def normalize_param(key, value)
|
115
|
+
param = ''
|
116
|
+
stack = []
|
117
|
+
|
118
|
+
if value.is_a?(Array)
|
119
|
+
param << value.map { |element| normalize_param("#{key}[]", element) }.join
|
120
|
+
elsif value.is_a?(Hash)
|
121
|
+
stack << [key,value]
|
122
|
+
else
|
123
|
+
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
|
124
|
+
end
|
125
|
+
|
126
|
+
stack.each do |parent, hash|
|
127
|
+
hash.each do |key, value|
|
128
|
+
if value.is_a?(Hash)
|
129
|
+
stack << ["#{parent}[#{key}]", value]
|
130
|
+
else
|
131
|
+
param << normalize_param("#{parent}[#{key}]", value)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
param
|
137
|
+
end
|
138
|
+
|
139
|
+
# @return <String> The hash as attributes for an XML tag.
|
140
|
+
#
|
141
|
+
# @example
|
142
|
+
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
|
143
|
+
# #=> 'one="1" two="TWO"'
|
144
|
+
def to_xml_attributes
|
145
|
+
map do |k,v|
|
146
|
+
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
|
147
|
+
end.join(' ')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class BlankSlate #:nodoc:
|
152
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
|
153
|
+
end
|
data/lib/http.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module HTTP
|
2
|
+
@@http = Resourceful::HttpAccessor.new
|
3
|
+
@@uri = 'http://api.trumpet.io:3000'
|
4
|
+
|
5
|
+
def self.get(path, options={})
|
6
|
+
do_request(:get, path, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.post(path, options={})
|
10
|
+
do_request(:post, path, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.put(path, options={})
|
14
|
+
do_request(:put, path, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.delete(path, options={})
|
18
|
+
do_request(:delete, path, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def self.do_request(method, path, options)
|
24
|
+
resource = @@http.resource("#{@@uri}#{URI.encode(path)}")
|
25
|
+
begin
|
26
|
+
response = if method == :post
|
27
|
+
resource.send(method, options[:parameters].to_params, :content_type => "application/x-www-form-urlencoded")
|
28
|
+
else
|
29
|
+
options[:parameters] ? resource.send(method, options[:parameters].to_params) : resource.send(method)
|
30
|
+
end
|
31
|
+
rescue Resourceful::UnsuccessfulHttpRequestError => e
|
32
|
+
response = e.http_response
|
33
|
+
error_string = JSON.parse(response.body).to_s
|
34
|
+
|
35
|
+
case response.code
|
36
|
+
when 400
|
37
|
+
raise Trumpet::BadRequest, error_string
|
38
|
+
when 403
|
39
|
+
raise Trumpet::Forbidden, error_string
|
40
|
+
when 404
|
41
|
+
raise Trumpet::NotFound, error_string
|
42
|
+
when 405
|
43
|
+
raise Trumpet::MethodNotAllowed, error_string
|
44
|
+
when 500
|
45
|
+
raise Trumpet::InternalServerError, error_string
|
46
|
+
when 501
|
47
|
+
raise Trumpet::NotImplemented, error_string
|
48
|
+
end
|
49
|
+
rescue IOError # Coudln't connect to server
|
50
|
+
raise Trumpet::ServerConnectionError, 'Could not connect to server'
|
51
|
+
end
|
52
|
+
|
53
|
+
(options[:parse_response] == false) ? response : JSON.parse(response.body)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.handle_response(response, parse_response)
|
57
|
+
case response.code
|
58
|
+
when 400
|
59
|
+
# TODO: what to do here?
|
60
|
+
when 404
|
61
|
+
raise Trumpet::ResourceNotFound JSON.parse(response.body)
|
62
|
+
when 500
|
63
|
+
# TODO: what to do here?
|
64
|
+
end
|
65
|
+
|
66
|
+
parse_response ? JSON.parse(response.body) : response
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Channel
|
3
|
+
@@attributes = [
|
4
|
+
:name,
|
5
|
+
:created_at,
|
6
|
+
:updated_at
|
7
|
+
]
|
8
|
+
|
9
|
+
attr_reader *@@attributes
|
10
|
+
|
11
|
+
def self.create(options)
|
12
|
+
Channel.new(HTTP.post('/channels', :parameters => options))
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find(name)
|
16
|
+
Channel.new(HTTP.post("/channels/#{name}"))
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.all
|
20
|
+
HTTP::get('/channels').map { |attributes| Channel.new(attributes) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def broadcast(message)
|
24
|
+
Message.new(HTTP.post("/channels/#{@name}/messages", :parameters => message.to_h))
|
25
|
+
end
|
26
|
+
|
27
|
+
def messages
|
28
|
+
messages = HTTP.get("/channels/#{@name}/messages")
|
29
|
+
messages.map { |attributes| Message.new(attributes) }
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def initialize(attributes)
|
35
|
+
@@attributes.each do |attr|
|
36
|
+
self.instance_variable_set(:"@#{attr.to_s}", attributes[attr.to_s]) if attributes[attr.to_s]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# TODO: Add more exceptions here
|
2
|
+
|
3
|
+
module Trumpet
|
4
|
+
|
5
|
+
# Exception raised when the connection to the server fails
|
6
|
+
class ServerConnectionError < StandardError; end
|
7
|
+
|
8
|
+
# A generic exception to use until we have more specific exceptions
|
9
|
+
# for everything
|
10
|
+
class TrumpetError < StandardError; end
|
11
|
+
|
12
|
+
# HTTP 400 Error
|
13
|
+
class BadRequest < StandardError; end
|
14
|
+
|
15
|
+
# HTTP 403 Error
|
16
|
+
class Forbidden < StandardError; end
|
17
|
+
|
18
|
+
# HTTP 404 Error
|
19
|
+
class NotFound < StandardError; end
|
20
|
+
|
21
|
+
# HTTP 405 Error
|
22
|
+
class MethodNotAllowed < StandardError; end
|
23
|
+
|
24
|
+
# HTTP 500 Error
|
25
|
+
class InternalServerError < StandardError; end
|
26
|
+
|
27
|
+
# HTTP 501 Error
|
28
|
+
class NotImplemented < StandardError; end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Listener
|
3
|
+
@@attributes = [
|
4
|
+
:receiver_id,
|
5
|
+
:uri,
|
6
|
+
:start,
|
7
|
+
:end,
|
8
|
+
:days,
|
9
|
+
:created_at,
|
10
|
+
:updated_at,
|
11
|
+
]
|
12
|
+
|
13
|
+
attr_reader *@@attributes
|
14
|
+
|
15
|
+
|
16
|
+
def self.create(options)
|
17
|
+
Listener.new(HTTP.post('/listeners', :parameters => options))
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.find(id)
|
21
|
+
Listener.new(HTTP.get("/listeners/#{id}"))
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.delete
|
25
|
+
HTTP.delete("/listeners/#{@id}")
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def initialize(attributes)
|
32
|
+
@@attributes.each do |attr|
|
33
|
+
self.instance_variable_set(:"@#{attr.to_s}", attributes[attr.to_s]) if attributes[attr.to_s]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Message
|
3
|
+
@@attributes = [
|
4
|
+
:url , :creator_id,
|
5
|
+
:author , :author_name,
|
6
|
+
:avatar_url , :authored_at,
|
7
|
+
:title , :description,
|
8
|
+
:tags , :location_string,
|
9
|
+
:lat , :lng,
|
10
|
+
:radius , :channel,
|
11
|
+
:public , :authorized_receiver_owners,
|
12
|
+
:content , :created_at,
|
13
|
+
:updated_at , :id
|
14
|
+
]
|
15
|
+
|
16
|
+
#TODO: created_at and updated_at maybe not writable
|
17
|
+
|
18
|
+
|
19
|
+
attr_accessor *@@attributes
|
20
|
+
|
21
|
+
def initialize(attributes)
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
@@attributes.each do |attr|
|
26
|
+
self.send "#{attr.to_s}=".to_sym, (attributes[attr] || attributes[attr.to_s])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_h
|
31
|
+
@@attributes.inject({}) { |hash, attribute| hash[attribute] = self.send(attribute); hash }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Receiver
|
3
|
+
include TrumpetResource
|
4
|
+
|
5
|
+
@@attributes = [
|
6
|
+
:id , :url,
|
7
|
+
:source_url , :creator_id ,
|
8
|
+
:author , :author_name ,
|
9
|
+
:avatar_url , :authored_at ,
|
10
|
+
:title , :description ,
|
11
|
+
:tags , :location_string ,
|
12
|
+
:lat , :lng ,
|
13
|
+
:radius , :channels ,
|
14
|
+
:keywords , :name ,
|
15
|
+
:created_at , :updated_at ,
|
16
|
+
:errors
|
17
|
+
]
|
18
|
+
attr_reader *@@attributes
|
19
|
+
|
20
|
+
|
21
|
+
def self.create(options={})
|
22
|
+
Receiver.new(HTTP.post("/receivers", :parameters => options))
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.find(id)
|
26
|
+
Receiver.new(HTTP.get("/receivers/#{id}"))
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete
|
30
|
+
HTTP.delete("/receivers/#{@id}")
|
31
|
+
end
|
32
|
+
|
33
|
+
def messages
|
34
|
+
messages = HTTP.get("/receivers/#{@id}/messages")
|
35
|
+
messages.map { |attributes| Message.new(attributes) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def listen
|
39
|
+
#TODO: impliment this
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def initialize(attributes)
|
46
|
+
@@attributes.each do |attr|
|
47
|
+
self.instance_variable_set(:"@#{attr.to_s}", attributes[attr.to_s]) if attributes[attr.to_s]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Sieve
|
3
|
+
include HTTParty
|
4
|
+
base_uri 'devour.cc:3000'
|
5
|
+
|
6
|
+
@@options_attributes = [
|
7
|
+
:keywords , :location ,
|
8
|
+
:via , :to ,
|
9
|
+
:days , :start ,
|
10
|
+
:end , :expires ,
|
11
|
+
:foreign_id
|
12
|
+
]
|
13
|
+
|
14
|
+
attr_accessor :id, :foreign_id, :name, :type, :created_at, :options
|
15
|
+
|
16
|
+
@@options_attributes.each do |attribute|
|
17
|
+
define_method "#{attribute}=" do |value|
|
18
|
+
@options[attribute] = value
|
19
|
+
end
|
20
|
+
define_method attribute do
|
21
|
+
@options[attribute]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def initialize(options={})
|
27
|
+
options.reject! { |key, value| value.nil? || (value.is_a?(String) && value.strip.empty?) }
|
28
|
+
|
29
|
+
@options = {}
|
30
|
+
@@options_attributes.each do |attribute|
|
31
|
+
value = options[attribute] || options[attribute.to_s]
|
32
|
+
@options[attribute] = value if value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def pull
|
37
|
+
add_to_server
|
38
|
+
|
39
|
+
self.class.get("/sieves/#{id}").inject([]) do |memo, attributes|
|
40
|
+
memo << Nugget.new(attributes)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def subscribe
|
45
|
+
wait_time ||= 2 # seconds
|
46
|
+
|
47
|
+
while true
|
48
|
+
results = pull
|
49
|
+
results.each { |nugget| yield nugget }
|
50
|
+
wait_time = results.nil? ? wait_time*2 : [wait_time/2, 2].max
|
51
|
+
sleep(wait_time)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete
|
56
|
+
self.class.delete("/sieves/#{@id}")
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_to_server
|
60
|
+
unless @id
|
61
|
+
attributes = self.class.post("/sieves", :query => options)
|
62
|
+
@id = attributes['id']
|
63
|
+
@type = attributes['type']
|
64
|
+
@name = attributes['name']
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.index
|
69
|
+
get("/sieves")
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.find(id)
|
73
|
+
attributes = get("/sieves/?id=#{id}").first
|
74
|
+
instantiate(attributes)
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.find_by_foreign_id(foreign_id)
|
78
|
+
sieve_attributes = get("/sieves/?foreign_id=#{foreign_id}")
|
79
|
+
sieve_attributes.collect { |attributes| instantiate(attributes) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.create(options={})
|
83
|
+
sieve = new(options)
|
84
|
+
sieve.add_to_server
|
85
|
+
sieve
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def self.instantiate(attributes)
|
92
|
+
sieve = allocate
|
93
|
+
|
94
|
+
sieve.id = attributes['id']
|
95
|
+
sieve.options = attributes['options']
|
96
|
+
sieve.type = attributes['type']
|
97
|
+
sieve.name = attributes['name']
|
98
|
+
|
99
|
+
sieve
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class Transmitter
|
3
|
+
@@attributes = [
|
4
|
+
:transmitter_id,
|
5
|
+
:source_url,
|
6
|
+
:tags,
|
7
|
+
:location_string,
|
8
|
+
:channel,
|
9
|
+
:lat,
|
10
|
+
:lng,
|
11
|
+
:radius,
|
12
|
+
:public,
|
13
|
+
:created_at,
|
14
|
+
:updated_at,
|
15
|
+
]
|
16
|
+
|
17
|
+
attr_reader *@@attributes
|
18
|
+
|
19
|
+
def self.create(options)
|
20
|
+
attributes = HTTP::post('transmitters', :query => options)
|
21
|
+
Transmitter.new(attributes)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.find(id)
|
25
|
+
attributes = get("/transmitters/#{id}")
|
26
|
+
Transmitter.new(attributes)
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete
|
30
|
+
self.class.delete('/')
|
31
|
+
end
|
32
|
+
|
33
|
+
def listeners
|
34
|
+
listeners = self.class.get("/transmitters/#{id}/listeners")
|
35
|
+
listeners.map { |attributes| Listener.new(attributes) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def broadcast(message)
|
39
|
+
self.class.post("/transmitters/#{@transmitter_id}/messages", :query => message.to_h)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
data/lib/trumpet/user.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Trumpet
|
2
|
+
class User
|
3
|
+
@@attributes = [:name]
|
4
|
+
attr_reader *@@attributes
|
5
|
+
|
6
|
+
def self.create(options)
|
7
|
+
attributes = post('/users', options)
|
8
|
+
User.new(attributes)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find(name)
|
12
|
+
attributes = get("/users/#{name}")
|
13
|
+
User.new(attributes) if attributes["name"]
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete
|
17
|
+
self.class.delete("/users/#{@name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/trumpet.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'resourceful'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
require 'core_extensions'
|
8
|
+
require 'http'
|
9
|
+
|
10
|
+
|
11
|
+
module Trumpet
|
12
|
+
include HTTP
|
13
|
+
|
14
|
+
def authenticate(name, token)
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def authenticated?
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'trumpet/trumpet_resource'
|
24
|
+
require 'trumpet/exceptions'
|
25
|
+
require 'trumpet/message'
|
26
|
+
require 'trumpet/receiver'
|
27
|
+
require 'trumpet/listener'
|
28
|
+
require 'trumpet/transmitter'
|
29
|
+
require 'trumpet/channel'
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trumpet-trumpet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Taras
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-30 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: resourceful
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: wtf@trumpet.io
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- LICENSE
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- VERSION.yml
|
49
|
+
- lib/core_extensions.rb
|
50
|
+
- lib/http.rb
|
51
|
+
- lib/trumpet.rb
|
52
|
+
- lib/trumpet/channel.rb
|
53
|
+
- lib/trumpet/exceptions.rb
|
54
|
+
- lib/trumpet/listener.rb
|
55
|
+
- lib/trumpet/message.rb
|
56
|
+
- lib/trumpet/receiver.rb
|
57
|
+
- lib/trumpet/sieve.rb
|
58
|
+
- lib/trumpet/transmitter.rb
|
59
|
+
- lib/trumpet/trumpet_resource.rb
|
60
|
+
- lib/trumpet/user.rb
|
61
|
+
- spec/channel_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/trumpet/trumpet
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.2.0
|
86
|
+
signing_key:
|
87
|
+
specification_version: 2
|
88
|
+
summary: The official trumpet gem
|
89
|
+
test_files:
|
90
|
+
- spec/channel_spec.rb
|
91
|
+
- spec/spec_helper.rb
|