Class: GitFit::Sync::GarminBase

Inherits:
Base
  • Object
show all
Defined in:
lib/git_fit/sync/garmin_base.rb

Overview

GarminBase handles OAuth1/OAuth2, SSO/MFA, token persistence, FIT decoding, timezone resolution — inherently large surface area. rubocop:disable Metrics/ClassLength

Direct Known Subclasses

GarminBaseDI, GarminCN

Constant Summary collapse

CLIENT_ID =
'GCM_ANDROID_DARK'
OAUTH_CONSUMER_URL =
'https://thegarth.s3.amazonaws.com/oauth_consumer.json'
SSO_HEADERS =
{
  'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
  'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language' => 'en-US,en;q=0.9',
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#auth_method, #progress_reported

Instance Method Summary collapse

Methods inherited from Base

adapters, #build_progress_info, #call, camelcase_to_snakecase, config_key, register_adapter, register_config, register_test_adapter, test_adapters

Constructor Details

#initializeGarminBase

Returns a new instance of GarminBase.



37
38
39
40
41
42
43
44
# File 'lib/git_fit/sync/garmin_base.rb', line 37

def initialize(...)
  super
  @domain = 'garmin.com'
  @email = @config['email']
  @password = @config['password']
  @secret = @config['secret']
  @ssl_verify = @config.key?('ssl_verify') ? @config['ssl_verify'] : true
end

Instance Attribute Details

#generated_secretObject (readonly)

Returns the value of attribute generated_secret.



46
47
48
# File 'lib/git_fit/sync/garmin_base.rb', line 46

def generated_secret
  @generated_secret
end

Instance Method Details

#authenticateObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/git_fit/sync/garmin_base.rb', line 53

def authenticate
  @oauth_consumer = fetch_oauth_consumer

  if @secret
    if parse_secret_string(@secret)
      if access_token_valid?
        @auth_method = 'secret'
        return true
      end
      begin
        if refresh_oauth2_via_oauth1
          @auth_method = 'refreshed'
          return true
        end
      rescue AuthError
      end
    end
  end

  begin
    if load_tokens && access_token_valid?
      @auth_method = 'token'
      return true
    end
    if load_tokens && refresh_oauth2_via_oauth1
      @auth_method = 'refreshed'
      return true
    end
  rescue AuthError
    remove_stale_tokens
  end

  return false if @email.to_s.empty? || @password.to_s.empty?

  ticket = 
  @oauth1_token = fetch_oauth1_token(ticket)
  @access_token = exchange_oauth2
  @generated_secret = dump_secret
  unless verify_access_token
    @generated_secret = nil
    raise AuthError, 'New tokens failed API verify — SSO result invalid'
  end
  save_secret_to_config
  save_tokens
  @auth_method = 'sso'
  true
end

#before_callObject



48
49
50
51
# File 'lib/git_fit/sync/garmin_base.rb', line 48

def before_call
  return false if @email.to_s.empty? && @secret.to_s.empty?
  super
end

#pending_idsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/git_fit/sync/garmin_base.rb', line 101

def pending_ids
  existing = existing_run_ids
  ids = []
  start = 0
  limit = 100
  loop do
    activities = fetch_activity_list(start, limit)
    break if activities.nil? || activities.empty?
    activities.each do |act|
      act_id = act['activityId'].to_s
      type = map_type(act['activityType'])
      next if skip_type?(type)
      next if existing.include?(act_id) && raw_file_exists?(act_id)
      ids << { id: act_id, type: type }
    end
    start += limit
  end
  ids
end

#process_one(pending) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git_fit/sync/garmin_base.rb', line 121

def process_one(pending)
  act_id = pending[:id]
  type = pending[:type]

  details = fetch_activity_detail(act_id)
  return nil unless details

  polyline = fetch_summary_polyline(act_id)

  unless raw_file_exists?(act_id)
    download_fit_raw(act_id)
    l2 = fit_to_l2(act_id)
    write_standardized_json(l2, act_id) if l2
  end

  attrs = build_attrs(details, act_id, type, polyline)

  if (start_t = parse_garmin_time(details.dig('summaryDTO', 'startTimeGMT')))
    local_t = resolve_local_time(start_t, act_id, polyline)
    attrs[:start_date_local] = local_t.iso8601 if local_t
  end

  upsert_activity(attrs)
  attrs
end