jellyfish 1.1.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f177b766380820557e01f05b9da4efacc4895984
4
- data.tar.gz: 800a25a7fa325e5c0c40c7604b94cbddb32c43a6
3
+ metadata.gz: be6525f7f0441a5863cd21be6c1e94ef5e9906b2
4
+ data.tar.gz: 00a83a35b93defbd52d8d4193f0298cc6009fda4
5
5
  SHA512:
6
- metadata.gz: 288710b695398769a27f730e3a2d2d6fe921b140455a968f57515503628af9f7759506c89c9291b860e862b1eb6ca56d076b9ca01c2429d53626bc021d189904
7
- data.tar.gz: 78edee35fbbac6dd332fc9eb9290b1dc26c27b74abd72c57166bb0d429ac6f3d441c3c88f56f014befdeb32825a47bf6d395dae04e21c624b6c52caa986aa589
6
+ metadata.gz: 9b3020e8a013a1a95057f9c59eb7bc8a9b05dba6a5fed1462838d8edb76b4e1d37c2daa63344b049d658361c1d60c85e603ac6a3609964a7df17f07c83c5cae7
7
+ data.tar.gz: 6e7e67cd52e5b34c4c9859d2446d42a2c7de2ef5026bdf1bcb8150e0ffa646be740b4f4343a54eb9fe07b1be31e8a5a60a67bc857c50389f74a74b111b0e9f02
data/.travis.yml CHANGED
@@ -1,11 +1,15 @@
1
-
1
+ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.0
5
5
  - 2.1
6
6
  - 2.2
7
- - rbx-2
8
- - jruby
7
+ - rbx
8
+ - jruby-9
9
9
 
10
+ before_install:
11
+ - rvm get head
12
+ - rvm reload
13
+ - rvm use --install $TRAVIS_RUBY_VERSION --binary --latest
10
14
  install: 'bundle install --retry=3'
11
- script: 'ruby -r bundler/setup -S rake test'
15
+ script: 'ruby -vr bundler/setup -S rake test'
data/CHANGES.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CHANGES
2
2
 
3
+ ## Jellyfish 1.1.1 -- 2015-12-22
4
+
5
+ ### Enhancements
6
+
7
+ * Added `Jellyfish::Rewrite` as an extension to `Jellyfish::Builder`.
8
+ Please check README.md for more detail.
9
+
3
10
  ## Jellyfish 1.1.0 -- 2015-09-25
4
11
 
5
12
  ### Incompatible changes
data/README.md CHANGED
@@ -236,7 +236,7 @@ run Tank.new
236
236
  GET /yell
237
237
  body = case RUBY_ENGINE
238
238
  when 'jruby'
239
- "No one hears you: (eval):9:in `Tank'\n"
239
+ "No one hears you: (eval):9:in `block in Tank'\n"
240
240
  when 'rbx'
241
241
  "No one hears you: kernel/delta/kernel.rb:78:in `yell (method_missing)'\n"
242
242
  else
@@ -583,6 +583,117 @@ Comparison:
583
583
  Rack::URLMap: 1702.0 i/s - 36.66x slower
