-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwtsutil.rb
175 lines (142 loc) · 3.14 KB
/
wtsutil.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
77
78
79
80
81
82
83
84
85
86
87
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'yaml'
module WTS
class WTSCatalog
include Enumerable
def initialize(catalogPath='config/catalog.yml')
@catalogPath = catalogPath
if File.exists?(catalogPath)
@catalog = YAML.load_file(catalogPath)
else
@catalog = {}
end
if not @catalog.include? :alias
@catalog[:alias] = {}
end
if not @catalog.include? :tle
@catalog[:tle] = {}
end
end
# .aliases and .tles used only for generating satellite list web page,
# which could arguably be a method of this class, like .export...
def aliases
@catalog[:alias]
end
def tles
@catalog[:tle]
end
def save
self.export(@catalogPath)
end
def export(exportPath)
File.open(exportPath, 'w') do |file|
YAML.dump(@catalog, file)
end
end
#
# Parameters:
# key, which may be an alias name or a canonical name
#
# Returns:
# TLE data corresponding to key, or nil if none
#
def [](key)
if @catalog[:alias].has_key?(key)
@catalog[:tle][@catalog[:alias][key]]
else
@catalog[:tle][key]
end
end
#
# Parameters:
# key, which may be an alias name or a canonical name
# (if the key does not exist, it is used as a new canonical name)
# value, to be assigned to key
#
# Result:
# updates or creates catalog key with value
#
# Returns:
# assigned value
#
def []=(key, value)
if @catalog[:alias].has_key?(key)
# if the key exists as an alias, the value is assigned to
# the associated canonical name
@catalog[:tle][@catalog[:alias][key]] = value
else
# if the key exists only as a canonical name, or if it does
# not exist at all, the value is assigned to that canonical key
@catalog[:tle][key] = value
end
end
#
# Parameters:
# key, which is interpreted as a canonical name
# (no facility is provided to programmatically delete aliases)
#
# Results:
# deletes key-value pair from TLE hash, if present
#
# Returns:
# value of deleted key, or nil if none
#
def delete(key)
if @catalog[:tle].has_key? key
@catalog[:tle].delete(key)
end
end
#
# Results:
# yields each catalog key (alias and canonical)
#
def each
@catalog[:alias].keys.each do |aliasKey|
yield aliasKey
end
@catalog[:tle].keys.each do |canonicalKey|
yield canonicalKey
end
end
end
class WTSConfig
def initialize(configPath='config/wts.yml')
@configPath = configPath
@config = YAML.load_file(configPath)
end
def save
self.export(@configPath)
end
def export(exportPath)
File.open(exportPath, 'w') do |file|
YAML.dump(@config, file)
end
end
def searchTerms
@config[:searchTerms]
end
def tleIndexURLs
@config[:tleIndexURLs]
end
def login
@config[:authentication]
end
def searchesSinceId
@config[:searchesSinceId]
end
def searchesSinceId=(id)
@config[:searchesSinceId] = id
end
def mentionsSinceId
@config[:mentionsSinceId]
end
def mentionsSinceId=(id)
@config[:mentionsSinceId] = id
end
def dmSinceId
@config[:dmSinceId]
end
def dmSinceId=(id)
@config[:dmSinceId] = id
end
end
end