-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the way how the client lazy initialisation works.
- Loading branch information
Showing
8 changed files
with
42 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
module KOSapiClient | ||
|
||
DEFAULT_KOSAPI_BASE_URL = 'https://kosapi.fit.cvut.cz/api/3' | ||
|
||
singleton_class.class_eval do | ||
|
||
attr_reader :client | ||
|
||
def new(credentials, base_url = DEFAULT_KOSAPI_BASE_URL) | ||
http_adapter = OAuth2HttpAdapter.new(credentials, base_url) | ||
http_client = HTTPClient.new(http_adapter) | ||
ApiClient.new(http_client, base_url) | ||
def new(options = {}) | ||
ApiClient.new(Configuration.new(options)) | ||
end | ||
|
||
def configure | ||
config = Configuration.new | ||
reset | ||
yield config | ||
@client = new(config.credentials) | ||
self | ||
end | ||
|
||
def client | ||
@client ||= ApiClient.new(config) | ||
end | ||
|
||
# Calling this method clears stored ApiClient instance | ||
# if configured previously. | ||
def reset | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
tszolar
Author
Member
|
||
@config = nil | ||
@client = nil | ||
end | ||
|
||
def method_missing(method, *args, &block) | ||
if @client.nil? | ||
raise "Client not configured. Either you forgot to call configure or you have typo in method name '#{method}'." | ||
end | ||
if @client.respond_to?(method) | ||
@client.send(method, *args, &block) | ||
if client.respond_to?(method) | ||
client.send(method, *args, &block) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(method_name, include_private = false) | ||
@client.respond_to?(method_name, include_private) | ||
client.respond_to?(method_name, include_private) | ||
end | ||
|
||
private | ||
def config | ||
@config ||= Configuration.new | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module KOSapiClient | ||
VERSION = '0.3.0' | ||
VERSION = '0.3.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Do you think this should be exposed as public API? It causes object to be in an invalid state. Maybe it would be better to not have this method at all and just rewrite
config
and@client
directly in theconfigure
method.