phlex-sinatra 0.1.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: d74cf72d60a394421ad29c44f264a94fbacb60e00434a52cb16ddc72f88fd586
4
- data.tar.gz: 05a8edc2a431763003c13b8ec3fc91f2471b0f5feadea234314dbae770535554
3
+ metadata.gz: 6281c981ddfb5118f6a6f0a38297dee0152e7052dc29aab21284de40814acfbb
4
+ data.tar.gz: 4d56528737f86832f8113ae8da2c05b0b41ef0643d7dc8f8a36d435e0ec7f00a
5
5
  SHA512:
6
- metadata.gz: a0e1f7096bdd91b5e7cbaa10bfbaaedd26447463d9e86c2f5ab71465302b1af9520a1a574721cd6402ea3aa8f0d80dfa67138cc7c2e73d2fa40927ef72cefb7e
7
- data.tar.gz: 00d710702436b31b687bbf25e0ae03c0e7356bd721f3c7e3f0b21c8ab1ecd3130fe3405100bc8a8ef4fdb351ab5886c25040c7ec6c5f6c1281c16209dc9919c2
6
+ metadata.gz: c818ef9cdee0f1bc70e2a8cdb98956c7e83313e2774d09330c6487a9c1f3f2b504c7aae5e57d5fefa7693311b609a3f94c16aa6caa63f18b76135f3c304d7309
7
+ data.tar.gz: c67a8d2d7d4b3b279fa812eb173b6c854af7c87d587894a6907059238e7c3df21daec28d2d0b14a2a600961b68f39deffe98d150bc7a2f0dce5386779e081893
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## Version 0.3.0 - 2023-12-13
2
+
3
+ - Add support for streaming a view. Pass `stream: true` to the `#phlex` helper so Phlex will use Sinatra's streaming capability.
4
+
5
+ ## Version 0.2.0 - 2023-04-24
6
+
7
+ - Allow passing a `content_type:` kwarg to the `#phlex` helper so it behaves like Sinatra's other template helpers (defaults to `:html` – or `:svg` for a `Phlex::SVG` instance).
8
+ - Raise an informative error message if the `#phlex` helper receives something other than a Phlex instance.
9
+
1
10
  ## Version 0.1.0 - 2023-04-17
2
11
 
3
12
  - Initial release
data/Gemfile CHANGED
@@ -5,6 +5,8 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in phlex-sinatra.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'capybara'
9
+ gem 'puma'
8
10
  gem 'rack-test'
9
11
  gem 'rake'
10
12
  gem 'rspec'
