exa-ai 0.15.0 → 0.16.0

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.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/README.md +72 -1
  4. data/exe/exa-ai +139 -1
  5. data/exe/exa-ai-enrichment-cancel +108 -0
  6. data/exe/exa-ai-enrichment-create +188 -0
  7. data/exe/exa-ai-enrichment-delete +122 -0
  8. data/exe/exa-ai-enrichment-get +104 -0
  9. data/exe/exa-ai-enrichment-list +99 -0
  10. data/exe/exa-ai-enrichment-update +140 -0
  11. data/exe/exa-ai-import-create +224 -0
  12. data/exe/exa-ai-import-delete +92 -0
  13. data/exe/exa-ai-import-get +92 -0
  14. data/exe/exa-ai-import-list +71 -0
  15. data/exe/exa-ai-import-update +130 -0
  16. data/exe/exa-ai-monitor-create +180 -0
  17. data/exe/exa-ai-monitor-delete +101 -0
  18. data/exe/exa-ai-monitor-get +92 -0
  19. data/exe/exa-ai-monitor-list +89 -0
  20. data/exe/exa-ai-monitor-runs-get +103 -0
  21. data/exe/exa-ai-monitor-runs-list +110 -0
  22. data/exe/exa-ai-monitor-update +192 -0
  23. data/exe/exa-ai-search +10 -0
  24. data/exe/exa-ai-search-monitor-create +1 -1
  25. data/exe/exa-ai-search-monitor-delete +1 -1
  26. data/exe/exa-ai-search-monitor-get +1 -1
  27. data/exe/exa-ai-search-monitor-list +1 -1
  28. data/exe/exa-ai-search-monitor-trigger +1 -1
  29. data/exe/exa-ai-webset-cancel +97 -0
  30. data/exe/exa-ai-webset-create +220 -0
  31. data/exe/exa-ai-webset-delete +111 -0
  32. data/exe/exa-ai-webset-get +93 -0
  33. data/exe/exa-ai-webset-item-delete +111 -0
  34. data/exe/exa-ai-webset-item-get +105 -0
  35. data/exe/exa-ai-webset-item-list +108 -0
  36. data/exe/exa-ai-webset-list +96 -0
  37. data/exe/exa-ai-webset-search-cancel +104 -0
  38. data/exe/exa-ai-webset-search-create +231 -0
  39. data/exe/exa-ai-webset-search-get +105 -0
  40. data/exe/exa-ai-webset-update +116 -0
  41. data/lib/exa/cli/formatters/enrichment_formatter.rb +125 -0
  42. data/lib/exa/cli/formatters/import_formatter.rb +153 -0
  43. data/lib/exa/cli/formatters/monitor_formatter.rb +144 -0
  44. data/lib/exa/cli/formatters/monitor_run_formatter.rb +126 -0
  45. data/lib/exa/cli/formatters/search_formatter.rb +22 -1
  46. data/lib/exa/cli/formatters/search_monitor_formatter.rb +1 -1
  47. data/lib/exa/cli/formatters/webset_formatter.rb +134 -0
  48. data/lib/exa/cli/formatters/webset_item_formatter.rb +132 -0
  49. data/lib/exa/cli/formatters/webset_search_formatter.rb +57 -0
  50. data/lib/exa/cli/search_parser.rb +4 -0
  51. data/lib/exa/client.rb +313 -1
  52. data/lib/exa/constants/websets.rb +19 -0
  53. data/lib/exa/resources/import.rb +111 -0
  54. data/lib/exa/resources/import_collection.rb +33 -0
  55. data/lib/exa/resources/monitor.rb +72 -0
  56. data/lib/exa/resources/monitor_collection.rb +30 -0
  57. data/lib/exa/resources/monitor_run.rb +82 -0
  58. data/lib/exa/resources/monitor_run_collection.rb +30 -0
  59. data/lib/exa/resources/search_monitor.rb +1 -0
  60. data/lib/exa/resources/search_result.rb +5 -1
  61. data/lib/exa/resources/webset.rb +106 -0
  62. data/lib/exa/resources/webset_collection.rb +33 -0
  63. data/lib/exa/resources/webset_enrichment.rb +71 -0
  64. data/lib/exa/resources/webset_enrichment_collection.rb +28 -0
  65. data/lib/exa/resources/webset_item.rb +63 -0
  66. data/lib/exa/resources/webset_item_collection.rb +33 -0
  67. data/lib/exa/resources/webset_search.rb +140 -0
  68. data/lib/exa/services/parameter_converter.rb +3 -2
  69. data/lib/exa/services/search.rb +2 -1
  70. data/lib/exa/services/websets/cancel.rb +19 -0
  71. data/lib/exa/services/websets/cancel_enrichment.rb +35 -0
  72. data/lib/exa/services/websets/cancel_search.rb +23 -0
  73. data/lib/exa/services/websets/create.rb +28 -0
  74. data/lib/exa/services/websets/create_enrichment.rb +35 -0
  75. data/lib/exa/services/websets/create_search.rb +28 -0
  76. data/lib/exa/services/websets/create_search_validator.rb +135 -0
  77. data/lib/exa/services/websets/create_validator.rb +214 -0
  78. data/lib/exa/services/websets/delete.rb +19 -0
  79. data/lib/exa/services/websets/delete_enrichment.rb +35 -0
  80. data/lib/exa/services/websets/delete_item.rb +20 -0
  81. data/lib/exa/services/websets/get_item.rb +20 -0
  82. data/lib/exa/services/websets/get_search.rb +22 -0
  83. data/lib/exa/services/websets/import_create.rb +22 -0
  84. data/lib/exa/services/websets/import_delete.rb +19 -0
  85. data/lib/exa/services/websets/import_get.rb +19 -0
  86. data/lib/exa/services/websets/import_list.rb +25 -0
  87. data/lib/exa/services/websets/import_update.rb +20 -0
  88. data/lib/exa/services/websets/import_upload.rb +59 -0
  89. data/lib/exa/services/websets/list.rb +25 -0
  90. data/lib/exa/services/websets/list_items.rb +26 -0
  91. data/lib/exa/services/websets/monitors/create.rb +31 -0
  92. data/lib/exa/services/websets/monitors/delete.rb +21 -0
  93. data/lib/exa/services/websets/monitors/get.rb +22 -0
  94. data/lib/exa/services/websets/monitors/list.rb +27 -0
  95. data/lib/exa/services/websets/monitors/runs/get.rb +25 -0
  96. data/lib/exa/services/websets/monitors/runs/list.rb +30 -0
  97. data/lib/exa/services/websets/monitors/update.rb +28 -0
  98. data/lib/exa/services/websets/retrieve.rb +30 -0
  99. data/lib/exa/services/websets/retrieve_enrichment.rb +35 -0
  100. data/lib/exa/services/websets/update.rb +40 -0
  101. data/lib/exa/services/websets/update_enrichment.rb +36 -0
  102. data/lib/exa/services/websets_parameter_converter.rb +51 -0
  103. data/lib/exa/version.rb +1 -1
  104. data/lib/exa.rb +51 -0
  105. metadata +331 -227
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "exa-ai"
5
+
6
+ # Parse arguments
7
+ webset_id = nil
8
+ search_id = nil
9
+ api_key = nil
10
+ output_format = "json"
11
+
12
+ args = ARGV.dup
13
+ while args.any?
14
+ arg = args.shift
15
+ case arg
16
+ when "--api-key"
17
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
18
+ when "--output-format"
19
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
20
+ when "--help", "-h"
21
+ puts <<~HELP
22
+ Usage: exa-ai webset-search-get <webset_id> <search_id> [OPTIONS]
23
+
24
+ Get details of a webset search
25
+
26
+ Arguments:
27
+ webset_id ID of the webset (required)
28
+ search_id ID of the search (required)
29
+
30
+ Options:
31
+ --api-key KEY Exa API key (or set EXA_API_KEY env var)
32
+ --output-format FMT Output format: json, pretty, or text (default: json)
33
+ --help, -h Show this help message
34
+
35
+ Examples:
36
+ exa-ai webset-search-get ws_123 search_456
37
+ exa-ai webset-search-get ws_123 search_456 --output-format pretty
38
+ exa-ai webset-search-get ws_123 search_456 --output-format text
39
+ HELP
40
+ exit 0
41
+ else
42
+ # First positional argument is webset_id, second is search_id
43
+ if webset_id.nil? && !arg.start_with?("-")
44
+ webset_id = arg
45
+ elsif search_id.nil? && !arg.start_with?("-")
46
+ search_id = arg
47
+ else
48
+ $stderr.puts "Unknown option: #{arg}"
49
+ exit 1
50
+ end
51
+ end
52
+ end
53
+
54
+ # Validate
55
+ if webset_id.nil?
56
+ $stderr.puts "Error: webset_id argument is required"
57
+ $stderr.puts "Usage: exa-ai webset-search-get <webset_id> <search_id> [OPTIONS]"
58
+ $stderr.puts "Try 'exa-ai webset-search-get --help' for more information"
59
+ exit 1
60
+ end
61
+
62
+ if search_id.nil?
63
+ $stderr.puts "Error: search_id argument is required"
64
+ $stderr.puts "Usage: exa-ai webset-search-get <webset_id> <search_id> [OPTIONS]"
65
+ $stderr.puts "Try 'exa-ai webset-search-get --help' for more information"
66
+ exit 1
67
+ end
68
+
69
+ begin
70
+ # Resolve API key and format
71
+ api_key = Exa::CLI::Base.resolve_api_key(api_key)
72
+ output_format = Exa::CLI::Base.resolve_output_format(output_format)
73
+
74
+ # Build client
75
+ client = Exa::CLI::Base.build_client(api_key)
76
+
77
+ # Get search
78
+ search = client.get_webset_search(webset_id: webset_id, id: search_id)
79
+
80
+ # Format and output
81
+ output = Exa::CLI::Formatters::WebsetSearchFormatter.format(search, output_format)
82
+ puts output
83
+ $stdout.flush
84
+
85
+ rescue Exa::NotFound => e
86
+ $stderr.puts "Search not found: #{e.message}"
87
+ exit 1
88
+ rescue Exa::Unauthorized => e
89
+ $stderr.puts "Authentication error: #{e.message}"
90
+ $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
91
+ exit 1
92
+ rescue Exa::ClientError => e
93
+ $stderr.puts "Client error: #{e.message}"
94
+ exit 1
95
+ rescue Exa::ServerError => e
96
+ $stderr.puts "Server error: #{e.message}"
97
+ exit 1
98
+ rescue Exa::Error => e
99
+ $stderr.puts "Error: #{e.message}"
100
+ exit 1
101
+ rescue StandardError => e
102
+ $stderr.puts "Unexpected error: #{e.message}"
103
+ $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
104
+ exit 1
105
+ end
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "exa-ai"
5
+ require "json"
6
+
7
+ # Parse arguments
8
+ webset_id = nil
9
+ title = nil
10
+ metadata = nil
11
+ api_key = nil
12
+ output_format = "json"
13
+
14
+ args = ARGV.dup
15
+ while args.any?
16
+ arg = args.shift
17
+ case arg
18
+ when "--title"
19
+ title = Exa::CLI::Base.text_value("--title", args.shift)
20
+ when "--metadata"
21
+ metadata = Exa::CLI::Base.parse_json_or_file("--metadata", args.shift, symbolize_names: true)
22
+ when "--api-key"
23
+ api_key = Exa::CLI::Base.text_value("--api-key", args.shift)
24
+ when "--output-format"
25
+ output_format = Exa::CLI::Base.text_value("--output-format", args.shift)
26
+ when "--help", "-h"
27
+ puts <<~HELP
28
+ Usage: exa-ai webset-update <webset_id> [OPTIONS]
29
+
30
+ Update a webset's title and/or metadata
31
+
32
+ Arguments:
33
+ webset_id ID of the webset to update (required)
34
+
35
+ Options:
36
+ --title TITLE Title for the webset
37
+ --metadata JSON Custom metadata to update (supports @file.json)
38
+ --api-key KEY Exa API key (or set EXA_API_KEY env var)
39
+ --output-format FMT Output format: json, pretty, or text (default: json)
40
+ --help, -h Show this help message
41
+
42
+ Examples:
43
+ exa-ai webset-update ws_abc123 --metadata '{"project":"Q1-2025"}'
44
+ exa-ai webset-update ws_abc123 --title "Q1 Research"
45
+ exa-ai webset-update ws_abc123 --title "Q1 Research" --metadata '{"project":"Q1-2025"}'
46
+ exa-ai webset-update ws_abc123 --metadata @metadata.json
47
+ exa-ai webset-update ws_abc123 --metadata '{"priority":"high"}' --output-format pretty
48
+ HELP
49
+ exit 0
50
+ else
51
+ # First positional argument is webset_id
52
+ if webset_id.nil? && !arg.start_with?("-")
53
+ webset_id = arg
54
+ else
55
+ $stderr.puts "Unknown option: #{arg}"
56
+ exit 1
57
+ end
58
+ end
59
+ end
60
+
61
+ # Validate required arguments
62
+ if webset_id.nil?
63
+ $stderr.puts "Error: webset_id argument is required"
64
+ $stderr.puts "Usage: exa-ai webset-update <webset_id> [OPTIONS]"
65
+ $stderr.puts "Try 'exa-ai webset-update --help' for more information"
66
+ exit 1
67
+ end
68
+
69
+ begin
70
+ # Resolve API key and format
71
+ api_key = Exa::CLI::Base.resolve_api_key(api_key)
72
+ output_format = Exa::CLI::Base.resolve_output_format(output_format)
73
+
74
+ # Build client
75
+ client = Exa::CLI::Base.build_client(api_key)
76
+
77
+ # Update webset
78
+ update_params = {}
79
+ update_params[:title] = title if title
80
+ update_params[:metadata] = metadata if metadata
81
+ webset = client.update_webset(webset_id, **update_params)
82
+
83
+ # Format and output
84
+ output = Exa::CLI::Formatters::WebsetFormatter.format(webset, output_format)
85
+ puts output
86
+ $stdout.flush
87
+
88
+ rescue Exa::NotFound => e
89
+ $stderr.puts "Webset not found: #{e.message}"
90
+ exit 1
91
+ rescue Exa::ConfigurationError => e
92
+ $stderr.puts "Configuration error: #{e.message}"
93
+ exit 1
94
+ rescue Exa::Unauthorized => e
95
+ $stderr.puts "Authentication error: #{e.message}"
96
+ $stderr.puts "Check your API key (set EXA_API_KEY or use --api-key)"
97
+ exit 1
98
+ rescue Exa::ClientError => e
99
+ $stderr.puts "Client error: #{e.message}"
100
+ exit 1
101
+ rescue Exa::ServerError => e
102
+ $stderr.puts "Server error: #{e.message}"
103
+ $stderr.puts "The Exa API may be experiencing issues. Please try again later."
104
+ exit 1
105
+ rescue Exa::Error => e
106
+ $stderr.puts "Error: #{e.message}"
107
+ exit 1
108
+ rescue ArgumentError => e
109
+ # update_webset refuses bad params, such as non-String metadata values.
110
+ $stderr.puts "Error: #{e.message}"
111
+ exit 1
112
+ rescue StandardError => e
113
+ $stderr.puts "Unexpected error: #{e.message}"
114
+ $stderr.puts e.backtrace.first(5) if ENV["DEBUG"]
115
+ exit 1
116
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Exa
4
+ module CLI
5
+ module Formatters
6
+ class EnrichmentFormatter
7
+ def self.format(enrichment, output_format)
8
+ case output_format
9
+ when "json"
10
+ JSON.generate(enrichment.to_h)
11
+ when "pretty"
12
+ format_as_pretty(enrichment)
13
+ when "text"
14
+ format_as_text(enrichment)
15
+ when "toon"
16
+ Exa::CLI::Base.encode_as_toon(enrichment.to_h)
17
+ else
18
+ raise ArgumentError, "Unknown output format: #{output_format}"
19
+ end
20
+ end
21
+
22
+ def self.format_collection(collection, output_format)
23
+ case output_format
24
+ when "json"
25
+ JSON.generate(collection.to_h)
26
+ when "pretty"
27
+ format_collection_as_pretty(collection)
28
+ when "text"
29
+ format_collection_as_text(collection)
30
+ when "toon"
31
+ Exa::CLI::Base.encode_as_toon(collection.to_h)
32
+ else
33
+ raise ArgumentError, "Unknown output format: #{output_format}"
34
+ end
35
+ end
36
+
37
+ def self.format_as_pretty(enrichment)
38
+ lines = []
39
+ lines << "Enrichment ID: #{enrichment.id}"
40
+ lines << "Webset ID: #{enrichment.webset_id}" if enrichment.webset_id
41
+ lines << "Status: #{enrichment.status}"
42
+ lines << "Title: #{enrichment.title}" if enrichment.title
43
+ lines << "Description: #{enrichment.description}" if enrichment.description
44
+ lines << "Format: #{enrichment.format}" if enrichment.format
45
+
46
+ if enrichment.options && !enrichment.options.empty?
47
+ lines << ""
48
+ lines << "Options (#{enrichment.options.length}):"
49
+ enrichment.options.each do |option|
50
+ lines << " • #{option['label']}" if option['label']
51
+ end
52
+ end
53
+
54
+ lines << ""
55
+ lines << "Created: #{enrichment.created_at}" if enrichment.created_at
56
+ lines << "Updated: #{enrichment.updated_at}" if enrichment.updated_at
57
+
58
+ lines.join("\n")
59
+ end
60
+ private_class_method :format_as_pretty
61
+
62
+ def self.format_as_text(enrichment)
63
+ lines = []
64
+ lines << "Enrichment: #{enrichment.id}"
65
+ lines << "Webset: #{enrichment.webset_id}" if enrichment.webset_id
66
+ lines << "Status: #{enrichment.status}"
67
+ lines << "Title: #{enrichment.title}" if enrichment.title
68
+ lines << "Description: #{enrichment.description}" if enrichment.description
69
+ lines << "Format: #{enrichment.format}" if enrichment.format
70
+
71
+ if enrichment.options && !enrichment.options.empty?
72
+ lines << "\nOptions:"
73
+ enrichment.options.each do |option|
74
+ lines << " - #{option['label']}" if option['label']
75
+ end
76
+ end
77
+
78
+ lines << "Created: #{enrichment.created_at}" if enrichment.created_at
79
+ lines << "Updated: #{enrichment.updated_at}" if enrichment.updated_at
80
+
81
+ lines.join("\n")
82
+ end
83
+ private_class_method :format_as_text
84
+
85
+ def self.format_collection_as_pretty(collection)
86
+ lines = []
87
+ lines << "Enrichments (#{collection.data.length} items)"
88
+ lines << ""
89
+
90
+ collection.data.each_with_index do |enr, idx|
91
+ lines << "" if idx > 0 # Blank line between enrichments
92
+
93
+ lines << "Enrichment ID: #{enr['id']}"
94
+ lines << "Webset ID: #{enr['websetId']}" if enr['websetId']
95
+ lines << "Status: #{enr['status']}"
96
+ lines << "Title: #{enr['title']}" if enr['title']
97
+ lines << "Description: #{enr['description']}" if enr['description']
98
+ lines << "Format: #{enr['format']}" if enr['format']
99
+ lines << "Created: #{enr['createdAt']}" if enr['createdAt']
100
+ lines << "Updated: #{enr['updatedAt']}" if enr['updatedAt']
101
+ end
102
+
103
+ if collection.has_more
104
+ lines << ""
105
+ lines << "Next Cursor: #{collection.next_cursor}"
106
+ end
107
+
108
+ lines.join("\n")
109
+ end
110
+ private_class_method :format_collection_as_pretty
111
+
112
+ def self.format_collection_as_text(collection)
113
+ lines = ["Enrichments (#{collection.data.length} items):"]
114
+ collection.data.each do |enr|
115
+ lines << "\n #{enr['id']}"
116
+ lines << " Status: #{enr['status']}"
117
+ lines << " Title: #{enr['title']}" if enr['title']
118
+ end
119
+ lines.join("\n")
120
+ end
121
+ private_class_method :format_collection_as_text
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Exa
4
+ module CLI
5
+ module Formatters
6
+ class ImportFormatter
7
+ def self.format(import, output_format)
8
+ case output_format
9
+ when "json"
10
+ JSON.generate(import.to_h)
11
+ when "pretty"
12
+ format_as_pretty(import)
13
+ when "text"
14
+ format_as_text(import)
15
+ when "toon"
16
+ Exa::CLI::Base.encode_as_toon(import.to_h)
17
+ else
18
+ raise ArgumentError, "Unknown output format: #{output_format}"
19
+ end
20
+ end
21
+
22
+ def self.format_collection(collection, output_format)
23
+ case output_format
24
+ when "json"
25
+ JSON.generate(collection.to_h)
26
+ when "pretty"
27
+ format_collection_as_pretty(collection)
28
+ when "text"
29
+ format_collection_as_text(collection)
30
+ when "toon"
31
+ Exa::CLI::Base.encode_as_toon(collection.to_h)
32
+ else
33
+ raise ArgumentError, "Unknown output format: #{output_format}"
34
+ end
35
+ end
36
+
37
+ def self.format_as_pretty(import)
38
+ lines = []
39
+ lines << "Import ID: #{import.id}"
40
+ lines << "Status: #{import.status}"
41
+ lines << "Title: #{import.title}" if import.title
42
+ lines << "Format: #{import.format}" if import.format
43
+
44
+ if import.entity
45
+ entity_type = import.entity['type'] || import.entity[:type]
46
+ lines << "Entity Type: #{entity_type}" if entity_type
47
+ end
48
+
49
+ lines << "Count: #{import.count}" if import.count
50
+
51
+ if import.failed?
52
+ lines << ""
53
+ lines << "Failure:"
54
+ lines << " Reason: #{import.failed_reason}" if import.failed_reason
55
+ lines << " Message: #{import.failed_message}" if import.failed_message
56
+ lines << " Failed At: #{import.failed_at}" if import.failed_at
57
+ end
58
+
59
+ if import.upload_url
60
+ lines << ""
61
+ lines << "Upload:"
62
+ lines << " URL: #{import.upload_url}"
63
+ lines << " Valid Until: #{import.upload_valid_until}" if import.upload_valid_until
64
+ end
65
+
66
+ lines << ""
67
+ lines << "Created: #{import.created_at}" if import.created_at
68
+ lines << "Updated: #{import.updated_at}" if import.updated_at
69
+
70
+ lines.join("\n")
71
+ end
72
+ private_class_method :format_as_pretty
73
+
74
+ def self.format_as_text(import)
75
+ lines = []
76
+ lines << "Import: #{import.id}"
77
+ lines << "Status: #{import.status}"
78
+ lines << "Title: #{import.title}" if import.title
79
+ lines << "Format: #{import.format}" if import.format
80
+
81
+ if import.entity
82
+ entity_type = import.entity['type'] || import.entity[:type]
83
+ lines << "Entity Type: #{entity_type}" if entity_type
84
+ end
85
+
86
+ lines << "Count: #{import.count}" if import.count
87
+
88
+ if import.failed?
89
+ lines << "\nFailure Details:"
90
+ lines << " Reason: #{import.failed_reason}" if import.failed_reason
91
+ lines << " Message: #{import.failed_message}" if import.failed_message
92
+ lines << " Failed At: #{import.failed_at}" if import.failed_at
93
+ end
94
+
95
+ if import.upload_url
96
+ lines << "\nUpload:"
97
+ lines << " URL: #{import.upload_url}"
98
+ lines << " Valid Until: #{import.upload_valid_until}" if import.upload_valid_until
99
+ end
100
+
101
+ lines << "\nCreated: #{import.created_at}" if import.created_at
102
+ lines << "Updated: #{import.updated_at}" if import.updated_at
103
+
104
+ lines.join("\n")
105
+ end
106
+ private_class_method :format_as_text
107
+
108
+ def self.format_collection_as_pretty(collection)
109
+ lines = []
110
+ lines << "Imports (#{collection.data.length} items)"
111
+ lines << ""
112
+
113
+ collection.data.each_with_index do |imp, idx|
114
+ lines << "" if idx > 0 # Blank line between imports
115
+
116
+ lines << "Import ID: #{imp.id}"
117
+ lines << "Status: #{imp.status}"
118
+ lines << "Title: #{imp.title}" if imp.title
119
+ lines << "Format: #{imp.format}" if imp.format
120
+ lines << "Entity Type: #{imp.entity['type']}" if imp.entity && imp.entity['type']
121
+ lines << "Count: #{imp.count}" if imp.count
122
+ lines << "Created: #{imp.created_at}" if imp.created_at
123
+ lines << "Updated: #{imp.updated_at}" if imp.updated_at
124
+
125
+ if imp.status == 'failed'
126
+ lines << "Failed Reason: #{imp.failed_reason}" if imp.failed_reason
127
+ end
128
+ end
129
+
130
+ if collection.has_more
131
+ lines << ""
132
+ lines << "Next Cursor: #{collection.next_cursor}"
133
+ end
134
+
135
+ lines.join("\n")
136
+ end
137
+ private_class_method :format_collection_as_pretty
138
+
139
+ def self.format_collection_as_text(collection)
140
+ lines = ["Imports (#{collection.data.length} items):"]
141
+ collection.data.each do |imp|
142
+ lines << "\n #{imp.id}"
143
+ lines << " Status: #{imp.status}"
144
+ lines << " Title: #{imp.title}" if imp.title
145
+ lines << " Count: #{imp.count}" if imp.count
146
+ end
147
+ lines.join("\n")
148
+ end
149
+ private_class_method :format_collection_as_text
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Exa
4
+ module CLI
5
+ module Formatters
6
+ class MonitorFormatter
7
+ def self.format(monitor, output_format)
8
+ case output_format
9
+ when "json"
10
+ JSON.generate(monitor.to_h)
11
+ when "pretty"
12
+ format_as_pretty(monitor)
13
+ when "text"
14
+ format_as_text(monitor)
15
+ when "toon"
16
+ Exa::CLI::Base.encode_as_toon(monitor.to_h)
17
+ else
18
+ raise ArgumentError, "Unknown output format: #{output_format}"
19
+ end
20
+ end
21
+
22
+ def self.format_collection(collection, output_format)
23
+ case output_format
24
+ when "json"
25
+ JSON.generate(collection.to_h)
26
+ when "pretty"
27
+ format_collection_as_pretty(collection)
28
+ when "text"
29
+ format_collection_as_text(collection)
30
+ when "toon"
31
+ Exa::CLI::Base.encode_as_toon(collection.to_h)
32
+ else
33
+ raise ArgumentError, "Unknown output format: #{output_format}"
34
+ end
35
+ end
36
+
37
+ def self.format_as_pretty(monitor)
38
+ lines = []
39
+ lines << "Monitor ID: #{monitor.id}"
40
+ lines << "Webset ID: #{monitor.webset_id}" if monitor.webset_id
41
+ lines << "Status: #{monitor.status}"
42
+
43
+ if monitor.cadence
44
+ lines << ""
45
+ lines << "Cadence:"
46
+ lines << " Cron: #{monitor.cadence['cron']}" if monitor.cadence['cron']
47
+ lines << " Timezone: #{monitor.cadence['timezone']}" if monitor.cadence['timezone']
48
+ end
49
+
50
+ if monitor.behavior
51
+ lines << ""
52
+ lines << "Behavior:"
53
+ lines << " Type: #{monitor.behavior['type']}" if monitor.behavior['type']
54
+ lines << " Query: #{monitor.behavior.dig('config', 'query')}" if monitor.behavior.dig('config', 'query')
55
+ lines << " Count: #{monitor.behavior.dig('config', 'count')}" if monitor.behavior.dig('config', 'count')
56
+ end
57
+
58
+ lines << ""
59
+ lines << "Created: #{monitor.created_at}" if monitor.created_at
60
+ lines << "Updated: #{monitor.updated_at}" if monitor.updated_at
61
+
62
+ lines.join("\n")
63
+ end
64
+ private_class_method :format_as_pretty
65
+
66
+ def self.format_as_text(monitor)
67
+ lines = []
68
+ lines << "Monitor: #{monitor.id}"
69
+ lines << "Webset: #{monitor.webset_id}" if monitor.webset_id
70
+ lines << "Status: #{monitor.status}"
71
+
72
+ if monitor.cadence
73
+ lines << "\nCadence:"
74
+ lines << " Cron: #{monitor.cadence['cron']}" if monitor.cadence['cron']
75
+ lines << " Timezone: #{monitor.cadence['timezone']}" if monitor.cadence['timezone']
76
+ end
77
+
78
+ if monitor.behavior
79
+ lines << "\nBehavior:"
80
+ lines << " Type: #{monitor.behavior['type']}" if monitor.behavior['type']
81
+ lines << " Query: #{monitor.behavior.dig('config', 'query')}" if monitor.behavior.dig('config', 'query')
82
+ lines << " Count: #{monitor.behavior.dig('config', 'count')}" if monitor.behavior.dig('config', 'count')
83
+ end
84
+
85
+ lines << "\nCreated: #{monitor.created_at}" if monitor.created_at
86
+ lines << "Updated: #{monitor.updated_at}" if monitor.updated_at
87
+
88
+ lines.join("\n")
89
+ end
90
+ private_class_method :format_as_text
91
+
92
+ def self.format_collection_as_pretty(collection)
93
+ lines = []
94
+ lines << "Monitors (#{collection.data.length} items)"
95
+ lines << ""
96
+
97
+ collection.data.each_with_index do |mon, idx|
98
+ lines << "" if idx > 0 # Blank line between monitors
99
+
100
+ lines << "Monitor ID: #{mon.id}"
101
+ lines << "Webset ID: #{mon.webset_id}" if mon.webset_id
102
+ lines << "Status: #{mon.status}"
103
+
104
+ if mon.cadence
105
+ lines << "Cron: #{mon.cadence['cron']}" if mon.cadence['cron']
106
+ lines << "Timezone: #{mon.cadence['timezone']}" if mon.cadence['timezone']
107
+ end
108
+
109
+ if mon.behavior
110
+ lines << "Query: #{mon.behavior.dig('config', 'query')}" if mon.behavior.dig('config', 'query')
111
+ end
112
+
113
+ lines << "Created: #{mon.created_at}" if mon.created_at
114
+ lines << "Updated: #{mon.updated_at}" if mon.updated_at
115
+ end
116
+
117
+ if collection.has_more
118
+ lines << ""
119
+ lines << "Next Cursor: #{collection.next_cursor}"
120
+ end
121
+
122
+ lines.join("\n")
123
+ end
124
+ private_class_method :format_collection_as_pretty
125
+
126
+ def self.format_collection_as_text(collection)
127
+ lines = ["Monitors (#{collection.data.length} items):"]
128
+ collection.data.each do |mon|
129
+ lines << "\n #{mon.id}"
130
+ lines << " Status: #{mon.status}"
131
+ lines << " Webset: #{mon.webset_id}" if mon.webset_id
132
+ end
133
+
134
+ if collection.has_more
135
+ lines << "\nMore available (cursor: #{collection.next_cursor})"
136
+ end
137
+
138
+ lines.join("\n")
139
+ end
140
+ private_class_method :format_collection_as_text
141
+ end
142
+ end
143
+ end
144
+ end