module Octo::Segmentation::Helpers

Public Class Methods

choices_for_dimensions(dimension, enterprise_id) click to toggle source

Helper method to return valid choices for a given dimension. It tries

to find the values from db first. In case, there is nothing, it
shows some default values, so the dashboard does not look totally
blank.

@param [Fixnum] dimension The dimension ID for which choices to be

found

@param [String] enterprise_id The enterprise ID for which the choices

to be found
# File lib/octocore-mongo/segment.rb, line 49
def choices_for_dimensions(dimension, enterprise_id)
  args = {
    enterprise_id: enterprise_id,
    dimension: dimension
  }
  res = Octo::DimensionChoice.where(args)
  choices = Array.new
  if res.count > 0
    choices = res.collect do |r|
      r.column
    end
  elsif dimension_choice.has_key?(dimension)
    func = dimension_choice[dimension]
    choices = self.send(func, enterprise_id)
  end
  mapping_as_choice Hash[Array.new(choices.count) { |i| i }.zip(choices)]
end
dimensions_as_choice() click to toggle source

Helper method for returning dimensions as choice to be used in the UX @return [Array<Hash{Symbol => String }>] The hash containing key :text

as the text to display, and another key :id as the id to be used
as reference while communicating
# File lib/octocore-mongo/segment.rb, line 32
def dimensions_as_choice
  mapping_as_choice dimension_text
end
logic_operators_as_choice() click to toggle source

Returns logic operatos as a choice for operating between dimensions

# File lib/octocore-mongo/segment.rb, line 37
def logic_operators_as_choice
  mapping_as_choice logic_text
end
operators_as_choice(dimension = nil) click to toggle source

Helper method for returning operators as a hash to be used in UX @param [Fixnum] dimension The dimension for which the operators

are to be fetched

@return [Array<Hash{Symbol => String }>] The hash containing key :text

as the text to display, and another key :id as the id to be used
as reference while communicating
# File lib/octocore-mongo/segment.rb, line 24
def operators_as_choice(dimension = nil)
  mapping_as_choice operator_text
end

Private Class Methods

browser_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 152
def browser_choices(enterprise_id=nil)
  ['Firefox', 'Chrome', 'Safari']
end
city_choices(enterprise_id=nil) click to toggle source

Generates the city choices for the enterprise @param [String] enterprise_id The enterpriseID for which city choices

to be found

@return [Array<String>] Array of string values

# File lib/octocore-mongo/segment.rb, line 132
def city_choices(enterprise_id=nil)
  ['New Delhi', 'Mumbai', 'Bengaluru', 'San Francisco', 'Seattle']
end
country_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 140
def country_choices(enterprise_id=nil)
  ['India', 'United States of America (USA)']
end
dimension_choice() click to toggle source

Return a hash containing keys as dimensions and the values as methods

which should be called to populate the values
# File lib/octocore-mongo/segment.rb, line 82
def dimension_choice
  {
    Octo::Segmentation::Dimensions::CITY => :city_choices,
    Octo::Segmentation::Dimensions::STATE => :state_choices,
    Octo::Segmentation::Dimensions::COUNTRY => :country_choices,
    Octo::Segmentation::Dimensions::OS => :os_choices,
    Octo::Segmentation::Dimensions::MANUFACTURER => :manufacturer_choices,
    Octo::Segmentation::Dimensions::BROWSER => :browser_choices,
    Octo::Segmentation::Dimensions::MODEL => :model_choices,
    Octo::Segmentation::Dimensions::ENGAGEMENT => :engagement_choices
  }
end
dimension_operator() click to toggle source

Defines which operators can be used for which dimensions

# File lib/octocore-mongo/segment.rb, line 112
def dimension_operator
  ops = Set.new([Octo::Segmentation::Operators::EQUAL,
         Octo::Segmentation::Operators::NOT_EQUAL,
         Octo::Segmentation::Operators::IN])
  Hash.new { |h,k| h[k] = ops }
end
dimension_text() click to toggle source

Returns a hash containing the dimension value and the string text

# File lib/octocore-mongo/segment.rb, line 96
def dimension_text
  {
    Octo::Segmentation::Dimensions::CITY => 'City',
    Octo::Segmentation::Dimensions::STATE => 'State',
    Octo::Segmentation::Dimensions::COUNTRY => 'Country',
    Octo::Segmentation::Dimensions::OS => 'OS',
    Octo::Segmentation::Dimensions::MANUFACTURER => 'Manufacturer',
    Octo::Segmentation::Dimensions::BROWSER => 'Browser',
    Octo::Segmentation::Dimensions::MODEL => 'Model',
    Octo::Segmentation::Dimensions::ENGAGEMENT => 'Engagement',
    Octo::Segmentation::Dimensions::LAST_ACTIVE => 'Last Active On',
    Octo::Segmentation::Dimensions::CREATED_ON => 'Created On'
  }
end
engagement_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 160
def engagement_choices(enterprise_id=nil)
  [Octo::UserPersona::HIGH_ENGAGED,
   Octo::UserPersona::MEDIUM_ENGAGED,
   Octo::UserPersona::LOW_ENGAGED,
   Octo::UserPersona::DEAD].collect { |x| Octo::UserPersona.engaged_text(x) }
end
logic_text() click to toggle source
# File lib/octocore-mongo/segment.rb, line 119
def logic_text
  {
    Octo::Segmentation::Operators::AND => 'AND',
    Octo::Segmentation::Operators::OR  => 'OR',
    Octo::Segmentation::Operators::NOT => 'NOT',
    Octo::Segmentation::Operators::XOR => 'XOR',
  }
end
manufacturer_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 148
def manufacturer_choices(enterprise_id=nil)
  ['Apple', 'Dell', 'HP', 'Samsung', 'Micromax']
end
mapping_as_choice(map) click to toggle source

Converts a hash mapping into choices ready for UX @return [Array<Hash{Symbol => String }>] The hash containing key :text

as the text to display, and another key :id as the id to be used
as reference while communicating
# File lib/octocore-mongo/segment.rb, line 172
def mapping_as_choice(map)
  map.inject([]) do | choices, pair |
    key, val = pair
    choices << { text: val, id: key }
  end
end
model_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 156
def model_choices(enterprise_id=nil)
  ['iPhone 6', 'iPhone 6s', 'iPhone 5', 'Samsung S6']
end
operator_text() click to toggle source

Returns a hash containing keys as Operators and the values as string

text corresponding to them.
# File lib/octocore-mongo/segment.rb, line 72
def operator_text
  {
    Octo::Segmentation::Operators::EQUAL => '= Equals',
    Octo::Segmentation::Operators::NOT_EQUAL => '!= Not Equals',
    Octo::Segmentation::Operators::IN => 'Within range'
  }
end
os_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 144
def os_choices(enterprise_id=nil)
  ['Windows', 'OS X', 'iOS', 'android']
end
state_choices(enterprise_id=nil) click to toggle source
# File lib/octocore-mongo/segment.rb, line 136
def state_choices(enterprise_id=nil)
  ['Delhi', 'Maharashtra', 'Karnataka', 'California']
end