sinatra-helpers-http-vary 0.1.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.
Files changed (3) hide show
  1. data/README.md +36 -0
  2. data/lib/sinatra-helpers/http/vary.rb +67 -0
  3. metadata +63 -0
@@ -0,0 +1,36 @@
1
+ Sinatra helpers to set the HTTP "`Vary`" header.
2
+
3
+ See [RFC 2616 (HTTP/1.1): Vary](http://tools.ietf.org/html/rfc2616#section-14.44)
4
+
5
+ Author: Philipp Kempgen, [http://kempgen.net](http://kempgen.net)
6
+
7
+
8
+ ## Usage
9
+
10
+ Include the module in your Sinatra application:
11
+
12
+ helpers ::Sinatra::Helpers::HTTP::Vary
13
+
14
+ Example:
15
+
16
+ vary! 'Accept'
17
+ vary! 'Accept-Language'
18
+ # headers['Vary'] is now "Accept, Accept-Language"
19
+
20
+ Example:
21
+
22
+ vary! '*'
23
+ # headers['Vary'] is now "*"
24
+
25
+ Example:
26
+
27
+ vary! '*'
28
+ vary! 'Accept-Language'
29
+ # headers['Vary'] is now "*"
30
+
31
+ Example:
32
+
33
+ vary! 'Accept-Language'
34
+ vary! '*'
35
+ # headers['Vary'] is now "*"
36
+
@@ -0,0 +1,67 @@
1
+ require 'sinatra/base'
2
+
3
+ # see Sinatra
4
+ module Sinatra
5
+ # see Sinatra
6
+ module Helpers
7
+
8
+ # HTTP helpers
9
+ module HTTP
10
+
11
+ # Helpers to set the HTTP "`Vary`" header.
12
+ #
13
+ # Usage:
14
+ #
15
+ # Include the module in your Sinatra application:
16
+ #
17
+ # helpers ::Sinatra::Helpers::HTTP::Vary
18
+ #
19
+ module Vary
20
+
21
+ VARY_HEADER = 'Vary'.freeze
22
+ VARY_UNSPECIFIED = '*'.freeze
23
+
24
+ # Sets the HTTP "`Vary`" header in Sinatra's response
25
+ # `headers`.
26
+ #
27
+ # @param hdr_name [String] The HTTP header name.
28
+ # @return The updated `headers`.
29
+ #
30
+ def vary!( hdr_name )
31
+ hdr_name = hdr_name.to_s if ! hdr_name.kind_of?( ::String )
32
+
33
+ # Shortcut to avoid expensive splitting in the
34
+ # simple "*" case:
35
+ if hdr_name == VARY_UNSPECIFIED
36
+ headers[ VARY_HEADER ] = VARY_UNSPECIFIED.dup unless (headers[ VARY_HEADER ] == VARY_UNSPECIFIED)
37
+
38
+ # Normal operation:
39
+ else
40
+ vary_hdrs = headers[ VARY_HEADER ].to_s.
41
+ split( /\s*(?:,\s*)+/ )
42
+
43
+ if vary_hdrs.include?( VARY_UNSPECIFIED )
44
+ headers[ VARY_HEADER ] = VARY_UNSPECIFIED.dup
45
+ else
46
+ headers[ VARY_HEADER ] =
47
+ vary_hdrs.
48
+ push( hdr_name ).
49
+ uniq( & :downcase ).
50
+ join( ',' )
51
+ end
52
+ end
53
+ return headers[ VARY_HEADER ]
54
+ end
55
+
56
+ #def vary=( str )
57
+ # headers[ VARY_HEADER ] = str
58
+ #end
59
+
60
+ end
61
+ end
62
+ end
63
+
64
+ #helpers Helpers::HTTP::Vary
65
+ #::Sinatra::Base.helpers Helpers::HTTP::Vary
66
+ end
67
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-helpers-http-vary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philipp Kempgen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ description: Helpers for setting the HTTP Vary header in Sinatra.
31
+ email:
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/sinatra-helpers/http/vary.rb
37
+ - README.md
38
+ homepage: https://github.com/philipp-kempgen/sinatra-helpers-http-vary
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.25
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Sinatra helpers to set the HTTP Vary header.
62
+ test_files: []
63
+ has_rdoc: