Class: Hisui::Request

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

Constant Summary collapse

BASIC_OPTION_KEYS =
[:start_date, :end_date, :comparing_start_date, :comparing_end_date, :limit, :sort]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile:, model:, **options) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
18
# File 'lib/hisui/request.rb', line 9

def initialize(profile:, model:, **options)
  @profile = profile
  @model = model
  @start_date = Time.current.advance(months: -1)
  @end_date = Time.current

  BASIC_OPTION_KEYS.each do |key|
    self.send("#{key}=".to_sym, options[key]) if options.try(:has_key?, key)
  end
end

Instance Attribute Details

#comparing_end_dateObject

Returns the value of attribute comparing_end_date.



7
8
9
# File 'lib/hisui/request.rb', line 7

def comparing_end_date
  @comparing_end_date
end

#comparing_start_dateObject

Returns the value of attribute comparing_start_date.



7
8
9
# File 'lib/hisui/request.rb', line 7

def comparing_start_date
  @comparing_start_date
end

#end_dateObject

Returns the value of attribute end_date.



7
8
9
# File 'lib/hisui/request.rb', line 7

def end_date
  @end_date
end

#limitObject

Returns the value of attribute limit.



7
8
9
# File 'lib/hisui/request.rb', line 7

def limit
  @limit
end

#modelObject

Returns the value of attribute model.



7
8
9
# File 'lib/hisui/request.rb', line 7

def model
  @model
end

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/hisui/request.rb', line 7

def profile
  @profile
end

#start_dateObject

Returns the value of attribute start_date.



7
8
9
# File 'lib/hisui/request.rb', line 7

def start_date
  @start_date
end

Instance Method Details

#fetch_dataObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hisui/request.rb', line 20

def fetch_data
  reporting_service = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
  reporting_service.authorization = profile.user.access_token.token

  date_ranges = []
  date_ranges << Google::Apis::AnalyticsreportingV4::DateRange.new(start_date: start_date.to_s, end_date: end_date.to_s)

  if comparing_start_date && comparing_end_date
    date_ranges << Google::Apis::AnalyticsreportingV4::DateRange.new(start_date: comparing_start_date.to_s, end_date: comparing_end_date.to_s)
  end

  request = Google::Apis::AnalyticsreportingV4::GetReportsRequest.new(
    report_requests: [Google::Apis::AnalyticsreportingV4::ReportRequest.new(
      view_id: profile.id,
      metrics: model.metrics,
      dimensions: model.dimensions,
      date_ranges: date_ranges,
      filters_expression: model.filters_expression,
      order_bys: model.order_bys,
      page_size: limit
    )]
  )

  response = reporting_service.batch_get_reports(request)
  Hisui::Response.new(response: response, request: self)
end