gopher2000 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +27 -0
  4. data/LICENSE.txt +14 -0
  5. data/README.markdown +344 -0
  6. data/Rakefile +38 -0
  7. data/bin/gopher2000 +51 -0
  8. data/examples/default_route.rb +22 -0
  9. data/examples/nyan.rb +62 -0
  10. data/examples/simple.rb +147 -0
  11. data/examples/twitter.rb +61 -0
  12. data/examples/weather.rb +69 -0
  13. data/gopher2000.gemspec +35 -0
  14. data/lib/gopher2000/base.rb +552 -0
  15. data/lib/gopher2000/dispatcher.rb +81 -0
  16. data/lib/gopher2000/dsl.rb +128 -0
  17. data/lib/gopher2000/errors.rb +14 -0
  18. data/lib/gopher2000/handlers/base_handler.rb +18 -0
  19. data/lib/gopher2000/handlers/directory_handler.rb +125 -0
  20. data/lib/gopher2000/rendering/abstract_renderer.rb +10 -0
  21. data/lib/gopher2000/rendering/base.rb +174 -0
  22. data/lib/gopher2000/rendering/menu.rb +129 -0
  23. data/lib/gopher2000/rendering/text.rb +10 -0
  24. data/lib/gopher2000/request.rb +21 -0
  25. data/lib/gopher2000/response.rb +25 -0
  26. data/lib/gopher2000/server.rb +85 -0
  27. data/lib/gopher2000/version.rb +4 -0
  28. data/lib/gopher2000.rb +33 -0
  29. data/scripts/god.rb +8 -0
  30. data/spec/application_spec.rb +54 -0
  31. data/spec/dispatching_spec.rb +144 -0
  32. data/spec/dsl_spec.rb +116 -0
  33. data/spec/gopher_spec.rb +1 -0
  34. data/spec/handlers/directory_handler_spec.rb +116 -0
  35. data/spec/helpers_spec.rb +16 -0
  36. data/spec/rendering/base_spec.rb +59 -0
  37. data/spec/rendering/menu_spec.rb +109 -0
  38. data/spec/rendering_spec.rb +84 -0
  39. data/spec/request_spec.rb +30 -0
  40. data/spec/response_spec.rb +33 -0
  41. data/spec/routing_spec.rb +92 -0
  42. data/spec/sandbox/old/socks.txt +0 -0
  43. data/spec/sandbox/socks.txt +0 -0
  44. data/spec/server_spec.rb +127 -0
  45. data/spec/spec_helper.rb +52 -0
  46. data/specs.watchr +60 -0
  47. metadata +211 -0
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #
5
+ # Simple gopher example
6
+ #
7
+
8
+ require 'gopher2000'
9
+
10
+ set :host, '0.0.0.0'
11
+ set :port, 7070
12
+
13
+ # you can specify a destination for access log, for stats/etc
14
+ set :access_log, "/tmp/access.log"
15
+
16
+ route '/gopher' do
17
+ "Greetings from Gopher 2000!" # You can output any text you want here
18
+ end
19
+
20
+ route '/prettytext' do
21
+ render :prettytext
22
+ end
23
+
24
+ #
25
+ # special text output rendering
26
+ #
27
+ text :prettytext do
28
+ @text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ullamcorper dictum euismod. Sed accumsan sem id quam rutrum eu hendrerit quam adipiscing. Fusce consequat accumsan eros, ac elementum eros molestie et. Vestibulum aliquet varius nulla nec rhoncus. Vivamus felis ipsum, commodo sit amet eleifend eu, lacinia et est. Nullam dolor sapien, luctus eu rhoncus non, ullamcorper vitae nibh. Proin viverra luctus dapibus. Integer aliquam, ante id consectetur vulputate, nibh sapien aliquet nisl, in porttitor massa elit a lectus. Maecenas nec diam nec nulla fringilla feugiat. Nulla facilisi. Proin odio libero, viverra at blandit eget, mattis id dui. Aliquam sed leo leo. Pellentesque eros ante, viverra in accumsan sit amet, pellentesque nec massa."
29
+
30
+ # nicely wrapped text
31
+ block @text
32
+
33
+ # spacing
34
+ br(2)
35
+
36
+ # smaller line-width
37
+ block @text, 30
38
+ end
39
+
40
+
41
+
42
+ #
43
+ # main route
44
+ #
45
+ route '/' do
46
+ render :index
47
+ end
48
+
49
+ #
50
+ # main index for the server
51
+ #
52
+ menu :index do
53
+ # output a text entry in the menu
54
+ text 'simple gopher example'
55
+
56
+ # use br(x) to add x space between lines
57
+ br(2)
58
+
59
+ # link somewhere
60
+ link 'current time', '/time'
61
+ br
62
+
63
+ # another link
64
+ link 'about', '/about'
65
+ br
66
+
67
+ # ask for some input
68
+ input 'Hey, what is your name?', '/hello'
69
+ br
70
+
71
+ # ask for some input
72
+ input 'echo test', '/echo_test'
73
+ br
74
+
75
+ # mount some files
76
+ menu 'filez', '/files'
77
+ end
78
+
79
+ #
80
+ # mounting a directory
81
+ #
82
+ mount '/files' => '/home/username/files', :filter => '*.jpg'
83
+
84
+
85
+ #
86
+ # actions have access to the request object, which has the following attributes:
87
+ #
88
+ # input: The input string, if provided (for searches, etc)
89
+ # selector: The path of the request being made
90
+ # ip_address: The remote IP address
91
+ #
92
+ route '/echo_test' do
93
+ render :echo, request
94
+ end
95
+
96
+ #
97
+ # output the results of a search request
98
+ #
99
+ menu :echo do |req|
100
+ text "#{req.selector} - #{req.ip_address} - #{req.input}"
101
+ end
102
+
103
+ route '/time' do
104
+ "It is currently #{Time.now}"
105
+ end
106
+
107
+ route '/about' do
108
+ "Gopher 2000 -- World Domination via Text Protocols"
109
+ end
110
+
111
+ #
112
+ # requests can have variables specified on the URL
113
+ #
114
+ route '/request/:x/:y' do
115
+ render :request_with_params
116
+ end
117
+
118
+ #menu :request_with_params do |params|
119
+ # text params.inspect
120
+ #end
121
+
122
+ #
123
+ # both the incoming params, and the request object are available when rendering
124
+ #
125
+ menu :request_with_params do
126
+ text params.inspect
127
+ text request.inspect
128
+ end
129
+
130
+ route '/hello' do
131
+ render :hello, request.input
132
+ end
133
+ menu :hello do |name|
134
+ text "Hello, #{name}!"
135
+ end
136
+
137
+ route "/junk" do
138
+ garble
139
+ end
140
+
141
+ route "/slow" do
142
+ "i am not fast"
143
+ end
144
+
145
+ helpers do
146
+ def garble; "hhdhd"; end
147
+ end
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ #
5
+ # Output twitter feed via gopher. Has two output methods -- it can
6
+ # output as text or as a gopher directory.
7
+ #
8
+
9
+ require 'open-uri'
10
+ require 'json'
11
+ require 'gopher2000'
12
+
13
+ set :host, '0.0.0.0'
14
+ set :port, 7070
15
+
16
+ route '/' do
17
+ @username = "mitchc2"
18
+ @url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=#{@username}"
19
+
20
+ data = open(@url).read
21
+
22
+ # to see the output as a gopher directory, try:
23
+ render :twitter_as_menu, @username, data
24
+
25
+ # to see text output, try:
26
+ #render :twitter, @username, data
27
+ end
28
+
29
+ text :twitter do |username, data|
30
+ big_header "The Tweets of #{username}"
31
+ JSON.parse(data).each do |t|
32
+ timestamp = Time.parse(t['created_at'])
33
+
34
+ block format_tweet(t)
35
+ end
36
+ # this stray break is a bit of a hack to ensure that this block returns
37
+ # the results of rendering, rather than the #each loop above
38
+ br
39
+ end
40
+
41
+ menu :twitter_as_menu do |username, data|
42
+ big_header "The Tweets of #{username}"
43
+ JSON.parse(data).each do |t|
44
+ timestamp = Time.parse(t['created_at'])
45
+
46
+ block format_tweet(t)
47
+ end
48
+ # this stray break is a bit of a hack to ensure that this block returns
49
+ # the results of rendering, rather than the #each loop above
50
+ br
51
+ end
52
+
53
+ helpers do
54
+ #
55
+ # simple helper for formatting a tweet
56
+ #
57
+ def format_tweet(t)
58
+ timestamp = Time.parse(t['created_at'])
59
+ "#{timestamp.strftime('%D %r')}: #{t['text']}"
60
+ end
61
+ end
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # gopher app for retrieving the weather
5
+ #
6
+
7
+ require 'weather-underground'
8
+ require 'gopher'
9
+
10
+
11
+ set :host, '0.0.0.0'
12
+ set :port, 7070
13
+
14
+ #
15
+ # main route
16
+ #
17
+ route '/' do
18
+ render :index
19
+ end
20
+
21
+
22
+ #
23
+ # main index for the server
24
+ #
25
+ menu :index do
26
+ # output a text entry in the menu
27
+ text "Let's check the weather!"
28
+
29
+ # use br(x) to add x space between lines
30
+ br(2)
31
+
32
+ # ask for some input
33
+ input 'Please enter your zipcode', '/lookup'
34
+
35
+ end
36
+
37
+
38
+ #
39
+ # actions have access to the request object, and can grab the following data:
40
+ #
41
+ # input: The input string, if provided (for searches, etc)
42
+ # selector: The path of the request being made
43
+ # ip_address: The remote IP address
44
+ #
45
+ # location = request.input.strip
46
+
47
+ route '/lookup' do
48
+ # location = request.input.strip
49
+ location = "01351"
50
+ w = WeatherUnderground::Base.new
51
+ f = w.TextForecast(location)
52
+
53
+ render :forecast, f, location
54
+ end
55
+
56
+ #
57
+ # output the results of a search request
58
+ #
59
+ menu :forecast do |f, location|
60
+ br
61
+ text "** FORECAST FOR #{location} **"
62
+ br
63
+ f.days.each do |day|
64
+ block "#{day.title}: #{day.text}", 70
65
+ br
66
+ end
67
+ br
68
+ text "** Powered by Gopher 2000 **"
69
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gopher2000/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gopher2000"
7
+ s.version = Gopher::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Colin Mitchell"]
10
+ s.email = ["colin@muffinlabs.com"]
11
+ s.homepage = "https://github.com/muffinista/gopher2000"
12
+ s.summary = %q{Gopher2000 - A Gopher server for the next millenium}
13
+ s.description = %q{Gopher2000 is a ruby-based Gopher server. It is built for speedy, enjoyable development of all sorts of gopher sites.}
14
+
15
+ s.rubyforge_project = "gopher2000"
16
+ s.licenses = ["WTFPL"]
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
+
23
+ # specify any dependencies here; for example:
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "redcarpet"
26
+ s.add_development_dependency "yard"
27
+ s.add_development_dependency "shoulda"
28
+ s.add_development_dependency "rdoc"
29
+ s.add_development_dependency "simplecov"
30
+ s.add_development_dependency "watchr"
31
+ s.add_development_dependency "eventmachine"
32
+
33
+ s.add_runtime_dependency "eventmachine"
34
+ s.add_runtime_dependency "logger"
35
+ end