http_link_header 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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in http_link_header.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = HttpLinkHeader
2
+
3
+ Parses and generates HTTP Link headers as defined by
4
+ {IETF RFC 5988}[http://tools.ietf.org/html/rfc5988].
5
+
6
+ Author:: Clyde Law (mailto:claw@alum.mit.edu)
7
+ License:: Released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "http_link_header/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "http_link_header"
7
+ s.version = HttpLinkHeader::VERSION
8
+ s.authors = ["Clyde Law"]
9
+ s.email = ["claw@alum.mit.edu"]
10
+ s.homepage = %q{http://github.com/Umofomia/http_link_header}
11
+ s.summary = %q{Parses and generates HTTP Link headers as defined by IETF RFC 5988.}
12
+ s.description = %q{Parses and generates HTTP Link headers as defined by IETF RFC 5988.}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "http_link_header"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,76 @@
1
+ class HttpLinkHeader::HttpLinkParams < ::Hash
2
+ DELIMETER = ';'
3
+ RELATION_TYPE_ATTRIBUTES = Set['rel', 'rev']
4
+
5
+ class RelationTypes < ::Set
6
+
7
+ def initialize(relation_types)
8
+ if relation_types.is_a?(Enumerable)
9
+ super(relation_types)
10
+ elsif relation_types.is_a?(String)
11
+ add(relation_types)
12
+ else
13
+ raise ArgumentError, "invalid relation-types: #{relation_types}"
14
+ end
15
+ end
16
+
17
+ def add(relation_types)
18
+ relation_types.strip.gsub!(/\A"|"\z/, '').split(' ').each do |relation_type|
19
+ relation_type = relation_type.strip
20
+ super(relation_type.downcase) unless relation_type.empty?
21
+ end
22
+ end
23
+
24
+ def include?(relation_type)
25
+ super(relation_type.downcase)
26
+ end
27
+
28
+ def to_s
29
+ %Q{"#{to_a.join(' ')}"}
30
+ end
31
+
32
+ end
33
+
34
+ def initialize(link_params)
35
+ add(link_params)
36
+ end
37
+
38
+ def [](attribute)
39
+ super(attribute.to_s)
40
+ end
41
+
42
+ def []=(attribute, value)
43
+ if RELATION_TYPE_ATTRIBUTES.include?(attribute.to_s)
44
+ if self[attribute]
45
+ self[attribute].add(value)
46
+ else
47
+ super(attribute.to_s, RelationTypes.new(value))
48
+ end
49
+ else
50
+ super(attribute.to_s, value)
51
+ end
52
+ end
53
+
54
+ def add(link_params)
55
+ link_params.split(DELIMETER).each do |link_param|
56
+ link_param.strip!
57
+ next if link_param.empty?
58
+
59
+ if link_param_match = link_param.match(/\A([\w!#\$&\+\-\.\^`\|~]+\*?)\s*(=\s*(.*))?\z/)
60
+ attribute, value = link_param_match.values_at(1, 3)
61
+ self[attribute.strip] = value ? value.strip : nil
62
+ else
63
+ raise ArgumentError, "invalid link-param: #{link_param}"
64
+ end
65
+ end
66
+ end
67
+
68
+ def to_a
69
+ map { |attribute, value| value ? "#{attribute}=#{value.to_s}" : attribute }
70
+ end
71
+
72
+ def to_s
73
+ to_a.join("#{DELIMETER} ")
74
+ end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ class HttpLinkHeader < ::Hash
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,62 @@
1
+ require "http_link_header/version"
2
+ require "http_link_header/http_link_params"
3
+
4
+ class HttpLinkHeader < ::Hash
5
+ DELIMETER = ','
6
+
7
+ def initialize(link_header = [])
8
+ link_header = link_header.split(DELIMETER) if link_header.is_a?(String)
9
+ link_header.each { |link_value| add(link_value) }
10
+ end
11
+
12
+ def [](uri_reference)
13
+ super(uri_reference.to_s)
14
+ end
15
+
16
+ def []=(uri_reference, link_params)
17
+ link_params = nil if link_params && link_params.empty?
18
+ if self[uri_reference]
19
+ self[uri_reference].add(link_params) unless link_params.nil?
20
+ else
21
+ super(uri_reference.to_s, link_params.nil? ? nil : HttpLinkParams.new(link_params))
22
+ end
23
+ end
24
+
25
+ def add(link_value)
26
+ if link_value_match = link_value.strip.match(/\A<([^>]+)>\s*(;\s*(.*))?\z/)
27
+ uri_reference, link_params = link_value_match.values_at(1, 3)
28
+ self[uri_reference.strip] = link_params ? link_params.strip : nil
29
+ else
30
+ raise ArgumentError, "invalid link-value: #{link_value}"
31
+ end
32
+ end
33
+
34
+ def to_a
35
+ map do |uri_reference, link_params|
36
+ link_value = "<#{uri_reference}>"
37
+ link_value << "#{HttpLinkParams::DELIMETER} #{link_params.to_s}" if link_params && !link_params.empty?
38
+ link_value
39
+ end
40
+ end
41
+
42
+ def to_s
43
+ to_a.join("#{DELIMETER} ")
44
+ end
45
+
46
+ # Define methods for each relation type attribute to find URIs that have
47
+ # the respective attribute set to the specified relation type.
48
+ #
49
+ HttpLinkParams::RELATION_TYPE_ATTRIBUTES.each do |relation_type_attribute|
50
+ class_eval(<<-RELATION_TYPE_METHOD, __FILE__, __LINE__ + 1)
51
+ def #{relation_type_attribute}(relation_type)
52
+ each do |uri_reference, link_params|
53
+ if (relation_types = link_params[#{relation_type_attribute.inspect}]) && relation_types.include?(relation_type.to_s)
54
+ return uri_reference
55
+ end
56
+ end
57
+ nil
58
+ end
59
+ RELATION_TYPE_METHOD
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_link_header
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Clyde Law
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-12 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Parses and generates HTTP Link headers as defined by IETF RFC 5988.
23
+ email:
24
+ - claw@alum.mit.edu
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.rdoc
35
+ - Rakefile
36
+ - http_link_header.gemspec
37
+ - lib/http_link_header.rb
38
+ - lib/http_link_header/http_link_params.rb
39
+ - lib/http_link_header/version.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/Umofomia/http_link_header
42
+ licenses:
43
+ - MIT
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: http_link_header
70
+ rubygems_version: 1.6.2
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Parses and generates HTTP Link headers as defined by IETF RFC 5988.
74
+ test_files: []
75
+