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
19
# 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
  @auth_method = nil
end

Instance Attribute Details

#auth_methodObject

Returns the value of attribute auth_method.



22
23
24
# File 'lib/git_fit/sync/base.rb', line 22

def auth_method
  @auth_method
end

#progress_reportedObject (readonly)

Returns the value of attribute progress_reported.



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

def progress_reported
  @progress_reported
end

Class Method Details

.adaptersObject



90
91
92
# File 'lib/git_fit/sync/base.rb', line 90

def adapters
  @adapters ||= []
end

.camelcase_to_snakecase(str) ⇒ Object



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

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

.config_key(source = nil) ⇒ Object



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

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

.register_adapterObject



98
99
100
# File 'lib/git_fit/sync/base.rb', line 98

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

.register_config(*keys) ⇒ Object



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

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



102
103
104
# File 'lib/git_fit/sync/base.rb', line 102

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

.test_adaptersObject



94
95
96
# File 'lib/git_fit/sync/base.rb', line 94

def test_adapters
  @test_adapters ||= []
end

Instance Method Details

#before_callObject



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

def before_call
  @start_time = Time.now
  true
end

#build_progress_info(attrs) ⇒ Object



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

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



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

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)


45
46
47
# File 'lib/git_fit/sync/base.rb', line 45

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

#process_one(pending) ⇒ Object

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/git_fit/sync/base.rb', line 49

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