Class: CypressViewportUpdater::Viewports

Inherits:
Object
  • Object
show all
Defined in:
app/sidekiq/cypress_viewport_updater/viewports.rb

Constant Summary collapse

NUM_TOP_VIEWPORTS =
{ mobile: 5, tablet: 5, desktop: 5 }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(user_report:) ⇒ Viewports

Returns a new instance of Viewports.



7
8
9
10
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 7

def initialize(user_report:)
  @total_users = user_report.data.totals.first.values.first.to_f
  @viewports = { mobile: [], tablet: [], desktop: [] }
end

Instance Method Details

#create(viewport_report:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 12

def create(viewport_report:)
  count = { mobile: 0, tablet: 0, desktop: 0 }

  viewport_report.data.rows.each do |row|
    viewport_type = row.dimensions.first.to_sym

    if @viewports[viewport_type].count < NUM_TOP_VIEWPORTS[viewport_type] &&
       width_and_height_set?(row)
      count[viewport_type] += 1
      @viewports[viewport_type] << make_viewport(row:, rank: count[viewport_type])
    end

    break if viewports_full?
  end

  self
end

#desktopObject



38
39
40
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 38

def desktop
  @viewports[:desktop]
end

#mobileObject



30
31
32
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 30

def mobile
  @viewports[:mobile]
end

#tabletObject



34
35
36
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 34

def tablet
  @viewports[:tablet]
end