DJTommek / better-location

Telegram bot for parsing and generating various of location formats.
https://t.me/BetterLocationBot
MIT License
7 stars 2 forks source link

Services refactoring #46

Closed DJTommek closed 3 years ago

DJTommek commented 3 years ago

Summary

Refactor whole system of service classes into plugin system. Currently designed static methods are very inefficient and doesn't allow scenario, if input passed isValid() but in parse*() was found out, that there are no valid coordinations (for example Wikipedia page without coordinates, or Google link without coordinate parameters (issue #41).

Idea

Disclaimer: Anything can change depending what obstacles will be while coding this.

  1. Completely rewrite to instances instead of static.
  2. Once instance is created with given input, that input will be used across all methods (validation, processing, generating message), so no more passing around as parameter. Same applies to parsed URL (currently there is lots of parseUrl())
  3. Add first very quick level of validation, for example based on domain
  4. Add deeper validator (url path is required, needs to start with /bla/ble, etc). At this point there might be multiple valid URLs and each require unique code to handle it (for example in Geocaching there is GUID, coordinates in map, or geocache ID) and this validation method should be able to save which code will be used.
  5. Instead of one class per service, it could be folder with strict class names. I guess it should be divided between:
    • processing URLs - input is standalone URL
    • string - input is text, which whole can be translated as url. For example "GC3DYC4" but not "lets go to find out GC3DYC4"
    • text - searching string inside of block of text (as described above: "lets go to find out GC3DYC4")
  6. All these folders (let's call them plugins) will be loaded dynamically, no need to update anything when added new service
  7. If any of the service needs to do any kind of request (even get redirect location from HTTP headers), it should be done in separate class to be able to create unit tests to each class and eventually mock them easily if needed.
  8. Result of all services should always be BetterLocationCollection, even if it will be empty or only with errors. Throw errors only if there (almost) 100% should be valid location, but is not (eg. bad website response).