data/README.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  [Phlex](https://github.com/phlex-ruby/phlex) already works with Sinatra (and everything else) but its normal usage leaves you without access to Sinatra's standard helper methods. This integration lets you use the `url()` helper method from within a Phlex view (along with the rest of the helper methods available in a Sinatra action).
4
4
 
5
+ ## Installation
6
+
7
+ Add phlex-sinatra to your application's Gemfile and run `bundle install`.
8
+
9
+ ```ruby
10
+ gem 'phlex-sinatra'
11
+ ```
12
+
5
13
  ## Usage
6
14
 
7
15
  To enable the integration use the `phlex` method in your Sinatra action and pass an _instance_ of the Phlex view (instead of using `.call` to get its output):
@@ -26,7 +34,64 @@ class MyView < Phlex::HTML
26
34
  end
27
35
  ```
28
36
 
29
- ## Why?
37
+ You can also pass an alternative content type – which defaults to `:html` (or `:svg` for a `Phlex::SVG` instance):
38
+
39
+ ```ruby
40
+ get '/foo' do
41
+ phlex MyView.new, content_type: :xml
42
+ end
43
+ ```
44
+
45
+ ## Streaming
46
+
47
+ Streaming a Phlex view can be enabled by passing `stream: true` which will cause Phlex to automatically write to the response after the closing `</head>` and buffer the remaining content:
48
+
49
+ ```ruby
50
+ get '/foo' do
51
+ phlex MyView.new, stream: true
52
+ end
53
+ ```
54
+
55
+ Even with no further intervention this small change means that the browser will receive the complete `<head>` as quickly as possible and can start fetching and processing its external resources while waiting for the rest of the page to download.
56
+
57
+ You can also manually flush the contents of the buffer at any point using Phlex's `#flush` method:
58
+
59
+ ```ruby
60
+ class Layout < Phlex::HTML
61
+ def template(&)
62
+ doctype
63
+ html {
64
+ head {
65
+ # All the usual stuff: links to external stylesheets and JavaScript etc.
66
+ }
67
+ # Phlex will automatically flush to the response at this point which will
68
+ # benefit all pages that opt in to streaming.
69
+ body {
70
+ # Standard site header and navigation.
71
+ render Header.new
72
+
73
+ yield_content(&)
74
+ }
75
+ }
76
+ end
77
+ end
78
+
79
+ class MyView < Phlex::HTML
80
+ def template
81
+ render Layout.new {
82
+ # Knowing that this page can take a while to generate we can choose to
83
+ # flush here so the browser can render the site header while downloading
84
+ # the rest of the page - which should help minimise the First Contentful
85
+ # Paint metric.
86
+ flush
87
+
88
+ # The rest of the big long page...
89
+ }
90
+ end
91
+ end
92
+ ```
93
+
94
+ ## Why do I need Sinatra's `url()` helper?
30
95
 
31
96
  It might not seem obvious at first why you'd use `url()` at all given that you mostly just pass the string you want to output and then probably `false` so the scheme/host isn't included.
32
97
 
@@ -34,13 +99,13 @@ There are a couple of reasons:
34
99
 
35
100
  1. **Linking to a full URL**
36
101
 
37
- Sometimes you need to link to a page on the site using its full URL -- for instance within a feed or for an `og:image` social media preview image link.
102
+ Sometimes you need to link to a page on the site using its full URL for instance within a feed or for an `og:image` social media preview image link.
38
103
 
39
104
  2. **Awareness that the app is being served from a subdirectory**
40
105
 
41
- This isn't something you encounter very often in a standard Sinatra app but you hit it quite quickly if you're using [Parklife](https://github.com/benpickles/parklife) to generate a static build which you host on GitHub Pages -- which is exactly what prompted me to write this integration.
106
+ This isn't something you encounter very often in a standard Sinatra app but you hit it quite quickly if you're using [Parklife](https://github.com/benpickles/parklife) to generate a static build hosted on GitHub Pages which is exactly what prompted me to write this integration.
42
107
 
43
- In this case by using the `url()` helper you won’t have to change anything when switching between serving the app from `/` in development and hosting it at `/my-repository/` in production -- internal links to other pages/stylesheets/etc will always be correct regardless.
108
+ In this case by using the `url()` helper you won’t have to change anything when switching between serving the app from `/` in development and hosting it at `/my-repository/` in production internal links to other pages/stylesheets/etc will always be correct regardless.
44
109
 
45
110
  ## Contributing
46
111
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Sinatra
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
data/lib/phlex-sinatra.rb CHANGED
@@ -5,6 +5,18 @@ require_relative 'phlex/sinatra/version'
5
5
 
6
6
  module Phlex
7
7
  module Sinatra
8
+ Error = Class.new(StandardError)
9
+
10
+ class TypeError < Error
11
+ MAX_SIZE = 32
12
+
13
+ def initialize(obj)
14
+ content = obj.inspect
15
+ content = content[0, MAX_SIZE] + '…' if content.size > MAX_SIZE
16
+ super "Expected a Phlex instance, received #{content}"
17
+ end
18
+ end
19
+
8
20
  module SGML
9
21
  module Overrides
10
22
  def helpers
@@ -25,8 +37,19 @@ end
25
37
 
26
38
  module Sinatra
27
39
  module Templates
28
- def phlex(obj)
29
- obj.call(view_context: self)
40
+ def phlex(obj, content_type: nil, stream: false)
41
+ raise Phlex::Sinatra::TypeError.new(obj) unless obj.is_a?(Phlex::SGML)
42
+
43
+ content_type ||= :svg if obj.is_a?(Phlex::SVG)
44
+ self.content_type(content_type) if content_type
45
+
46
+ if stream
47
+ self.stream do |out|
48
+ obj.call(out, view_context: self)
49
+ end
50
+ else
51
+ obj.call(view_context: self)
52
+ end
30
53
  end
31
54
  end
32
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Pickles
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-17 00:00:00.000000000 Z
11
+ date: 2023-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 1.7.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 1.7.0
27
27
  description: A Phlex adapter for Sinatra
28
28
  email:
29
29
  - spideryoung@gmail.com
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubygems_version: 3.4.6
67
+ rubygems_version: 3.4.10
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: A Phlex adapter for Sinatra