kenhirakawa-astrotrain 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +26 -0
  2. data/LICENSE +20 -0
  3. data/README +47 -0
  4. data/Rakefile +124 -0
  5. data/VERSION +1 -0
  6. data/astrotrain.gemspec +141 -0
  7. data/config/sample.rb +12 -0
  8. data/lib/astrotrain.rb +56 -0
  9. data/lib/astrotrain/api.rb +52 -0
  10. data/lib/astrotrain/logged_mail.rb +48 -0
  11. data/lib/astrotrain/mapping.rb +162 -0
  12. data/lib/astrotrain/mapping/http_post.rb +18 -0
  13. data/lib/astrotrain/mapping/jabber.rb +28 -0
  14. data/lib/astrotrain/mapping/transport.rb +55 -0
  15. data/lib/astrotrain/message.rb +342 -0
  16. data/lib/astrotrain/tmail.rb +58 -0
  17. data/lib/astrotrain/worker.rb +65 -0
  18. data/lib/vendor/rest-client/README.rdoc +104 -0
  19. data/lib/vendor/rest-client/Rakefile +84 -0
  20. data/lib/vendor/rest-client/bin/restclient +65 -0
  21. data/lib/vendor/rest-client/foo.diff +66 -0
  22. data/lib/vendor/rest-client/lib/rest_client.rb +188 -0
  23. data/lib/vendor/rest-client/lib/rest_client/net_http_ext.rb +23 -0
  24. data/lib/vendor/rest-client/lib/rest_client/payload.rb +185 -0
  25. data/lib/vendor/rest-client/lib/rest_client/request_errors.rb +75 -0
  26. data/lib/vendor/rest-client/lib/rest_client/resource.rb +103 -0
  27. data/lib/vendor/rest-client/rest-client.gemspec +18 -0
  28. data/lib/vendor/rest-client/spec/base.rb +5 -0
  29. data/lib/vendor/rest-client/spec/master_shake.jpg +0 -0
  30. data/lib/vendor/rest-client/spec/payload_spec.rb +71 -0
  31. data/lib/vendor/rest-client/spec/request_errors_spec.rb +44 -0
  32. data/lib/vendor/rest-client/spec/resource_spec.rb +52 -0
  33. data/lib/vendor/rest-client/spec/rest_client_spec.rb +219 -0
  34. data/test/api_test.rb +32 -0
  35. data/test/fixtures/apple_multipart.txt +100 -0
  36. data/test/fixtures/bad_content_type.txt +27 -0
  37. data/test/fixtures/basic.txt +14 -0
  38. data/test/fixtures/custom.txt +15 -0
  39. data/test/fixtures/fwd.txt +0 -0
  40. data/test/fixtures/gb2312_encoding.txt +16 -0
  41. data/test/fixtures/gb2312_encoding_invalid.txt +15 -0
  42. data/test/fixtures/html.txt +10 -0
  43. data/test/fixtures/html_multipart.txt +16 -0
  44. data/test/fixtures/iso-8859-1.txt +13 -0
  45. data/test/fixtures/mapped.txt +13 -0
  46. data/test/fixtures/multipart.txt +213 -0
  47. data/test/fixtures/multipart2.txt +213 -0
  48. data/test/fixtures/multiple.txt +13 -0
  49. data/test/fixtures/multiple_delivered_to.txt +14 -0
  50. data/test/fixtures/multiple_with_body_recipients.txt +15 -0
  51. data/test/fixtures/reply.txt +16 -0
  52. data/test/fixtures/undisclosed.txt +14 -0
  53. data/test/fixtures/utf-8.txt +13 -0
  54. data/test/logged_mail_test.rb +67 -0
  55. data/test/mapping_test.rb +129 -0
  56. data/test/message_test.rb +492 -0
  57. data/test/test_helper.rb +56 -0
  58. data/test/transport_test.rb +113 -0
  59. metadata +330 -0
