Sha256: 18b768988000bafbbf908af1129c4e1f9525a4091746d76605ceec59b7e7c0f8

Contents?: true

Size: 959 Bytes

Versions: 19

Compression:

Stored size: 959 Bytes

Contents

require 'uri'
module Restfully
  class Link

    attr_reader :rel, :title, :href, :errors, :type

    def initialize(attributes = {})
      attributes = attributes.symbolize_keys
      @rel = attributes[:rel]
      @title = attributes[:title] || @rel
      @href = URI.parse(attributes[:href].to_s)
      @type = attributes[:type]
      @id = attributes[:id]
    end

    def self?; @rel == 'self'; end

    def types
      type.split(";")
    end

    def valid?
      @errors = []
      if type.nil? || type.empty?
        errors << "type cannot be blank"
      elsif media_type.nil?
        errors << "cannot find a MediaType for type #{type.inspect}"
      end
      if href.nil?
        errors << "href cannot be nil"
      end
      errors.empty?
    end

    def media_type
      @media_type ||= MediaType.find(type)
    end # def catalog
    
    def id
      title.to_s.downcase.gsub(/[^a-z]/,'_').squeeze('_').to_sym
    end

  end # class Link
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
restfully-1.0.0.rc2 lib/restfully/link.rb
restfully-1.0.0.rc1 lib/restfully/link.rb
restfully-0.8.8 lib/restfully/link.rb
restfully-0.8.7 lib/restfully/link.rb
restfully-0.8.6 lib/restfully/link.rb
restfully-0.8.5 lib/restfully/link.rb
restfully-0.8.4 lib/restfully/link.rb
restfully-0.8.3 lib/restfully/link.rb
restfully-0.8.2 lib/restfully/link.rb
restfully-0.8.1 lib/restfully/link.rb
restfully-0.8.0 lib/restfully/link.rb
restfully-0.7.1.rc6 lib/restfully/link.rb
restfully-0.7.1.rc5 lib/restfully/link.rb
restfully-0.7.1.rc4 lib/restfully/link.rb
restfully-0.7.1.rc3 lib/restfully/link.rb
restfully-0.7.1.rc2 lib/restfully/link.rb
restfully-0.7.1.rc1 lib/restfully/link.rb
restfully-0.7.1.pre lib/restfully/link.rb
restfully-0.7.0.pre lib/restfully/link.rb