tricorder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +51 -0
  3. data/LICENSE +21 -0
  4. data/README.md +61 -0
  5. data/bin/tricorder +50 -0
  6. data/lib/dsl/operators/.rb +0 -0
  7. data/lib/dsl/operators/base_operators.rb +260 -0
  8. data/lib/dsl/operators/string.rb +9 -0
  9. data/lib/dsl/operators/tricorders.rb +68 -0
  10. data/lib/dsl/operators/tricorders/animal.rb +19 -0
  11. data/lib/dsl/operators/tricorders/astronomical_object.rb +19 -0
  12. data/lib/dsl/operators/tricorders/book.rb +19 -0
  13. data/lib/dsl/operators/tricorders/book_collection.rb +19 -0
  14. data/lib/dsl/operators/tricorders/book_series.rb +19 -0
  15. data/lib/dsl/operators/tricorders/character.rb +19 -0
  16. data/lib/dsl/operators/tricorders/comic_collection.rb +19 -0
  17. data/lib/dsl/operators/tricorders/comic_series.rb +19 -0
  18. data/lib/dsl/operators/tricorders/comic_strip.rb +19 -0
  19. data/lib/dsl/operators/tricorders/comics.rb +19 -0
  20. data/lib/dsl/operators/tricorders/company.rb +19 -0
  21. data/lib/dsl/operators/tricorders/conflict.rb +19 -0
  22. data/lib/dsl/operators/tricorders/element.rb +19 -0
  23. data/lib/dsl/operators/tricorders/episode.rb +19 -0
  24. data/lib/dsl/operators/tricorders/food.rb +19 -0
  25. data/lib/dsl/operators/tricorders/literature.rb +19 -0
  26. data/lib/dsl/operators/tricorders/location.rb +19 -0
  27. data/lib/dsl/operators/tricorders/magazine.rb +19 -0
  28. data/lib/dsl/operators/tricorders/magazine_series.rb +19 -0
  29. data/lib/dsl/operators/tricorders/material.rb +19 -0
  30. data/lib/dsl/operators/tricorders/medical_condition.rb +19 -0
  31. data/lib/dsl/operators/tricorders/movie.rb +19 -0
  32. data/lib/dsl/operators/tricorders/occupation.rb +19 -0
  33. data/lib/dsl/operators/tricorders/organization.rb +19 -0
  34. data/lib/dsl/operators/tricorders/performer.rb +19 -0
  35. data/lib/dsl/operators/tricorders/season.rb +19 -0
  36. data/lib/dsl/operators/tricorders/series.rb +19 -0
  37. data/lib/dsl/operators/tricorders/soundtrack.rb +19 -0
  38. data/lib/dsl/operators/tricorders/spacecraft.rb +19 -0
  39. data/lib/dsl/operators/tricorders/spacecraft_class.rb +19 -0
  40. data/lib/dsl/operators/tricorders/species.rb +19 -0
  41. data/lib/dsl/operators/tricorders/staff.rb +19 -0
  42. data/lib/dsl/operators/tricorders/technology.rb +19 -0
  43. data/lib/dsl/operators/tricorders/title.rb +19 -0
  44. data/lib/dsl/operators/tricorders/trading_card.rb +19 -0
  45. data/lib/dsl/operators/tricorders/trading_card_deck.rb +19 -0
  46. data/lib/dsl/operators/tricorders/trading_card_set.rb +19 -0
  47. data/lib/dsl/operators/tricorders/video_game.rb +19 -0
  48. data/lib/dsl/operators/tricorders/video_release.rb +19 -0
  49. data/lib/dsl/operators/tricorders/weapon.rb +19 -0
  50. data/lib/dsl/tricorder.rb +10 -0
  51. data/lib/dsl/tricorder_dsl.rb +23 -0
  52. data/lib/tricorder.rb +18 -0
  53. data/lib/version.rb +9 -0
  54. data/tricorder.gemspec +35 -0
  55. metadata +169 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 171d739dab9839ce2f0be74b57694a1876d063d5089cdb0d90b2090984e88346
