class ApiHammer::Weblink
a RFC5988 Web Link
Attributes
link attributes
the context uri of the link, as an Addressable URI. this URI must be absolute, and the target_uri
may be resolved against it. this is most typically the request URI of a request to a service
the context uri of the link, as an Addressable URI. this URI must be absolute, and the target_uri
may be resolved against it. this is most typically the request URI of a request to a service
returns the target URI as an Addressable::URI
returns the target URI as an Addressable::URI
Public Class Methods
# File lib/api_hammer/weblink.rb, line 54 def initialize(target_uri, attributes, context_uri=nil) @target_uri = to_addressable_uri(target_uri) @attributes = attributes @context_uri = to_addressable_uri(context_uri) end
parses an array of Web Links from the value an HTTP Link header, as described in tools.ietf.org/html/rfc5988#section-5
returns an Array of Weblink
objects
# File lib/api_hammer/weblink.rb, line 19 def self.parse_link_value(link_value, context_uri=nil) links = [] return links unless link_value attr_char = /[a-zA-Z0-9!#\$&+\-.^_`|~]/ # defined in https://tools.ietf.org/html/rfc5987#section-3.2.1 ptoken = %r([a-zA-Z0-9!#\$%&'()*+\-./:<=>?@\[\]^_`{|}~]) quoted_string = /"([^"]*)"/ require 'strscan' ss = StringScanner.new(link_value) parse_fail = proc do raise ParseError, "Unable to parse link value: #{link_value} " + "around character #{ss.pos}: #{ss.peek(link_value.length - ss.pos)}" end while !ss.eos? # get the target_uri, within some angle brackets ss.scan(/\s*<([^>]+)>/) || parse_fail.call target_uri = ss[1] attributes = {} # get the attributes: semicolon, some attr_chars, an optional asterisk, equals, and a quoted # string or series of unquoted ptokens while ss.scan(/\s*;\s*(#{attr_char.source}+\*?)\s*=\s*(?:#{quoted_string.source}|(#{ptoken.source}+))\s*/) attributes[ss[1]] = ss[2] || ss[3] end links << new(target_uri, attributes, context_uri) unless ss.eos? # either the string ends or has a comma followed by another link ss.scan(/\s*,\s*/) || parse_fail.call end end links end
Public Instance Methods
subscript returns an attribute of this Link, if defined, otherwise nil
# File lib/api_hammer/weblink.rb, line 88 def [](attribute_key) @attributes[attribute_key] end
attempts to make target_uri
absolute, using context_uri
if available. raises if there is not information available to make an absolute target URI
# File lib/api_hammer/weblink.rb, line 74 def absolute_target_uri if target_uri.absolute? target_uri elsif context_uri context_uri + target_uri else raise NoContextError, "Target URI is relative but no Context URI given - cannot determine absolute target URI" end end
link rel attribute
# File lib/api_hammer/weblink.rb, line 93 def rel self['rel'] end
compares relation types in a case-insensitive manner as mandated in tools.ietf.org/html/rfc5988#section-4.1
# File lib/api_hammer/weblink.rb, line 100 def rel?(other_rel) rel && other_rel && rel.downcase == other_rel.downcase end
a string of this weblink, appropriate for adding to a Link header
# File lib/api_hammer/weblink.rb, line 105 def to_s "<#{target_uri}>" + attributes.map { |k,v| %Q(; #{k}="#{v}") }.join('') end
Private Instance Methods
if uri is nil, returns nil; otherwise, tries to return a Addressable::URI
# File lib/api_hammer/weblink.rb, line 111 def to_addressable_uri(uri) uri.nil? || uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri) end