Class: Pairity::GoogleSync

Inherits:
Object
  • Object
show all
Defined in:
lib/pairity/google_sync.rb

Constant Summary collapse

CONFIG_FILE =
Dir.home + "/.pairity_google.json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ GoogleSync

Returns a new instance of GoogleSync.



14
15
16
17
18
# File 'lib/pairity/google_sync.rb', line 14

def initialize(matrix)
  @matrix = matrix
  @people = {}
  @sheet_url = nil
end

Instance Attribute Details

#sheet_urlObject (readonly)

Returns the value of attribute sheet_url.



12
13
14
# File 'lib/pairity/google_sync.rb', line 12

def sheet_url
  @sheet_url
end

Instance Method Details

#clear_worksheet(ws) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/pairity/google_sync.rb', line 79

def clear_worksheet(ws)
  (1..ws.num_rows).each do |i|
    (1..ws.num_rows).each do |j|
      ws[i, j] = ""
      ws[j, i] = ""
    end
  end
end

#edit_edge(p1, p2, data, i) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/pairity/google_sync.rb', line 156

def edit_edge(p1, p2, data, i)
  case i
  when 1
    @matrix[p1, p2].days = data.to_i
  when 2
    @matrix[p1, p2].resistance = (data ? 1 : data.to_i)
  when 3
    @matrix[p1, p2].weight = data.to_i
  end
end

#find_person(name) ⇒ Object



167
168
169
170
171
# File 'lib/pairity/google_sync.rb', line 167

def find_person(name)

  person = @people.find { |person| person.name == name }

end

#loadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/pairity/google_sync.rb', line 20

def load
  unless File.exists?(CONFIG_FILE)
    puts "Welcome, newcomer!"
    puts "Please follow these instructions to allow #{Rainbow("Pairity").white} to use Google Sheets to sync its precious data."
  end

  session = GoogleDrive.saved_session(CONFIG_FILE)

  puts "Loading Matrix from Google Sheets..."
  progressbar = ProgressBar.create(total: 100)

  progressbar.progress += 20
  sheet = session.spreadsheet_by_title("Pairity")
  unless sheet
    puts "Creating a new spreadsheet called: Pairity"
    sheet = session.create_spreadsheet("Pairity")
    sheet.add_worksheet("Days")
    sheet.add_worksheet("Resistance")
    sheet.add_worksheet("Weights")
    ws = sheet.worksheets[0]
    ws.title = "People"
    ws.save
  else
  end
  @sheet_url = sheet.worksheets[0].human_url

  ws = sheet.worksheets[0]

  # Add People and Tiers to Matrix
  ws.num_rows.times do |row|
    next unless row > 0
    name = ws[row + 1, 1]
    next if name.strip.empty?
    tier = ws[row + 1, 2]
    if name == "Han Solo"
      person = @matrix.han_solo
    else
      person = Person.new(name: name, tier: tier)
      @matrix.add_person(person)
    end
    @people[person] = row + 1
  end

  # Add data to edges
  (1..3).each do |i|
    ws = sheet.worksheets[i]
    @people.each do |p1, row|
      @people.each do |p2, col|
        next if p1 == p2
        data = ws[row, col]
        edit_edge(p1, p2, data, i)
      end
    end
  end

  @matrix
end

#saveObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/pairity/google_sync.rb', line 88

def save
  puts "Saving Matrix to Google Sheets..."
  progressbar = ProgressBar.create(total: 100)

  session = GoogleDrive.saved_session(CONFIG_FILE)
  sheet = session.spreadsheet_by_title("Pairity")
  @people = @matrix.all_people.sort
  p @matrix.matrix

  progressbar.progress += 20

  ws = sheet.worksheets[0]

  clear_worksheet(ws)

  ws[1, 1] = "Name"
  ws[1, 2] = "Tier (1-3)"
  @people.each_with_index do |person, index|
    ws[index + 2, 1] = person.name
    ws[index + 2, 2] = person.tier
  end
  ws.save

  (1..3).each do |i|
    ws = sheet.worksheets[i]

    clear_worksheet(ws)

    @people.each_with_index do |person, index|
      ws[1, index + 2] = person.name
      ws[index + 2, 1] = person.name
    end

    progressbar.progress += 20
    @people.combination(2) do |pair|
      p1, p2 = pair
      index1 = @people.index(p1)
      index2 = @people.index(p2)
      edge = @matrix[p1,p2]
      case i
      when 1
        ws[1,1] = "Days"
        data = edge.days
      when 2
        ws[1,1] = "Resistances"
        data = edge.resistance
      when 3
        ws[1,1] = "Weights"
        data = edge.weight
      end
      ws[index1 + 2, index2 + 2] = data
      ws[index2 + 2, index1 + 2] = data
    end

    max = ws.max_rows

    @people.each_with_index do |person, index|
      ws[index + 2, index + 2] = ""
      ws[index + 2, index + 2] = ""
      max = index + 3
    end

    ws.save
  end
  progressbar.progress += 20

end