http-accept 1.7.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/lib/.DS_Store +0 -0
- data/lib/http/accept/charsets.rb +3 -3
- data/lib/http/accept/content_type.rb +4 -2
- data/lib/http/accept/encodings.rb +3 -3
- data/lib/http/accept/languages.rb +3 -1
- data/lib/http/accept/media_types/map.rb +2 -0
- data/lib/http/accept/media_types.rb +22 -11
- data/lib/http/accept/parse_error.rb +2 -0
- data/lib/http/accept/quoted_string.rb +2 -0
- data/lib/http/accept/sort.rb +2 -0
- data/lib/http/accept/version.rb +3 -1
- data/lib/http/accept.rb +2 -2
- data.tar.gz.sig +0 -0
- metadata +57 -30
- metadata.gz.sig +0 -0
- data/.gitignore +0 -9
- data/.rspec +0 -4
- data/.simplecov +0 -9
- data/.travis.yml +0 -5
- data/Gemfile +0 -9
- data/README.md +0 -137
- data/Rakefile +0 -8
- data/http-accept.gemspec +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df0246f182c9234cb68ea3049b0db11f86c503bfc53a7e72fa4f7ecc13da7151
|
4
|
+
data.tar.gz: a07dea0b41e4c7a0962e8be0543e7f714b7d1a20cf820cd413d90acaf0a1a0e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b030b40a094e4b46726aa5e20a4c1b89b332218c165d2b1667159528229446b45cdc34c624acc7a90aed4cdd4dae663d125e61697825739b61abb01f255d4b5
|
7
|
+
data.tar.gz: cdcd43429064dd0510cd6333a76f707e9fae42f5388316e5a977bfe34caf24c0ff047ffb08cdbcdb109d553b844857d55b765b48cbde60058ea57d8170762ac1
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/http/accept/charsets.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright (C) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -65,9 +67,7 @@ module HTTP
|
|
65
67
|
|
66
68
|
# Parse the list of browser preferred charsets and return ordered by priority.
|
67
69
|
def self.browser_preferred_charsets(env)
|
68
|
-
if accept_charsets = env[HTTP_ACCEPT_CHARSET]
|
69
|
-
accept_charsets.strip!
|
70
|
-
|
70
|
+
if accept_charsets = env[HTTP_ACCEPT_CHARSET]&.strip
|
71
71
|
if accept_charsets.empty?
|
72
72
|
# https://tools.ietf.org/html/rfc7231#section-5.3.3 :
|
73
73
|
#
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -25,9 +27,9 @@ module HTTP
|
|
25
27
|
module Accept
|
26
28
|
# A content type is different from a media range, in that a content type should not have any wild cards.
|
27
29
|
class ContentType < MediaTypes::MediaRange
|
28
|
-
def initialize(
|
30
|
+
def initialize(type, subtype, parameters = {})
|
29
31
|
# We do some basic validation here:
|
30
|
-
raise ArgumentError.new("#{self.class} can not have wildcards: #{
|
32
|
+
raise ArgumentError.new("#{self.class} can not have wildcards: #{type}", "#{subtype}") if type.include?('*') || subtype.include?('*')
|
31
33
|
|
32
34
|
super
|
33
35
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright (C) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -72,9 +74,7 @@ module HTTP
|
|
72
74
|
# specified, the behaviour is the same as if `Accept-Encoding: identity` was provided
|
73
75
|
# (according to RFC).
|
74
76
|
def self.browser_preferred_content_codings(env)
|
75
|
-
if accept_content_codings = env[HTTP_ACCEPT_ENCODING]
|
76
|
-
accept_content_codings.strip!
|
77
|
-
|
77
|
+
if accept_content_codings = env[HTTP_ACCEPT_ENCODING]&.strip
|
78
78
|
if accept_content_codings.empty?
|
79
79
|
# "An Accept-Encoding header field with a combined field-value that is
|
80
80
|
# empty implies that the user agent does not want any content-coding in
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -30,7 +32,7 @@ module HTTP
|
|
30
32
|
LOCALE = /\*|[A-Z]{1,8}(-[A-Z0-9]{1,8})*/i
|
31
33
|
|
32
34
|
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
|
33
|
-
QVALUE = /0(\.[0-9]{0,
|
35
|
+
QVALUE = /0(\.[0-9]{0,6})?|1(\.[0]{0,6})?/
|
34
36
|
|
35
37
|
# https://greenbytes.de/tech/webdav/rfc7231.html#quality.values
|
36
38
|
LANGUAGE_RANGE = /(?<locale>#{LOCALE})(\s*;\s*q=(?<q>#{QVALUE}))?/
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -31,11 +33,15 @@ module HTTP
|
|
31
33
|
# Parse and process the HTTP Accept: header.
|
32
34
|
module MediaTypes
|
33
35
|
# According to https://tools.ietf.org/html/rfc7231#section-5.3.2
|
34
|
-
MIME_TYPE = /(
|
36
|
+
MIME_TYPE = /(?<type>#{TOKEN})\/(?<subtype>#{TOKEN})/
|
35
37
|
PARAMETER = /\s*;\s*(?<key>#{TOKEN})=((?<value>#{TOKEN})|(?<quoted_value>#{QUOTED_STRING}))/
|
36
38
|
|
37
39
|
# A single entry in the Accept: header, which includes a mime type and associated parameters.
|
38
|
-
MediaRange = Struct.new(:
|
40
|
+
MediaRange = Struct.new(:type, :subtype, :parameters) do
|
41
|
+
def initialize(type, subtype = '*', parameters = {})
|
42
|
+
super(type, subtype, parameters)
|
43
|
+
end
|
44
|
+
|
39
45
|
def parameters_string
|
40
46
|
return '' if parameters == nil or parameters.empty?
|
41
47
|
|
@@ -52,8 +58,12 @@ module HTTP
|
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
61
|
+
def mime_type
|
62
|
+
"#{type}/#{subtype}"
|
63
|
+
end
|
64
|
+
|
55
65
|
def to_s
|
56
|
-
"#{
|
66
|
+
"#{type}/#{subtype}#{parameters_string}"
|
57
67
|
end
|
58
68
|
|
59
69
|
alias to_str to_s
|
@@ -62,8 +72,8 @@ module HTTP
|
|
62
72
|
parameters.fetch('q', 1.0).to_f
|
63
73
|
end
|
64
74
|
|
65
|
-
def split(
|
66
|
-
|
75
|
+
def split(*args)
|
76
|
+
return [type, subtype]
|
67
77
|
end
|
68
78
|
|
69
79
|
def self.parse_parameters(scanner, normalize_whitespace)
|
@@ -86,10 +96,13 @@ module HTTP
|
|
86
96
|
def self.parse(scanner, normalize_whitespace = true)
|
87
97
|
return to_enum(:parse, scanner, normalize_whitespace) unless block_given?
|
88
98
|
|
89
|
-
while
|
99
|
+
while scanner.scan(MIME_TYPE)
|
100
|
+
type = scanner[:type]
|
101
|
+
subtype = scanner[:subtype]
|
102
|
+
|
90
103
|
parameters = parse_parameters(scanner, normalize_whitespace)
|
91
104
|
|
92
|
-
yield self.new(
|
105
|
+
yield self.new(type, subtype, parameters)
|
93
106
|
|
94
107
|
# Are there more?
|
95
108
|
break unless scanner.scan(/\s*,\s*/)
|
@@ -108,13 +121,11 @@ module HTTP
|
|
108
121
|
end
|
109
122
|
|
110
123
|
HTTP_ACCEPT = 'HTTP_ACCEPT'.freeze
|
111
|
-
WILDCARD_MEDIA_RANGE = MediaRange.new("
|
124
|
+
WILDCARD_MEDIA_RANGE = MediaRange.new("*", "*", {}).freeze
|
112
125
|
|
113
126
|
# Parse the list of browser preferred content types and return ordered by priority. If no `Accept:` header is specified, the behaviour is the same as if `Accept: */*` was provided (according to RFC).
|
114
127
|
def self.browser_preferred_media_types(env)
|
115
|
-
if accept_content_types = env[HTTP_ACCEPT]
|
116
|
-
accept_content_types.strip!
|
117
|
-
|
128
|
+
if accept_content_types = env[HTTP_ACCEPT]&.strip
|
118
129
|
unless accept_content_types.empty?
|
119
130
|
return HTTP::Accept::MediaTypes.parse(accept_content_types)
|
120
131
|
end
|
data/lib/http/accept/sort.rb
CHANGED
data/lib/http/accept/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -20,6 +22,6 @@
|
|
20
22
|
|
21
23
|
module HTTP
|
22
24
|
module Accept
|
23
|
-
VERSION = "
|
25
|
+
VERSION = "2.2.0"
|
24
26
|
end
|
25
27
|
end
|
data/lib/http/accept.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
1
3
|
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -18,8 +20,6 @@
|
|
18
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
21
|
# THE SOFTWARE.
|
20
22
|
|
21
|
-
require "http/accept/version"
|
22
|
-
|
23
23
|
require_relative 'accept/version'
|
24
24
|
|
25
25
|
# Accept: header
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
CHANGED
@@ -1,43 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http-accept
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
- Matthew Kerwin
|
9
|
+
- Alif Rachmawadi
|
10
|
+
- Andy Brody
|
11
|
+
- Khaled Hassan Hussein
|
12
|
+
- Olle Jonsson
|
13
|
+
- Robert Pritzkow
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain:
|
17
|
+
- |
|
18
|
+
-----BEGIN CERTIFICATE-----
|
19
|
+
MIIEhDCCAuygAwIBAgIBATANBgkqhkiG9w0BAQsFADA3MTUwMwYDVQQDDCxzYW11
|
20
|
+
ZWwud2lsbGlhbXMvREM9b3Jpb250cmFuc2Zlci9EQz1jby9EQz1uejAeFw0yMTA4
|
21
|
+
MTYwNjMzNDRaFw0yMjA4MTYwNjMzNDRaMDcxNTAzBgNVBAMMLHNhbXVlbC53aWxs
|
22
|
+
aWFtcy9EQz1vcmlvbnRyYW5zZmVyL0RDPWNvL0RDPW56MIIBojANBgkqhkiG9w0B
|
23
|
+
AQEFAAOCAY8AMIIBigKCAYEAyXLSS/cw+fXJ5e7hi+U/TeChPWeYdwJojDsFY1xr
|
24
|
+
xvtqbTTL8gbLHz5LW3QD2nfwCv3qTlw0qI3Ie7a9VMJMbSvgVEGEfQirqIgJXWMj
|
25
|
+
eNMDgKsMJtC7u/43abRKx7TCURW3iWyR19NRngsJJmaR51yGGGm2Kfsr+JtKKLtL
|
26
|
+
L188Wm3f13KAx7QJU8qyuBnj1/gWem076hzdA7xi1DbrZrch9GCRz62xymJlrJHn
|
27
|
+
9iZEZ7AxrS7vokhMlzSr/XMUihx/8aFKtk+tMLClqxZSmBWIErWdicCGTULXCBNb
|
28
|
+
E/mljo4zEVKhlTWpJklMIhr55ZRrSarKFuW7en0+tpJrfsYiAmXMJNi4XAYJH7uL
|
29
|
+
rgJuJwSaa/dMz+VmUoo7VKtSfCoOI+6v5/z0sK3oT6sG6ZwyI47DBq2XqNC6tnAj
|
30
|
+
w+XmCywiTQrFzMMAvcA7rPI4F0nU1rZId51rOvvfxaONp+wgTi4P8owZLw0/j0m4
|
31
|
+
8C20DYi6EYx4AHDXiLpElWh3AgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8E
|
32
|
+
BAMCBLAwHQYDVR0OBBYEFB6ZaeWKxQjGTI+pmz7cKRmMIywwMC4GA1UdEQQnMCWB
|
33
|
+
I3NhbXVlbC53aWxsaWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWB
|
34
|
+
I3NhbXVlbC53aWxsaWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEB
|
35
|
+
CwUAA4IBgQBVoM+pu3dpdUhZM1w051iw5GfiqclAr1Psypf16Tiod/ho//4oAu6T
|
36
|
+
9fj3DPX/acWV9P/FScvqo4Qgv6g4VWO5ZU7z2JmPoTXZtYMunRAmQPFL/gSUc6aK
|
37
|
+
vszMHIyhtyzRc6DnfW2AiVOjMBjaYv8xXZc9bduniRVPrLR4J7ozmGLh4o4uJp7w
|
38
|
+
x9KCFaR8Lvn/r0oJWJOqb/DMAYI83YeN2Dlt3jpwrsmsONrtC5S3gOUle5afSGos
|
39
|
+
bYt5ocnEpKSomR9ZtnCGljds/aeO1Xgpn2r9HHcjwnH346iNrnHmMlC7BtHUFPDg
|
40
|
+
Ts92S47PTOXzwPBDsrFiq3VLbRjHSwf8rpqybQBH9MfzxGGxTaETQYOd6b4e4Ag6
|
41
|
+
y92abGna0bmIEb4+Tx9rQ10Uijh1POzvr/VTH4bbIPy9FbKrRsIQ24qDbNJRtOpE
|
42
|
+
RAOsIl+HOBTb252nx1kIRN5hqQx272AJCbCjKx8egcUQKffFVVCI0nye09v5CK+a
|
43
|
+
HiLJ8VOFx6w=
|
44
|
+
-----END CERTIFICATE-----
|
45
|
+
date: 2022-08-02 00:00:00.000000000 Z
|
12
46
|
dependencies:
|
13
47
|
- !ruby/object:Gem::Dependency
|
14
48
|
name: bundler
|
15
49
|
requirement: !ruby/object:Gem::Requirement
|
16
50
|
requirements:
|
17
|
-
- - "
|
51
|
+
- - ">="
|
18
52
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
53
|
+
version: '0'
|
20
54
|
type: :development
|
21
55
|
prerelease: false
|
22
56
|
version_requirements: !ruby/object:Gem::Requirement
|
23
57
|
requirements:
|
24
|
-
- - "
|
58
|
+
- - ">="
|
25
59
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
60
|
+
version: '0'
|
27
61
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
62
|
+
name: covered
|
29
63
|
requirement: !ruby/object:Gem::Requirement
|
30
64
|
requirements:
|
31
|
-
- - "
|
65
|
+
- - ">="
|
32
66
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
67
|
+
version: '0'
|
34
68
|
type: :development
|
35
69
|
prerelease: false
|
36
70
|
version_requirements: !ruby/object:Gem::Requirement
|
37
71
|
requirements:
|
38
|
-
- - "
|
72
|
+
- - ">="
|
39
73
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
74
|
+
version: '0'
|
41
75
|
- !ruby/object:Gem::Dependency
|
42
76
|
name: rspec
|
43
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,21 +86,13 @@ dependencies:
|
|
52
86
|
- - "~>"
|
53
87
|
- !ruby/object:Gem::Version
|
54
88
|
version: '3.0'
|
55
|
-
description:
|
89
|
+
description:
|
56
90
|
email:
|
57
|
-
- samuel.williams@oriontransfer.co.nz
|
58
91
|
executables: []
|
59
92
|
extensions: []
|
60
93
|
extra_rdoc_files: []
|
61
94
|
files:
|
62
|
-
-
|
63
|
-
- ".rspec"
|
64
|
-
- ".simplecov"
|
65
|
-
- ".travis.yml"
|
66
|
-
- Gemfile
|
67
|
-
- README.md
|
68
|
-
- Rakefile
|
69
|
-
- http-accept.gemspec
|
95
|
+
- lib/.DS_Store
|
70
96
|
- lib/http/accept.rb
|
71
97
|
- lib/http/accept/charsets.rb
|
72
98
|
- lib/http/accept/content_type.rb
|
@@ -79,9 +105,11 @@ files:
|
|
79
105
|
- lib/http/accept/sort.rb
|
80
106
|
- lib/http/accept/version.rb
|
81
107
|
homepage: https://github.com/ioquatix/http-accept
|
82
|
-
licenses:
|
83
|
-
|
84
|
-
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata:
|
111
|
+
funding_uri: https://github.com/sponsors/ioquatix/
|
112
|
+
post_install_message:
|
85
113
|
rdoc_options: []
|
86
114
|
require_paths:
|
87
115
|
- lib
|
@@ -96,9 +124,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
124
|
- !ruby/object:Gem::Version
|
97
125
|
version: '0'
|
98
126
|
requirements: []
|
99
|
-
|
100
|
-
|
101
|
-
signing_key:
|
127
|
+
rubygems_version: 3.3.7
|
128
|
+
signing_key:
|
102
129
|
specification_version: 4
|
103
130
|
summary: Parse Accept and Accept-Language HTTP headers.
|
104
131
|
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.simplecov
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,137 +0,0 @@
|
|
1
|
-
# HTTP::Accept
|
2
|
-
|
3
|
-
Provides a robust set of parsers for dealing with HTTP `Accept`, `Accept-Language`, `Accept-Encoding`, `Accept-Charset` headers.
|
4
|
-
|
5
|
-
[![Build Status](https://secure.travis-ci.org/ioquatix/http-accept.svg)](http://travis-ci.org/ioquatix/http-accept)
|
6
|
-
[![Code Climate](https://codeclimate.com/github/ioquatix/http-accept.svg)](https://codeclimate.com/github/ioquatix/http-accept)
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/ioquatix/http-accept/badge.svg)](https://coveralls.io/r/ioquatix/http-accept)
|
8
|
-
|
9
|
-
## Motivation
|
10
|
-
|
11
|
-
I've been [developing some tools for building RESTful endpoints](https://github.com/ioquatix/utopia/blob/master/lib/utopia/controller/respond.rb) and part of that involved versioning. After reviewing the options, I settled on using the `Accept: application/json;version=1` method [as outlined here](http://labs.qandidate.com/blog/2014/10/16/using-the-accept-header-to-version-your-api/).
|
12
|
-
|
13
|
-
The `version=1` part of the `media-type` is a `parameter` as defined by [RFC7231 Section 3.1.1.1](https://tools.ietf.org/html/rfc7231#section-3.1.1.1). After reviewing several existing different options for parsing the `Accept:` header, I noticed a disturbing trend: `header.split(',')`. Because parameters may contain quoted strings which contain commas, this is clearly not an appropriate way to parse the header.
|
14
|
-
|
15
|
-
I am concerned about correctness, security and performance. As such, I implemented this gem to provide a simple high level interface for both parsing and correctly interpreting these headers.
|
16
|
-
|
17
|
-
## Installation
|
18
|
-
|
19
|
-
Add this line to your application's Gemfile:
|
20
|
-
|
21
|
-
```ruby
|
22
|
-
gem 'http-accept'
|
23
|
-
```
|
24
|
-
|
25
|
-
And then execute:
|
26
|
-
|
27
|
-
$ bundle
|
28
|
-
|
29
|
-
Or install it yourself as:
|
30
|
-
|
31
|
-
$ gem install http-accept
|
32
|
-
|
33
|
-
## Usage
|
34
|
-
|
35
|
-
Here are some examples of how to parse various headers.
|
36
|
-
|
37
|
-
### Parsing Accept: headers
|
38
|
-
|
39
|
-
You can parse the incoming `Accept:` header:
|
40
|
-
|
41
|
-
```ruby
|
42
|
-
media_types = HTTP::Accept::MediaTypes.parse("text/html;q=0.5, application/json; version=1")
|
43
|
-
|
44
|
-
expect(media_types[0].mime_type).to be == "application/json"
|
45
|
-
expect(media_types[0].parameters).to be == {'version' => '1'}
|
46
|
-
expect(media_types[1].mime_type).to be == "text/html"
|
47
|
-
expect(media_types[1].parameters).to be == {'q' => '0.5'}
|
48
|
-
```
|
49
|
-
|
50
|
-
Normally, you'd want to match the media types against some set of available mime types:
|
51
|
-
|
52
|
-
```ruby
|
53
|
-
module ToJSON
|
54
|
-
def content_type
|
55
|
-
HTTP::Accept::ContentType.new("application/json", charset: 'utf-8')
|
56
|
-
end
|
57
|
-
|
58
|
-
# Used for inserting into map.
|
59
|
-
def split(*args)
|
60
|
-
content_type.split(*args)
|
61
|
-
end
|
62
|
-
|
63
|
-
def convert(object, options)
|
64
|
-
object.to_json
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
module ToXML
|
69
|
-
# Are you kidding?
|
70
|
-
end
|
71
|
-
|
72
|
-
map = HTTP::Accept::MediaTypes::Map.new
|
73
|
-
map << ToJSON
|
74
|
-
map << ToXML
|
75
|
-
|
76
|
-
object, media_range = map.for(media_types)
|
77
|
-
content = object.convert(model, media_range.parameters)
|
78
|
-
response = [200, {'Content-Type' => object.content_type}, [content]]
|
79
|
-
```
|
80
|
-
|
81
|
-
### Parsing Accept-Language: headers
|
82
|
-
|
83
|
-
You can parse the incoming `Accept-Language:` header:
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
languages = HTTP::Accept::Languages.parse("da, en-gb;q=0.8, en;q=0.7")
|
87
|
-
|
88
|
-
expect(languages[0].locale).to be == "da"
|
89
|
-
expect(languages[1].locale).to be == "en-gb"
|
90
|
-
expect(languages[2].locale).to be == "en"
|
91
|
-
```
|
92
|
-
|
93
|
-
Normally, you'd want to match the languages against some set of available localizations:
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
available_localizations = HTTP::Accept::Languages::Locales.new(["en-nz", "en-us"])
|
97
|
-
|
98
|
-
# Given the languages that the user wants, and the localizations available, compute the set of desired localizations.
|
99
|
-
desired_localizations = available_localizations & languages
|
100
|
-
```
|
101
|
-
|
102
|
-
The `desired_localizations` in the example above is a subset of `available_localizations`.
|
103
|
-
|
104
|
-
`HTTP::Accept::Languages::Locales` provides an efficient data-structure for matching the Accept-Languages header to set of available localizations according to https://tools.ietf.org/html/rfc7231#section-5.3.5 and https://tools.ietf.org/html/rfc4647#section-2.3
|
105
|
-
|
106
|
-
## Contributing
|
107
|
-
|
108
|
-
1. Fork it
|
109
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
110
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
111
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
112
|
-
5. Create new Pull Request
|
113
|
-
|
114
|
-
## License
|
115
|
-
|
116
|
-
Released under the MIT license.
|
117
|
-
|
118
|
-
Copyright, 2016, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
|
119
|
-
Copyright, 2016, by [Matthew Kerwin](http://kerwin.net.au).
|
120
|
-
|
121
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
122
|
-
of this software and associated documentation files (the "Software"), to deal
|
123
|
-
in the Software without restriction, including without limitation the rights
|
124
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
125
|
-
copies of the Software, and to permit persons to whom the Software is
|
126
|
-
furnished to do so, subject to the following conditions:
|
127
|
-
|
128
|
-
The above copyright notice and this permission notice shall be included in
|
129
|
-
all copies or substantial portions of the Software.
|
130
|
-
|
131
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
132
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
133
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
134
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
135
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
136
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
137
|
-
THE SOFTWARE.
|
data/Rakefile
DELETED
data/http-accept.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'http/accept/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "http-accept"
|
8
|
-
spec.version = HTTP::Accept::VERSION
|
9
|
-
spec.authors = ["Samuel Williams"]
|
10
|
-
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
11
|
-
|
12
|
-
spec.summary = %q{Parse Accept and Accept-Language HTTP headers.}
|
13
|
-
spec.homepage = "https://github.com/ioquatix/http-accept"
|
14
|
-
|
15
|
-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
-
spec.bindir = "exe"
|
17
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
-
end
|