gopher2000 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: febb4c0d9036ca121d7ee16c85346ee7afa10ad7f09158d3b6c6c823d1256256
4
- data.tar.gz: abfa74d3dd3742bc383a176770e0c68d24ec9ec458a0b0806a288fee7f096480
3
+ metadata.gz: 599d0bfe771be1795d77d08bd7fa8ef1df0b5c8ef89a6270d344aa9ad206e5ab
4
+ data.tar.gz: 64ee23ab4b4f63351c9572e19a995c5ca3da96919a79703e47a2830b9ddf6fba
5
5
  SHA512:
6
- metadata.gz: 971a695d248ddad837c5e7ff17d36d70696729ea383842d09fa289e1d20516e75b7b5c2f40727bd45b28224b3c540af4e3c0e7bf90fb389522f616c792e7db84
7
- data.tar.gz: b0290447843496e4cf77e435f523804824be33728ba45cc236465737f574f2e443a2cd8796dc2e01170b311ea91687e6a882a8e5b19dfda8edfc043ecc6ee80d
6
+ metadata.gz: 7c16b9f80341276ad4f93aacbbf505d01b3aea5264da1e3119d43a10ea0037984b4d423ca51ff9f174a9af805daf8e079a71686296b9baee12607c0767706c59
7
+ data.tar.gz: 20adee6253ced84ab8d4a8c4c7413874b6f034a0b3ae12a86418336fa04acf16ca81ffc19fef41c6c4bdb28c348c7960a1214a4d9563274a80075a81d72fffae
@@ -59,7 +59,7 @@ menu :index do
59
59
  br(2)
60
60
 
61
61
  # link somewhere
62
- link 'current time', '/time'
62
+ text_link 'current time', '/time'
63
63
  br
64
64
  end
65
65
 
@@ -57,11 +57,11 @@ menu :index do
57
57
  br(2)
58
58
 
59
59
  # link somewhere
60
- link 'current time', '/time'
60
+ text_link 'current time', '/time'
61
61
  br
62
62
 
63
63
  # another link
64
- link 'about', '/about'
64
+ text_link 'about', '/about'
65
65
  br
66
66
 
67
67
  # ask for some input
@@ -169,12 +169,11 @@ module Gopher
169
169
  #
170
170
  def lookup(selector)
171
171
  unless routes.nil?
172
- routes.each do |pattern, keys, block|
173
-
172
+ routes.each do |pattern, keys, block|
174
173
  if match = pattern.match(selector)
175
174
  match = match.to_a
176
175
  url = match.shift
177
-
176
+
178
177
  params = to_params_hash(keys, match)
179
178
 
180
179
  #
@@ -208,6 +207,9 @@ module Gopher
208
207
  if ! @request.valid?
209
208
  response.body = handle_invalid_request
210
209
  response.code = :error
210
+ elsif @request.url?
211
+ response.body = handle_url(@request)
212
+ response.code = :success
211
213
  else
212
214
  begin
213
215
  debug_log("do lookup for #{@request.selector}")
@@ -354,6 +356,15 @@ module Gopher
354
356
  menus.include?(:error) ? :error : :'internal/error'
355
357
  end
356
358
 
359
+
360
+ #
361
+ # get the id of the template that will be used when rendering an html page
362
+ # @return name of error template
363
+ #
364
+ def url_template
365
+ menus.include?(:html) ? :html : :'internal/url'
366
+ end
367
+
357
368
  #
358
369
  # get the id of the template that will be used when rendering an
359
370
  # invalid request
@@ -438,20 +449,18 @@ module Gopher
438
449
 
439
450
  class << self
440
451
 
441
- #
442
- # Sanitizes a gopher selector
443
- #
444
- def sanitize_selector(raw)
445
- "/#{raw}".dup.
446
- strip. # Strip whitespace
447
- sub(/\/$/, ''). # Strip last rslash
448
- sub(/^\/*/, '/'). # Strip extra lslashes
449
- gsub(/\.+/, '.') # Don't want consecutive dots!
450
-
451
- #raw = "/#{raw}" if ! raw.match(/^\//)
452
- end
453
-
454
- #
452
+ #
453
+ # Sanitizes a gopher selector
454
+ #
455
+ def sanitize_selector(raw)
456
+ "/#{raw}".dup.
457
+ strip. # Strip whitespace
458
+ sub(/\/$/, ''). # Strip last rslash
459
+ sub(/^\/*/, '/'). # Strip extra lslashes
460
+ gsub(/\.+/, '.') # Don't want consecutive dots!
461
+ end
462
+
463
+ #
455
464
  # generate a method which we will use to run routes. this is
456
465
  # based on #generate_method as used by sinatra.
457
466
  # @see https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb
@@ -464,7 +473,7 @@ module Gopher
464
473
  method
465
474
  end
466
475
  end
467
-
476
+
468
477
  #
469
478
  # output a debugging message
470
479
  #
@@ -481,15 +490,37 @@ module Gopher
481
490
  #
482
491
  def register_defaults
483
492
  menu :'internal/not_found' do
484
- text "Sorry, #{@request.selector} was not found"
493
+ error "Sorry, #{@request.selector} was not found"
485
494
  end
