Class: Spaceship::StatsMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
spaceship/lib/spaceship/stats_middleware.rb

Defined Under Namespace

Classes: ServiceOption, URLLog

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ StatsMiddleware

Returns a new instance of StatsMiddleware.



39
40
41
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 39

def initialize(app)
  super(app)
end

Class Method Details

.request_logsObject



33
34
35
36
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 33

def request_logs
  @request_logs ||= []
  @request_logs
end

.service_statsObject



29
30
31
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 29

def service_stats
  @service_stats ||= Hash.new(0)
end

.servicesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 10

def services
  return @services if @services

  require_relative 'tunes/tunes_client'
  require_relative 'portal/portal_client'
  require_relative 'connect_api/testflight/client'
  require_relative 'connect_api/provisioning/client'

  @services ||= [
    ServiceOption.new("App Store Connect API (official)", "api.appstoreconnect.apple.com", "JWT"),
    ServiceOption.new("App Store Connect API (web session)", Spaceship::ConnectAPI::TestFlight::Client.hostname.gsub("https://", ""), "Web session"),
    ServiceOption.new("App Store Connect API (web session)", Spaceship::ConnectAPI::Provisioning::Client.hostname.gsub("https://", ""), "Web session"),
    ServiceOption.new("Legacy iTunesConnect Auth", "idmsa.apple.com", "Web session"),
    ServiceOption.new("Legacy iTunesConnect Auth", "appstoreconnect.apple.com/olympus/v1/", "Web session"),
    ServiceOption.new("Legacy iTunesConnect", Spaceship::TunesClient.hostname.gsub("https://", ""), "Web session"),
    ServiceOption.new("Legacy iTunesConnect Developer Portal", Spaceship::PortalClient.hostname.gsub("https://", ""), "Web session")
  ]
end

Instance Method Details

#call(env) ⇒ Object



43
44
45
46
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 43

def call(env)
  log(env)
  @app.call(env)
end

#log(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'spaceship/lib/spaceship/stats_middleware.rb', line 48

def log(env)
  return false unless env && env.url && (uri = URI.parse(env.url.to_s))
  service = StatsMiddleware.services.find do |s|
    uri.to_s.include?(s.url)
  end

  service = ServiceOption.new("Unknown", uri.host, "Unknown") if service.nil?
  StatsMiddleware.service_stats[service] += 1

  StatsMiddleware.request_logs << URLLog.new(uri.to_s, service.auth_type)

  return true
rescue => e
  puts("Failed to log spaceship stats - #{e.message}") if Spaceship::Globals.verbose?
  return false
end