my_api_client 0.5.3 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/my_api_client/rspec/stub.rb +73 -1
- data/lib/my_api_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad19788202ac1ee6711537e81d0ab8d459918a9660d85bced28ae051dbf06d23
|
4
|
+
data.tar.gz: f36355f04e35e09473a3d4fdc6be1bb46bbd3b39d99d18b942d41891d581808a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 856baa51b7ee09c7178b6e3c3145ff313908b1790f4f051a19a987e645f9f9b4c4de8a2e2e0cd76d1cb9dac9dbabf210b90ff7dd8d78f4f1a1225cedb9f22628
|
7
|
+
data.tar.gz: 984dc3fb7a188b11e454eff1c92086e58d7346498aa9f8333aa7c429896c695d708e19b34f3b96a98311e1e9539c5d4e0975d0b71d2d52bd673a5eb01c9b3ca7
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
my_api_client (0.
|
4
|
+
my_api_client (0.6.0)
|
5
5
|
activesupport (>= 4.2.0)
|
6
6
|
jsonpath
|
7
7
|
sawyer
|
@@ -72,7 +72,7 @@ GEM
|
|
72
72
|
rspec-support (3.8.2)
|
73
73
|
rspec_junit_formatter (0.4.1)
|
74
74
|
rspec-core (>= 2, < 4, != 2.12.0)
|
75
|
-
rubocop (0.
|
75
|
+
rubocop (0.72.0)
|
76
76
|
jaro_winkler (~> 1.5.1)
|
77
77
|
parallel (~> 1.10)
|
78
78
|
parser (>= 2.6)
|
@@ -3,6 +3,55 @@
|
|
3
3
|
module MyApiClient
|
4
4
|
# Test helper module for RSpec
|
5
5
|
module Stub
|
6
|
+
# Stubs all instance of arbitrary MyApiClient class.
|
7
|
+
# And returns a stubbed arbitrary MyApiClient instance.
|
8
|
+
#
|
9
|
+
# @param klass [Class]
|
10
|
+
# Stubbing target class.
|
11
|
+
# @param actions_and_options [Hash]
|
12
|
+
# Stubbing target method and options
|
13
|
+
# @example
|
14
|
+
# stub_api_client_all(
|
15
|
+
# ExampleApiClient,
|
16
|
+
# get_user: { response: { id: 1 } }, # Returns an arbitrary response.
|
17
|
+
# post_users: { status: 'created' }, # You can ommit `response` keyword.
|
18
|
+
# patch_user: ->(params) { { id: params[:id] } }, # Returns calculated result as response.
|
19
|
+
# delete_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
|
20
|
+
# )
|
21
|
+
# response = ExampleApiClient.new.get_user(id: 123)
|
22
|
+
# response.id # => 1
|
23
|
+
# @return [InstanceDouble]
|
24
|
+
# Returns a spy object of the stubbed ApiClient.
|
25
|
+
def stub_api_client_all(klass, **actions_and_options)
|
26
|
+
instance = stub_api_client(klass, actions_and_options)
|
27
|
+
allow(klass).to receive(:new).and_return(instance)
|
28
|
+
instance
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a stubbed arbitrary MyApiClient instance.
|
32
|
+
#
|
33
|
+
# @param klass [Class]
|
34
|
+
# Stubbing target class.
|
35
|
+
# @param actions_and_options [Hash]
|
36
|
+
# Stubbing target method and options
|
37
|
+
# @example
|
38
|
+
# api_client = stub_api_client(
|
39
|
+
# ExampleApiClient,
|
40
|
+
# get_user: { response: { id: 1 } }, # Returns an arbitrary response.
|
41
|
+
# post_users: { status: 'created' }, # You can ommit `response` keyword.
|
42
|
+
# patch_user: ->(params) { { id: params[:id] } }, # Returns calculated result as response.
|
43
|
+
# delete_user: { raise: MyApiClient::ClientError } # Raises an arbitrary error.
|
44
|
+
# )
|
45
|
+
# response = api_client.get_user(id: 123)
|
46
|
+
# response.id # => 1
|
47
|
+
# @return [InstanceDouble]
|
48
|
+
# Returns a spy object of the stubbed ApiClient.
|
49
|
+
def stub_api_client(klass, **actions_and_options)
|
50
|
+
instance = instance_double(klass)
|
51
|
+
actions_and_options.each { |action, options| stubbing(instance, action, options) }
|
52
|
+
instance
|
53
|
+
end
|
54
|
+
|
6
55
|
# Provides stubbing feature for `MyApiClient`.
|
7
56
|
#
|
8
57
|
# @param klass [Class]
|
@@ -20,6 +69,10 @@ module MyApiClient
|
|
20
69
|
# Returns a spy object for the ApiClient.
|
21
70
|
# rubocop:disable Metrics/AbcSize
|
22
71
|
def my_api_client_stub(klass, action, response: nil, raise: nil)
|
72
|
+
ActiveSupport::Deprecation.warn(<<~MSG)
|
73
|
+
`my_api_client_stub` is deprecated. Please use `stub_api_client` or `stub_api_client_all`.
|
74
|
+
MSG
|
75
|
+
|
23
76
|
instance = instance_double(klass)
|
24
77
|
allow(klass).to receive(:new).and_return(instance)
|
25
78
|
if raise.present?
|
@@ -35,6 +88,25 @@ module MyApiClient
|
|
35
88
|
|
36
89
|
private
|
37
90
|
|
91
|
+
# rubocop:disable Metrics/AbcSize
|
92
|
+
def stubbing(instance, action, options)
|
93
|
+
case options
|
94
|
+
when Proc
|
95
|
+
allow(instance).to receive(action) { |*request| stub_as_sawyer(options.call(*request)) }
|
96
|
+
when Hash
|
97
|
+
if options[:raise].present?
|
98
|
+
allow(instance).to receive(action).and_raise(process_raise_option(options[:raise]))
|
99
|
+
elsif options[:response]
|
100
|
+
allow(instance).to receive(action).and_return(stub_as_sawyer(options[:response]))
|
101
|
+
else
|
102
|
+
allow(instance).to receive(action).and_return(stub_as_sawyer(options))
|
103
|
+
end
|
104
|
+
else
|
105
|
+
allow(instance).to receive(action).and_return(stub_as_sawyer(options))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
# rubocop:enable Metrics/AbcSize
|
109
|
+
|
38
110
|
# Provides a shorthand for `raise` option.
|
39
111
|
# `MyApiClient::Error` requires `MyApiClient::Params::Params` instance on
|
40
112
|
# initialize, but it makes trubolesome. `MyApiClient::NetworkError` is more.
|
@@ -62,7 +134,7 @@ module MyApiClient
|
|
62
134
|
def stub_as_sawyer(params)
|
63
135
|
case params
|
64
136
|
when Hash then Sawyer::Resource.new(agent, params)
|
65
|
-
when Array then params.map { |hash|
|
137
|
+
when Array then params.map { |hash| stub_as_sawyer(hash) }
|
66
138
|
when nil then nil
|
67
139
|
else params
|
68
140
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: my_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ryz310
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|