4
+ data.tar.gz: fe3b21010cedcb7ef2a08fa94ce002eb90bfaaf9c48c269e4e0b60e8610f2e66
5
+ SHA512:
6
+ metadata.gz: 4f91847fe743bfb9a2153d4e1c906235abf15b3f7a174c7740bd35e548e42af685603976ca101a4d16a0508e5ac415cc0990e960776eb9337621f05e9b09a740
7
+ data.tar.gz: e74154e4d1a80bba53d4010bdeedcdf2dbd1117365b7bcd080dfc3842b73cc66f8ee972bb588646963ce1e964c5649c3795874466edc0de5507bb0c46a50a4ca
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ .byebug_history
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Joel Bryan Juliano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # tricorder
2
+ A Domain-Specific Language for Star Trek API (http://stapi.co)
3
+
4
+ # Installation
5
+
6
+ ```
7
+ gem install tricorder
8
+ ```
9
+
10
+ # Usage
11
+
12
+ ## CLI
13
+ Once installed, you can run the command `tricorder`.
14
+
15
+ To display all the available options for search type `tricorder help` or `tricorder help search`.
16
+
17
+ To search the API, invoke the `search <keyword>` command, which searches all databases by default.
18
+
19
+ ```
20
+ tricorder search Uhura --api-key=ABC123
21
+ ```
22
+
23
+ There are only 250 queries for hour for non-authenticated users, to specify the API Key, add the `--api-key=APIKEY` set the API Key on each query.
24
+
25
+ To search only specific database, use the `--database <database name>` to specify the database, for example
26
+
27
+ ```
28
+ tricorder search Uhura --database character book
29
+ ```
30
+
31
+ To print extra details, add the `--print-details` in the command, for example
32
+
33
+ ```
34
+ tricorder search uhura --database character --print-details
35
+ ```
36
+
37
+ To scope on a specific detail, add the `--print-info <scope>` scoping a result
38
+
39
+ ```
40
+ $ tricorder search Uhura --database character --print-only-once --print-info=characterSpecies name
41
+ Human
42
+ ```
43
+
44
+ ## Domain Specific Language
45
+ Here's an example of using tricorder programmatically via it's own DSL syntax,
46
+
47
+ ```
48
+ require 'tricorder'
49
+ include Tricorder
50
+
51
+ tricorder do
52
+ no_logging
53
+ api_key('MYAPIKEY')
54
+ set_subject('Uhura')
55
+ search_locations(:character)
56
+ print_only_once
57
+ print_info(['characterSpecies', 'name'])
58
+ end
59
+ ```
60
+
61
+ It set no_logging, set's the API key, set the subject to 'Uhura', set the database to 'character', scope to 'characterSpecies name' and print only once.
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require_relative '../lib/tricorder'
5
+
6
+ module Tricorder
7
+ class CLI < Thor
8
+ include Tricorder
9
+
10
+ map ["-v", "--version"] => :version
11
+ desc "--version, -v", "Display Tricorder version"
12
+ def version
13
+ puts Tricorder::VERSION::STRING
14
+ end
15
+
16
+ option :database, type: :array, desc: 'Specify the database to be searched'
17
+ option :disable_logging, type: :boolean, desc: 'Disable Logging'
18
+ option :api_key, type: :string, desc: 'API key to be used'
19
+ option :result_number, type: :numeric, desc: 'Choose a result number'
20
+ option :print_only_once, type: :boolean, desc: 'Disable printing multiple info results'
21
+ option :print_info, type: :array, desc: 'Print info scope. i.e. --print-info=characterSpecies name'
22
+ option :print_details, type: :boolean, desc: 'Print all the details of each queried info'
23
+
24
+ desc 'search <keyword>', 'Perform search in Star Trek API on all databases'
25
+ def search(keyword)
26
+ locations = if options[:database]
27
+ options[:database].map(&:to_sym)
28
+ else
29
+ Tricorders::TRICORDERS
30
+ end
31
+
32
+ command = "set_subject('#{keyword}')"
33
+ command += ".no_logging" if options[:disable_logging]
34
+ command += ".api_key('#{options[:api_key]}')" if options[:api_key]
35
+ command += ".print_only_once" if options[:print_only_once]
36
+ command += ".search_locations(#{locations})"
37
+ command += ".get_result_number(#{options[:result_number]})" if options[:result_number]
38
+ command += ".print_all_results" unless options[:result_number] || options[:print_only_once]
39
+ command += ".print_result" if options[:result_number] && !options[:print_only_once]
40
+ command += ".print_info(#{options[:print_info]})" if options[:print_info]
41
+ command += ".print_all_info" if options[:print_details]
42
+
43
+ tricorder do
44
+ eval command
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ Tricorder::CLI.start(ARGV)
File without changes
@@ -0,0 +1,260 @@
1
+ require_relative 'tricorders'
2
+
3
+ module BaseOperators
4
+ include Tricorders
5
+
6
+ class BaseDSLOperators
7
+ attr_reader :page
8
+
9
+ def set_subject(name)
10
+ @subject = name
11
+
12
+ self
13
+ end
14
+
15
+ def api_key(key)
16
+ @api_key = key
17
+ self
18
+ end
19
+
20
+ def set_tricorder(name)
21
+ @tricorder = name.to_sym
22
+ self
23
+ end
24
+
25
+ def init_error_messages
26
+ if @tricorder.nil? || !Tricorders::TRICORDERS.include?(@tricorder)
27
+ ap "Invalid Tricorder Name! #{@tricorder}"
28
+ ap "Set the correct tricorder via 'set_tricorder(name)'"
29
+ ap "Tricorders can either be #{Tricorders::TRICORDERS}"
30
+ @error = true
31
+ end
32
+
33
+ if @subject.nil?
34
+ ap 'No Subject Defined!'
35
+ ap "Set a subject via 'set_subject(name)'"
36
+ @error = true
37
+ end
38
+
39
+ exit(0) if @error
40
+ end
41
+
42
+ def no_logging
43
+ @verbose = false
44
+ self
45
+ end
46
+
47
+ def log(message, options = {})
48
+ return unless options[:verbose] || @verbose
49
+
50
+ if options[:pager]
51
+ self.send(options[:pager], message)
52
+ else
53
+ ap(message)
54
+ end
55
+ end
56
+
57
+ def set_path(path)
58
+ @path = path
59
+ self
60
+ end
61
+
62
+ def set_storedir(path)
63
+ @storedir = path
64
+ end
65
+
66
+ def params
67
+ {
68
+ apiKey: @api_key,
69
+ pageSize: 1000
70
+ }
71
+ end
72
+
73
+ def set_collection(collection)
74
+ exit(0) if @error
75
+
76
+ @collection = collection
77
+ self
78
+ end
79
+
80
+ def search_locations(*tricorders)
81
+ exit(0) if @error
82
+
83
+ tricorders.flatten.each do |location|
84
+ self.send("search_#{location.to_s}".to_sym)
85
+ end
86
+
87
+ self
88
+ end
89
+
90
+ def merge_results
91
+ exit(0) if @error
92
+
93
+ @results = @results.inject(:merge)
94
+ self
95
+ end
96
+
97
+ def get_result_number(num)
98
+ exit(0) if @error
99
+
100
+ begin
101
+ @result = @results[num - 1]
102
+ set_uid(@result['uid'])
103
+ rescue
104
+ ap "No results found!"
105
+ @error = true
106
+ end
107
+
108
+ self
109
+ end
110
+
111
+ def set_uid(uid)
112
+ exit(0) if @error
113
+
114
+ @uid = uid
115
+
116
+ self
117
+ end
118
+
119
+ def print_only_once
120
+ @print_only_once = true
121
+
122
+ self
123
+ end
124
+
125
+ def fetch_info
126
+ exit(0) if @error
127
+
128
+ response = Net::HTTP.get(URI.parse(@path + "?uid=#{@uid}"))
129
+
130
+ @info = JSON.parse(response)
131
+
132
+ @info[@tricorder.to_s].each_key do |key,val|
133
+ @info[@tricorder.to_s].compact!
134
+ @info[@tricorder.to_s].delete_if { |_k, v| v == false }
135
+ if @info[@tricorder.to_s][key].is_a?(Array)
136
+ @info[@tricorder.to_s][key].each_with_index do |_, index|
137
+ @info[@tricorder.to_s][key][index].compact!
138
+ @info[@tricorder.to_s][key][index].delete_if { |_k, v| v == false }
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ def search
145
+ exit(0) if @error
146
+
147
+ log "Searching '#{humanized_tricorder_name}' databank for '#{@subject}'..."
148
+
149
+ search_params = params.merge(
150
+ name: @subject,
151
+ title: @subject,
152
+ description: @subject
153
+ )
154
+
155
+ response = Net::HTTP.post_form(URI.parse(@path + '/search'), search_params)
156
+
157
+ @search << JSON.parse(response.body)
158
+
159
+ if @search[0].key?('code')
160
+ ap @search[0]['code']
161
+ @error = true
162
+ end
163
+
164
+ @search.flatten!
165
+
166
+ count = @search.count
167
+
168
+ 0.upto(count).each do |c|
169
+ next if @search.nil? || @search[c].nil? || @search[c][@collection].nil?
170
+ @results << @search[c][@collection]
171
+ @results.flatten!
172
+ @results.each_with_index do |result, index|
173
+ result.compact!
174
+ result.delete_if { |_k, v| v == false }
175
+ @results[index] = result
176
+ end
177
+ end
178
+
179
+ self
180
+ end
181
+
182
+ def humanized_tricorder_name
183
+ exit(0) if @error
184
+
185
+ init_error_messages
186
+
187
+ @tricorder.to_s.split('_').map(&:capitalize).join(' ')
188
+ end
189
+
190
+ def printer(object)
191
+ exit(0) if @error
192
+
193
+ init_error_messages
194
+
195
+ if object.empty?
196
+ "No info found!"
197
+ else
198
+ object
199
+ end
200
+ end
201
+
202
+ def print_info(*fields)
203
+ collection, field, number = fields.flatten
204
+ num = 0 || number
205
+
206
+ @results.each do |res|
207
+ set_uid(res['uid'])
208
+
209
+ fetch_info
210
+ begin
211
+ info = printer(@info[@tricorder.to_s])
212
+
213
+ print_info = if field
214
+ info[collection][num][field]
215
+ else
216
+ info[collection]
217
+ end
218
+
219
+ if @print_only_once
220
+ @out ||= print_info
221
+ else
222
+ @out = print_info
223
+ end
224
+
225
+ log(@out, {pager: :puts, verbose: true}) unless @printed
226
+
227
+ @printed = true if @print_only_once
228
+ rescue
229
+ log "Collection not found or empty"
230
+ log "Available collections are #{@info.delete_if { |_k, v| v == false || v.is_a?(Array) && v&.empty? }.keys.join(", ")}"
231
+ end
232
+ end
233
+
234
+ self
235
+ end
236
+
237
+ def print_all_info
238
+ @results.each do |result|
239
+ set_uid(result['uid'])
240
+
241
+ fetch_info
242
+ ap printer(@info)
243
+ end
244
+
245
+ self
246
+ end
247
+
248
+ def print_result
249
+ ap printer(@result)
250
+
251
+ self
252
+ end
253
+
254
+ def print_all_results
255
+ ap printer(@results)
256
+
257
+ self
258
+ end
259
+ end
260
+ end