Class: GitFit::Sync::GarminBase

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

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

#progress_reported

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initializeGarminBase

Returns a new instance of GarminBase.



32
33
34
35
36
37
38
39
# File 'lib/git_fit/sync/garmin_base.rb', line 32

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.



41
42
43
# File 'lib/git_fit/sync/garmin_base.rb', line 41

def generated_secret
  @generated_secret
end

Instance Method Details

#authenticateObject



48
49
50
51
52
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
# File 'lib/git_fit/sync/garmin_base.rb', line 48

def authenticate
  @oauth_consumer = fetch_oauth_consumer

  if @secret
    if parse_secret_string(@secret)
      return true if access_token_valid?
      begin
        return true if refresh_oauth2_via_oauth1
      rescue AuthError
      end
    end
  end

  begin
    return true if load_tokens && access_token_valid?
    return true if load_tokens && refresh_oauth2_via_oauth1
  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
  true
end

#before_callObject



43
44
45
46
# File 'lib/git_fit/sync/garmin_base.rb', line 43

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

#pending_idsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/git_fit/sync/garmin_base.rb', line 83

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



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

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