Sha256: 28b5831198d38c5bbc75c13fe23dc1e1ef1ccfc30f3b4d9c234302b7fad8d8a1

Contents?: true

Size: 1.16 KB

Versions: 11

Compression:

Stored size: 1.16 KB

Contents

class Card
  module Env
    module Location
      # card_path    makes a relative path site-absolute (if not already)
      # card_url     makes it a full url (if not already)

      def card_path rel_path
        unless rel_path.is_a? String
          Rails.logger.warn "Pass only strings to card_path. "\
                            "(#{rel_path} = #{rel_path.class})"
        end
        if rel_path.match? %r{^(https?:)?/}
          rel_path
        else
          "#{Card.config.relative_url_root}/#{rel_path}"
        end
      end

      def card_url rel
        rel.match?(/^https?:/) ? rel : "#{protocol_and_host}#{card_path rel}"
      end

      def protocol_and_host
        Card.config.protocol_and_host || "#{Env[:protocol]}#{Env[:host]}"
      end

      def cardname_from_url url
        return unless (cardname = cardname_from_url_regexp)

        m = url.match cardname
        m ? Card::Name[m[:mark]] : nil
      end

      private

      def cardname_from_url_regexp
        return unless Env[:host]

        %r{#{Regexp.escape Env[:host]}/(?<mark>[^?]+)}
      end

      extend Location # allows calls on Location constant, eg Location.card_url
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
card-1.104.1 lib/card/env/location.rb
card-1.104.0 lib/card/env/location.rb
card-1.103.4 lib/card/env/location.rb
card-1.103.3 lib/card/env/location.rb
card-1.103.2 lib/card/env/location.rb
card-1.103.1 lib/card/env/location.rb
card-1.103.0 lib/card/env/location.rb
card-1.101.7 lib/card/env/location.rb
card-1.102.0 lib/card/env/location.rb
card-1.101.6 lib/card/env/location.rb
card-1.101.5 lib/card/env/location.rb