Class: CoRE::Link
- Inherits:
-
Object
- Object
- CoRE::Link
- Defined in:
- lib/core/link.rb
Overview
Implements CoRE Link Format (RFC6690) tools.ietf.org/html/rfc6690 TODO Handle repeated attributes
Constant Summary collapse
- VALID_ATTRS =
[ :anchor, :ct, :exp, :hreflang, :if, :ins, :media, :obs, :rt, :rel, :rev, :sz, :title, :type ]
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
- .parse(data) ⇒ Object
-
.parse_multiple(data) ⇒ Object
TODO Handle misplaced commas.
Instance Method Summary collapse
-
#initialize(uri, attrs = {}) ⇒ Link
constructor
A new instance of Link.
- #merge!(hash) ⇒ Object
- #method_missing(symbol, *args) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(uri, attrs = {}) ⇒ Link
Returns a new instance of Link.
16 17 18 19 20 21 |
# File 'lib/core/link.rb', line 16 def initialize(uri, attrs = {}) @uri = uri || raise(ArgumentError.new('URI can not be unset.')) @attrs = attrs validate_attrs!(@attrs) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/core/link.rb', line 29 def method_missing(symbol, *args) attr = symbol.to_s.delete('=').to_sym if !VALID_ATTRS.include?(attr) raise ArgumentError.new("Invalid attribute «#{attr}».") end if symbol[-1] == '=' @attrs[attr] = args.first.to_s else @attrs[attr] end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
14 15 16 |
# File 'lib/core/link.rb', line 14 def attrs @attrs end |
#uri ⇒ Object
Returns the value of attribute uri.
13 14 15 |
# File 'lib/core/link.rb', line 13 def uri @uri end |
Class Method Details
.parse(data) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/core/link.rb', line 53 def self.parse(data) parts = data.split(';') uri = parts.shift uri = uri.match(/\A\<(.+)\>\z/) if uri.nil? raise ArgumentError.new("Invalid URI in '#{data}'.") end link = Link.new(uri[1]) parts.each do |part| attr, value = part.split('=') attr = (attr + '=').to_sym value = value.delete('"') unless value.nil? link.send(attr, value) end link end |
.parse_multiple(data) ⇒ Object
TODO Handle misplaced commas
78 79 80 81 82 83 84 85 86 |
# File 'lib/core/link.rb', line 78 def self.parse_multiple(data) results = [] data.split(',').each do |part| results << self.parse(part) end results end |
Instance Method Details
#merge!(hash) ⇒ Object
23 24 25 26 27 |
# File 'lib/core/link.rb', line 23 def merge!(hash) a = @attrs.merge(hash) validate_attrs!(a) @attrs = a end |
#to_s ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/core/link.rb', line 43 def to_s s = "<#{@uri}>" @attrs.sort.each do |attr, value| s += ";#{attr}=\"#{value}\"" end s end |