phlex-sinatra 0.1.0 → 0.2.0

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: d74cf72d60a394421ad29c44f264a94fbacb60e00434a52cb16ddc72f88fd586
4
- data.tar.gz: 05a8edc2a431763003c13b8ec3fc91f2471b0f5feadea234314dbae770535554
3
+ metadata.gz: a0e0c3d6038f57d3a01b5abb0172d1598a0edb08d46e455dede38da9ad708560
4
+ data.tar.gz: 6e5025677940fc22ed1830405646c18d4d441f8125a4c37f3179d8c8e735a354
5
5
  SHA512:
6
- metadata.gz: a0e1f7096bdd91b5e7cbaa10bfbaaedd26447463d9e86c2f5ab71465302b1af9520a1a574721cd6402ea3aa8f0d80dfa67138cc7c2e73d2fa40927ef72cefb7e
7
- data.tar.gz: 00d710702436b31b687bbf25e0ae03c0e7356bd721f3c7e3f0b21c8ab1ecd3130fe3405100bc8a8ef4fdb351ab5886c25040c7ec6c5f6c1281c16209dc9919c2
6
+ metadata.gz: e94e93fbb09a9b6043667f8139cc515a4d6b5302f3459b5216d4f900d471579b4621716e1734d4dd38ead39788074c6442ed3f0c46ce63e7aa9faa4868bb4cda
7
+ data.tar.gz: 5026ea9e6f0af3ae2b064dc475d441e85b6753e49e37ef1e57f6aca00d3d7e15feb8a6eb188a6939eff70c0dae7ff1fa4db8447f3cd2a6c512a9212273ef4a4e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Version 0.2.0 - 2023-04-24
2
+
3
+ - 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).
4
+ - Raise an informative error message if the `#phlex` helper receives something other than a Phlex instance.
5
+
1
6
  ## Version 0.1.0 - 2023-04-17
2
7
 
3
8
  - Initial release
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,6 +34,14 @@ class MyView < Phlex::HTML
26
34
  end
27
35
  ```
28
36
 
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
+
29
45
  ## Why?
30
46
 
31
47
  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.
@@ -34,13 +50,13 @@ There are a couple of reasons:
34
50
 
35
51
  1. **Linking to a full URL**
36
52
 
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.
53
+ 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
54
 
39
55
  2. **Awareness that the app is being served from a subdirectory**
40
56
 
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.
57
+ 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.
42
58
 
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.
59
+ 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
60
 
45
61
  ## Contributing
46
62
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Sinatra
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.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,7 +37,12 @@ end
25
37
 
26
38
  module Sinatra
27
39
  module Templates
28
- def phlex(obj)
40
+ def phlex(obj, content_type: nil)
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
+
29
46
  obj.call(view_context: self)
30
47
  end
31
48
  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.2.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-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex