Class: SentimentInsights::Export::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiment_insights/export/exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @result = result
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/sentiment_insights/export/exporter.rb', line 8

def options
  @options
end

#resultObject (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)
  options = { 
    filter: { 
      segments: { segment_type.to_sym => Array(segment_values) } 
    } 
  }
  
  case format
  when :csv
    to_csv(filename, options)
  when :excel, :xlsx
    to_excel(filename, options)
  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 = {})
  merged_options = @options.merge(options)
  
  if filename_or_format.nil?
    # Default to CSV with auto-generated filename
    to_csv(nil, merged_options)
  elsif filename_or_format.is_a?(Symbol)
    # Format specified as symbol
    case filename_or_format
    when :csv
      to_csv(nil, merged_options)
    when :excel, :xlsx
      to_excel(nil, merged_options)
    when :json
      to_json(nil, merged_options)
    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, merged_options)
    when '.xlsx', '.xls'
      to_excel(filename_or_format, merged_options)
    when '.json'
      to_json(filename_or_format, merged_options)
    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, merged_options)
    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.message}"
  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)
  options = { filter: { sentiment: [:negative] } }
  
  case format
  when :csv
    to_csv(filename, options)
  when :excel, :xlsx
    to_excel(filename, options)
  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)
  options = { filter: { sentiment: [:positive] } }
  
  case format
  when :csv
    to_csv(filename, options)
  when :excel, :xlsx
    to_excel(filename, options)
  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 = {})
  merged_options = @options.merge(options)
  CsvExporter.new(@result, merged_options).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 = {})
  merged_options = @options.merge(options)
  ExcelExporter.new(@result, merged_options).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 = {})
  merged_options = @options.merge(options)
  JsonExporter.new(@result, merged_options).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 = {})
  merged_options = @options.merge(options)
  JsonExporter.new(@result, merged_options).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 = {})
  merged_options = @options.merge(options)
  JsonExporter.new(@result, merged_options).to_json_string
end