Sha256: fa7b46b426f8f0f27c073e29bf86cebddbf97f97a588e931fd48dee6072e9e7b

Contents?: true

Size: 815 Bytes

Versions: 8

Compression:

Stored size: 815 Bytes

Contents

module Restfulie::Client::HTTP
  
  # Adds support to extracting and navigating through a link in the representation header.
  module LinkHeader
    
    def links
      r = link_header_to_array
      Medie::Xml::Links.new(r)
    end
    
    def link(rel)
      links[rel]
    end
        
    private

    def link_header_to_array
      links = self["link"][0].split(",")
      links.map do |link|
        string_to_hash(link)
      end
    end

    def string_to_hash(l)
      c = l[/<[^>]*/]
      uri = c[1..c.size]
      rest = l[/;.*/]
      rel = extract(rest, "rel")
      type = extract(rest, "type")
      { "href" => uri, "rel" => rel, "type" => type }
    end
    
    def extract(from, what)
      found = Regexp.new("#{what}=\"([^\"]*)\"").match(from)
      found ? found[1] : nil
    end

  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
restfulie-nosqlite-1.0.4 lib/restfulie/client/http/link_header.rb
restfulie-1.1.1 lib/restfulie/client/http/link_header.rb
restfulie-1.1.0 lib/restfulie/client/http/link_header.rb
restfulie-nosqlite-1.0.3 lib/restfulie/client/http/link_header.rb
restfulie-1.0.3 lib/restfulie/client/http/link_header.rb
restfulie-1.0.0 lib/restfulie/client/http/link_header.rb
restfulie-1.0.0.beta5 lib/restfulie/client/http/link_header.rb
restfulie-1.0.0.beta4 lib/restfulie/client/http/link_header.rb