Sha256: 163fa6da051662b652053761fe0ee7d31fff79fe97f199d0b9f6658a1ed95da7

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

module FlipFab
  class CookiePersistence < FlipFab::Persistence
    COOKIE_PATH = '/'.freeze
    COOKIE_DURATION_MONTHS = 12
    # See: https://github.com/rails/rails/blob/b1124a2ac88778c0feb0157ac09367cbd204bf01/actionpack/lib/action_dispatch/middleware/cookies.rb#L214
    DOMAIN_REGEXP          = /[^.]*\.([^.]*|..\...|...\...)$/

    def initialize(feature_name, context)
      super
    end

    def read
      value.to_sym unless value.nil?
    end

    def write(state)
      context.response.set_cookie key, value: state,
                                       expires: cookie_expiration,
                                       path: COOKIE_PATH
    end

    private

    def key
      "flip_fab.#{feature_name}"
    end

    def value
      @value ||= context.request.cookies[key]
    end

    # See: https://github.com/rails/rails/blob/b1124a2ac88778c0feb0157ac09367cbd204bf01/actionpack/lib/action_dispatch/middleware/cookies.rb#L286-L294
    def top_level_domain
      Regexp.last_match(0) if (host !~ /^[\d.]+$/) && (host =~ DOMAIN_REGEXP)
    end

    def cookie_expiration
      (Time.now.utc.to_datetime >> COOKIE_DURATION_MONTHS).to_time
    end

    def host
      context.request.host
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
flip_fab-1.1.21 lib/flip_fab/cookie_persistence.rb
flip_fab-1.1.20 lib/flip_fab/cookie_persistence.rb
flip_fab-1.1.19 lib/flip_fab/cookie_persistence.rb
flip_fab-1.1.18 lib/flip_fab/cookie_persistence.rb
flip_fab-1.0.18 lib/flip_fab/cookie_persistence.rb
flip_fab-1.0.17 lib/flip_fab/cookie_persistence.rb
flip_fab-1.0.16 lib/flip_fab/cookie_persistence.rb