Sha256: 40fe5aa5916acd4611f91c371c5c94c92b24fce94c548dcaf1b3e5a7ea3b115c

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

class SiteHub
  class CandidateRoutes
    module FromJson
      extend CollectionMethods

      def self.extended(clazz)
        clazz.class_eval do
          include InstanceMethods
        end
      end

      # TODO: support nested routes, i.e. support rule name being passed in
      def from_hash(hash, sitehub_cookie_name, sitehub_cookie_path)
        cookie_path = hash[:sitehub_cookie_path] || sitehub_cookie_path
        cookie_name = hash[:sitehub_cookie_name] || sitehub_cookie_name
        new(version_cookie: TrackingCookieDefinition.new(cookie_name, cookie_path),
            mapped_path: hash[:path]) do
          handle_routes(hash, self)
          default url: hash[:default] if hash[:default]
        end
      end

      module InstanceMethods
        private

        def handle_routes(hash, routes)
          extract_splits(hash, routes)
          extract_routes(hash, routes)
        end

        def extract_routes(hash, routes)
          collection(hash, :routes).each do |route|
            routes.route(url: route[:url], label: route[:label])
          end
        end

        def extract_splits(hash, routes)
          collection(hash, :splits).each do |split|
            label = split[:label]
            percentage = split[:percentage]
            cookie_name = split[:sitehub_cookie_name] || sitehub_cookie_name

            if split[:splits] || split[:routes]
              routes.split(percentage: percentage, label: label) { handle_routes(split, self) }
            else
              routes.split(percentage: percentage, label: label, url: split[:url])
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sitehub-0.5.0.alpha12 lib/sitehub/candidate_routes/from_json.rb