link_header 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 0.0.1 2009-04-04
2
+
3
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ link_header, Copyright (c) 2009 Mike Burrows
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,12 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ PostInstall.txt
5
+ README.rdoc
6
+ Rakefile
7
+ lib/link_header.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ test/test_helper.rb
12
+ test/test_link_header.rb
data/PostInstall.txt ADDED
@@ -0,0 +1 @@
1
+ link-header gem installed. See README.rdoc in the installation directory or at http://github.com/asplake/link_header.
data/README.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = link_header README
2
+
3
+ Parse and format HTTP link headers as described in the draft spec http://tools.ietf.org/id/draft-nottingham-http-link-header-06.txt
4
+
5
+ == Description
6
+
7
+ Converts conforming link headers to and from text, LinkHeader objects and corresponding (JSON-friendly) Array representations.
8
+
9
+ == Installation
10
+
11
+ sudo gem install link-header
12
+
13
+ == Author
14
+
15
+ Mike Burrows (asplake), email mailto:mjb@asplake.co.uk, website positiveincline.com[http://positiveincline.com]
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ $:.push File.dirname(__FILE__) + '/lib'
3
+ require 'link_header'
4
+ require 'hoe'
5
+
6
+ # Generate all the Rake tasks
7
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
8
+ $hoe = Hoe.spec 'link_header' do
9
+ developer('Mike Burrows', 'mjb@asplake.co.uk')
10
+ self.version = LinkHeader::VERSION
11
+ self.readme_file = "README.rdoc"
12
+ self.changes = paragraphs_of("History.txt", 0..1).join("\n\n")
13
+ self.rubyforge_name = 'link-header'
14
+ self.url = 'http://github.com/asplake/link_header/tree'
15
+ self.extra_deps = [
16
+ ]
17
+ self.extra_dev_deps = [
18
+ ['newgem', ">= #{::Newgem::VERSION}"]
19
+ ]
20
+
21
+ self.clean_globs |= %w[**/.DS_Store tmp *.log]
22
+ path = (rubyforge_name == name) ? rubyforge_name : "\#{rubyforge_name}/\#{name}"
23
+ self.remote_rdoc_dir = File.join(path.gsub(/^#{rubyforge_name}\/?/,''), 'rdoc')
24
+ self.rsync_args = '-av --delete --ignore-errors'
25
+ end
26
+
27
+ task :info do
28
+ puts "version=#{LinkHeader::VERSION}"
29
+ [:description, :summary, :changes, :author, :url].each do |attr|
30
+ puts "#{attr}=#{$hoe.send(attr)}\n"
31
+ end
32
+ end
33
+
34
+ require 'newgem/tasks' # load /tasks/*.rake
35
+ Dir['tasks/**/*.rake'].each { |t| load t }
36
+
@@ -0,0 +1,104 @@
1
+ require "strscan"
2
+
3
+ class LinkHeader
4
+ VERSION = "0.0.1"
5
+
6
+ # an array of Link objects
7
+ attr_reader :links
8
+
9
+ #
10
+ # Initialize from array of Link objects or the data from which said Link objects can be created
11
+ #
12
+ def initialize(links=[])
13
+ @links= links.map{|l| l.kind_of?(Link) ? l : Link.new(*l)}
14
+ end
15
+
16
+ #
17
+ # Convert to a JSON-friendly array
18
+ #
19
+ def to_a
20
+ links.map{|l| l.to_a}
21
+ end
22
+
23
+ #
24
+ # Convert to string representation as per the link header spec
25
+ #
26
+ def to_s
27
+ links.join(', ')
28
+ end
29
+
30
+ HREF = / *<([^>]*)> *;? */
31
+ TOKEN = /([a-zA-Z0-9_\-]+)/
32
+ QUOTED = /"([^"]*)"/
33
+ ATTR = /#{TOKEN} *= *(#{TOKEN}|#{QUOTED}) */
34
+ SEMI = /; */
35
+ COMMA = /, */
36
+
37
+ #
38
+ # Parse a link header, returning a new LinkHeader object
39
+ #
40
+ def self.parse(link_header)
41
+ return new unless link_header
42
+
43
+ scanner = StringScanner.new(link_header)
44
+ links = []
45
+ while scanner.scan(HREF)
46
+ href = scanner[1]
47
+ attrs = []
48
+ while scanner.scan(ATTR)
49
+ attrs.push([scanner[1], scanner[3] || scanner[4]])
50
+ break unless scanner.scan(SEMI)
51
+ end
52
+ links.push(Link.new(href, attrs))
53
+ break unless scanner.scan(COMMA)
54
+ end
55
+
56
+ new(links)
57
+ end
58
+
59
+ #
60
+ # Represents a link - an href and a list of attributes (key value pairs)
61
+ #
62
+ class Link
63
+ # The Link's href (a URI string)
64
+ attr_reader :href
65
+
66
+ # The link's attributes, an array of key-value pairs
67
+ attr_reader :attr_pairs
68
+
69
+ #
70
+ # Initialize a Link from an href and attribute list
71
+ #
72
+ def initialize(href, attr_pairs)
73
+ @href, @attr_pairs = href, attr_pairs
74
+ end
75
+
76
+ #
77
+ # Lazily convert the attribute list to a Hash
78
+ #
79
+ def attrs
80
+ @attrs ||= Hash[*attr_pairs.flatten]
81
+ end
82
+
83
+ #
84
+ # Access an attribute by key
85
+ #
86
+ def [](key)
87
+ attrs[key]
88
+ end
89
+
90
+ #
91
+ # Convert to a JSON-friendly Array
92
+ #
93
+ def to_a
94
+ [href, attr_pairs]
95
+ end
96
+
97
+ #
98
+ # Convert to string representation as per the link header spec
99
+ #
100
+ def to_s
101
+ (["<#{href}>"] + attr_pairs.map{|k, v| "#{k}=\"#{v}\""}).join('; ')
102
+ end
103
+ end
104
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/link_header.rb'}"
9
+ puts "Loading link_header gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/link_header'
@@ -0,0 +1,52 @@
1
+ require "test/unit"
2
+
3
+ LINK_HEADER_S_A = [
4
+ %Q(<http://example.com/>; rel="up"; meta="bar"),
5
+ %Q(<http://example.com/foo>; rel="self"),
6
+ %Q(<http://example.com/>)
7
+ ]
8
+ LINK_HEADER_S = LINK_HEADER_S_A.join(', ')
9
+
10
+ LINK_HEADER_A = [
11
+ ["http://example.com/", [["rel", "up"], ["meta", "bar"]]],
12
+ ["http://example.com/foo", [["rel", "self"]]],
13
+ ["http://example.com/", []]
14
+ ]
15
+
16
+ class TestLinkHeader < Test::Unit::TestCase
17
+ def test_new_link
18
+ link = LinkHeader::Link.new(*LINK_HEADER_A[0])
19
+ assert_equal("http://example.com/", link.href)
20
+ assert_equal([["rel", "up"], ["meta", "bar"]], link.attr_pairs)
21
+ assert_equal({"rel"=>"up", "meta"=>"bar"}, link.attrs)
22
+ end
23
+
24
+ def test_link_to_a
25
+ assert_equal(LINK_HEADER_A[0], LinkHeader::Link.new(*LINK_HEADER_A[0]).to_a)
26
+ end
27
+
28
+ def test_link_to_s
29
+ assert_equal(LINK_HEADER_S_A[0], LinkHeader::Link.new(*LINK_HEADER_A[0]).to_s)
30
+ end
31
+
32
+ def test_new_link_header
33
+ link_header = LinkHeader.new(LINK_HEADER_A)
34
+ assert_equal(LINK_HEADER_A.length, link_header.links.length)
35
+ link = link_header.links[0]
36
+ assert_equal("http://example.com/", link.href)
37
+ assert_equal([["rel", "up"], ["meta", "bar"]], link.attr_pairs)
38
+ assert_equal({"rel"=>"up", "meta"=>"bar"}, link.attrs)
39
+ end
40
+
41
+ def test_link_header_to_a
42
+ assert_equal(LINK_HEADER_A, LinkHeader.new(LINK_HEADER_A).to_a)
43
+ end
44
+
45
+ def test_parse_link_header
46
+ assert_equal(LINK_HEADER_A, LinkHeader.parse(LINK_HEADER_S).to_a)
47
+ end
48
+
49
+ def test_link_header_to_s
50
+ assert_equal(LINK_HEADER_S, LinkHeader.new(LINK_HEADER_A).to_s)
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: link_header
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Burrows
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-23 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.5.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.2
34
+ version:
35
+ description: Converts conforming link headers to and from text, LinkHeader objects and corresponding (JSON-friendly) Array representations.
36
+ email:
37
+ - mjb@asplake.co.uk
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - PostInstall.txt
46
+ files:
47
+ - History.txt
48
+ - LICENSE
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.rdoc
52
+ - Rakefile
53
+ - lib/link_header.rb
54
+ - script/console
55
+ - script/destroy
56
+ - script/generate
57
+ - test/test_helper.rb
58
+ - test/test_link_header.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/asplake/link_header/tree
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --main
66
+ - README.rdoc
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project: link-header
84
+ rubygems_version: 1.3.4
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Converts conforming link headers to and from text, LinkHeader objects and corresponding (JSON-friendly) Array representations.
88
+ test_files:
89
+ - test/test_helper.rb
90
+ - test/test_link_header.rb