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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

adapters, #auth_error?, #build_progress_info, #call, camelcase_to_snakecase, config_key, #last_auth_code, register_adapter, register_config, register_test_adapter, test_adapters

Constructor Details

#initializeGarminBase

Returns a new instance of GarminBase.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/git_fit/sync/garmin_base.rb', line 39

def initialize(...)
  if instance_of?(GarminBase)
    raise NotImplementedError,
          'GarminBase is abstract — instantiate a concrete subclass (Garmin, GarminCN)'
  end
  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.



52
53
54
# File 'lib/git_fit/sync/garmin_base.rb', line 52

def generated_secret
  @generated_secret
end

Class Method Details

.source_prefixObject

Raises:

  • (NotImplementedError)


152
153
154
# File 'lib/git_fit/sync/garmin_base.rb', line 152

def self.source_prefix
  raise NotImplementedError, "#{self} must implement source_prefix"
end

Instance Method Details

#authenticateObject



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
100
101
102
103
104
# File 'lib/git_fit/sync/garmin_base.rb', line 59

def authenticate
  @oauth_consumer = fetch_oauth_consumer

  if @secret && 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 => e
      warn e.message
    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



54
55
56
57
# File 'lib/git_fit/sync/garmin_base.rb', line 54

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

#pending_idsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/git_fit/sync/garmin_base.rb', line 106

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/git_fit/sync/garmin_base.rb', line 126

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

#source_prefixObject



156
157
158
# File 'lib/git_fit/sync/garmin_base.rb', line 156

def source_prefix
  self.class.source_prefix
end