Sha256: 9e8956b6593dd087db7151b9188c3ca1874053b4bb0895117fc7373a06f0adb0

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

class LogicalModel
  module UrlHelper

    def self.included(base)
      base.send(:extend, ClassMethods)
    end

    # adds following setters
    # - force_ssl
    # - set_resource_host
    # - set_resource_path
    #
    # add reader
    # - resource_uri
    module ClassMethods

      attr_accessor :host,
                    :resource_path,
                    :use_ssl

      # Will return path to resource
      # @param id [String] (nil)
      def resource_uri(id=nil)
        sufix  = (id.nil?)? "" : "/#{id}"
        "#{url_protocol_prefix}#{host}#{resource_path}#{sufix}"
      end

      # If called in class, will make al request through SSL.
      # @example
      #   class Client < LogicalModel
      #     force_ssl
      #     ...
      #   end
      def force_ssl
        @use_ssl = true
      end

      # @param new_host [String] resource host. Should NOT include protocol (http)
      # @param new_path [String] resource path in host
      def set_resource_url(new_host,new_path)
        @host = new_host
        @resource_path = new_path
      end

      def set_resource_host(new_host)
        @host = new_host
      end

      def set_resource_path(new_path)
        @resource_path = new_path
      end

      ##
      # Default use_ssl to ssl_recommend?
      # @return [Boolean]
      def use_ssl?
        @use_ssl ||= ssl_recommended?
      end

      # @return [String]
      def url_protocol_prefix
        (use_ssl?)? "https://" : "http://"
      end

      # Returns true if ssl is recommended according to environment.
      #
      # - production, staging -> true
      # - other -> false
      #
      # @return [Boolean]
      def ssl_recommended?
        ssl_recommended_environments = %W(production staging)
        ssl_recommended_environments.include?(defined?(Rails)? Rails.env : ENV['RACK_ENV'] )
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
logical_model-0.5.5 lib/logical_model/url_helper.rb
logical_model-0.5.4 lib/logical_model/url_helper.rb
logical_model-0.5.3 lib/logical_model/url_helper.rb
logical_model-0.5.2 lib/logical_model/url_helper.rb
logical_model-0.5.1 lib/logical_model/url_helper.rb
logical_model-0.5.0 lib/logical_model/url_helper.rb