togglv8
togglv8 copied to clipboard
Improve initialization
Background
Use of TogglV8::API#initialize() is convoluted. It supports three authentication alternatives:
toggl = TogglV8::API.new() # with API token in ~/.toggl
toggl = TogglV8::API.new(api_token)
toggl = TogglV8::API.new(username, password)
Problems
The source code is not clear on how it can be used.
def initialize(username=nil, password=API_TOKEN, opts={})
- Passing an API token as
username
with no correspondingpassword
is unexpected. - The no-argument option is perhaps too magical.
- It is only possible to pass a value for
opts
with the username/password authentication (or by explicitly passing "api_token" as the second parameter.
Solution
- Separate the three types of authentication and require one to be called explicitly.
- Provide the credentials and create the connection after
new()
, not during. - Consider allowing other options in
opts
(e.g. enable debugging) or remove it entirely and replace with explicit method calls