584
584
  ```
585
585
 
586
+ #### Extension: Jellyfish::Rewrite
587
+
588
+ `Jellyfish::Builder` is mostly compatible with `Rack::Builder`, and
589
+ `Jellyfish::Rewrite` is an extension to `Rack::Builder` which allows
590
+ you to rewrite `env['PATH_INFO']` in an easy way. In an ideal world,
591
+ we don't really need this. But in real world, we might want to have some
592
+ backward compatible API which continues to work even if the API endpoint
593
+ has already been changed.
594
+
595
+ Suppose the old API was: `/users/me`, and we want to change to `/profiles/me`,
596
+ while leaving the `/users/list` as before. We may have this:
597
+
598
+ ``` ruby
599
+ require 'jellyfish'
600
+
601
+ users_api = lambda{ |env| [200, {}, ["users\n"]] }
602
+ profiles_api = lambda{ |env| [200, {}, ["profiles\n"]] }
603
+
604
+ run Jellyfish::Builder.app{
605
+ rewrite '/users/me' => '/profiles/me' do
606
+ run profiles_api
607
+ end
608
+ map '/profiles' do
609
+ run profiles_api
610
+ end
611
+ map '/users' do
612
+ run users_api
613
+ end
614
+ }
615
+ ```
616
+
617
+ <!---
618
+ GET /users/me
619
+ [200, {}, ["profiles\n"]]
620
+
621
+ GET /users/list
622
+ [200, {}, ["users\n"]]
623
+
624
+ GET /profiles/me
625
+ [200, {}, ["profiles\n"]]
626
+
627
+ GET /profiles/list
628
+ [200, {}, ["profiles\n"]]
629
+ -->
630
+
631
+ This way, we would rewrite `/users/me` to `/profiles/me` and serve it with
632
+ our profiles API app, while leaving all other paths begin with `/users`
633
+ continue to work with the old users API app.
634
+
635
+ ##### Extension: Jellyfish::Rewrite (`map path, to:`)
636
+
637
+ Note that you could also use `map path, :to` if you prefer this API more:
638
+
639
+ ``` ruby
640
+ require 'jellyfish'
641
+
642
+ users_api = lambda{ |env| [200, {}, ["users\n"]] }
643
+ profiles_api = lambda{ |env| [200, {}, ["profiles\n"]] }
644
+
645
+ run Jellyfish::Builder.app{
646
+ map '/users/me', to: '/profiles/me' do
647
+ run profiles_api
648
+ end
649
+ map '/profiles' do
650
+ run profiles_api
651
+ end
652
+ map '/users' do
653
+ run users_api
654
+ end
655
+ }
656
+ ```
657
+
658
+ <!---
659
+ GET /users/me
660
+ [200, {}, ["profiles\n"]]
661
+
662
+ GET /users/list
663
+ [200, {}, ["users\n"]]
664
+
665
+ GET /profiles/me
666
+ [200, {}, ["profiles\n"]]
667
+
668
+ GET /profiles/list
669
+ [200, {}, ["profiles\n"]]
670
+ -->
671
+
672
+ ##### Extension: Jellyfish::Rewrite (`rewrite rules`)
673
+
674
+ Note that `rewrite` takes a hash which could contain more than one rule:
675
+
676
+ ``` ruby
677
+ require 'jellyfish'
678
+
679
+ profiles_api = lambda{ |env| [200, {}, ["profiles\n"]] }
680
+
681
+ run Jellyfish::Builder.app{
682
+ rewrite '/users/me' => '/profiles/me',
683
+ '/users/fa' => '/profiles/fa' do
684
+ run profiles_api
685
+ end
686
+ }
687
+ ```
688
+
689
+ <!---
690
+ GET /users/me
691
+ [200, {}, ["profiles\n"]]
692
+
693
+ GET /users/fa
694
+ [200, {}, ["profiles\n"]]
695
+ -->
696
+
586
697
  ### Extension: NormalizedParams (with force_encoding)
587
698
 
588
699
  ``` ruby
data/jellyfish.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: jellyfish 1.1.0 ruby lib
2
+ # stub: jellyfish 1.1.1 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "jellyfish"
6
- s.version = "1.1.0"
6
+ s.version = "1.1.1"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Lin Jen-Shin (godfat)"]
11
- s.date = "2015-09-25"
11
+ s.date = "2015-12-22"
12
12
  s.description = "Pico web framework for building API-centric web applications.\nFor Rack applications or Rack middleware. Around 250 lines of code.\n\nCheck [jellyfish-contrib][] for extra extensions.\n\n[jellyfish-contrib]: https://github.com/godfat/jellyfish-contrib"
13
13
  s.email = ["godfat (XD) godfat.org"]
14
14
  s.files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/jellyfish/public/302.html",
36
36
  "lib/jellyfish/public/404.html",
37
37
  "lib/jellyfish/public/500.html",
38
+ "lib/jellyfish/rewrite.rb",
38
39
  "lib/jellyfish/test.rb",
39
40
  "lib/jellyfish/urlmap.rb",
40
41
  "lib/jellyfish/version.rb",
@@ -51,11 +52,12 @@ Gem::Specification.new do |s|
51
52
  "test/test_inheritance.rb",
52
53
  "test/test_log.rb",
53
54
  "test/test_misc.rb",
55
+ "test/test_rewrite.rb",
54
56
  "test/test_threads.rb",
55
57
  "test/test_websocket.rb"]
56
58
  s.homepage = "https://github.com/godfat/jellyfish"
57
59
  s.licenses = ["Apache License 2.0"]
58
- s.rubygems_version = "2.4.8"
60
+ s.rubygems_version = "2.5.1"
59
61
  s.summary = "Pico web framework for building API-centric web applications."
60
62
  s.test_files = [
61
63
  "test/rack/test_builder.rb",
@@ -68,6 +70,7 @@ Gem::Specification.new do |s|
68
70
  "test/test_inheritance.rb",
69
71
  "test/test_log.rb",
70
72
  "test/test_misc.rb",
73
+ "test/test_rewrite.rb",
71
74
  "test/test_threads.rb",
72
75
  "test/test_websocket.rb"]
73
76
  end
@@ -3,8 +3,8 @@ require 'jellyfish/urlmap'
3
3
 
4
4
  module Jellyfish
5
5
  class Builder
6
- def self.app app=nil, &block
7
- new(app, &block).to_app
6
+ def self.app app=nil, to=nil, &block
7
+ new(app, &block).to_app(to)
8
8
  end
9
9
 
10
10
  def initialize app=nil, &block
@@ -28,23 +28,30 @@ module Jellyfish
28
28
  @warmup = lam || block
29
29
  end
30
30
 
31
- def map path, &block
32
- (@map ||= {})[path] = block
31
+ def map path, to: nil, &block
32
+ (@map ||= {})[path] = [block, to]
33
33
  end
