Conditions | 14 |
Total Lines | 144 |
Code Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like darksky.GetWeatherImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package darksky |
||
142 | func GetWeatherImage(ctx *bot.Context) (buf *bytes.Buffer, err error) { |
||
143 | var ( |
||
144 | forecast DarkSkyResponse |
||
145 | city = ctx.GetGuild().WeatherCity |
||
146 | ) |
||
147 | |||
148 | if len(ctx.Args) > 0 { |
||
149 | city = strings.Join(ctx.Args, "+") |
||
150 | } |
||
151 | |||
152 | loc, err := location.New(ctx.Conf.General.GeonamesUsername, city) |
||
153 | if err != nil { |
||
154 | fmt.Printf("Location API: %v\n", err) |
||
155 | return |
||
156 | } |
||
157 | |||
158 | cityName := loc.Geonames[0].CountryName + ", " + loc.Geonames[0].Name |
||
159 | |||
160 | // Get coordinates and get weather data |
||
161 | newlat, newlng := loc.GetCoordinates() |
||
162 | resp, err := http.Get(fmt.Sprintf("https://api.darksky.net/forecast/%v/%v,%v?units=ca&lang=%v", |
||
163 | ctx.Conf.DarkSky.Token, newlat, newlng, ctx.Conf.General.Language)) |
||
164 | if err != nil { |
||
165 | fmt.Printf("Weather API: %v\n", err) |
||
166 | return |
||
167 | } |
||
168 | |||
169 | err = json.NewDecoder(resp.Body).Decode(&forecast) |
||
170 | if err != nil { |
||
171 | fmt.Printf("Weather Decode: %v\n", err) |
||
172 | return |
||
173 | } |
||
174 | |||
175 | // Drawing weather widget |
||
176 | gc := gg.NewContext(400, 650) |
||
177 | gc.SetRGBA(0, 0, 0, 0) |
||
178 | gc.Clear() |
||
179 | |||
180 | // Template |
||
181 | gc.SetRGB255(242, 97, 73) |
||
182 | gc.DrawRoundedRectangle(0, 0, 400, 650, 10) |
||
183 | gc.Fill() |
||
184 | |||
185 | // Weather lines |
||
186 | gc.SetRGB255(234, 89, 65) |
||
187 | gc.DrawRectangle(0, 250, 400, 100) |
||
188 | gc.DrawRectangle(0, 450, 400, 100) |
||
189 | gc.Fill() |
||
190 | |||
191 | gc.SetLineWidth(2) |
||
192 | gc.SetRGBA(0, 0, 0, 0.05) |
||
193 | gc.DrawLine(0, 250, 400, 250) |
||
194 | gc.DrawLine(0, 349, 400, 348) |
||
195 | gc.DrawLine(0, 450, 400, 450) |
||
196 | gc.DrawLine(0, 549, 400, 548) |
||
197 | gc.Stroke() |
||
198 | |||
199 | // Text |
||
200 | if err := gc.LoadFontFace("lato.ttf", 20); err != nil { |
||
201 | panic(err) |
||
202 | } |
||
203 | |||
204 | // Header (place and date) |
||
205 | gc.SetRGBA(1, 1, 1, 0.7) |
||
206 | gc.DrawStringAnchored(cityName, 10, 15, 0, 0.5) |
||
207 | gc.SetRGBA(1, 1, 1, 0.4) |
||
208 | gc.DrawStringAnchored(time.Now().Format("Jan 2, 2006"), 270, 15, 0, 0.5) |
||
209 | |||
210 | // First weather data |
||
211 | gc.SetRGBA(1, 1, 1, 0.5) |
||
212 | if err := gc.LoadFontFace("lato.ttf", 30); err != nil { |
||
213 | panic(err) |
||
214 | } |
||
215 | |||
216 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Currently.GetTime(forecast.Timezone, ctx.Conf.General.Timezone).Hour()), 50, 200, 0.5, 0.5) |
||
217 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", int(forecast.Currently.Humidity*100)), 200, 200, 0.5, 0.5) |
||
218 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Currently.CloudCover*100)), 350, 200, 0.5, 0.5) |
||
219 | |||
220 | gc.SetRGBA(1, 1, 1, 1) |
||
221 | if err := gc.LoadFontFace("lato.ttf", 90); err != nil { |
||
222 | panic(err) |
||
223 | } |
||
224 | |||
225 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Currently.Temperature)), 100, 120, 0.5, 0.5) |
||
226 | |||
227 | if err := gc.LoadFontFace("weathericons.ttf", 70); err != nil { |
||
228 | panic(err) |
||
229 | } |
||
230 | |||
231 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Currently.Icon)), 250, 120, 0, 0.7) |
||
232 | |||
233 | if err := gc.LoadFontFace("lato.ttf", 30); err != nil { |
||
234 | panic(err) |
||
235 | } |
||
236 | |||
237 | // Time |
||
238 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Hourly.Data[2].GetTime(forecast.Timezone, ctx.Conf.General.Timezone).Hour()), 100, 285, 0, 0.5) |
||
239 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Hourly.Data[4].GetTime(forecast.Timezone, ctx.Conf.General.Timezone).Hour()), 100, 385, 0, 0.5) |
||
240 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Hourly.Data[6].GetTime(forecast.Timezone, ctx.Conf.General.Timezone).Hour()), 100, 485, 0, 0.5) |
||
241 | gc.DrawStringAnchored(fmt.Sprintf("%.2v:00", forecast.Hourly.Data[8].GetTime(forecast.Timezone, ctx.Conf.General.Timezone).Hour()), 100, 585, 0, 0.5) |
||
242 | |||
243 | // Humidity and cloudiness |
||
244 | if err := gc.LoadFontFace("lato.ttf", 20); err != nil { |
||
245 | panic(err) |
||
246 | } |
||
247 | gc.SetRGBA(1, 1, 1, 0.5) |
||
248 | |||
249 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", int(forecast.Hourly.Data[2].Humidity*100)), 100, 315, 0, 0.5) |
||
250 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", int(forecast.Hourly.Data[4].Humidity*100)), 100, 415, 0, 0.5) |
||
251 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", int(forecast.Hourly.Data[6].Humidity*100)), 100, 515, 0, 0.5) |
||
252 | gc.DrawStringAnchored(fmt.Sprintf("H:%v%%", int(forecast.Hourly.Data[8].Humidity*100)), 100, 615, 0, 0.5) |
||
253 | |||
254 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Hourly.Data[2].CloudCover*100)), 170, 315, 0, 0.5) |
||
255 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Hourly.Data[4].CloudCover*100)), 170, 415, 0, 0.5) |
||
256 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Hourly.Data[6].CloudCover*100)), 170, 515, 0, 0.5) |
||
257 | gc.DrawStringAnchored(fmt.Sprintf("C:%v%%", int(forecast.Hourly.Data[8].CloudCover*100)), 170, 615, 0, 0.5) |
||
258 | |||
259 | gc.SetRGBA(1, 1, 1, 1) |
||
260 | if err := gc.LoadFontFace("lato.ttf", 50); err != nil { |
||
261 | panic(err) |
||
262 | } |
||
263 | |||
264 | // Temperature |
||
265 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Hourly.Data[2].Temperature)), 320, 300, 0.5, 0.5) |
||
266 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Hourly.Data[4].Temperature)), 320, 400, 0.5, 0.5) |
||
267 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Hourly.Data[6].Temperature)), 320, 500, 0.5, 0.5) |
||
268 | gc.DrawStringAnchored(fmt.Sprintf("%v°", int(forecast.Hourly.Data[8].Temperature)), 320, 600, 0.5, 0.5) |
||
269 | |||
270 | if err := gc.LoadFontFace("weathericons.ttf", 40); err != nil { |
||
271 | panic(err) |
||
272 | } |
||
273 | |||
274 | // Weather icon |
||
275 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Hourly.Data[2].Icon)), 20, 300, 0, 0.7) |
||
276 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Hourly.Data[4].Icon)), 20, 400, 0, 0.7) |
||
277 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Hourly.Data[6].Icon)), 20, 500, 0, 0.7) |
||
278 | gc.DrawStringAnchored(ctx.WeatherCode(fmt.Sprintf("%v", forecast.Hourly.Data[8].Icon)), 20, 600, 0, 0.7) |
||
279 | |||
280 | buf = new(bytes.Buffer) |
||
281 | pngerr := png.Encode(buf, gc.Image()) |
||
282 | if pngerr != nil { |
||
283 | fmt.Printf("Image: %v\n", pngerr) |
||
284 | } |
||
285 | return |
||
286 | } |
||
434 |