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

#make_viewport(row:, rank:) ⇒ Object (private)



48
49
50
51
52
53
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 48

def make_viewport(row:, rank:)
  CypressViewportUpdater::
    Viewport.new(row:,
                 rank:,
                 total_users: @total_users)
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

#viewports_full?Boolean (private)

Returns:

  • (Boolean)


55
56
57
58
59
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 55

def viewports_full?
  @viewports[:mobile].count >= NUM_TOP_VIEWPORTS[:mobile] &&
    @viewports[:tablet].count >= NUM_TOP_VIEWPORTS[:tablet] &&
    @viewports[:desktop].count >= NUM_TOP_VIEWPORTS[:desktop]
end

#width_and_height_set?(row) ⇒ Boolean (private)

Returns:

  • (Boolean)


44
45
46
# File 'app/sidekiq/cypress_viewport_updater/viewports.rb', line 44

def width_and_height_set?(row)
  row.dimensions[1] != '(not set)'
end