Class: Sensor::Actuator::AnalyticsRetrieval

Inherits:
Object
  • Object
show all
Extended by:
Garb::Model
Defined in:
lib/sensor/actuator/analytics_retrieval.rb

Constant Summary collapse

CA_CERT_FILE =
File.join(File.dirname(__FILE__),
'../../../certs/cacert.pem')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_range) ⇒ AnalyticsRetrieval

Returns a new instance of AnalyticsRetrieval.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sensor/actuator/analytics_retrieval.rb', line 17

def initialize(time_range)
  @time_range = time_range
  client = OAuth2::Client.new(ENV['GOOGLE_CLIENT_ID'],
    ENV['GOOGLE_CLIENT_SECRET'],
    {
      site: 'https://accounts.google.com',
      authorize_url: "/o/oauth2/auth",
      token_url: "/o/oauth2/token"
    })
  token = OAuth2::AccessToken.from_hash(client, refresh_token: ENV['GOOGLE_REFRESH_TOKEN']).refresh!
  Garb::Session.access_token = token
end

Class Method Details

.enable_garb_ssl!Object



48
49
50
# File 'lib/sensor/actuator/analytics_retrieval.rb', line 48

def enable_garb_ssl!
  Garb.ca_cert_file = CA_CERT_FILE
end

Instance Method Details

#acquireObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sensor/actuator/analytics_retrieval.rb', line 30

def acquire
  results = self.class.results(profile,
    start_date: @time_range.start_date,
    end_date: @time_range.end_date)
  if results
    map = results.to_a[0].marshal_dump.inject({}) do |map, key_values|
      map[key_values[0]] = key_values[1].to_f
      map
    end
    {
      analytics: map
    }
  else
    {}
  end
end