api_gears 1.0.2 → 1.0.3
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.
Potentially problematic release.
This version of api_gears might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/api_gears.rb +34 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 548bb48ccb9ac58a7b7ffaeb721c6555a2277def81c9760d72a15a94d815a834
|
4
|
+
data.tar.gz: b47a91c3db56462aaed913aa3d0f35f69691e4683bca4c7c9f52f3002b244a7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a7edbbf6e91f9c3487af26ee57f27a4e219dd80a000ff7295b2117e7c1579f7e5c832a044e695b252269cfcb060c420db73e6ae63ff85fb4592c78fdc4fe53d
|
7
|
+
data.tar.gz: 60b35d6156a5af96defb2e2dea720aa72e16dfccf2b84a5a14271917a373b82ee1bea856afcca36485f619e4af3afa90ed9450b1e8f1daac9371dff222ac9e3e
|
data/lib/api_gears.rb
CHANGED
@@ -2,9 +2,13 @@ require 'uri'
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'date'
|
4
4
|
require 'json'
|
5
|
-
|
5
|
+
# Base class for API client development, specify endpoints with `endpoint "shortcut_name", path:"api/subpath/"` syntax and method calls to valid shortcut_names will be caught with method_missing
|
6
6
|
class ApiGears
|
7
|
-
|
7
|
+
# Intializes a new ApiGears instance.
|
8
|
+
#
|
9
|
+
# @param url [String] the base url for the API being queried
|
10
|
+
# @param options [Hash] allows setting :query_interval, global params (such as api-key, and default query_method)
|
11
|
+
# @return [ApiGears] a new ApiGears Instance
|
8
12
|
def initialize(url, **options)
|
9
13
|
@uri = URI.parse(url)
|
10
14
|
@endpoints = {}
|
@@ -32,7 +36,11 @@ class ApiGears
|
|
32
36
|
@query_method = :GET
|
33
37
|
end
|
34
38
|
end
|
35
|
-
|
39
|
+
# Provides information on the request an endpoint will send.
|
40
|
+
#
|
41
|
+
# @param endpoint [Symbol] the base url for the API being queried
|
42
|
+
# @param args [Hash] params to flesh-out the API call.
|
43
|
+
# @return [nil]
|
36
44
|
def request_info(endpoint,**args)
|
37
45
|
if(args.nil?)
|
38
46
|
args = {}
|
@@ -102,6 +110,12 @@ class ApiGears
|
|
102
110
|
# end
|
103
111
|
|
104
112
|
end
|
113
|
+
# Handles calls to any endpoint created during initialization
|
114
|
+
#
|
115
|
+
# @param m [Symbol] the method name being called
|
116
|
+
# @param args [Hash] params passed to method call
|
117
|
+
# @param block [Block] As yet unused.
|
118
|
+
# @return [Hash] or [Array], depending on what API returns.
|
105
119
|
def method_missing(m, **args, &block)
|
106
120
|
if(!@endpoints[m.to_sym].nil?)
|
107
121
|
request(endpoint:m.to_sym,**args)
|
@@ -110,15 +124,24 @@ class ApiGears
|
|
110
124
|
end
|
111
125
|
|
112
126
|
end
|
127
|
+
# Provides a list of endpoints that can be called as method names on the ApiGears object. Api calls are caught with #method_missing
|
128
|
+
#
|
129
|
+
# @return [Array]
|
113
130
|
def endpoints
|
114
131
|
@endpoints.keys
|
115
132
|
end
|
133
|
+
# Provides a list of args that a particular endpoint needs
|
134
|
+
# @param e [Symbol] or [String]
|
135
|
+
# @return [Array] of argument symbols sought during endpoint call.
|
116
136
|
def args_for(e)
|
117
137
|
@endpoints[e.to_sym][:args].map{|arg| arg.to_sym}
|
118
138
|
end
|
119
139
|
def query_params_for(e)
|
120
140
|
@endpoints[e.to_sym][:query_params].map{|arg| arg.to_sym}.concat(@endpoints[e.to_sym][:set_query_params].keys)
|
121
141
|
end
|
142
|
+
# Can be called during override of #request to modify API response data before return
|
143
|
+
# @param data [Hash] or [Array]
|
144
|
+
# @return [Hash] or [Array] depending on the data in the API response
|
122
145
|
def prepare_data(data, depth=0,&block)
|
123
146
|
if(!block.nil?)
|
124
147
|
yield(data,depth)
|
@@ -160,7 +183,10 @@ class ApiGears
|
|
160
183
|
uf = url_for(args[:endpoint],args)
|
161
184
|
return {query_method:args[:query_method],**uf}
|
162
185
|
end
|
163
|
-
|
186
|
+
# Allows implementer to specify an API endpoint to be queried by a specific method name.
|
187
|
+
# @param name [String] the method name that will execute the query
|
188
|
+
# @param args [Hash] accepts path (subpath to query), query_params (query parameters to be sent when method is called) and set_query_params (query parameters that are specified ahead of time for each execution of the method being defined)
|
189
|
+
# @return [nil]
|
164
190
|
def endpoint(name, **args, &block)
|
165
191
|
if(args[:path] == nil)
|
166
192
|
args[:path] = "/#{name.to_s}"
|
@@ -186,7 +212,10 @@ class ApiGears
|
|
186
212
|
end
|
187
213
|
@endpoints[name.to_sym] = {path:args[:path],args:path_args,query_params:args[:query_params],query_method:args[:query_method] ,set_query_params:args[:set_query_params], block:block}
|
188
214
|
end
|
189
|
-
|
215
|
+
# Builds URL and args for a specific endpoint call
|
216
|
+
# @param endpoint [String] or [Symbol] the method name that has been called
|
217
|
+
# @param args [Hash] the args passed in during the method call
|
218
|
+
# @return [Hash] with url and query params
|
190
219
|
def url_for(endpoint, args)
|
191
220
|
url = @uri.dup
|
192
221
|
|