postbin 0.1.0 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +6 -0
- data/README.md +18 -4
- data/Rakefile +1 -0
- data/bin/postbin +9 -31
- data/lib/postbin.rb +6 -5
- data/lib/postbin/post.rb +2 -2
- data/lib/postbin/server.rb +34 -5
- data/lib/postbin/storage.rb +2 -0
- data/lib/postbin/version.rb +1 -1
- data/postbin.gemspec +2 -1
- data/test/post_test.rb +1 -1
- data/test/server_test.rb +3 -3
- data/test/storage_test.rb +12 -7
- data/test/test_helper.rb +17 -8
- data/test/version_test.rb +1 -1
- metadata +32 -21
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -8,8 +8,11 @@ Quick Start
|
|
8
8
|
|
9
9
|
$ gem install postbin
|
10
10
|
$ postbin
|
11
|
-
== PostBin
|
12
|
-
==
|
11
|
+
== Starting PostBin on http://127.0.0.1:6969
|
12
|
+
== Sinatra/1.3.1 has taken the stage on 6969 for development with backup from Thin
|
13
|
+
>> Thin web server (v1.2.11 codename Bat-Shit Crazy)
|
14
|
+
>> Maximum connections set to 1024
|
15
|
+
>> Listening on 127.0.0.1:6969, CTRL+C to stop
|
13
16
|
|
14
17
|
When running postbin from the command line, requests are stored in a temporary
|
15
18
|
file database, they will be lost once the server terminates.
|
@@ -21,13 +24,24 @@ You can run a more permeant install by running PostBin as a Rack application:
|
|
21
24
|
|
22
25
|
# example config.ru
|
23
26
|
require 'postbin'
|
27
|
+
|
24
28
|
# path of pstore file to use for storage.
|
25
29
|
pstore = File.expand_path(File.join(File.dirname(__FILE__), 'postbin.pstore'))
|
30
|
+
|
26
31
|
# start the server.
|
32
|
+
PostBin::Server.set :pstore_file, pstore
|
27
33
|
run PostBin::Server.new(pstore)
|
28
34
|
|
29
35
|
Command Line Options
|
30
36
|
--------------------
|
31
37
|
|
32
|
-
|
33
|
-
|
38
|
+
Usage: postbin [options]
|
39
|
+
|
40
|
+
PostBin options:
|
41
|
+
-v, --version show version number
|
42
|
+
-h, --help show this message
|
43
|
+
|
44
|
+
Rack options:
|
45
|
+
-s, --server SERVER server (webrick, mongrel, thin, etc.)
|
46
|
+
-a, --address HOST listen on HOST address (default: 127.0.0.1)
|
47
|
+
-p, --port PORT use PORT number (default: 6969)
|
data/Rakefile
CHANGED
data/bin/postbin
CHANGED
@@ -1,35 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
path = ::File.dirname(::File.expand_path(__FILE__))
|
9
|
-
$LOAD_PATH.unshift path + '/../lib' if File.directory?(path) && !$LOAD_PATH.include?(path)
|
10
|
-
require 'postbin'
|
11
|
-
end
|
3
|
+
# DEVELOPMENT NOTE
|
4
|
+
# To run this executiable using the local code add ./lib/ to your ruby load path:
|
5
|
+
#
|
6
|
+
# ruby -Ilib bin/postbin
|
7
|
+
#
|
12
8
|
|
13
|
-
|
14
|
-
require '
|
9
|
+
require 'rubygems' unless defined?(Gem)
|
10
|
+
require 'postbin'
|
15
11
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
# available options.
|
20
|
-
opts = OptionParser.new('', 24, ' ') do |opts|
|
21
|
-
opts.banner = 'Usage: postbin [options]'
|
22
|
-
opts.separator ''
|
23
|
-
opts.separator 'PostBin options:'
|
24
|
-
opts.on('-v', '--version', 'show version number') { puts 'PostBin ' + PostBin::Version; exit }
|
25
|
-
opts.on('-h', '--help', 'show this message') { puts opts; exit; }
|
26
|
-
opts.separator ''
|
27
|
-
opts.separator 'Rack options:'
|
28
|
-
opts.on('-s', '--server SERVER', 'server (webrick, mongrel, thin, etc.)') { |s| options[:server] = s }
|
29
|
-
opts.on('-a', '--address HOST', 'listen on HOST address (default: 127.0.0.1)') { |host| options[:bind] = host }
|
30
|
-
opts.on('-p', '--port PORT', 'use PORT number (default: 6969)') { |port| options[:port] = port }
|
31
|
-
end.parse!
|
32
|
-
|
33
|
-
# --- start sinatra server ---------------------------------------------------
|
34
|
-
puts "== Starting PostBin on http://#{options[:bind]}:#{options[:port]}"
|
35
|
-
PostBin::Server.run!(options)
|
12
|
+
# run command line instance of application
|
13
|
+
PostBin::Server.run_command_line!(ARGV)
|
data/lib/postbin.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
require 'optparse'
|
1
2
|
require 'pstore'
|
2
|
-
require '
|
3
|
+
require 'multi_json'
|
3
4
|
require 'sinatra/base'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
require 'postbin/post'
|
7
|
+
require 'postbin/server'
|
8
|
+
require 'postbin/storage'
|
9
|
+
require 'postbin/version'
|
data/lib/postbin/post.rb
CHANGED
@@ -18,8 +18,8 @@ module PostBin
|
|
18
18
|
body == other.body
|
19
19
|
end
|
20
20
|
|
21
|
-
def to_json(*
|
22
|
-
{ received_at
|
21
|
+
def to_json(*options)
|
22
|
+
MultiJson.encode({ :received_at => received_at, :headers => headers, :body => body })
|
23
23
|
end
|
24
24
|
|
25
25
|
end
|
data/lib/postbin/server.rb
CHANGED
@@ -8,7 +8,7 @@ module PostBin
|
|
8
8
|
set :static, true
|
9
9
|
|
10
10
|
# by default we store in a temp file.
|
11
|
-
set :pstore_file, Tempfile.new(['postbin', 'pstore'])
|
11
|
+
set :pstore_file, Tempfile.new(['postbin', 'pstore']).path
|
12
12
|
|
13
13
|
# Redirect lost users.
|
14
14
|
get '/?' do; redirect '/postbin/overview'; end
|
@@ -34,7 +34,7 @@ module PostBin
|
|
34
34
|
storage = PostBin::Storage.new(settings.pstore_file)
|
35
35
|
|
36
36
|
content_type :json
|
37
|
-
storage.hits
|
37
|
+
MultiJson.encode(storage.hits)
|
38
38
|
end
|
39
39
|
|
40
40
|
# Display posts for the given URL as JSON.
|
@@ -43,7 +43,7 @@ module PostBin
|
|
43
43
|
storage = PostBin::Storage.new(settings.pstore_file)
|
44
44
|
|
45
45
|
content_type :json
|
46
|
-
storage.posts(url)
|
46
|
+
MultiJson.encode(storage.posts(url))
|
47
47
|
end
|
48
48
|
|
49
49
|
# Catch all for post requests.
|
@@ -54,14 +54,43 @@ module PostBin
|
|
54
54
|
status 201
|
55
55
|
end
|
56
56
|
|
57
|
+
# Runs a server on local machine, called by command line executable.
|
58
|
+
def self.run_command_line!(argv)
|
59
|
+
options = parse_args(argv)
|
60
|
+
$stderr.puts "== Starting PostBin on http://#{options[:bind]}:#{options[:port]}"
|
61
|
+
run!(options)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Parse command line args.
|
65
|
+
def self.parse_args(argv)
|
66
|
+
# default options.
|
67
|
+
options = { :bind => '127.0.0.1', :port => 6969, :server => 'thin', :enviroment => :production }
|
68
|
+
|
69
|
+
# available options.
|
70
|
+
opts = OptionParser.new('', 24, ' ') do |opts|
|
71
|
+
opts.banner = 'Usage: postbin [options]'
|
72
|
+
opts.separator ''
|
73
|
+
opts.separator 'PostBin options:'
|
74
|
+
opts.on('-v', '--version', 'show version number') { $stderr.puts 'PostBin ' + PostBin::Version; exit }
|
75
|
+
opts.on('-h', '--help', 'show this message') { $stderr.puts opts; exit; }
|
76
|
+
opts.separator ''
|
77
|
+
opts.separator 'Rack options:'
|
78
|
+
opts.on('-s', '--server SERVER', 'server (webrick, mongrel, thin, etc.)') { |s| options[:server] = s }
|
79
|
+
opts.on('-a', '--address HOST', 'listen on HOST address (default: 127.0.0.1)') { |host| options[:bind] = host }
|
80
|
+
opts.on('-p', '--port PORT', 'use PORT number (default: 6969)') { |port| options[:port] = port }
|
81
|
+
end.parse!(argv)
|
82
|
+
|
83
|
+
options
|
84
|
+
end
|
85
|
+
|
57
86
|
private
|
58
87
|
# Returns an array of all client side HTTP request headers.
|
59
88
|
def client_request_headers
|
60
89
|
# POST /some/url HTTP/1.1
|
61
90
|
# Accept: application/json
|
62
91
|
# Content-Type: application/json
|
63
|
-
headers = request.env.select { |k,v| k.start_with? 'HTTP_' }
|
64
|
-
.collect { |pair| [ pair[0].sub(/^HTTP_/, ''), pair[1] ] }
|
92
|
+
headers = request.env.select { |k,v| k.start_with? 'HTTP_' } \
|
93
|
+
.collect { |pair| [ pair[0].sub(/^HTTP_/, ''), pair[1] ] } \
|
65
94
|
.collect { |pair| pair.join(': ') }
|
66
95
|
|
67
96
|
headers
|
data/lib/postbin/storage.rb
CHANGED
@@ -6,6 +6,7 @@ module PostBin
|
|
6
6
|
def initialize(pstore_file)
|
7
7
|
# setup file database for storage.
|
8
8
|
@db = PStore.new(pstore_file)
|
9
|
+
@db.ultra_safe = true if @db.respond_to?(:ultra_safe)
|
9
10
|
end
|
10
11
|
|
11
12
|
# Store a post in the database.
|
@@ -20,6 +21,7 @@ module PostBin
|
|
20
21
|
# store post.
|
21
22
|
@db[url] << post
|
22
23
|
end
|
24
|
+
true
|
23
25
|
end
|
24
26
|
|
25
27
|
# Returns hash, key being url and value being number of posts received.
|
data/lib/postbin/version.rb
CHANGED
data/postbin.gemspec
CHANGED
@@ -17,10 +17,11 @@ spec = Gem::Specification.new do |s|
|
|
17
17
|
s.default_executable = 'postbin'
|
18
18
|
|
19
19
|
s.add_dependency('sinatra')
|
20
|
+
s.add_dependency('multi_json')
|
21
|
+
s.add_dependency('json')
|
20
22
|
s.add_development_dependency('rake')
|
21
23
|
s.add_development_dependency('yard')
|
22
24
|
s.add_development_dependency('minitest')
|
23
|
-
s.add_development_dependency('simplecov')
|
24
25
|
s.add_development_dependency('rack-test')
|
25
26
|
s.add_development_dependency('bundler')
|
26
27
|
|
data/test/post_test.rb
CHANGED
data/test/server_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestServer < MiniTest::Unit::TestCase
|
4
4
|
def app
|
5
5
|
PostBin::Server.new
|
6
6
|
# use a new pstore file for each request.
|
7
|
-
PostBin::Server.set :pstore_file, Tempfile.new(['postbin', 'pstore'])
|
7
|
+
PostBin::Server.set :pstore_file, Tempfile.new(['postbin', 'pstore']).path
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_roots_should_redirect_to_overview
|
@@ -54,7 +54,7 @@ class TestServer < MiniTest::Unit::TestCase
|
|
54
54
|
|
55
55
|
# read it back.
|
56
56
|
get '/postbin/posts/cats'
|
57
|
-
post =
|
57
|
+
post = MultiJson.decode(last_response.body)[0]
|
58
58
|
assert_equal 'they miaow', post['body']
|
59
59
|
end
|
60
60
|
|
data/test/storage_test.rb
CHANGED
@@ -1,24 +1,29 @@
|
|
1
|
-
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class TestStorage < MiniTest::Unit::TestCase
|
4
4
|
|
5
|
+
def setup
|
6
|
+
test_storage_tempfile = Tempfile.new(['postbin', 'pstore'])
|
7
|
+
@storage_path = test_storage_tempfile.path
|
8
|
+
end
|
9
|
+
|
5
10
|
def test_new_instance
|
6
|
-
assert PostBin::Storage.new(
|
11
|
+
assert PostBin::Storage.new(@storage_path)
|
7
12
|
end
|
8
13
|
|
9
14
|
def test_all_urls_returns_empty_hash_by_default
|
10
|
-
storage = PostBin::Storage.new(
|
15
|
+
storage = PostBin::Storage.new(@storage_path)
|
11
16
|
assert_empty storage.urls, 'should return empty array by default'
|
12
17
|
end
|
13
18
|
|
14
19
|
def test_can_store_post
|
15
|
-
storage = PostBin::Storage.new(
|
20
|
+
storage = PostBin::Storage.new(@storage_path)
|
16
21
|
post = PostBin::Post.new('1', '2')
|
17
22
|
assert storage.store('/test/url', post)
|
18
23
|
end
|
19
24
|
|
20
25
|
def test_can_get_all_urls
|
21
|
-
storage = PostBin::Storage.new(
|
26
|
+
storage = PostBin::Storage.new(@storage_path)
|
22
27
|
post = PostBin::Post.new('1', '2')
|
23
28
|
storage.store('/foobar', post)
|
24
29
|
|
@@ -27,7 +32,7 @@ class TestStorage < MiniTest::Unit::TestCase
|
|
27
32
|
end
|
28
33
|
|
29
34
|
def test_keeps_count_of_posts_to_same_url
|
30
|
-
storage = PostBin::Storage.new(
|
35
|
+
storage = PostBin::Storage.new(@storage_path)
|
31
36
|
post = PostBin::Post.new('1', '2')
|
32
37
|
storage.store('/foobar', post)
|
33
38
|
storage.store('/foobar', post)
|
@@ -39,7 +44,7 @@ class TestStorage < MiniTest::Unit::TestCase
|
|
39
44
|
end
|
40
45
|
|
41
46
|
def test_can_retrieve_saved_post_data
|
42
|
-
storage = PostBin::Storage.new(
|
47
|
+
storage = PostBin::Storage.new(@storage_path)
|
43
48
|
post = PostBin::Post.new('4', '2')
|
44
49
|
storage.store('/retrieve', post)
|
45
50
|
|
data/test/test_helper.rb
CHANGED
@@ -1,24 +1,33 @@
|
|
1
1
|
#
|
2
2
|
# Test helper init script.
|
3
3
|
#
|
4
|
-
require 'simplecov'
|
5
4
|
|
6
|
-
#
|
7
|
-
|
8
|
-
|
5
|
+
# --- load rubygems if needed ------------------------------------------------
|
6
|
+
require 'rubygems' unless defined?(Gem)
|
7
|
+
|
8
|
+
# --- load simplecov if the gem is installed ---------------------------------
|
9
|
+
begin
|
10
|
+
require 'simplecov'
|
11
|
+
|
12
|
+
# code coverage groups.
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter 'test/'
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
# dont run code coverage if gem isn't installed.
|
9
18
|
end
|
10
19
|
|
11
|
-
# load our dependencies using bundler
|
20
|
+
# --- load our dependencies using bundler ------------------------------------
|
12
21
|
require 'bundler/setup'
|
13
22
|
Bundler.setup
|
14
23
|
require 'minitest/autorun'
|
15
24
|
require 'minitest/pride'
|
16
25
|
require 'rack/test'
|
17
26
|
|
18
|
-
# load postbin lib
|
19
|
-
|
27
|
+
# --- load postbin lib -------------------------------------------------------
|
28
|
+
require 'postbin'
|
20
29
|
|
21
|
-
# extend main TestCase
|
30
|
+
# --- extend main TestCase ---------------------------------------------------
|
22
31
|
class MiniTest::Unit::TestCase
|
23
32
|
include Rack::Test::Methods
|
24
33
|
end
|
data/test/version_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postbin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
16
|
-
requirement: &
|
16
|
+
requirement: &2166202940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,21 +21,32 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2166202940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: multi_json
|
27
|
+
requirement: &2166202180 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0'
|
33
|
-
type: :
|
33
|
+
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2166202180
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
37
|
+
name: json
|
38
|
+
requirement: &2166201760 !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: *2166201760
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &2166201340 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *2166201340
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
59
|
+
name: yard
|
60
|
+
requirement: &2166200900 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *2166200900
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement: &
|
70
|
+
name: minitest
|
71
|
+
requirement: &2166200440 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,10 +76,10 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *2166200440
|
69
80
|
- !ruby/object:Gem::Dependency
|
70
81
|
name: rack-test
|
71
|
-
requirement: &
|
82
|
+
requirement: &2166200000 !ruby/object:Gem::Requirement
|
72
83
|
none: false
|
73
84
|
requirements:
|
74
85
|
- - ! '>='
|
@@ -76,10 +87,10 @@ dependencies:
|
|
76
87
|
version: '0'
|
77
88
|
type: :development
|
78
89
|
prerelease: false
|
79
|
-
version_requirements: *
|
90
|
+
version_requirements: *2166200000
|
80
91
|
- !ruby/object:Gem::Dependency
|
81
92
|
name: bundler
|
82
|
-
requirement: &
|
93
|
+
requirement: &2166199580 !ruby/object:Gem::Requirement
|
83
94
|
none: false
|
84
95
|
requirements:
|
85
96
|
- - ! '>='
|
@@ -87,7 +98,7 @@ dependencies:
|
|
87
98
|
version: '0'
|
88
99
|
type: :development
|
89
100
|
prerelease: false
|
90
|
-
version_requirements: *
|
101
|
+
version_requirements: *2166199580
|
91
102
|
description: ! " A ruby command line/rack application for testing systems that send
|
92
103
|
WebHooks\n using a HTTP POST request. Requests can be viewed via a simple web interface.\n"
|
93
104
|
email:
|