Class: SentimentInsights::Export::Exporter
- Inherits:
-
Object
- Object
- SentimentInsights::Export::Exporter
- Defined in:
- lib/sentiment_insights/export/exporter.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #by_segment(segment_type, segment_values, format = :csv, filename = nil) ⇒ Object
-
#export(filename_or_format = nil, options = {}) ⇒ Object
Main export method - auto-detects format from filename.
-
#export_all(base_filename = nil) ⇒ Object
Batch export - multiple formats at once.
-
#initialize(result, options = {}) ⇒ Exporter
constructor
A new instance of Exporter.
- #negative_only(format = :csv, filename = nil) ⇒ Object
-
#positive_only(format = :csv, filename = nil) ⇒ Object
Filtered exports.
-
#to_csv(filename = nil, options = {}) ⇒ Object
Specific format methods.
- #to_csv_responses_only(filename = nil) ⇒ Object
-
#to_csv_summary_only(filename = nil) ⇒ Object
Convenience methods for common export scenarios.
- #to_excel(filename = nil, options = {}) ⇒ Object
- #to_excel_detailed(filename = nil) ⇒ Object
-
#to_hash(options = {}) ⇒ Object
Return as hash (for API responses).
- #to_json(filename = nil, options = {}) ⇒ Object
-
#to_json_string(options = {}) ⇒ Object
Return JSON as string (for API responses).
Constructor Details
#initialize(result, options = {}) ⇒ Exporter
Returns a new instance of Exporter.
10 11 12 13 |
# File 'lib/sentiment_insights/export/exporter.rb', line 10 def initialize(result, = {}) @result = result @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/sentiment_insights/export/exporter.rb', line 8 def @options end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
8 9 10 |
# File 'lib/sentiment_insights/export/exporter.rb', line 8 def result @result end |
Instance Method Details
#by_segment(segment_type, segment_values, format = :csv, filename = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/sentiment_insights/export/exporter.rb', line 120 def by_segment(segment_type, segment_values, format = :csv, filename = nil) = { filter: { segments: { segment_type.to_sym => Array(segment_values) } } } case format when :csv to_csv(filename, ) when :excel, :xlsx to_excel(filename, ) else raise ArgumentError, "Unsupported format: #{format}" end end |
#export(filename_or_format = nil, options = {}) ⇒ Object
Main export method - auto-detects format from filename
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 |
# File 'lib/sentiment_insights/export/exporter.rb', line 16 def export(filename_or_format = nil, = {}) = @options.merge() if filename_or_format.nil? # Default to CSV with auto-generated filename to_csv(nil, ) elsif filename_or_format.is_a?(Symbol) # Format specified as symbol case filename_or_format when :csv to_csv(nil, ) when :excel, :xlsx to_excel(nil, ) when :json to_json(nil, ) else raise ArgumentError, "Unsupported export format: #{filename_or_format}" end else # Filename specified - detect format from extension extension = File.extname(filename_or_format).downcase case extension when '.csv' to_csv(filename_or_format, ) when '.xlsx', '.xls' to_excel(filename_or_format, ) when '.json' to_json(filename_or_format, ) else # Default to CSV if no extension or unknown extension csv_filename = filename_or_format.include?('.') ? filename_or_format : "#{filename_or_format}.csv" to_csv(csv_filename, ) end end end |
#export_all(base_filename = nil) ⇒ Object
Batch export - multiple formats at once
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/sentiment_insights/export/exporter.rb', line 138 def export_all(base_filename = nil) base_name = base_filename || detect_base_filename files = {} files[:csv] = to_csv("#{base_name}.csv") begin files[:excel] = to_excel("#{base_name}.xlsx") rescue => e puts "Warning: Excel export failed: #{e.}" end files end |
#negative_only(format = :csv, filename = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sentiment_insights/export/exporter.rb', line 107 def negative_only(format = :csv, filename = nil) = { filter: { sentiment: [:negative] } } case format when :csv to_csv(filename, ) when :excel, :xlsx to_excel(filename, ) else raise ArgumentError, "Unsupported format: #{format}" end end |
#positive_only(format = :csv, filename = nil) ⇒ Object
Filtered exports
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sentiment_insights/export/exporter.rb', line 94 def positive_only(format = :csv, filename = nil) = { filter: { sentiment: [:positive] } } case format when :csv to_csv(filename, ) when :excel, :xlsx to_excel(filename, ) else raise ArgumentError, "Unsupported format: #{format}" end end |
#to_csv(filename = nil, options = {}) ⇒ Object
Specific format methods
53 54 55 56 |
# File 'lib/sentiment_insights/export/exporter.rb', line 53 def to_csv(filename = nil, = {}) = @options.merge() CsvExporter.new(@result, ).export(filename) end |
#to_csv_responses_only(filename = nil) ⇒ Object
85 86 87 |
# File 'lib/sentiment_insights/export/exporter.rb', line 85 def to_csv_responses_only(filename = nil) to_csv(filename, include_summary: false, include_segments: false) end |
#to_csv_summary_only(filename = nil) ⇒ Object
Convenience methods for common export scenarios
81 82 83 |
# File 'lib/sentiment_insights/export/exporter.rb', line 81 def to_csv_summary_only(filename = nil) to_csv(filename, include_segments: false) end |
#to_excel(filename = nil, options = {}) ⇒ Object
58 59 60 61 |
# File 'lib/sentiment_insights/export/exporter.rb', line 58 def to_excel(filename = nil, = {}) = @options.merge() ExcelExporter.new(@result, ).export(filename) end |
#to_excel_detailed(filename = nil) ⇒ Object
89 90 91 |
# File 'lib/sentiment_insights/export/exporter.rb', line 89 def to_excel_detailed(filename = nil) to_excel(filename, include_summary: true, include_segments: true) end |
#to_hash(options = {}) ⇒ Object
Return as hash (for API responses)
75 76 77 78 |
# File 'lib/sentiment_insights/export/exporter.rb', line 75 def to_hash( = {}) = @options.merge() JsonExporter.new(@result, ).to_hash end |
#to_json(filename = nil, options = {}) ⇒ Object
63 64 65 66 |
# File 'lib/sentiment_insights/export/exporter.rb', line 63 def to_json(filename = nil, = {}) = @options.merge() JsonExporter.new(@result, ).export(filename) end |
#to_json_string(options = {}) ⇒ Object
Return JSON as string (for API responses)
69 70 71 72 |
# File 'lib/sentiment_insights/export/exporter.rb', line 69 def to_json_string( = {}) = @options.merge() JsonExporter.new(@result, ).to_json_string end |