Skip to content

Commit 0e33df0

Browse files
DraCzrisTomáš Polívka
authored andcommitted
Mocking fix refactored
Less hackier way to fix mocking invoking `to_str` method which resulted to raising error with missing client even from separeted units of API client (as signal was bubbling to top module)
1 parent 2e79897 commit 0e33df0

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

lib/kosapi_client/kosapi_client.rb

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
11
module KOSapiClient
22

3+
DEFAULT_KOSAPI_BASE_URL = 'https://kosapi.fit.cvut.cz/api/3'
4+
35
singleton_class.class_eval do
46

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)
715
end
816

917
def configure
10-
reset
18+
config = Configuration.new
1119
yield config
12-
self
13-
end
14-
15-
def client
16-
@client ||= ApiClient.new(config)
20+
@client = new(config.credentials)
1721
end
1822

1923
# Calling this method clears stored ApiClient instance
2024
# if configured previously.
2125
def reset
22-
@config = nil
2326
@client = nil
2427
end
2528

2629
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)
2935
else
3036
super
3137
end
3238
end
3339

3440
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)
4642
end
4743
end
4844
end

0 commit comments

Comments
 (0)