Class: GitFit::Sync::XingZhe

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

Constant Summary collapse

BASE_HOST =
'www.imxingzhe.com'
LOGIN_PATH =
'/api/v1/user/login/'
WORKOUTS_PATH =
'/api/v1/pgworkout/'
STREAM_PATH =
'/api/v1/pgworkout/'
DETAIL_PATH =
'/api/v1/pgworkout/'
EXPORT_BASE =
'/api/v1/workout/'
RSA_PUBKEY =
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDmuQkBbijudDAJgfffDeeIButqWHZvUwcRuvWdg89393FSdz3IJUHc0rgI/S3WuU8N0VePJLmVAZtCOK4qe4FY/eKmWpJmn7JfXB4HTMWjPVoyRZmSYjW4L8GrWmh51Qj7DwpTADadF3aq04o+s1b8LXJa8r6+TIqqL5WUHtRqmQIDAQAB'
SPORT_MAP =
{
  3 => 'ride', 1 => 'run', 2 => 'hike',
  5 => 'swim', 6 => 'walk', 11 => 'ski', 13 => 'workout'
}.freeze
SESSION_TTL =
30 * 24 * 60 * 60

Instance Attribute Summary

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, cumulative_distances, extract_location, haversine, register_adapter, register_config, register_test_adapter, test_adapters, training_samples

Constructor Details

#initializeXingZhe

Returns a new instance of XingZhe.



31
32
33
34
35
36
# File 'lib/git_fit/sync/xingzhe.rb', line 31

def initialize(...)
  super
  @email = @config['email']
  @password = @config['password']
  @session_path = @config['session_path'] || GitFit::Auth.file('xingzhe', 'session.json')
end

Instance Method Details

#auth_error?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/git_fit/sync/xingzhe.rb', line 79

def auth_error?
  [302, 401, 403].include?(@last_http_code)
end

#authenticateObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git_fit/sync/xingzhe.rb', line 43

def authenticate
  if load_session && session_valid?
    @auth_method = 'cached'
    return true
  end
  if 
    @auth_method = 'login'
    return true
  end
  false
end

#before_callObject



38
39
40
41
# File 'lib/git_fit/sync/xingzhe.rb', line 38

def before_call
  return false if @email.nil? || @password.nil?
  super
end

#last_auth_codeObject



83
84
85
# File 'lib/git_fit/sync/xingzhe.rb', line 83

def last_auth_code
  @last_http_code
end

#pending_idsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/git_fit/sync/xingzhe.rb', line 55

def pending_ids
  existing = existing_run_ids
  ids = []
  offset = 0
  limit = 50
  loop do
    acts = fetch_page(offset, limit)
    break if acts.nil? || acts.empty?
    acts.each do |act|
      aid = act['id'].to_s
      next if existing.include?(aid) && raw_file_exists?(aid)
      type = map_sport(act['sport'])
      next if skip_type?(type)
      ids << { id: aid, type: type, summary: act }
    end
    offset += limit
  end
  ids
end

#process_one(pending) ⇒ Object



75
76
77
# File 'lib/git_fit/sync/xingzhe.rb', line 75

def process_one(pending)
  process_activity(pending[:summary], pending[:type])
end

#raw_detail_path(platform_id) ⇒ Object



96
97
98
# File 'lib/git_fit/sync/xingzhe.rb', line 96

def raw_detail_path(platform_id)
  File.join(raw_dir, platform_id, 'detail.json')
end

#raw_file_exists?(platform_id) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/git_fit/sync/xingzhe.rb', line 108

def raw_file_exists?(platform_id)
  dir = File.join(raw_dir, platform_id)
  %w[stream.json detail.json].all? { |f| File.exist?(File.join(dir, f)) }
end

#raw_fit_path(platform_id) ⇒ Object



104
105
106
# File 'lib/git_fit/sync/xingzhe.rb', line 104

def raw_fit_path(platform_id)
  File.join(raw_dir, platform_id, "#{platform_id}.fit")
end

#raw_gpx_path(platform_id) ⇒ Object



100
101
102
# File 'lib/git_fit/sync/xingzhe.rb', line 100

def raw_gpx_path(platform_id)
  File.join(raw_dir, platform_id, "#{platform_id}.gpx")
end

#raw_path(platform_id) ⇒ Object

── Raw directory layout (Phase 1, mirrors garmin Phase 1) ────────── data/raw/xingzhe/id/stream.json — stream envelope data/raw/xingzhe/id/detail.json — detail payload — inner data:workout,user,slopes,segments,pois,laps data/raw/xingzhe/id/id.gpx — GPX export (when available) data/raw/xingzhe/id/id.fit — FIT export (when is_fit)



92
93
94
# File 'lib/git_fit/sync/xingzhe.rb', line 92

def raw_path(platform_id)
  File.join(raw_dir, platform_id, 'stream.json')
end

#write_raw_file(data, filename, platform_id) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/git_fit/sync/xingzhe.rb', line 118

def write_raw_file(data, filename, platform_id)
  dir = File.join(raw_dir, platform_id)
  FileUtils.mkdir_p(dir)
  tmp = File.join(dir, ".#{filename}.tmp")
  dest = File.join(dir, filename)
  case data
  when String then File.write(tmp, data)
  when Hash, Array then File.write(tmp, JSON.generate(data))
  end
  File.rename(tmp, dest)
end

#write_source_archive(data, platform_id, _ext = 'json') ⇒ Object



113
114
115
116
# File 'lib/git_fit/sync/xingzhe.rb', line 113

def write_source_archive(data, platform_id, _ext = 'json')
  # Back-compat entry point — now writes stream.json into per-id dir
  write_raw_file(data, 'stream.json', platform_id)
end