|
1 | 1 | module KOSapiClient |
2 | 2 |
|
| 3 | + DEFAULT_KOSAPI_BASE_URL = 'https://kosapi.fit.cvut.cz/api/3' |
| 4 | + |
3 | 5 | singleton_class.class_eval do |
4 | 6 |
|
5 | | - def new(options = {}) |
6 | | - ApiClient.new(Configuration.new(options)) |
| 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) |
7 | 15 | end |
8 | 16 |
|
9 | 17 | def configure |
10 | | - reset |
| 18 | + config = Configuration.new |
11 | 19 | yield config |
12 | | - self |
13 | | - end |
14 | | - |
15 | | - def client |
16 | | - @client ||= ApiClient.new(config) |
| 20 | + @client = new(config.credentials) |
17 | 21 | end |
18 | 22 |
|
19 | 23 | # Calling this method clears stored ApiClient instance |
20 | 24 | # if configured previously. |
21 | 25 | def reset |
22 | | - @config = nil |
23 | 26 | @client = nil |
24 | 27 | end |
25 | 28 |
|
26 | 29 | def method_missing(method, *args, &block) |
27 | | - if client.respond_to?(method) |
28 | | - client.send(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) |
29 | 35 | else |
30 | 36 | super |
31 | 37 | end |
32 | 38 | end |
33 | 39 |
|
34 | 40 | def respond_to_missing?(method_name, include_private = false) |
35 | | - client.respond_to?(method_name, include_private) |
36 | | - end |
37 | | - |
38 | | - private |
39 | | - def config |
40 | | - @config ||= Configuration.new |
41 | | - end |
42 | | - |
43 | | - # Was interfering with mocking |
44 | | - def to_str |
45 | | - "KOSapi client" |
| 41 | + @client.respond_to?(method_name, include_private) |
46 | 42 | end |
47 | 43 | end |
48 | 44 | end |
0 commit comments