http_header_link 0.1.2
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/Gemfile +9 -0
- data/README.md +62 -0
- data/Rakefile +6 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/http_header_link.gemspec +26 -0
- data/lib/http_header_link.rb +5 -0
- data/lib/http_header_link/link.rb +75 -0
- data/lib/http_header_link/link_header.rb +70 -0
- data/lib/http_header_link/version.rb +3 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e867bd978028405d356dd834248d7f7fcadf78e7caf6e668da2b6ed517e71624
|
|
4
|
+
data.tar.gz: dd1f43402be3a14f5275093737118018913af44f3ab30b0695aa0e71aecb23c1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 68ac72d5939c710c984de4c0e69f57aa3102203856bb83eb31bbb69f17fca489c651ce35a85fef9b54dd1a05ee49e8b283d2e562c06939096f95a6a66f2143ae
|
|
7
|
+
data.tar.gz: d0566b0c8d4c758414abf355a424d290ed575637d259f3e0a36b98f9ed4e5243886033af113498818ead2334e1d2f76d14376394e0bdd9924e4b8c3727194365
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# HttpHeaderLink
|
|
2
|
+
|
|
3
|
+
The library handling the Link header, written in Ruby.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'http_header_link', git: 'git@github.com:sainu/http_header_link.git', branch: 'master'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install http_header_link git@github.com:sainu/http_header_link.git master
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Generating Link Header
|
|
24
|
+
|
|
25
|
+
#### Basic
|
|
26
|
+
|
|
27
|
+
```rb
|
|
28
|
+
link_header = HttpHeaderLink::LinkHeader.new
|
|
29
|
+
link_header.add_link('/?page=1', rel: 'previous')
|
|
30
|
+
link_header.add_link('/?page=3', rel: 'next')
|
|
31
|
+
link_header.generate
|
|
32
|
+
=> "</?page=1>; rel=\"previous\", </?page=3>; rel=\"next\""
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### Configure Base URL
|
|
36
|
+
|
|
37
|
+
```rb
|
|
38
|
+
link_header = HttpHeaderLink::LinkHeader.new(base_url: 'http://localhost')
|
|
39
|
+
link_header.add_link('/?page=1', rel: 'previous')
|
|
40
|
+
link_header.add_link('/?page=3', rel: 'next')
|
|
41
|
+
link_header.generate
|
|
42
|
+
=> "<http://localhost/?page=1>; rel=\"previous\", <http://localhost/?page=3>; rel=\"next\""
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Parsing Link Header
|
|
46
|
+
|
|
47
|
+
```rb
|
|
48
|
+
link_header = HttpHeaderLink::LinkHeader.parse('</?page=2>; rel="next"')
|
|
49
|
+
next_page = link_header.find_by(:rel, 'next')&.get_query(:page)&.to_i
|
|
50
|
+
=> 2
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
56
|
+
|
|
57
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
58
|
+
|
|
59
|
+
## Contributing
|
|
60
|
+
|
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sainuio/http_header_link.
|
|
62
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "http_header_link"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
require "pry"
|
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require_relative 'lib/http_header_link/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "http_header_link"
|
|
5
|
+
spec.version = HttpHeaderLink::VERSION
|
|
6
|
+
spec.authors = ["sainu"]
|
|
7
|
+
spec.email = ["katsutoshi.saino@gmail.com"]
|
|
8
|
+
|
|
9
|
+
spec.summary = %q{The library handling the Link header, written in Ruby.}
|
|
10
|
+
spec.description = %q{The library handling the Link header, written in Ruby.}
|
|
11
|
+
spec.homepage = "https://github.com/sainuio/http_header_link/"
|
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
13
|
+
|
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/sainuio/http_header_link/"
|
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/sainuio/http_header_link/blob/master/CHANGELOG.md"
|
|
17
|
+
|
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
22
|
+
end
|
|
23
|
+
spec.bindir = "exe"
|
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HttpHeaderLink
|
|
4
|
+
class Link
|
|
5
|
+
# @return [String]
|
|
6
|
+
attr_reader :url
|
|
7
|
+
attr_reader :attributes
|
|
8
|
+
|
|
9
|
+
# @param [String] url
|
|
10
|
+
# @param [Hash] options
|
|
11
|
+
# @option options [String] :rel
|
|
12
|
+
# @option options [String] :title
|
|
13
|
+
# @option options [String] :hreflang
|
|
14
|
+
# @option options [String] :media
|
|
15
|
+
# @option options [String] :type
|
|
16
|
+
def initialize(url, **options)
|
|
17
|
+
@url = url
|
|
18
|
+
@attributes = options.slice(:rel, :title, :hreflang, :media, :type)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @param [String] base_url
|
|
22
|
+
# @return [String]
|
|
23
|
+
def generate(base_url: nil)
|
|
24
|
+
src = base_url ? URI.join(base_url, url) : url
|
|
25
|
+
str = "<#{src}>"
|
|
26
|
+
attributes.each do |name, value|
|
|
27
|
+
str += %(; #{name}="#{value}")
|
|
28
|
+
end
|
|
29
|
+
str
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @param [Symbol] name
|
|
33
|
+
# @return [String, nil]
|
|
34
|
+
def get_query(name)
|
|
35
|
+
query_hash[name.to_s]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @return [String, nil]
|
|
39
|
+
def rel
|
|
40
|
+
attributes[:rel]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [String, nil]
|
|
44
|
+
def hreflang
|
|
45
|
+
attributes[:hreflang]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @return [String, nil]
|
|
49
|
+
def title
|
|
50
|
+
attributes[:title]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [String, nil]
|
|
54
|
+
def media
|
|
55
|
+
attributes[:media]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @return [String, nil]
|
|
59
|
+
def type
|
|
60
|
+
attributes[:type]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
# @return [URI]
|
|
66
|
+
def uri
|
|
67
|
+
URI.parse(url)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @return [Hash{ String => String }]
|
|
71
|
+
def query_hash
|
|
72
|
+
URI::decode_www_form(uri.query || '').to_h
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HttpHeaderLink
|
|
4
|
+
class LinkHeader
|
|
5
|
+
class << self
|
|
6
|
+
# @param [String] target
|
|
7
|
+
# @return [HttpHeaderLink::ParseResult]
|
|
8
|
+
def parse(target)
|
|
9
|
+
parts = target.split(',')
|
|
10
|
+
links = parts.map do |part|
|
|
11
|
+
sections = part.split(';')
|
|
12
|
+
url = sections.shift[/<(.*)>/, 1]
|
|
13
|
+
options = {}
|
|
14
|
+
sections.each do |section|
|
|
15
|
+
name, val = section.split('=').map(&:strip)
|
|
16
|
+
val.slice!(0, 1) if val.start_with?('"')
|
|
17
|
+
val.slice!(-1, 1) if val.end_with?('"')
|
|
18
|
+
options[name.to_sym] = val
|
|
19
|
+
end
|
|
20
|
+
Link.new(url, **options)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
new(links)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [Array<HttpHeaderLink::Link>]
|
|
28
|
+
attr_reader :links
|
|
29
|
+
|
|
30
|
+
# @param [Array<HttpHeaderLink::Link>] links
|
|
31
|
+
# @param [Hash] options
|
|
32
|
+
# @option options [String] :base_url
|
|
33
|
+
def initialize(*links, **options)
|
|
34
|
+
_links = links.flatten
|
|
35
|
+
@links = _links.empty? ? [] : _links
|
|
36
|
+
@base_url = options.delete(:base_url)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [String]
|
|
40
|
+
def generate
|
|
41
|
+
links.flatten.compact.map { |l| l.generate(base_url: base_url) }.join(', ')
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @param [String] url
|
|
45
|
+
# @param [Hash] options
|
|
46
|
+
# @option options [String] :rel
|
|
47
|
+
# @option options [String] :title
|
|
48
|
+
# @option options [String] :hreflang
|
|
49
|
+
# @option options [String] :media
|
|
50
|
+
# @option options [String] :type
|
|
51
|
+
def add_link(url, **options)
|
|
52
|
+
links << Link.new(url, options)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @return [Boolean]
|
|
56
|
+
def present?
|
|
57
|
+
!links.empty?
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @param [Symbol] attribute
|
|
61
|
+
# @param [String] value
|
|
62
|
+
def find_by(attribute, value)
|
|
63
|
+
links.find { |link| link.public_send(attribute) == value }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
attr_reader :base_url
|
|
69
|
+
end
|
|
70
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: http_header_link
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- sainu
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-11-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: The library handling the Link header, written in Ruby.
|
|
14
|
+
email:
|
|
15
|
+
- katsutoshi.saino@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".gitignore"
|
|
21
|
+
- ".rspec"
|
|
22
|
+
- ".travis.yml"
|
|
23
|
+
- Gemfile
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- bin/console
|
|
27
|
+
- bin/setup
|
|
28
|
+
- http_header_link.gemspec
|
|
29
|
+
- lib/http_header_link.rb
|
|
30
|
+
- lib/http_header_link/link.rb
|
|
31
|
+
- lib/http_header_link/link_header.rb
|
|
32
|
+
- lib/http_header_link/version.rb
|
|
33
|
+
homepage: https://github.com/sainuio/http_header_link/
|
|
34
|
+
licenses: []
|
|
35
|
+
metadata:
|
|
36
|
+
homepage_uri: https://github.com/sainuio/http_header_link/
|
|
37
|
+
source_code_uri: https://github.com/sainuio/http_header_link/
|
|
38
|
+
changelog_uri: https://github.com/sainuio/http_header_link/blob/master/CHANGELOG.md
|
|
39
|
+
post_install_message:
|
|
40
|
+
rdoc_options: []
|
|
41
|
+
require_paths:
|
|
42
|
+
- lib
|
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 2.3.0
|
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
requirements: []
|
|
54
|
+
rubygems_version: 3.1.2
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: The library handling the Link header, written in Ruby.
|
|
58
|
+
test_files: []
|