api_me 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6edbf66319db35e695cd3b4b686e3b5688a2c2fe56d108eb954976732bc11bb5
4
- data.tar.gz: 6063c1ca77185b64178360c103f6c3511b6fe9652575ee00da2a99d97d626dfc
3
+ metadata.gz: 9646af7af8c9ba9da48b8655d133eaea222e669a916435eb86670c2ab19aa848
4
+ data.tar.gz: c580cea45bc61f08bb8cadb103db8090b7d87f7e560d7859d990dfa256735f6a
5
5
  SHA512:
6
- metadata.gz: d08b61cc42bcaa27102961bea0b54c651e1fa18adbc7034d69531147076e1acdcd5fd09204c83741880b8b527a6269d0ee2634e339159910dd043a7b8bb664c4
7
- data.tar.gz: 7c610c2d85ca5009c265c1456e9c3e21d653a762e933ce8f00d6f245dd2efa57fa480c9feee608a9c52a4ff2299bb3f4ed6fd60b471989fb9071e04ffca37589
6
+ metadata.gz: 8e5bf1017206e3569e3998b419e4cd310ba80ab6787f2d71e317bc3880e99a1f32e29d3eb4d4355dc357a9c1429a1aa2f35e8a7fc6003da0faa65f0726e59189
7
+ data.tar.gz: 482033012c3342badf0c2b1fbeb099a04b32d217ec50af2d4940bc1bb33bd96c6e26bc5d6ef7ddebeefeac7314415e022e998f40196d97c2656b7e899b33c8f9
@@ -9,6 +9,7 @@ require 'api_me/version'
9
9
  require 'api_me/base_filter'
10
10
  require 'api_me/sorting'
11
11
  require 'api_me/pagination'
12
+ require 'api_me/csv_stream_writer'
12
13
 
13
14
  module ApiMe
14
15
  extend ActiveSupport::Concern
@@ -76,15 +77,39 @@ module ApiMe
76
77
  @sorted_scope = sort_scope(@filter_scope)
77
78
  @pagination_object = paginate_scope(@sorted_scope, page_params)
78
79
 
79
- render(
80
- json: @pagination_object.results,
81
- root: collection_root_key,
82
- each_serializer: serializer_klass,
83
- meta: {
84
- page: @pagination_object.page_meta,
85
- sort: sorting_meta(@filter_scope)
86
- }
87
- )
80
+ respond_to do |format|
81
+ format.csv do
82
+ @csv_includes_scope = csv_includes_scope(@sorted_scope)
83
+ response.headers['Content-Type'] = 'text/csv'
84
+ response.headers['Cache-Control'] = 'no-cache'
85
+ # Hack to work around https://github.com/rack/rack/issues/1619
86
+ response.headers['Last-Modified'] = Time.current.httpdate
87
+ # Disable gzip in nginx
88
+ response.headers['X-Accel-Buffering'] = 'no'
89
+ response.headers['Content-Disposition'] = "attachment; filename=\"#{csv_filename}\""
90
+
91
+ ::ApiMe::CsvStreamWriter.generate(response.stream) do |csv|
92
+ # headers
93
+ csv << csv_headers
94
+ @csv_includes_scope.find_each do |object|
95
+ csv << object.try('to_csv')
96
+ end
97
+ end
98
+ ensure
99
+ response.stream.close
100
+ end
101
+ format.all do
102
+ render(
103
+ json: @pagination_object.results,
104
+ root: collection_root_key,
105
+ each_serializer: serializer_klass,
106
+ meta: {
107
+ page: @pagination_object.page_meta,
108
+ sort: sorting_meta(@filter_scope)
109
+ }
110
+ )
111
+ end
112
+ end
88
113
  end
89
114
 
90
115
  def show
@@ -129,6 +154,18 @@ module ApiMe
129
154
 
130
155
  protected
131
156
 
157
+ def csv_filename
158
+ "#{model_klass.name.dasherize}-#{Time.zone.now.to_date.to_s(:default)}.csv"
159
+ end
160
+
161
+ def csv_headers
162
+ model_klass.respond_to?('csv_headers') ? model_klass.csv_headers : []
163
+ end
164
+
165
+ def csv_includes_scope(scope)
166
+ scope
167
+ end
168
+
132
169
  def singular_root_key
133
170
  model_klass.name.singularize.underscore
134
171
  end
@@ -0,0 +1,32 @@
1
+ require 'csv'
2
+ # frozen_string_literal: true
3
+
4
+ module ApiMe
5
+ class CsvStreamWriter
6
+ # @!attribute [r] stream
7
+ # @return [IO]
8
+ attr_reader :stream
9
+
10
+ # Provides a similar interface to CSV.generate but compatible with an IO stream
11
+ # @example
12
+ # CsvStreamWriter.generate(stream) do |csv|
13
+ # csv << ['foo', 'bar']
14
+ # end
15
+ #
16
+ # @param [IO]
17
+ # @yield [CsvStreamWriter] csv
18
+ def self.generate(stream)
19
+ yield new(stream)
20
+ end
21
+
22
+ # @param [IO]
23
+ def initialize(stream)
24
+ @stream = stream
25
+ end
26
+
27
+ # @param [Array<String>]
28
+ def <<(row)
29
+ stream.write CSV.generate_line(row)
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ApiMe
4
- VERSION = '0.13.0'
4
+ VERSION = '0.14.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Clopton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-08-31 00:00:00.000000000 Z
12
+ date: 2020-12-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -162,6 +162,7 @@ files:
162
162
  - Rakefile
163
163
  - lib/api_me.rb
164
164
  - lib/api_me/base_filter.rb
165
+ - lib/api_me/csv_stream_writer.rb
165
166
  - lib/api_me/engine.rb
166
167
  - lib/api_me/model.rb
167
168
  - lib/api_me/pagination.rb