@@ -0,0 +1,26 @@
1
+ .DS_Store
2
+ .rake_tasks
3
+ log/*
4
+ tmp/*
5
+ TAGS
6
+ *~
7
+ .#*
8
+ pkg
9
+ schema/schema.rb
10
+ schema/*_structure.sql
11
+ schema/*.sqlite3
12
+ schema/*.sqlite
13
+ schema/*.db
14
+ *.sqlite
15
+ *.sqlite3
16
+ *.db
17
+ src/*
18
+ .hgignore
19
+ .hg/*
20
+ .svn/*
21
+ doc
22
+ config/database.yml
23
+ merb/custom.rb
24
+ test/fixtures/queue
25
+ messages
26
+ test/messages
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-* ENTP
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 ADDED
@@ -0,0 +1,47 @@
1
+ astrotrain
2
+ ==========
3
+
4
+ NOTE: Astrotrain is a full gem now. If you're looking for the old Astrotrain on merb:
5
+ http://github.com/entp/astrotrain/tree/merb
6
+ git://github.com/entp/astrotrain.git (merb branch)
7
+
8
+ Scans incoming emails for mapped recipients and sends an HTTP POST somewhere.
9
+
10
+ # setup a config file.
11
+ # Point the queue_path at a directory that your mail server dumps each raw incoming mail.
12
+ require 'astrotrain'
13
+
14
+ Astrotrain.load path do
15
+ DataMapper.setup(:default, {
16
+ :adapter => "mysql",
17
+ :database => "astrotrain",
18
+ :username => "root",
19
+ :host => "localhost"
20
+ })
21
+ end
22
+ Astrotrain::Message.queue_path = "/path/to/maildir"
23
+ Astrotrain::Mapping::Transport.processing = true
24
+
25
+ # start up IRB
26
+ irb -I /var/astrotrain/lib -r config.rb
27
+
28
+ # manage mappings
29
+ LIB=/var/astrotrain/lib CONFIG=config.rb rake at:mappings
30
+ LIB=/var/astrotrain/lib CONFIG=config.rb rake at:map EMAIL=support@foo.com DEST=http://foo.com/email
31
+ LIB=/var/astrotrain/lib CONFIG=config.rb rake at:unmap MAP=123
32
+
33
+ # start the server that runs over the queue directory
34
+ LIB=/var/astrotrain/lib CONFIG=config.rb rake at:process
35
+
36
+ # start the sinatra API
37
+ ruby -I /var/astrotrain/lib /var/astrotrain/lib/astrotrain/api.rb config.rb
38
+
39
+ A single Astrotrain process currently handles email for two production applications, processing thousands daily.
40
+ It's far from perfect, but definitely usable.
41
+
42
+ TODO
43
+ ====
44
+
45
+ Docs
46
+ bounced emails (i've been rolling with http://github.com/whatcould/bounce-email for now)
47
+ Mail gem, TMail is old school
@@ -0,0 +1,124 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "astrotrain"
7
+ gem.summary = %Q{email => http post}
8
+ gem.email = "technoweenie@gmail.com"
9
+ gem.homepage = "http://github.com/entp/astrotrain"
10
+ gem.authors = ["technoweenie"]
11
+
12
+ dm_ver = "0.9.11"
13
+ gem.add_runtime_dependency 'addressable', '2.0.2'
14
+ gem.add_runtime_dependency "tmail", "1.2.7.1"
15
+ gem.add_runtime_dependency "data_objects", dm_ver
16
+ gem.add_runtime_dependency "dm-core", dm_ver # The datamapper ORM
17
+ gem.add_runtime_dependency "dm-aggregates", dm_ver # Provides your DM models with count, sum, avg, min, max, etc.
18
+ gem.add_runtime_dependency "dm-timestamps", dm_ver # Automatically populate created_at, created_on, etc. when those properties are present.
19
+ gem.add_runtime_dependency "dm-types", dm_ver # Provides additional types, including csv, json, yaml.
20
+ gem.add_runtime_dependency "dm-validations", dm_ver # Validation framework
21
+ gem.add_development_dependency "context"
22
+ gem.add_development_dependency "rr"
23
+ gem.add_development_dependency "sinatra", "~> 1.0.0"
24
+ gem.add_development_dependency "rack-test"
25
+ gem.add_development_dependency "xmppr4-simple"
26
+ end
27
+
28
+ rescue LoadError
29
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
30
+ end
31
+
32
+ namespace :at do
33
+ task :init do
34
+ $LOAD_PATH.unshift File.expand_path(ENV['LIB']) if ENV['LIB']
35
+ require ENV['CONFIG'] if ENV['CONFIG']
36
+
37
+ if !Object.const_defined?(:Astrotrain)
38
+ require 'astrotrain'
39
+ Astrotrain.load
40
+ end
41
+
42
+ Astrotrain::Message.queue_path = ENV['QUEUE'] if ENV['QUEUE']
43
+ if !File.exist?(Astrotrain::Message.queue_path)
44
+ puts "No Queue: #{Astrotrain.queue_path.inspect}"
45
+ exit
46
+ end
47
+ end
48
+
49
+ desc "List Mappings"
50
+ task :mappings => :init do
51
+ Astrotrain::Mapping.all.each do |map|
52
+ puts "##{map.id}: #{map.full_email} => #{map.destination}"
53
+ puts "Separated by: #{map.separator.inspect}" if !map.separator.blank?
54
+ puts
55
+ end
56
+ end
57
+
58
+ desc "Add mapping EMAIL=ticket*@foo.com DEST=http://foo.com/email [TRANS=(http_post|jabber)] [SEP=REPLYABOVEHERE] [HEADERS=delivered_to,to,original_to]"
59
+ task :map => :init do
60
+ map = Astrotrain::Mapping.new
61
+ map.email_user, map.email_domain = ENV['EMAIL'].to_s.split('@')
62
+ map.destination = ENV['DEST']
63
+ map.transport = ENV['TRANS'] if ENV['TRANS']
64
+ map.separator = ENV['SEP'] if ENV['SEP']
65
+ if map.save
66
+ puts "Mapping created for #{map.full_email} => #{map.destination}"
67
+ Rake::Task['at:mappings'].invoke
68
+ else
69
+ puts map.inspect
70
+ puts map.errors.inspect
71
+ end
72
+ end
73
+
74
+ desc "Remove mapping MAP=123"
75
+ task :unmap => :init do
76
+ map = Astrotrain::Mapping.get(ENV["MAP"])
77
+ map.destroy if map
78
+ Rake::Task['at:mappings'].invoke
79
+ end
80
+
81
+ desc "Start astrotrain DRb server."
82
+ task :process => :init do
83
+ require 'astrotrain/worker'
84
+ Astrotrain::Worker.start
85
+ end
86
+ end
87
+
88
+ require 'rake/testtask'
89
+ Rake::TestTask.new do |t|
90
+ t.test_files = FileList['test/*_test.rb']
91
+ t.verbose = true
92
+ end
93
+
94
+ begin
95
+ require 'rcov/rcovtask'
96
+ Rcov::RcovTask.new do |test|
97
+ test.libs << 'test'
98
+ test.pattern = 'test/**/*_test.rb'
99
+ test.verbose = true
100
+ end
101
+ rescue LoadError
102
+ task :rcov do
103
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
104
+ end
105
+ end
106
+
107
+ require 'rake/rdoctask'
108
+ Rake::RDocTask.new do |rdoc|
109
+ if File.exist?('VERSION.yml')
110
+ config = YAML.load(File.read('VERSION.yml'))
111
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
112
+ else
113
+ version = ""
114
+ end
115
+
116
+ rdoc.rdoc_dir = 'rdoc'
117
+ rdoc.title = "astrotrain #{version}"
118
+ rdoc.rdoc_files.include('README*')
119
+ rdoc.rdoc_files.include('lib/**/*.rb')
120
+ end
121
+
122
+
123
+ desc 'Default: run test examples'
124
+ task :default => 'test'
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.4
@@ -0,0 +1,141 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{kenhirakawa-astrotrain}
8
+ s.version = "0.5.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["technoweenie"]
12
+ s.date = %q{2010-04-01}
13
+ s.email = %q{technoweenie@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "astrotrain.gemspec",
25
+ "config/sample.rb",
26
+ "lib/astrotrain.rb",
27
+ "lib/astrotrain/api.rb",
28
+ "lib/astrotrain/logged_mail.rb",
29
+ "lib/astrotrain/mapping.rb",
30
+ "lib/astrotrain/mapping/http_post.rb",
31
+ "lib/astrotrain/mapping/jabber.rb",
32
+ "lib/astrotrain/mapping/transport.rb",
33
+ "lib/astrotrain/message.rb",
34
+ "lib/astrotrain/tmail.rb",
35
+ "lib/astrotrain/worker.rb",
36
+ "lib/vendor/rest-client/README.rdoc",
37
+ "lib/vendor/rest-client/Rakefile",
38
+ "lib/vendor/rest-client/bin/restclient",
39
+ "lib/vendor/rest-client/foo.diff",
40
+ "lib/vendor/rest-client/lib/rest_client.rb",
41
+ "lib/vendor/rest-client/lib/rest_client/net_http_ext.rb",
42
+ "lib/vendor/rest-client/lib/rest_client/payload.rb",
43
+ "lib/vendor/rest-client/lib/rest_client/request_errors.rb",
44
+ "lib/vendor/rest-client/lib/rest_client/resource.rb",
45
+ "lib/vendor/rest-client/rest-client.gemspec",
46
+ "lib/vendor/rest-client/spec/base.rb",
47
+ "lib/vendor/rest-client/spec/master_shake.jpg",
48
+ "lib/vendor/rest-client/spec/payload_spec.rb",
49
+ "lib/vendor/rest-client/spec/request_errors_spec.rb",
50
+ "lib/vendor/rest-client/spec/resource_spec.rb",
51
+ "lib/vendor/rest-client/spec/rest_client_spec.rb",
52
+ "test/api_test.rb",
53
+ "test/fixtures/apple_multipart.txt",
54
+ "test/fixtures/bad_content_type.txt",
55
+ "test/fixtures/basic.txt",
56
+ "test/fixtures/custom.txt",
57
+ "test/fixtures/fwd.txt",
58
+ "test/fixtures/gb2312_encoding.txt",
59
+ "test/fixtures/gb2312_encoding_invalid.txt",
60
+ "test/fixtures/html.txt",
61
+ "test/fixtures/html_multipart.txt",
62
+ "test/fixtures/iso-8859-1.txt",
63
+ "test/fixtures/mapped.txt",
64
+ "test/fixtures/multipart.txt",
65
+ "test/fixtures/multipart2.txt",
66
+ "test/fixtures/multiple.txt",
67
+ "test/fixtures/multiple_delivered_to.txt",
68
+ "test/fixtures/multiple_with_body_recipients.txt",
69
+ "test/fixtures/reply.txt",
70
+ "test/fixtures/undisclosed.txt",
71
+ "test/fixtures/utf-8.txt",
72
+ "test/logged_mail_test.rb",
73
+ "test/mapping_test.rb",
74
+ "test/message_test.rb",
75
+ "test/test_helper.rb",
76
+ "test/transport_test.rb"
77
+ ]
78
+ s.homepage = %q{http://github.com/entp/astrotrain}
79
+ s.rdoc_options = ["--charset=UTF-8"]
80
+ s.require_paths = ["lib"]
81
+ s.rubygems_version = %q{1.3.6}
82
+ s.summary = %q{email => http post}
83
+ s.test_files = [
84
+ "test/api_test.rb",
85
+ "test/logged_mail_test.rb",
86
+ "test/mapping_test.rb",
87
+ "test/message_test.rb",
88
+ "test/test_helper.rb",
89
+ "test/transport_test.rb"
90
+ ]
91
+
92
+ if s.respond_to? :specification_version then
93
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
94
+ s.specification_version = 3
95
+
96
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
97
+ s.add_runtime_dependency(%q<addressable>, ["= 2.0.2"])
98
+ s.add_runtime_dependency(%q<tmail>, ["= 1.2.7.1"])
99
+ s.add_runtime_dependency(%q<data_objects>, ["= 0.9.11"])
100
+ s.add_runtime_dependency(%q<dm-core>, ["= 0.9.11"])
101
+ s.add_runtime_dependency(%q<dm-aggregates>, ["= 0.9.11"])
102
+ s.add_runtime_dependency(%q<dm-timestamps>, ["= 0.9.11"])
103
+ s.add_runtime_dependency(%q<dm-types>, ["= 0.9.11"])
104
+ s.add_runtime_dependency(%q<dm-validations>, ["= 0.9.11"])
105
+ s.add_development_dependency(%q<context>, [">= 0"])
106
+ s.add_development_dependency(%q<rr>, [">= 0"])
107
+ s.add_development_dependency(%q<sinatra>, ["~> 1.0.0"])
108
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
109
+ s.add_development_dependency(%q<xmppr4-simple>, [">= 0"])
110
+ else
111
+ s.add_dependency(%q<addressable>, ["= 2.0.2"])
112
+ s.add_dependency(%q<tmail>, ["= 1.2.7.1"])
113
+ s.add_dependency(%q<data_objects>, ["= 0.9.11"])
114
+ s.add_dependency(%q<dm-core>, ["= 0.9.11"])
115
+ s.add_dependency(%q<dm-aggregates>, ["= 0.9.11"])
116
+ s.add_dependency(%q<dm-timestamps>, ["= 0.9.11"])
117
+ s.add_dependency(%q<dm-types>, ["= 0.9.11"])
118
+ s.add_dependency(%q<dm-validations>, ["= 0.9.11"])
119
+ s.add_dependency(%q<context>, [">= 0"])
120
+ s.add_dependency(%q<rr>, [">= 0"])
121
+ s.add_dependency(%q<sinatra>, ["~> 1.0.0"])
122
+ s.add_dependency(%q<rack-test>, [">= 0"])
123
+ s.add_dependency(%q<xmppr4-simple>, [">= 0"])
124
+ end
125
+ else
126
+ s.add_dependency(%q<addressable>, ["= 2.0.2"])
127
+ s.add_dependency(%q<tmail>, ["= 1.2.7.1"])
128
+ s.add_dependency(%q<data_objects>, ["= 0.9.11"])
129
+ s.add_dependency(%q<dm-core>, ["= 0.9.11"])
130
+ s.add_dependency(%q<dm-aggregates>, ["= 0.9.11"])
131
+ s.add_dependency(%q<dm-timestamps>, ["= 0.9.11"])
132
+ s.add_dependency(%q<dm-types>, ["= 0.9.11"])
133
+ s.add_dependency(%q<dm-validations>, ["= 0.9.11"])
134
+ s.add_dependency(%q<context>, [">= 0"])
135
+ s.add_dependency(%q<rr>, [">= 0"])
136
+ s.add_dependency(%q<sinatra>, ["~> 1.0.0"])
137
+ s.add_dependency(%q<rack-test>, [">= 0"])
138
+ s.add_dependency(%q<xmppr4-simple>, [">= 0"])
139
+ end
140
+ end
141
+
@@ -0,0 +1,12 @@
1
+ path = File.join(File.dirname(__FILE__), '..')
2
+ $LOAD_PATH.unshift File.join(path, 'lib')
3
+ require 'astrotrain'
4
+
5
+ Astrotrain.load path do
6
+ DataMapper.setup(:default, {
7
+ :adapter => "mysql",
8
+ :database => "astrotrain",
9
+ :username => "root",
10
+ :host => "localhost"
11
+ })
12
+ end
@@ -0,0 +1,56 @@
1
+ module Astrotrain
2
+ class ProcessingCancelled < StandardError; end
3
+
4
+ CALLBACK_TYPES = [:pre_mapping, :pre_processing, :post_processing]
5
+ class << self
6
+ attr_accessor :root, :lib_root, :callbacks
7
+ end
8
+
9
+ def self.load(root = Dir.pwd)
10
+ self.root = File.expand_path(root)
11
+ self.lib_root = File.expand_path(File.dirname(__FILE__))
12
+ load_dependencies
13
+ yield if block_given?
14
+ %w(tmail message mapping logged_mail mapping/transport mapping/http_post mapping/jabber).each do |lib|
15
+ require "astrotrain/#{lib}"
16
+ end
17
+ Astrotrain::Mail::ALLOW_MULTIPLE['delivered-to'] = true
18
+ end
19
+
20
+ def self.callback(name, *args, &block)
21
+ found = callbacks[name]
22
+ if block
23
+ found << block
24
+ else
25
+ found.each { |cback| cback.call(*args) }
26
+ end
27
+ found
28
+ end
29
+
30
+ def self.clear_callbacks
31
+ self.callbacks = CALLBACK_TYPES.inject({}) { |memo, ctype| memo.update(ctype => []) }
32
+ end
33
+
34
+ clear_callbacks
35
+
36
+ private
37
+ # help me ryan tomayko, you're my only help
38
+ def self.load_dependencies
39
+ require 'rubygems'
40
+ gem 'addressable', '2.0.2'
41
+
42
+ dm_ver = "0.9.11"
43
+ gem "data_objects", dm_ver
44
+ gem "dm-core", dm_ver # The datamapper ORM
45
+ gem "dm-aggregates", dm_ver # Provides your DM models with count, sum, avg, min, max, etc.
46
+ gem "dm-timestamps", dm_ver # Automatically populate created_at, created_on, etc. when those properties are present.
47
+ gem "dm-types", dm_ver # Provides additional types, including csv, json, yaml.
48
+ gem "dm-validations", dm_ver # Validation framework
49
+
50
+ $LOAD_PATH.unshift File.join(lib_root, 'vendor', 'rest-client', 'lib')
51
+
52
+ %w(dm-core dm-aggregates dm-timestamps dm-types dm-validations tmail rest_client).each do |lib|
53
+ require lib
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,52 @@
1
+ require 'sinatra'
2
+
3
+ before do
4
+ response['Content-Type'] = 'text/plain'
5
+ end
6
+
7
+ get '/queue_size' do
8
+ if File.exist?(Astrotrain::Message.queue_path)
9
+ (Dir.entries(Astrotrain::Message.queue_path).size - 2).to_s
10
+ else
11
+ '0'
12
+ end
13
+ end
14
+
15
+ get '/queue/*' do
16
+ path = params[:splat].first
17
+ path.gsub! /^\/+/, ''
18
+ file = File.join(Astrotrain::Message.queue_path, path)
19
+ if File.exist?(file)
20
+ IO.read(file)
21
+ else
22
+ halt 404, "#{path.inspect} was not found."
23
+ end
24
+ end
25
+
26
+ get '/queue' do
27
+ data = []
28
+ Dir.entries(Astrotrain::Message.queue_path).each do |e|
29
+ data << e unless e =~ /^\.{1,2}$/
30
+ end
31
+ data * "\n"
32
+ end
33
+
34
+ unless $testing
35
+ configure do
36
+ # the idea being, you pass the name of the config file
37
+ #
38
+ # # loads File.join(Dir.pwd, 'config.rb')
39
+ # CONFIG=config ruby lib/astrotrain/api.rb
40
+ #
41
+ # # loads File.join(Dir.pwd, 'config.rb')
42
+ # ruby lib/astrotrain/api.rb config
43
+ #
44
+ # That file contains the Greymalkin.load block that initializes Sequel
45
+ #
46
+ path = ENV['CONFIG'] || ARGV.shift
47
+ if path[0..0] != '/'
48
+ path = File.join(Dir.pwd, path)
49
+ end
50
+ require File.expand_path(path)
51
+ end
52
+ end