api_gears 1.0.1

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.

Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/api_gears.rb +234 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c62c13b7038d6613f9b86fbfdf5eac31a01f34d716166cbb6e1259df3c68fed2
4
+ data.tar.gz: 1acc11103f1bc5616b82d47e36f2efe3ab2321b312d3e53c6bd9e74c5527367b
5
+ SHA512:
6
+ metadata.gz: 18b73f487c6ded347c9848a2861f538b1fbf7e9424ec2e6435ec2188c3bdfaddb69503450a0fea8e2b48593f4a58c3a0f6617119deebe3304f947bd012e2d1ed
7
+ data.tar.gz: 86cec73c079089886c6c020124f00def60218cc7abc4ac93a2fac0e20d048d2dee053e5ddc8382d911c85cc9c720f5ad3ae297a72560c7fa79c6e18ca72d1fa7
@@ -0,0 +1,234 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'date'
4
+ require 'json'
5
+
6
+ class ApiGears
7
+
8
+ def initialize(url, **options)
9
+ @uri = URI.parse(url)
10
+ @endpoints = {}
11
+ @last_query_time = 0
12
+ if(!options[:query_interval].nil?)
13
+ @query_interval = options[:query_interval]
14
+ else
15
+ @query_interval = 0
16
+ end
17
+
18
+ if(!options[:params].nil?)
19
+ @params = options[:params]
20
+ else
21
+ @params = {}
22
+ end
23
+ if(!options[:content_type].nil?)
24
+ @content_type = options[:content_type]
25
+ else
26
+ @content_type = "plain"
27
+ end
28
+
29
+ if(!options[:query_method].nil?)
30
+ @query_method = options[:query_method].to_sym
31
+ else
32
+ @query_method = :GET
33
+ end
34
+ end
35
+
36
+ def request_info(endpoint,**args)
37
+ if(args.nil?)
38
+ args = {}
39
+ end
40
+ args[:endpoint] = endpoint.to_sym
41
+ r_info = build_request(args)
42
+ query_method = r_info[:query_method]
43
+ url = r_info[:url]
44
+ puts "this method will #{query_method} #{url}"
45
+ puts "With query params:"+r_info[:params].keys.collect{|k| "#{k}:#{r_info[:params][k]}"}.join("\n")
46
+ end
47
+
48
+ def request(**args)
49
+ r_info = build_request(args)
50
+ current_elapsed = DateTime.now.strftime('%s').to_i - @last_query_time
51
+ if(current_elapsed < @query_interval)
52
+ cooldown = @query_interval - current_elapsed
53
+ puts "Sleeping #{cooldown} seconds to accomodate query interval."
54
+ sleep(cooldown)
55
+ end
56
+ query_method = r_info[:query_method]
57
+ url = r_info[:url]
58
+
59
+ puts "#{query_method} #{url}"
60
+ http = Net::HTTP.new(url.host, url.port)
61
+ http.use_ssl = true
62
+ if(query_method == :GET)
63
+ request = Net::HTTP::Get.new(url)
64
+ if(!@params.nil?) # Set API-wide header params, such as api-key
65
+ @params.each_pair do |param_name,param|
66
+ request[param_name.to_s] = param
67
+ end
68
+ end
69
+ end
70
+ if(query_method == :POST)
71
+ request = Net::HTTP::Post.new(url)
72
+ end
73
+ if(query_method == :PATCH)
74
+ request = Net::HTTP::Patch.new(url)
75
+ end
76
+ if(query_method == :PUT)
77
+ request = Net::HTTP::Put.new(url)
78
+ end
79
+
80
+ if(query_method != :GET)
81
+ if(@content_type == "json")
82
+ r_info[:params] = JSON.generate(r_info[:params])
83
+ elsif(@content_type == "plain")
84
+ r_info[:params] = URI.encode_www_form(r_info[:params])
85
+ end
86
+ response = http.request(request,r_info[:params])
87
+ else
88
+ response = http.request(request)
89
+ end
90
+ @last_query_time = DateTime.now.strftime('%s').to_i
91
+ data = JSON.parse(response.read_body)
92
+ if(response.code.to_i >= 200 && response.code.to_i < 300)
93
+ puts "success"
94
+ return data
95
+ else
96
+ puts response.code.to_s+" "+response.message
97
+ end
98
+ # if(@endpoint[args[:endpoint][:block]])
99
+ # return @endpoint[args[:endpoint]][:block](data)
100
+ # else
101
+
102
+ # end
103
+
104
+ end
105
+ def method_missing(m, **args, &block)
106
+ if(!@endpoints[m.to_sym].nil?)
107
+ request(endpoint:m.to_sym,**args)
108
+ else
109
+ raise NoMethodError.new "endpoint '#{m}' not defined in #{self.class.name}. Call `<#{self.class.name} instance>.endpoints` to get a list of defined endpoints."
110
+ end
111
+
112
+ end
113
+ def endpoints
114
+ @endpoints.keys
115
+ end
116
+ def args_for(e)
117
+ @endpoints[e.to_sym][:args].map{|arg| arg.to_sym}
118
+ end
119
+ def query_params_for(e)
120
+ @endpoints[e.to_sym][:query_params].map{|arg| arg.to_sym}.concat(@endpoints[e.to_sym][:set_query_params].keys)
121
+ end
122
+ def prepare_data(data, depth=0,&block)
123
+ if(!block.nil?)
124
+ yield(data,depth)
125
+ end
126
+ depth = depth + 1
127
+ if(data.is_a? Array)
128
+ data.each_index do |i|
129
+ if(!block.nil?)
130
+ data[i] = self.prepare_data(data[i],depth, &block)
131
+ else
132
+ data[i] = self.prepare_data(data[i],depth)
133
+ end
134
+ end
135
+ end
136
+ if(data.is_a? Hash)
137
+ data.symbolize_keys!
138
+ data.each_pair do |k,v|
139
+ if(!block.nil?)
140
+ data[k] = self.prepare_data(v,depth, &block)
141
+ else
142
+ data[k] = self.prepare_data(v,depth)
143
+ end
144
+ end
145
+ end
146
+ return data
147
+ end
148
+ private
149
+ def build_request(args)
150
+ if(args[:endpoint])
151
+ endpoint_data = @endpoints[args[:endpoint].to_sym]
152
+ end
153
+ if(args[:query_method])
154
+ args[:query_method] = args[:query_method].to_s.upcase.to_sym
155
+ elsif(endpoint_data[:query_method])
156
+ args[:query_method] = endpoint_data[:query_method].to_s.upcase.to_sym
157
+ else
158
+ args[:query_method] = @query_method.to_s.upcase.to_sym
159
+ end
160
+ uf = url_for(args[:endpoint],args)
161
+ return {query_method:args[:query_method],**uf}
162
+ end
163
+
164
+ def endpoint(name, **args, &block)
165
+ if(args[:path] == nil)
166
+ args[:path] = "/#{name.to_s}"
167
+ end
168
+ if(args[:args].nil?)
169
+ args[:args] = []
170
+ else
171
+ args[:args] = args[:args].map do |arg_name|
172
+ arg_name.to_sym
173
+ end
174
+ end
175
+ if(args[:path].match(/\{([a-zA-Z_0-9\-]*)\}/))
176
+ path_args = args[:path].scan(/\{([a-zA-Z_0-9\-]*)\}/).flatten
177
+ path_args.each do |name|
178
+ args[:args] << name.to_sym
179
+ end
180
+ end
181
+ if(args[:query_params].nil?)
182
+ args[:query_params] = {}
183
+ end
184
+ if(args[:set_query_params].nil?)
185
+ args[:set_query_params] = {}
186
+ end
187
+ @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
+ end
189
+
190
+ def url_for(endpoint, args)
191
+ url = @uri.dup
192
+
193
+ e_path = @endpoints[endpoint.to_sym][:path]
194
+
195
+ if(!@endpoints[endpoint.to_sym][:args].nil? && @endpoints[endpoint.to_sym][:args].length > 0)
196
+ args_for(endpoint.to_sym).each do |arg|
197
+ if(!args[arg].nil? && arg.to_s.split("_").length == 2 && args[arg.to_s.split("_")[0].to_sym].nil?)
198
+ value = args[arg.to_s.split("_")[0].to_sym]
199
+ else
200
+ value = args[arg]
201
+ end
202
+ e_path = e_path.gsub("{#{arg}}", value.to_s)
203
+ end
204
+ end
205
+
206
+ if(e_path.include? @uri.path)
207
+ url.path = e_path
208
+ else
209
+ url.path = url.path + e_path
210
+ end
211
+
212
+
213
+ if(@endpoints[endpoint.to_sym][:query_params])
214
+ query_set = {}
215
+ query_params_for(endpoint.to_sym).each do |param|
216
+ if(args[param.to_sym])
217
+ query_set[param.to_sym] = args[param.to_sym]
218
+ elsif @endpoints[endpoint.to_sym][:set_query_params][param.to_sym]
219
+ query_set[param.to_sym] = @endpoints[endpoint.to_sym][:set_query_params][param.to_sym]
220
+ end
221
+ end
222
+
223
+ if(args[:query_method] == :GET)
224
+ url.query = URI.encode_www_form(query_set)
225
+ return {url:url,params:{}}
226
+ else
227
+ return {url:url,params:query_set}
228
+ end
229
+ else
230
+
231
+ return {url:url,params:{}}
232
+ end
233
+ end
234
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_gears
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greg Mikeska
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ApiGears enables rapid deployment of API clients.
14
+ email: digialintaglio@lavabit.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/api_gears.rb
20
+ homepage: https://github.com/gmikeska/api_engine
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Rapid API client deployment library
43
+ test_files: []