486
495
 
487
496
  menu :'internal/error' do |details|
488
- text "Sorry, there was an error #{details}"
497
+ error "Sorry, there was an error #{details}"
489
498
  end
490
499
 
491
500
  menu :'internal/invalid_request' do
492
- text "invalid request"
501
+ error "invalid request"
502
+ end
503
+
504
+ menu :'internal/url' do
505
+ output = <<-EOHTML
506
+ <html>
507
+ <head>
508
+ <meta http-equiv="refresh" content="5;URL=#{@request.url}">
509
+ </head>
510
+ <body>
511
+ <p>
512
+ You are following a link from gopher to a website. If your browser supports it, you will be
513
+ automatically taken to the web site shortly. If you do not get
514
+ sent there, please click <a href="#{@request.url}">here</a>.
515
+ </p>
516
+ <p>
517
+ The URL linked is: <a href="#{@request.url}">#{@request.url}</a>.
518
+ </p>
519
+ <p>Have a nice day!</p>
520
+ </body>
521
+ </html>
522
+ EOHTML
523
+ output
493
524
  end
494
525
  end
495
526
 
@@ -497,6 +528,10 @@ module Gopher
497
528
  render not_found_template
498
529
  end
499
530
 
531
+ def handle_url(request)
532
+ render url_template, request
533
+ end
534
+
500
535
  def handle_error(e)
501
536
  render error_template, e
502
537
  end
@@ -7,12 +7,21 @@ module Gopher
7
7
  attr_accessor :selector, :input, :ip_address
8
8
 
9
9
  def initialize(raw, ip_addr=nil)
10
- @selector, @input = raw.chomp.split("\t")
10
+ @raw = raw
11
+ @selector, @input = @raw.chomp.split("\t")
11
12
 
12
13
  @selector = Gopher::Application.sanitize_selector(@selector)
13
14
  @ip_address = ip_addr
14
15
  end
15
16
 
17
+ def url?
18
+ @raw =~ /^URL\:/
19
+ end
20
+
21
+ def url
22
+ @raw.chomp.split("\t").first.gsub(/^URL\:/, '')
23
+ end
24
+
16
25
  # confirm that this is actually a valid gopher request
17
26
  # @return [Boolean] true if the request is valid, false otherwise
18
27
  def valid?
@@ -1,4 +1,4 @@
1
1
  module Gopher
2
2
  # current version of the app
3
- VERSION = "0.5.4"
3
+ VERSION = "0.5.5"
4
4
  end
@@ -51,6 +51,7 @@ describe Gopher::Application do
51
51
 
52
52
  @response = @server.dispatch(@request)
53
53
  expect(@response.code).to eq(:missing)
54
+ expect(@response.body).to contain_any_error
54
55
  end
55
56
 
56
57
  it "should respond with error if invalid request" do
@@ -59,6 +60,7 @@ describe Gopher::Application do
59
60
 
60
61
  @response = @server.dispatch(@request)
61
62
  expect(@response.code).to eq(:error)
63
+ expect(@response.body).to contain_any_error
62
64
  end
63
65
 
64
66
  it "should respond with error if there's an exception" do
@@ -67,6 +69,7 @@ describe Gopher::Application do
67
69
 
68
70
  @response = @server.dispatch(@request)
69
71
  expect(@response.code).to eq(:error)
72
+ expect(@response.body).to contain_any_error
70
73
  end
71
74
  end
72
75
 
@@ -127,7 +130,17 @@ describe Gopher::Application do
127
130
  end
128
131
  end
129
132
 
133
+ describe 'dispatch for URL requests' do
134
+ let(:target) { 'URL:http://github.com/muffinista/gopher2000' }
135
+ let(:request) { Gopher::Request.new(target) }
136
+ let(:response) { @server.dispatch(request) }
130
137
 
138
+ it "should return web page" do
139
+ expect(response.body).to include('<meta http-equiv="refresh" content="5;URL=http://github.com/muffinista/gopher2000">')
140
+ end
141
+ end
142
+
143
+
131
144
  describe "globs" do
132
145
  before(:each) do
133
146
  @server.route '/about/*' do
@@ -142,3 +155,11 @@ describe Gopher::Application do
142
155
  end
143
156
  end
144
157
  end
158
+
159
+ RSpec::Matchers.define :contain_any_error do
160
+ match do |actual|
161
+ actual.split(/\r?\n/).any? do |line|
162
+ line.match(/^3.*?\tnull\t\(FALSE\)\t0$/)
163
+ end
164
+ end
165
+ end
@@ -32,4 +32,10 @@ describe Gopher::Request do
32
32
  request = Gopher::Request.new("x" * 255, "bar")
33
33
  expect(request.valid?).to eq(false)
34
34
  end
35
+
36
+ it 'detects urls' do
37
+ request = Gopher::Request.new("URL:http://github.com/muffinista/gopher2000")
38
+ expect(request).to be_url
39
+ expect(request.url).to eql('http://github.com/muffinista/gopher2000')
40
+ end
35
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gopher2000
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Mitchell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-21 00:00:00.000000000 Z
11
+ date: 2020-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec