cocRb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cocRb/clanWar.rb CHANGED
@@ -1,300 +1,307 @@
1
- require 'faraday'
2
- require 'time'
1
+ require "faraday"
2
+ require "time"
3
3
  require "json"
4
4
 
5
5
  module CocRb
6
+
6
7
  class << self
7
- attr_accessor :configuration
8
- end
8
+ attr_accessor :configuration
9
+ end
9
10
 
10
11
  def self.configure
11
- @configuration ||= Configuration.new
12
- yield(configuration)
13
- end
12
+ @configuration ||= Configuration.new
13
+ yield(configuration)
14
+ end
14
15
 
15
16
  class Configuration
16
- attr_accessor :token, :url
17
+ attr_accessor :token, :url
17
18
 
18
- def initialize
19
- @token = nil
20
- @url = nil
19
+ def initialize
20
+ @token = nil
21
+ @url = nil
21
22
  end
22
- end
23
+ end
24
+
23
25
  class Settings
24
- def self.get
25
- @conn = Faraday.new(
26
- url:"https://api.clashofclans.com" ,
27
- headers: {
28
- 'Content-Type' => 'application/json',
29
- 'Authorization' => "Bearer #{CocRb.configuration.token}"
26
+ def self.get
27
+ @conn = Faraday.new(
28
+ url:"https://api.clashofclans.com" ,
29
+ headers: {
30
+ 'Content-Type' => 'application/json',
31
+ 'Authorization' => "Bearer #{CocRb.configuration.token}"
30
32
  }
31
33
  )
32
- rescue => e
33
- raise "Oops Unexpected error Caught!"
34
- puts e
34
+ rescue => e
35
+ raise "Oops Unexpected error Caught!"
36
+ puts e
37
+ end
35
38
  end
36
- end
37
- class ClanWar < Settings
38
- def self.get_WarLog(tag:, _limit:false, status: false)
39
- get
40
- io = tag
41
- tag = io.gsub('#', '%23')
42
- res = @conn.get("v1/clans/#{tag}/warlog") do |req|
43
- req.params[:limit] = _limit if _limit
44
- end
45
-
46
- if status
47
- puts res.status
48
- else
49
- val = res.body
50
- convert = JSON.parse(val)
51
- end
52
- end
53
39
 
54
- def self.get_ClanWar(tag:, status: false)
55
- get
56
- io = tag
57
- tag = io.gsub('#', '%23')
58
- res = @conn.get("v1/clans/#{tag}/currentwar")
59
-
60
- if status
61
- puts res.status
62
- else
63
- val = res.body
64
- convert = JSON.parse(val)
65
- end
40
+ class ClanWar < Settings
41
+ def self.get_WarLog(tag:, _limit:false, status: false)
42
+ get
43
+ io = tag
44
+ tag = io.gsub('#', '%23')
45
+ res = @conn.get("v1/clans/#{tag}/warlog") do |req|
46
+ req.params[:limit] = _limit if _limit
47
+ end
66
48
 
49
+ if status
50
+ puts res.status
51
+ else
52
+ val = res.body
53
+ convert = JSON.parse(val)
54
+ end
67
55
  end
68
56
 
69
- def self.get_ClanWarMembers(tag:, teamInput:, status: false)
70
- get
71
- teamInput_Options = ["clan", "opponent"]
57
+ def self.get_ClanWar(tag:, status: false)
58
+ get
59
+ io = tag
60
+ tag = io.gsub('#', '%23')
61
+ res = @conn.get("v1/clans/#{tag}/currentwar")
72
62
 
73
- check = teamInput_Options.include?(teamInput)
63
+ if status
64
+ puts res.status
65
+ else
66
+ val = res.body
74
67
 
75
- if check == false
68
+ convert = JSON.parse(val)
69
+ end
70
+ end
76
71
 
77
- puts 'Not a valid teamInput Option'
72
+ def self.get_ClanWarMembers(tag:, teamInput:, status: false)
73
+ get
74
+ teamInput_Options = ["clan", "opponent"]
78
75
 
79
- elsif check == true
76
+ check = teamInput_Options.include?(teamInput)
80
77
 
81
- io = tag
82
- tag = io.gsub('#', '%23')
83
- res = @conn.get("v1/clans/#{tag}/currentwar")
78
+ if check == false
79
+ puts 'Not a valid teamInput Option'
80
+ elsif check == true
81
+ io = tag
82
+ tag = io.gsub('#', '%23')
83
+ res = @conn.get("v1/clans/#{tag}/currentwar")
84
84
 
85
85
  if status
86
- puts res.status
86
+ puts res.status
87
87
  else
88
- val = res.body
89
- convert = JSON.parse(val)
88
+ val = res.body
90
89
 
91
- getClan_War_Members = convert[teamInput]['members']
90
+ convert = JSON.parse(val)
92
91
 
93
- hash = getClan_War_Members.sort_by { |s| s['townhallLevel'] }
92
+ getClan_War_Members = convert[teamInput]['members']
94
93
 
95
- sort = hash.sort_by { |_k, v| v }.reverse
96
-
97
- end
98
- end
94
+ hash = getClan_War_Members.sort_by { |s| s['townhallLevel'] }
99
95
 
96
+ sort = hash.sort_by { |_k, v| v }.reverse
100
97
 
98
+ end
99
+ end
101
100
  end
102
101
 
103
-
104
-
105
- def self.getWarTime(tag:,status:false)
106
- get
107
- io = tag
108
- tag = io.gsub('#', '%23')
109
- res = @conn.get("v1/clans/#{tag}/currentwar")
110
- if status
111
- puts res.status
102
+ def self.get_WarTime(tag:, status: false)
103
+ get
104
+ io = tag
105
+ tag = io.gsub('#', '%23')
106
+ res = @conn.get("v1/clans/#{tag}/currentwar")
107
+ if status
108
+ puts res.status
112
109
  else
113
- val = res.body
110
+ val = res.body
114
111
 
115
- convert = JSON.parse(val)
112
+ convert = JSON.parse(val)
116
113
 
117
- time = convert["preparationStartTime"]
114
+ time = convert["preparationStartTime"]
118
115
 
119
- time2 = convert["startTime"]
116
+ time2 = convert["startTime"]
120
117
 
121
- time3 = convert["endTime"]
118
+ time3 = convert["endTime"]
122
119
 
123
- t = Time.parse(time).strftime('%y-%m-%d, %I:%M:%S %p')
124
- t1 = Time.parse(time2).strftime('%y-%m-%d, %I:%M:%S %p')
125
- t2 = Time.parse(time3).strftime('%y-%m-%d, %I:%M:%S %p')
120
+ t = Time.parse(time).strftime('%y-%m-%d, %I:%M:%S %p')
121
+ t1 = Time.parse(time2).strftime('%y-%m-%d, %I:%M:%S %p')
122
+ t2 = Time.parse(time3).strftime('%y-%m-%d, %I:%M:%S %p')
126
123
 
127
- warTimes = {
128
- "preparationStartTime" => time,
129
- "startTime" => time2,
130
- "endTime" => time3
131
- },
132
- {
133
- "parsed_preparationStartTime" => t,
134
- "parsed_startTime" => t1,
135
- "parsed_endTime" => t2
136
- }.freeze
137
- end
138
- end
124
+ warTimes = {
125
+ "preparationStartTime" => time,
126
+ "startTime" => time2,
127
+ "endTime" => time3
128
+ },
129
+ {
130
+ "parsed_preparationStartTime" => t,
131
+ "parsed_startTime" => t1,
132
+ "parsed_endTime" => t2
133
+ }.freeze
134
+ end
135
+ end
139
136
  end
137
+
140
138
  class ClanWarLeague < Settings
141
- def self.get_ClanWarLeagueGroup(tag:,status: false)
142
- get
143
- io = tag
144
- tag = io.gsub('#', '%23')
145
- res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
139
+ def self.get_ClanWarLeagueGroup(tag:,status: false)
140
+ get
141
+ io = tag
146
142
 
147
- if status
148
- res.status
149
- else
150
- val = res.body
151
- convert = JSON.parse(val)
152
- end
153
- end
143
+ tag = io.gsub('#', '%23')
154
144
 
145
+ res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
155
146
 
156
- def self.get_ClanWarLeagueClanItems(tag:,status: false)
157
- get
158
- io = tag
159
- tag = io.gsub('#', '%23')
147
+ if status
148
+ res.status
149
+ else
150
+ val = res.body
151
+ convert = JSON.parse(val)
152
+ end
153
+ end
160
154
 
161
- res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
155
+ def self.get_ClanWarLeagueClanItems(tag:,status: false)
156
+ get
157
+ io = tag
158
+ tag = io.gsub('#', '%23')
159
+
160
+ res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
162
161
 
163
162
  if status
164
- res.status
163
+ res.status
165
164
  else
166
- parse = res.body
165
+ parse = res.body
167
166
 
168
- convert = JSON.parse(parse)
167
+ convert = JSON.parse(parse)
169
168
 
170
- clans = convert['clans']
169
+ clans = convert['clans']
171
170
 
172
- items_name = clans.map { |n| n['name'] }
171
+ items_name = clans.map { |n| n['name'] }
173
172
 
174
- items_tag = clans.map { |t| t['tag'] }
173
+ items_tag = clans.map { |t| t['tag'] }
175
174
 
176
- items_clanLevel = clans.map { |c| c['clanLevel'] }
175
+ items_clanLevel = clans.map { |c| c['clanLevel'] }
177
176
 
178
- items_badgeUrls = clans.map { |b| b['badgeUrls'] }
177
+ items_badgeUrls = clans.map { |b| b['badgeUrls'] }
179
178
 
180
- clanItems = {
179
+ clanItems = {
181
180
  'tag' => items_tag[0],
182
181
  'name' => items_name[0],
183
182
  'clanLevel' => items_clanLevel[0],
184
183
  'badgeUrls' => items_badgeUrls[0]
185
- },
186
- {
184
+ },
185
+ {
187
186
  'tag' => items_tag[1],
188
187
  'name' => items_name[1],
189
188
  'clanLevel' => items_clanLevel[1],
190
189
  'badgeUrls' => items_badgeUrls[1]
191
- },
192
- {
190
+ },
191
+ {
193
192
  'tag' => items_tag[2],
194
193
  'name' => items_name[2],
195
194
  'clanLevel' => items_clanLevel[2],
196
195
  'badgeUrls' => items_badgeUrls[2]
197
- },
198
- {
196
+ },
197
+ {
199
198
  'tag' => items_tag[3],
200
199
  'name' => items_name[3],
201
200
  'clanLevel' => items_clanLevel[3],
202
201
  'badgeUrls' => items_badgeUrls[3]
203
- },
204
- {
202
+ },
203
+ {
205
204
  'tag' => items_tag[4],
206
205
  'name' => items_name[4],
207
206
  'clanLevel' => items_clanLevel[4],
208
207
  'badgeUrls' => items_badgeUrls[4]
209
- },
210
- {
208
+ },
209
+ {
211
210
  'tag' => items_tag[5],
212
211
  'name' => items_name[5],
213
212
  'clanLevel' => items_clanLevel[5],
214
213
  'badgeUrls' => items_badgeUrls[5]
215
- },
216
- {
214
+ },
215
+ {
217
216
  'tag' => items_tag[6],
218
217
  'name' => items_name[6],
219
218
  'clanLevel' => items_clanLevel[6],
220
219
  'badgeUrls' => items_badgeUrls[6]
221
- },
222
- {
220
+ },
221
+ {
223
222
  'tag' => items_tag[7],
224
223
  'name' => items_name[7],
225
224
  'clanLevel' => items_clanLevel[7],
226
225
  'badgeUrls' => items_badgeUrls[7]
227
- }.freeze
226
+ }.freeze
228
227
 
229
- end
228
+ end
230
229
  end
231
230
 
232
231
  def self.get_WarTags(tag:, status:false)
233
- get
234
- io = tag
235
- tag = io.gsub('#', '%23')
232
+ get
236
233
 
237
- res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
234
+ io = tag
238
235
 
239
- if status
240
- res.status
236
+ tag = io.gsub('#', '%23')
237
+
238
+ res = @conn.get("v1/clans/#{tag}/currentwar/leaguegroup")
241
239
 
240
+ if status
241
+ res.status
242
242
  else
243
- val = res.body
244
- convert = JSON.parse(val)
245
- rounds = convert['rounds']
243
+ val = res.body
246
244
 
247
- end
248
- end
245
+ convert = JSON.parse(val)
249
246
 
250
- def self.get_ClanWarLeague_Wars(warTag:, status:false)
251
- get
252
- io = warTag
253
- tag = io.gsub('#', '%23')
254
- res = @conn.get("v1/clanwarleagues/wars/#{tag}")
247
+ rounds = convert['rounds']
255
248
 
256
- if status
257
- res.status
258
- else
259
- val = res.body
260
- convert = JSON.parse(val)
261
249
  end
250
+ end
251
+
252
+ def self.get_ClanWarLeagueWars(warTag:, status:false)
253
+ get
254
+ io = warTag
255
+
256
+ tag = io.gsub('#', '%23')
257
+
258
+ res = @conn.get("v1/clanwarleagues/wars/#{tag}")
259
+
260
+ if status
261
+ res.status
262
+ else
263
+ val = res.body
264
+ convert = JSON.parse(val)
265
+ end
262
266
  end
263
267
 
264
- def self.getClanWarLeagueWarTimes(warTag:, status: false)
265
- get
266
- io = warTag
267
- tag = io.gsub('#', '%23')
268
- res = @conn.get("v1/clanwarleagues/wars/#{tag}")
268
+ def self.get_ClanWarLeagueWarTimes(warTag:, status: false)
269
+ get
270
+ io = warTag
271
+
272
+ tag = io.gsub('#', '%23')
273
+
274
+ res = @conn.get("v1/clanwarleagues/wars/#{tag}")
269
275
 
270
- if status
271
- res.status
276
+ if status
277
+ res.status
272
278
  else
273
- val = res.body
274
- convert = JSON.parse(val)
275
- prepStartTime = convert["preparationStartTime"]
276
- startTime = convert["startTime"]
277
- endTime = convert["endTime"]
278
-
279
- parsed_preparationStartTime = Time.parse(prepStartTime).strftime('%y-%m-%d, %I:%M:%S %p')
280
- parsed_startTime = Time.parse(startTime).strftime('%y-%m-%d, %I:%M:%S %p')
281
- parsed_endTime = Time.parse(endTime).strftime('%y-%m-%d, %I:%M:%S %p')
282
-
283
- times = {
284
- "preparationStartTime" => prepStartTime,
285
- "startTime" => startTime,
286
- "endTime" => endTime
287
- },
288
- {
289
- "parsed_preparationStartTime" => parsed_preparationStartTime,
290
- "parsed_startTime" => parsed_startTime,
291
- "parsed_endTime" => parsed_endTime
292
- }
279
+ val = res.body
280
+ convert = JSON.parse(val)
293
281
 
294
- end
282
+ prepStartTime = convert["preparationStartTime"]
295
283
 
284
+ startTime = convert["startTime"]
296
285
 
286
+ endTime = convert["endTime"]
297
287
 
298
- end
299
- end
288
+ parsed_preparationStartTime = Time.parse(prepStartTime).strftime('%y-%m-%d, %I:%M:%S %p')
289
+
290
+ parsed_startTime = Time.parse(startTime).strftime('%y-%m-%d, %I:%M:%S %p')
291
+
292
+ parsed_endTime = Time.parse(endTime).strftime('%y-%m-%d, %I:%M:%S %p')
293
+
294
+ times = {
295
+ "preparationStartTime" => prepStartTime,
296
+ "startTime" => startTime,
297
+ "endTime" => endTime
298
+ },
299
+ {
300
+ "parsed_preparationStartTime" => parsed_preparationStartTime,
301
+ "parsed_startTime" => parsed_startTime,
302
+ "parsed_endTime" => parsed_endTime
303
+ }
304
+ end
305
+ end
306
+ end
300
307
  end