Sha256: 450603ab334286d10aa0a64ebeb81b744b965c0d8ec79600e634b3303017231d

Contents?: true

Size: 1 KB

Versions: 13

Compression:

Stored size: 1 KB

Contents

require 'uri'
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 = 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

13 entries across 13 versions & 1 rubygems

Version Path
restfully-0.5.10 lib/restfully/link.rb
restfully-0.5.9 lib/restfully/link.rb
restfully-0.5.8 lib/restfully/link.rb
restfully-0.5.7 lib/restfully/link.rb
restfully-0.5.6 lib/restfully/link.rb
restfully-0.5.5 lib/restfully/link.rb
restfully-0.5.4 lib/restfully/link.rb
restfully-0.5.3 lib/restfully/link.rb
restfully-0.5.2 lib/restfully/link.rb
restfully-0.5.1 lib/restfully/link.rb
restfully-0.5.0 lib/restfully/link.rb
restfully-0.4.1 lib/restfully/link.rb
restfully-0.4.0 lib/restfully/link.rb