Module: SentimentInsights::Export::Exportable

Included in:
Result
Defined in:
lib/sentiment_insights/export/exportable.rb

Overview

Module to be extended on result hashes to add export functionality

Instance Method Summary collapse

Instance Method Details

#export(filename_or_format = nil, options = {}) ⇒ String

Export to specified format (auto-detects from filename extension)

Parameters:

  • filename_or_format (String, Symbol) (defaults to: nil)

    Filename with extension or format symbol (:csv, :excel)

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (String)

    Path to exported file



59
60
61
62
63
# File 'lib/sentiment_insights/export/exportable.rb', line 59

def export(filename_or_format = nil, options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.export(filename_or_format)
end

#export_all(base_filename = nil) ⇒ Object

Export to both CSV and Excel formats



91
92
93
# File 'lib/sentiment_insights/export/exportable.rb', line 91

def export_all(base_filename = nil)
  exporter.export_all(base_filename)
end

#export_by_segment(segment_type, segment_values, format = :csv, filename = nil) ⇒ Object

Export responses from specific segments



86
87
88
# File 'lib/sentiment_insights/export/exportable.rb', line 86

def export_by_segment(segment_type, segment_values, format = :csv, filename = nil)
  exporter.by_segment(segment_type, segment_values, format, filename)
end

#export_negative(format = :csv, filename = nil) ⇒ Object

Export only negative responses



81
82
83
# File 'lib/sentiment_insights/export/exportable.rb', line 81

def export_negative(format = :csv, filename = nil)
  exporter.negative_only(format, filename)
end

#export_positive(format = :csv, filename = nil) ⇒ Object

Export only positive responses



76
77
78
# File 'lib/sentiment_insights/export/exportable.rb', line 76

def export_positive(format = :csv, filename = nil)
  exporter.positive_only(format, filename)
end

#exporter(options = {}) ⇒ SentimentInsights::Export::Exporter

Create an exporter instance for advanced operations

Parameters:

  • options (Hash) (defaults to: {})

    Export options

Returns:



68
69
70
71
# File 'lib/sentiment_insights/export/exportable.rb', line 68

def exporter(options = {})
  merged_options = (@export_options || {}).merge(options)
  SentimentInsights::Export::Exporter.new(self, merged_options)
end

#filter_segments(segment_filters) ⇒ Object

Filter by segment criteria



129
130
131
132
133
# File 'lib/sentiment_insights/export/exportable.rb', line 129

def filter_segments(segment_filters)
  current_filter = @export_options&.dig(:filter) || {}
  new_filter = current_filter.merge(segments: segment_filters)
  with_export_options(filter: new_filter)
end

#filter_sentiment(*sentiments) ⇒ Object

Filter by sentiment



124
125
126
# File 'lib/sentiment_insights/export/exportable.rb', line 124

def filter_sentiment(*sentiments)
  with_export_options(filter: { sentiment: sentiments.flatten })
end

#responses_onlyObject

Export only response data (no summary/segments)



109
110
111
# File 'lib/sentiment_insights/export/exportable.rb', line 109

def responses_only
  with_export_options(include_summary: false, include_segments: false)
end

#summary_onlyObject

Export only summary data (no individual responses)



104
105
106
# File 'lib/sentiment_insights/export/exportable.rb', line 104

def summary_only
  with_export_options(include_segments: false, summary_only: true)
end

#to_csv(filename = nil, options = {}) ⇒ String

Export to CSV format

Parameters:

  • filename (String, nil) (defaults to: nil)

    Optional filename. If nil, auto-generates timestamp-based name

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (String)

    Path to exported file



11
12
13
14
15
# File 'lib/sentiment_insights/export/exportable.rb', line 11

def to_csv(filename = nil, options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.to_csv(filename)
end

#to_excel(filename = nil, options = {}) ⇒ String

Export to Excel format

Parameters:

  • filename (String, nil) (defaults to: nil)

    Optional filename. If nil, auto-generates timestamp-based name

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (String)

    Path to exported file



21
22
23
24
25
# File 'lib/sentiment_insights/export/exportable.rb', line 21

def to_excel(filename = nil, options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.to_excel(filename)
end

#to_hash(options = {}) ⇒ Hash

Return as hash structure (for API responses)

Parameters:

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (Hash)

    Structured data hash



49
50
51
52
53
# File 'lib/sentiment_insights/export/exportable.rb', line 49

def to_hash(options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.to_hash
end

#to_json(filename = nil, options = {}) ⇒ String

Export to JSON format

Parameters:

  • filename (String, nil) (defaults to: nil)

    Optional filename. If nil, auto-generates timestamp-based name

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (String)

    Path to exported file



31
32
33
34
35
# File 'lib/sentiment_insights/export/exportable.rb', line 31

def to_json(filename = nil, options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.to_json(filename)
end

#to_json_string(options = {}) ⇒ String

Return JSON as string (for API responses)

Parameters:

  • options (Hash) (defaults to: {})

    Export options

Returns:

  • (String)

    JSON string



40
41
42
43
44
# File 'lib/sentiment_insights/export/exportable.rb', line 40

def to_json_string(options = {})
  merged_options = (@export_options || {}).merge(options)
  exporter = SentimentInsights::Export::Exporter.new(self, merged_options)
  exporter.to_json_string
end

#with_export_options(options) ⇒ Object

Configure export with options and return self for chaining



98
99
100
101
# File 'lib/sentiment_insights/export/exportable.rb', line 98

def with_export_options(options)
  @export_options = (@export_options || {}).merge(options)
  self
end

#with_segmentsObject

Include detailed segment analysis



114
115
116
# File 'lib/sentiment_insights/export/exportable.rb', line 114

def with_segments
  with_export_options(include_segments: true)
end

#without_segmentsObject

Exclude segment analysis



119
120
121
# File 'lib/sentiment_insights/export/exportable.rb', line 119

def without_segments
  with_export_options(include_segments: false)
end