Class: Hisui::Response::GaData

Inherits:
Object
  • Object
show all
Defined in:
lib/hisui/response/ga_data.rb

Constant Summary collapse

DATE_DIMENSIONS =
%w[ga:date ga:dateHour ga:dateHourMinute].freeze
MONTH_DIMENSIONS =
%w[ga:yearMonth].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, request:) ⇒ GaData

Returns a new instance of GaData.



9
10
11
12
# File 'lib/hisui/response/ga_data.rb', line 9

def initialize(response:, request:)
  @response = response
  @date_ranges = [Range.new(request.start_date.beginning_of_day, request.end_date.end_of_day), Range.new(request.try(:comparing_start_date).try(:beginning_of_day), request.try(:comparing_end_date).try(:end_of_day))]
end

Instance Attribute Details

#date_rangesObject (readonly)

Returns the value of attribute date_ranges.



4
5
6
# File 'lib/hisui/response/ga_data.rb', line 4

def date_ranges
  @date_ranges
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/hisui/response/ga_data.rb', line 4

def response
  @response
end

Instance Method Details

#construct(ordinal) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hisui/response/ga_data.rb', line 14

def construct(ordinal)
  row_data_struct = Struct.new(*fields)

  data.try(:rows).try(:each_with_object, []) do |row, arr|
    next if date_indices.present? && date_indices.all? { |index| date_ranges.try(ordinal.to_sym).exclude?(row.dimensions[index].in_time_zone) }
    next if month_indices.present? && month_indices.all? { |index| date_ranges.try(ordinal.to_sym).exclude?("#{row.dimensions[index]}01".in_time_zone) }

    row_data = []
    row.dimensions.each do |dimension|
      row_data << dimension
    end

    row.try(:metrics).try(ordinal.to_sym).try(:values).try(:each) do |value|
      row_data << value
    end

    arr << row_data_struct.new(*row_data)
  end
end

#data?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/hisui/response/ga_data.rb', line 65

def data?
  data.row_count.to_i > 0
end

#rowsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hisui/response/ga_data.rb', line 39

def rows
  @rows ||= begin
    data.try(:rows).try(:each_with_object, []) do |row, arr|
      dimension_values = row.dimensions
      primary_data = []
      comparing_data = []

      row.metrics.first.values.each do |value|
        primary_data << value
      end

      if row.metrics.second
        row.metrics.second.values.each do |value|
          comparing_data << value
        end
      end

      rows_struct = Struct.new(:dimensions, :primary, :comparing)
      dimension_struct = Struct.new(*dimensions)
      metric_struct = Struct.new(*metrics)

      arr << rows_struct.new(dimension_struct.new(*dimension_values), metric_struct.new(*primary_data), metric_struct.new(*comparing_data))
    end
  end
end

#sum(ordinal) ⇒ Object



34
35
36
37
# File 'lib/hisui/response/ga_data.rb', line 34

def sum(ordinal)
  total_struct = Struct.new(*metrics)
  total_struct.new(*data.totals.try(ordinal).try(:values))
end