Sha256: 14bf445b581bd18c5e0971f0c3c8064c636e2bb29b59c897eea52cf19a18a8c4

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module ReTrack
  class RefererTracking
    include ::Mongoid::Document
    include ::Mongoid::Timestamps

    store_in collection: 'retrack_referer_trackings'

    belongs_to :trackable, polymorphic: true

    field :referer_url,      type: String
    field :first_url,        type: String
    field :user_agent,       type: String
    field :first_visited_at, type: DateTime
    field :ip, type: String

    # Extract query parameters from referer_url and first_url.
    #
    # @example
    #   r = ReTrack::RefererTracking.new first_url: 'http://google.de/?q=a'
    #   r.query('q')                 # => 'a'
    #   r.query('a')                 # => nil
    #   r.query('q', 'referer_url')  # => nil
    #
    # @param parameter [String] The Query String Parameter to look up.
    # @param url_field_name [String] The URL field to query. Either
    #   'referer_url' or 'first_url'.
    #
    # @return [String] The value for the given query parameter or nil.
    def query(parameter, url_field_name = 'first_url')
      return nil unless url = value_for(url_field_name)
      query_hash(url)[parameter.to_s]
    end

    private

      def query_hash(url)
        Rack::Utils.parse_query URI.parse(CGI::unescape(url)).query rescue {}
      end

      def value_for(field)
        return nil unless ['referer_url', 'first_url'].include? field.to_s
        public_send("#{field}")
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
re_track-0.3.0 app/models/re_track/referer_tracking.rb