Module: ApiRecipes
- Defined in:
- lib/api_recipes.rb,
lib/api_recipes/version.rb,
lib/api_recipes/endpoint.rb,
lib/api_recipes/resource.rb,
lib/api_recipes/response.rb,
lib/api_recipes/settings.rb,
lib/api_recipes/exceptions.rb,
lib/api_recipes/configuration.rb
Defined Under Namespace
Modules: Settings
Classes: Configuration, Endpoint, EndpointConfigIsNotAnHash, EndpointNameClashError, MissingRouteAttribute, NoConfigurationGivenForEndpoint, ProvidedObjectNotAsResponseData, Resource, Response, ResponseCodeNotAsExpected, RouteAndResourceNamesClashError, RouteNameClashWithExistentMethod
Constant Summary
collapse
- VERSION =
'1.0.0'
Class Method Summary
collapse
Class Method Details
._aprcps_define_global_endpoints ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/api_recipes.rb', line 104
def self._aprcps_define_global_endpoints
configuration.endpoints_configs.each do |endpoint_name, endpoint_configs|
endpoint_name = endpoint_name.to_sym
_aprcps_global_storage[endpoint_name] = Endpoint.new endpoint_name, endpoint_configs
define_singleton_method endpoint_name do
_aprcps_global_storage[endpoint_name]
end
end
end
|
._aprcps_global_storage ⇒ Object
114
115
116
117
118
119
|
# File 'lib/api_recipes.rb', line 114
def self._aprcps_global_storage
unless @storage
@storage = {}
end
@storage
end
|
._aprcps_merge_endpoints_configs(endpoint_name, configs = nil) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/api_recipes.rb', line 128
def self._aprcps_merge_endpoints_configs(endpoint_name, configs = nil)
unless endpoint_name.is_a?(String) || endpoint_name.is_a?(Symbol)
raise ArgumentError, "no enpoint_name provided. Given: #{endpoint_name.inspect}"
end
unless ApiRecipes.configuration.endpoints_configs[endpoint_name]
ApiRecipes.configuration.endpoints_configs[endpoint_name] = {}
end
if configs
ApiRecipes.configuration.endpoints_configs[endpoint_name].merge(configs) do |_, old_val, new_val|
if new_val.nil?
old_val
else
new_val
end
end
else
ApiRecipes.configuration.endpoints_configs[endpoint_name]
end
end
|
._aprcps_thread_storage ⇒ Object
121
122
123
124
125
126
|
# File 'lib/api_recipes.rb', line 121
def self._aprcps_thread_storage
unless Thread.current[:api_recipes]
Thread.current[:api_recipes] = {}
end
Thread.current[:api_recipes]
end
|
.configuration ⇒ Object
56
57
58
59
60
61
|
# File 'lib/api_recipes.rb', line 56
def self.configuration
unless @configuration
@configuration = Configuration.new
end
@configuration
end
|
63
64
65
66
67
68
69
|
# File 'lib/api_recipes.rb', line 63
def self.configure
if block_given?
yield(configuration)
else
configuration
end
end
|
.copy_global_authorizations_to_endpoint(endpoint) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/api_recipes.rb', line 71
def self.copy_global_authorizations_to_endpoint(endpoint)
if _aprcps_global_storage[endpoint.name]
if auth = _aprcps_global_storage[endpoint.name].basic_auth
endpoint.authorization = auth
end
if auth = _aprcps_global_storage[endpoint.name].authorization
endpoint.authorization = auth
end
end
end
|
.included(receiver) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/api_recipes.rb', line 13
def self.included(receiver)
def receiver.endpoint(endpoint_name, configs = {})
unless endpoint_name.is_a?(String) || endpoint_name.is_a?(Symbol)
raise ArgumentError, "endpoint name must be a Symbol or String"
end
if configs && !configs.is_a?(Hash)
raise ApiRecipes::EndpointConfigIsNotAnHash.new(endpoint_name)
end
endpoint_name = endpoint_name.to_sym
configs = ApiRecipes._aprcps_merge_endpoints_configs(endpoint_name, configs.deep_symbolize_keys)
if self.respond_to? endpoint_name
raise EndpointNameClashError.new(self, endpoint_name)
else
ep = Endpoint.new(endpoint_name, configs)
ApiRecipes.copy_global_authorizations_to_endpoint ep
ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
ApiRecipes._aprcps_thread_storage[endpoint_name][self] = ep
define_method endpoint_name do
unless ApiRecipes._aprcps_thread_storage[endpoint_name]
ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
end
unless ApiRecipes._aprcps_thread_storage[endpoint_name][self.class]
ApiRecipes._aprcps_thread_storage[endpoint_name][self.class] = ep.clone
end
ApiRecipes._aprcps_thread_storage[endpoint_name][self.class]
end
define_singleton_method endpoint_name do
unless ApiRecipes._aprcps_thread_storage[endpoint_name]
ApiRecipes._aprcps_thread_storage[endpoint_name] = {}
end
unless ApiRecipes._aprcps_thread_storage[endpoint_name][self]
ApiRecipes._aprcps_thread_storage[endpoint_name][self] = ep.clone
end
ApiRecipes._aprcps_thread_storage[endpoint_name][self]
end
end
end
end
|
.set_authorization_for_endpoint(authorization, endpoint_name) ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/api_recipes.rb', line 82
def self.set_authorization_for_endpoint(authorization, endpoint_name)
endpoint_name = endpoint_name.to_sym
if _aprcps_thread_storage[endpoint_name]
_aprcps_thread_storage[endpoint_name].each do |_, endpoint|
endpoint.authorization = authorization
end
end
end
|
.set_basic_auth_for_endpoint(basic_auth, endpoint_name) ⇒ Object
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/api_recipes.rb', line 93
def self.set_basic_auth_for_endpoint(basic_auth, endpoint_name)
endpoint_name = endpoint_name.to_sym
if _aprcps_thread_storage[endpoint_name]
_aprcps_thread_storage[endpoint_name].each do |_, endpoint|
endpoint.authorization = basic_auth
end
end
end
|