This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscientist
executable file
·250 lines (209 loc) · 7.61 KB
/
scientist
1
2
3
4
5
6
7
8
9
10
11
12
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env ruby
require "csv"
require "json"
require "rest-client"
require "thor"
module Scientist
class CLI < Thor
class_option :quiet, type: :boolean, default: false
class_option :raw, type: :boolean, default: false
no_tasks do
def logger
@logger ||= Logger.new("status.log")
end
def client_id
ENV["CLIENT_ID"]
end
def client_secret
ENV["CLIENT_SECRET"]
end
def protocol
ENV["PROTOCOL"] || "https"
end
def base_url
ENV["BASE_URL"] || "app.scientist.com"
end
def token_url
"#{protocol}://#{client_id}:#{client_secret}@#{base_url}/oauth/token?grant_type=client_credentials"
end
def api_url
"#{protocol}://#{base_url}/api"
end
def provider_url(provider_id)
"#{api_url}/providers/#{provider_id}.json"
end
def providers_url(search_term=nil, per_page=200)
"#{api_url}/providers.json?#{URI.encode_www_form({q: search_term, per_page: per_page}.compact)}"
end
def providers_by_ware_url(ware_id, per_page=200)
"#{api_url}/wares/#{ware_id}/providers.json?#{URI.encode_www_form({ per_page: per_page }.compact)}"
end
def ware_url(ware_id)
"#{api_url}/wares/#{ware_id}.json"
end
def wares_url(search_term=nil, per_page=200)
"#{api_url}/wares.json?#{URI.encode_www_form({q: search_term, per_page: per_page}.compact)}"
end
def wares_by_beacon_url(beacon_slug, per_page=200)
"#{api_url}/wares.json?#{URI.encode_www_form({"facets[beacon_ancestor_slugs][]" => beacon_slug, per_page: per_page}.compact)}"
end
def wares_by_provider_url(provider_id, per_page=200)
"#{api_url}/providers/#{provider_id}/wares.json?#{URI.encode_www_form({ per_page: per_page }.compact)}"
end
def categories_url
"#{api_url}/categories.json"
end
# 'research_area_add_delete' => $scientistUrl . '/api/wares/:ware_id/providers/:provider_id.json',
# 'research_area_get' => $scientistUrl . '/api/wares/:ware_id/providers.json'
def client_credentials
@token ||= JSON.parse(RestClient.get(token_url))
end
def client_credentials_token
client_credentials["access_token"]
end
def _providers(search_term=nil)
_get(providers_url(search_term))
end
def _provider(provider_id)
_get(provider_url(provider_id))
end
def _providers_by_ware(ware_id)
_get(providers_by_ware_url(ware_id))
end
def _wares(search_term=nil)
_get(wares_url(search_term))
end
def _ware(ware_id)
_get(ware_url(ware_id))
end
def _categories
_get(categories_url)
end
def _wares_by_beacon(beacon_slug)
_get(wares_by_beacon_url(beacon_slug))
end
def _wares_by_provider(provider_id)
_get(wares_by_provider_url(provider_id))
end
def _get(url)
start_time = Time.now
begin
response = RestClient.get(url, { Authorization: "Bearer #{client_credentials_token}" })
duration = Time.now - start_time
result = JSON.parse(response)
logger.info([start_time, duration, response.code, response.size, url].join(", "))
rescue => e
result = nil
logger.info([start_time, nil, e.message, nil, url].join(", "))
end
result
end
def recurse_beacon(beacon, parent_slugs, &block)
block.call(beacon, parent_slugs)
if beacon["children"]
beacon["children"].each do |child|
recurse_beacon(child, parent_slugs + [beacon["slug"]], &block)
end
end
end
end
desc "token", "Get client credentials token"
def token
token = client_credentials
puts "TOKEN [#{token["access_token"]}]" unless options[:quiet]
pp token if options[:raw]
end
desc "stress", "Stress test the system"
def stress(search_str=nil)
puts "finding [#{search_str}]" unless options[:quiet]
result = _providers(search_str)
result["provider_refs"].each do |provider_ref|
puts "provider [#{provider_ref["name"]}]" unless options[:quiet]
result = _provider(provider_ref["id"])
result = _wares_by_provider(provider_ref["id"])
result["ware_refs"].each do |ware_ref|
puts "ware [#{provider_ref["name"]}] > [#{ware_ref["name"]}]" unless options[:quiet]
result = _providers_by_ware(ware_ref["id"])
end
end
_categories["category_refs"].each do |category_ref|
recurse_beacon(category_ref, []) do |beacon, parent_slugs|
puts "category [#{(parent_slugs + [beacon["slug"]]).join(" > ")}]" unless options[:quiet]
result = _wares_by_beacon(beacon["slug"])
result["ware_refs"].each do |ware_ref|
puts "ware [#{ware_ref["name"]}]" unless options[:quiet]
result = _providers_by_ware(ware_ref["id"])
end
end
end
end
desc "providers <SEARCH STRING>", "Find providers (search string optional)"
def providers(search_str=nil)
result = _providers(search_str)
result["provider_refs"].each do |provider_ref|
puts CSV.generate_line([provider_ref["id"], provider_ref["name"]]) unless options[:quiet]
end
pp result if options[:raw]
end
desc "provider PROVIDER_ID", "Load a provider given an id"
def provider(provider_id)
result = _provider(provider_id)
pp result if options[:raw]
end
desc "provider_wares PROVIDER_ID", "Load a provider's wares given an id"
def provider_wares(provider_id)
result = _wares_by_provider(provider_id)
unless options[:quiet]
result["ware_refs"].each do |ware_ref|
puts CSV.generate_line([ware_ref["id"], ware_ref["slug"], ware_ref["name"]])
end
end
pp result if options[:raw]
end
desc "wares <SEARCH STRING>", "Find wares (search string optional)"
def wares(search_str=nil)
result = _wares(search_str)
unless options[:quiet]
result["ware_refs"].each do |ware_ref|
puts CSV.generate_line([ware_ref["id"], ware_ref["slug"], ware_ref["name"]])
end
end
pp result if options[:raw]
end
desc "ware WARE_ID", "Load a ware given an id"
def ware(ware_id)
result = _ware(ware_id)
puts CSV.generate_line([result.dig("ware", "id"), result.dig("ware", "slug"), result.dig("ware", "name")]) unless options[:quiet]
pp result if options[:raw]
end
desc "ware_providers WARE_ID", "Load a ware's providers given an id"
def ware_providers(ware_id)
result = _providers_by_ware(ware_id)
unless options[:quiet]
result["provider_refs"].each do |provider_ref|
puts CSV.generate_line([provider_ref["id"], provider_ref["slug"], provider_ref["name"]])
end
end
pp result if options[:raw]
end
desc "categories", "Load the categories"
def categories
result = _categories
result["category_refs"].each do |category_ref|
recurse_beacon(category_ref, []) do |beacon, parent_slugs|
puts (parent_slugs + [beacon["slug"]]).join(" > ") unless options[:quiet]
end
end
pp result if options[:raw]
end
desc "category_wares BEACON_SLUG", "Load wares by category"
def category_wares(beacon_slug)
result = _wares_by_beacon(beacon_slug)
result["ware_refs"].each do |ware_ref|
puts CSV.generate_line([ware_ref["id"], ware_ref["name"]]) unless options[:quiet]
end
pp result if options[:raw]
end
end
end
Scientist::CLI.start(ARGV)