Class: GitFit::Sync::Base

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

Direct Known Subclasses

GarminBase, IGPSPORT, Keep, Lorem, Strava, XOSS, XingZhe

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, db:, activity_filter: nil, privacy: nil, time_budget: nil) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
# File 'lib/git_fit/sync/base.rb', line 10

def initialize(config:, db:, activity_filter: nil, privacy: nil, time_budget: nil)
  @config = config
  @db = db
  @activity_filter = activity_filter
  @privacy = privacy
  @time_budget = time_budget
  @start_time = nil
  @progress_reported = false
end

Instance Attribute Details

#progress_reportedObject (readonly)

Returns the value of attribute progress_reported.



20
21
22
# File 'lib/git_fit/sync/base.rb', line 20

def progress_reported
  @progress_reported
end

Class Method Details

.adaptersObject



88
89
90
# File 'lib/git_fit/sync/base.rb', line 88

def adapters
  @adapters ||= []
end

.camelcase_to_snakecase(str) ⇒ Object



112
113
114
# File 'lib/git_fit/sync/base.rb', line 112

def camelcase_to_snakecase(str)
  str.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end

.config_key(source = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/git_fit/sync/base.rb', line 104

def config_key(source = nil)
  if source
    @config_key = source
  else
    @config_key ||= camelcase_to_snakecase(name.split('::').last)
  end
end

.register_adapterObject



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

def register_adapter
  Base.adapters << self unless Base.adapters.include?(self)
end

.register_config(*keys) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/git_fit/sync/base.rb', line 77

def self.register_config(*keys)
  return if keys.empty?
  source = config_key
  GitFit.register_config_source(
    prefix: "GIT_FIT_#{source.upcase}",
    keys: keys,
    config_path: ['sync', source]
  )
end

.register_test_adapterObject



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

def register_test_adapter
  Base.test_adapters << self unless Base.test_adapters.include?(self)
end

.test_adaptersObject



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

def test_adapters
  @test_adapters ||= []
end

Instance Method Details

#before_callObject



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

def before_call
  @start_time = Time.now
  true
end

#build_progress_info(attrs) ⇒ Object



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
# File 'lib/git_fit/sync/base.rb', line 51

def build_progress_info(attrs)
  parts = []
  sd = attrs['start_date'] || attrs[:start_date]
  if sd
    d = Date.parse(sd.to_s) rescue nil
    if d
      date_str = d.year == Date.today.year ? d.strftime('%m-%d') : d.strftime('%Y-%m-%d')
      parts << date_str
    end
  end
  type = attrs['sport_category'] || attrs[:sport_category]
  parts << type if type
  dist = attrs['distance'] || attrs[:distance]
  if dist && dist.to_f > 0
    parts << "#{format("%.1f", dist.to_f / 1000)}km"
  end
  source = attrs['source'] || attrs[:source] || source_label
  name = attrs['name'] || attrs[:name]
  display_name = name.to_s.strip
  display_name = "#{source} Activity" if display_name.empty?
  chars = display_name.chars
  display_name = chars.first(30).join + '' if chars.size > 30
  parts << display_name
  parts.join(' | ') unless parts.empty?
end

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/git_fit/sync/base.rb', line 22

def call
  return 0 unless before_call
  return 0 unless respond_to?(:authenticate) ? authenticate : true
  ids = pending_ids
  return 0 if ids.nil? || ids.empty?
  total = ids.size
  count = 0
  ids.filter_map do |p|
    next if skip_type?(p[:type])
    r = process_one(p)
    count += 1
    report_progress(count, total, build_progress_info(r)) if r
    r
  end.size
end

#pending_idsObject

Raises:

  • (NotImplementedError)


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

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

#process_one(pending) ⇒ Object

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/git_fit/sync/base.rb', line 47

def process_one(pending)
  raise NotImplementedError, "#{self.class} must implement #process_one"
end