Module: GitFit::StravaWeb::FileCheck
- Defined in:
- lib/git_fit/strava_web/file_check.rb
Overview
Content validation for downloaded Strava files (FIT/TCX/GPX/JSON).
Class Method Summary collapse
- .describe_error(content) ⇒ Object
- .valid_format?(content, format) ⇒ Boolean
- .validate_existing_file(path) ⇒ Object
Class Method Details
.describe_error(content) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/git_fit/strava_web/file_check.rb', line 50 def describe_error(content) head = content[0, 200].b if head.include?('<html') || head.include?('<!DOCTYPE') "Strava returned HTML (likely rate-limit or auth page) — #{content.length} bytes" else sample = content[0, 80].force_encoding('UTF-8').scrub('?') "unexpected content format — #{sample}" end end |
.valid_format?(content, format) ⇒ Boolean
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/git_fit/strava_web/file_check.rb', line 11 def valid_format?(content, format) case format when :fit content[0, 64].include?('.FIT'.b) || content.start_with?("\x0E\x10".b) when :tcx content.include?('TrainingCenterDatabase'.b) when :gpx content.include?('<gpx'.b) when :json JSON.parse(content) true else !content.empty? end rescue StandardError false end |
.validate_existing_file(path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/git_fit/strava_web/file_check.rb', line 29 def validate_existing_file(path) return false unless File.exist?(path) && File.size(path) > 0 content = File.read(path, mode: 'rb', length: 4096) case File.extname(path).downcase when '.fit' content.start_with?("\x0E\x10".b) when '.tcx' content.include?('TrainingCenterDatabase'.b) when '.gpx' content.include?('<gpx'.b) when '.json' JSON.parse(content) true else !content.empty? end rescue StandardError false end |