Sha256: 17609eeba874c59b1b522f51cab334baeb8093061be7809d3e1618842a3e9486

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 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 =~ %r{^(https?\:)?/}
          rel_path
        else
          "#{Card.config.relative_url_root}/#{rel_path}"
        end
      end

      def card_url rel
        rel =~ /^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

6 entries across 6 versions & 1 rubygems

Version Path
card-1.101.4 lib/card/env/location.rb
card-1.101.3 lib/card/env/location.rb
card-1.101.2 lib/card/env/location.rb
card-1.101.1 lib/card/env/location.rb
card-1.101.0 lib/card/env/location.rb
card-1.100.0 lib/card/env/location.rb