34
34
 
35
- def to_app
35
+ def rewrite rules, &block
36
+ rules.each do |path, to|
37
+ map(path, :to => to, &block)
38
+ end
39
+ end
40
+
41
+ def to_app to=nil
36
42
  run = if @map then generate_map(@map, @run) else @run end
37
43
  fail 'missing run or map statement' unless run
38
44
  app = @use.inject(run){ |a, m| m.call(a) }
39
- @warmup.call(app) if @warmup
40
- app
45
+ rewrite = if to then Rewrite.new(app, to) else app end
46
+ @warmup.call(rewrite) if @warmup
47
+ rewrite
41
48
  end
42
49
 
43
50
  private
44
51
  def generate_map current_map, app
45
52
  mapped = if app then {'' => app} else {} end
46
- current_map.each do |path, block|
47
- mapped[path.chomp('/')] = self.class.app(app, &block)
53
+ current_map.each do |path, (block, to)|
54
+ mapped[path.chomp('/')] = self.class.app(app, to, &block)
48
55
  end
49
56
  URLMap.new(mapped)
50
57
  end
@@ -0,0 +1,8 @@
1
+
2
+ module Jellyfish
3
+ class Rewrite < Struct.new(:app, :to)
4
+ def call env
5
+ app.call(env.merge('PATH_INFO' => "#{env['PATH_INFO']}#{to}"))
6
+ end
7
+ end
8
+ end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module Jellyfish
3
- VERSION = '1.1.0'
3
+ VERSION = '1.1.1'
4
4
  end
data/lib/jellyfish.rb CHANGED
@@ -7,6 +7,7 @@ module Jellyfish
7
7
  autoload :NormalizedPath , 'jellyfish/normalized_path'
8
8
 
9
9
  autoload :Builder , 'jellyfish/builder'
10
+ autoload :Rewrite , 'jellyfish/rewrite'
10
11
  autoload :ChunkedBody, 'jellyfish/chunked_body'
11
12
  autoload :WebSocket , 'jellyfish/websocket'
12
13
 
@@ -0,0 +1,38 @@
1
+
2
+ require 'jellyfish/test'
3
+ require 'jellyfish/urlmap'
4
+
5
+ describe Jellyfish::Rewrite do
6
+ lam = lambda{ |env| [200, {}, [env['PATH_INFO']]] }
7
+
8
+ def call app, env={}
9
+ app.call({'SCRIPT_NAME' => ''}.merge(env)).last.first
10
+ end
11
+
12
+ would 'map to' do
13
+ app = Jellyfish::Builder.app do
14
+ map '/from', to: '/to' do
15
+ run lam
16
+ end
17
+ end
18
+
19
+ expect(call(app, 'PATH_INFO' => '/from')).eq '/to'
20
+ end
21
+
22
+ would 'rewrite and fallback' do
23
+ app = Jellyfish::Builder.app do
24
+ rewrite '/from/inner' => '/to/inner',
25
+ '/from/outer' => '/to/outer' do
26
+ run lam
27
+ end
28
+
29
+ map '/from' do
30
+ run lam
31
+ end
32
+ end
33
+
34
+ expect(call(app, 'PATH_INFO' => '/from' )).eq ''
35
+ expect(call(app, 'PATH_INFO' => '/from/inner')).eq '/to/inner'
36
+ expect(call(app, 'PATH_INFO' => '/from/outer')).eq '/to/outer'
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jellyfish
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Jen-Shin (godfat)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2015-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Pico web framework for building API-centric web applications.
@@ -46,6 +46,7 @@ files:
46
46
  - lib/jellyfish/public/302.html
47
47
  - lib/jellyfish/public/404.html
48
48
  - lib/jellyfish/public/500.html
49
+ - lib/jellyfish/rewrite.rb
49
50
  - lib/jellyfish/test.rb
50
51
  - lib/jellyfish/urlmap.rb
51
52
  - lib/jellyfish/version.rb
@@ -62,6 +63,7 @@ files:
62
63
  - test/test_inheritance.rb
63
64
  - test/test_log.rb
64
65
  - test/test_misc.rb
66
+ - test/test_rewrite.rb
65
67
  - test/test_threads.rb
66
68
  - test/test_websocket.rb
67
69
  homepage: https://github.com/godfat/jellyfish
@@ -84,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
86
  version: '0'
85
87
  requirements: []
86
88
  rubyforge_project:
87
- rubygems_version: 2.4.8
89
+ rubygems_version: 2.5.1
88
90
  signing_key:
89
91
  specification_version: 4
90
92
  summary: Pico web framework for building API-centric web applications.
@@ -99,5 +101,6 @@ test_files:
99
101
  - test/test_inheritance.rb
100
102
  - test/test_log.rb
101
103
  - test/test_misc.rb
104
+ - test/test_rewrite.rb
102
105
  - test/test_threads.rb
103
106
  - test/test_websocket.rb