Sha256: c5b86cc4efb5f2377c6a3ed3d5ef668e01edf66f116ab6d3e9b27a72b424c97a

Contents?: true

Size: 1.12 KB

Versions: 10

Compression:

Stored size: 1.12 KB

Contents

require "addressable"

module RelatonBib
  # Typed URI
  class TypedUri
    # @return [Symbol] :src/:obp/:rss
    attr_reader :type
    # @retutn [Addressable::URI]
    attr_reader :content

    # @param type [String, NilClass] src/obp/rss
    # @param content [String]
    def initialize(content:, type: nil)
      @type    = type
      @content = Addressable::URI.parse content if content
    end

    # @parma url [String]
    def content=(url)
      @content = Addressable::URI.parse url
    end

    # @param builder [Nokogiri::XML::Builder]
    def to_xml(builder)
      doc = builder.uri content.to_s
      doc[:type] = type if type
    end

    # @return [Hash]
    def to_hash
      hash = { "content" => content.to_s }
      hash["type"] = type.to_s if type
      hash
    end

    # @param prefix [String]
    # @param count [Integer] number of links
    # @return [String]
    def to_asciibib(prefix = "", count = 1)
      pref = prefix.empty? ? "link" : "#{prefix}.link"
      out = count > 1 ? "#{pref}::\n" : ""
      out += "#{pref}.type:: #{type}\n" if type
      out += "#{pref}.content:: #{content}\n"
      out
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
relaton-bib-1.9.17 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.16 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.15 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.14 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.13 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.12 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.11 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.10 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.9 lib/relaton_bib/typed_uri.rb
relaton-bib-1.9.8 lib/relaton_bib/typed_uri.rb