11module KOSapiClient
22
3- DEFAULT_KOSAPI_BASE_URL = 'https://kosapi.fit.cvut.cz/api/3'
4-
53 singleton_class . class_eval do
64
7- attr_reader :client
8-
9- alias_method :to_str , :to_s
10-
11- def new ( credentials , base_url = DEFAULT_KOSAPI_BASE_URL )
12- http_adapter = OAuth2HttpAdapter . new ( credentials , base_url )
13- http_client = HTTPClient . new ( http_adapter )
14- ApiClient . new ( http_client , base_url )
5+ def new ( options = { } )
6+ ApiClient . new ( Configuration . new ( options ) )
157 end
168
179 def configure
18- config = Configuration . new
10+ reset
1911 yield config
20- @client = new ( config . credentials )
12+ self
13+ end
14+
15+ def client
16+ @client ||= ApiClient . new ( config )
2117 end
2218
2319 # Calling this method clears stored ApiClient instance
2420 # if configured previously.
2521 def reset
22+ @config = nil
2623 @client = nil
2724 end
2825
2926 def method_missing ( method , *args , &block )
30- if @client . nil?
31- raise "Client not configured. Either you forgot to call configure or you have typo in method name '#{ method } '."
32- end
33- if @client . respond_to? ( method )
34- @client . send ( method , *args , &block )
27+ if client . respond_to? ( method )
28+ client . send ( method , *args , &block )
3529 else
3630 super
3731 end
3832 end
3933
4034 def respond_to_missing? ( method_name , include_private = false )
41- @ client. respond_to? ( method_name , include_private )
35+ client . respond_to? ( method_name , include_private )
4236 end
37+
38+ private
39+ def config
40+ @config ||= Configuration . new
41+ end
42+
4343 end
44- end
44+
45+ end
0 commit comments