Sha256: efbe1c4b809816247b1f0db08ce171a64372bc4fac26c4f912a05f2db8f87a14

Contents?: true

Size: 1013 Bytes

Versions: 9

Compression:

Stored size: 1013 Bytes

Contents

module Restfully
  class Link
    
    VALID_RELATIONSHIPS = %w{member parent collection self alternate}
    RELATIONSHIPS_REQUIRING_TITLE = %w{collection member}
    
    attr_reader :rel, :title, :href, :errors
    
    def initialize(attributes = {})
      @rel = attributes['rel']
      @title = attributes['title']
      @href = attributes['href']
      @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? || href.empty?
        errors << "href cannot be empty."
      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

9 entries across 9 versions & 2 rubygems

Version Path
crohr-restfully-0.1.1 lib/restfully/link.rb
crohr-restfully-0.2.1 lib/restfully/link.rb
crohr-restfully-0.2.2 lib/restfully/link.rb
restfully-0.3.2 lib/restfully/link.rb
restfully-0.3.1 lib/restfully/link.rb
restfully-0.3.0 lib/restfully/link.rb
restfully-0.2.3 lib/restfully/link.rb
restfully-0.2.2 lib/restfully/link.rb
restfully-0.2.1 lib/restfully/link.rb