Sha256: d4bf845be7b2d15b1866c4ca32d3cf597e408e04f9e2305d2c80a0c3df711673

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

require 'uri'
module Restfully
  class Link
    
    VALID_RELATIONSHIPS = %w{member parent collection self alternate next}
    RELATIONSHIPS_REQUIRING_TITLE = %w{collection member}
    
    attr_reader :rel, :title, :href, :errors
    
    def initialize(attributes = {})
      @rel = attributes['rel']
      @title = attributes['title']
      @href = URI.parse(attributes['href'].to_s)
      @resolvable = attributes['resolvable'] || false
      @resolved = attributes['resolved'] || false
    end
    
    def resolvable?; @resolvable == true; end
    def resolved?; @resolved == true; end
    def self?; @rel == 'self'; end
    
    def valid?
      @errors = []
      if href.nil?
        errors << "href cannot be nil."
      end
      unless VALID_RELATIONSHIPS.include?(rel)
        errors << "#{rel} is not a valid link relationship."
      end
      if (!title || title.empty?) && RELATIONSHIPS_REQUIRING_TITLE.include?(rel)
        errors << "#{rel} #{href} has no title."
      end
      errors.empty?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
restfully-0.6.3 lib/restfully/link.rb
restfully-0.6.2 lib/restfully/link.rb
restfully-0.6.1 lib/restfully/link.rb
restfully-0.6.0 lib/restfully/link.rb