cloudxls 2.0.0 → 2.0.1
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.
- checksums.yaml +4 -4
- data/README.md +18 -6
- data/lib/cloudxls/version.rb +1 -1
- data/lib/cloudxls.rb +82 -62
- data/test/cloudxls_test.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fab7cbcc052f7807d5d721580dedaa82d7cb64e
|
4
|
+
data.tar.gz: 9f6c7a7e75036f8d7d79ddbdebac9e03f6178b7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ce70375e14270a59099be0f95b57eb6f0bd38938362d52d2867e2ad2c7e9f84e1a1791d8d50c9de63a3b1d8de59d894e53aa6537e2a935e398198b1911e5599
|
7
|
+
data.tar.gz: 15247999df556ae0b684e40676cb46487c089f63f4ff150f665ea32521d16e289cd4f554e431fbad338dfeaba2fab4e6b75bbd23478dd1c7d74e99fa5962f333
|
data/README.md
CHANGED
@@ -52,7 +52,7 @@ Output
|
|
52
52
|
From a remote url
|
53
53
|
|
54
54
|
```ruby
|
55
|
-
data = Cloudxls.read(file_url: "http://example.org/data.xls").to_h
|
55
|
+
data = Cloudxls.read(file_url: "http://example.org/data.xls").as_json.to_h
|
56
56
|
```
|
57
57
|
|
58
58
|
Save json to a file
|
@@ -66,7 +66,7 @@ Or access the response_stream directly
|
|
66
66
|
|
67
67
|
```ruby
|
68
68
|
io = File.new("output.json", "w")
|
69
|
-
Cloudxls.read(file: File.new("/path/to/my-excel.xls")).each do |chunk|
|
69
|
+
Cloudxls.read(file: File.new("/path/to/my-excel.xls")).as_csv.each do |chunk|
|
70
70
|
io.write chunk
|
71
71
|
end
|
72
72
|
io.close
|
@@ -80,7 +80,9 @@ Write a xls file with a single sheet.
|
|
80
80
|
csv_string = "hello,world\nfoo,bar"
|
81
81
|
|
82
82
|
Cloudxls.write(csv: csv_string)
|
83
|
+
.as_xls
|
83
84
|
.save_as("/tmp/hello-world.xls")
|
85
|
+
|
84
86
|
```
|
85
87
|
|
86
88
|
Write xlsx:
|
@@ -99,7 +101,8 @@ req = Cloudxls.write(
|
|
99
101
|
offset: "B2",
|
100
102
|
sheet_name: "Data"
|
101
103
|
)
|
102
|
-
req.
|
104
|
+
xls_response = req.as_xls
|
105
|
+
xls_response.save_as("/tmp/hello-world.xls")
|
103
106
|
```
|
104
107
|
|
105
108
|
Multiple sheets:
|
@@ -108,6 +111,7 @@ Multiple sheets:
|
|
108
111
|
Cloudxls.write(csv: csv_string)
|
109
112
|
.add_data(csv: "more,data")
|
110
113
|
.add_data(csv: "more,data", sheet_name: "foobar")
|
114
|
+
.as_xls
|
111
115
|
.save_as("/tmp/hello-world.xls")
|
112
116
|
```
|
113
117
|
|
@@ -116,6 +120,7 @@ Append data to a excel file (xls or xlsx)
|
|
116
120
|
```ruby
|
117
121
|
Cloudxls.write(csv: csv_string)
|
118
122
|
.target_file(File.new("/path/to/my-file.xls"))
|
123
|
+
.as_xls
|
119
124
|
.save_as("/tmp/hello-world.xls")
|
120
125
|
```
|
121
126
|
|
@@ -126,9 +131,16 @@ Assign the result of a `#write` or `#read` call to the `response_body`.
|
|
126
131
|
|
127
132
|
```ruby
|
128
133
|
def index
|
129
|
-
|
130
|
-
|
134
|
+
csv_data = "hello,world"
|
135
|
+
|
136
|
+
headers["Content-Type"] = Mime::Type.lookup_by_extension(params[:format])
|
137
|
+
headers["Content-disposition"] = "attachment; filename=data.#{params[:format]}"
|
138
|
+
|
139
|
+
respond_to do |format|
|
131
140
|
|
132
|
-
self.response_body =
|
141
|
+
format.csv { self.response_body = csv_data }
|
142
|
+
format.xls { self.response_body = Cloudxls.write(csv: csv_data).as_xls }
|
143
|
+
format.xlsx { self.response_body = Cloudxls.write(csv: csv_data).as_xlsx }
|
144
|
+
end
|
133
145
|
end
|
134
146
|
```
|
data/lib/cloudxls/version.rb
CHANGED
data/lib/cloudxls.rb
CHANGED
@@ -8,8 +8,6 @@ class Cloudxls
|
|
8
8
|
class ApiError < Exception
|
9
9
|
end
|
10
10
|
|
11
|
-
@api_key = ENV.fetch("CLOUDXLS_API_KEY", nil)
|
12
|
-
@api_base = ENV.fetch("CLOUDXLS_API_BASE", "api.cloudxls.com")
|
13
11
|
@sandbox_base = "sandbox.cloudxls.com"
|
14
12
|
@api_version = "v2".freeze
|
15
13
|
|
@@ -22,9 +20,9 @@ class Cloudxls
|
|
22
20
|
|
23
21
|
def client_options
|
24
22
|
{
|
25
|
-
api_key: self.api_key,
|
23
|
+
api_key: self.api_key || ENV.fetch("CLOUDXLS_API_KEY", nil),
|
26
24
|
api_version: self.api_version,
|
27
|
-
api_base: self.api_base,
|
25
|
+
api_base: self.api_base || ENV.fetch("CLOUDXLS_API_BASE", "api.cloudxls.com"),
|
28
26
|
port: 443
|
29
27
|
}
|
30
28
|
end
|
@@ -35,7 +33,7 @@ class Cloudxls
|
|
35
33
|
# @return [WriteRequest] write request object
|
36
34
|
#
|
37
35
|
def write(params = nil)
|
38
|
-
WriteRequest.new(client_options).add_data(params)
|
36
|
+
WriteRequest.new(self.client_options).add_data(params)
|
39
37
|
end
|
40
38
|
|
41
39
|
# Initializes a Read request
|
@@ -44,14 +42,13 @@ class Cloudxls
|
|
44
42
|
# @return [WriteRequest] write request object
|
45
43
|
#
|
46
44
|
def read(params = nil)
|
47
|
-
ReadRequest.new(client_options).add_data(params)
|
45
|
+
ReadRequest.new(self.client_options).add_data(params)
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
51
49
|
module BaseRequest
|
52
50
|
def initialize(client_options = nil)
|
53
51
|
@post_data = []
|
54
|
-
|
55
52
|
@finished = false
|
56
53
|
@client_options = client_options || Cloudxls.client_options
|
57
54
|
end
|
@@ -90,10 +87,54 @@ class Cloudxls
|
|
90
87
|
"/#{client_options[:api_version]}/#{path}"
|
91
88
|
end
|
92
89
|
|
93
|
-
|
90
|
+
|
91
|
+
# Starts request and yields response to block
|
92
|
+
#
|
93
|
+
# @params [String] path
|
94
|
+
#
|
95
|
+
def each(&block)
|
96
|
+
raise "#{self.class.name} already executed" if @finished
|
97
|
+
|
98
|
+
start do |http|
|
99
|
+
request = Net::HTTP::Post::Multipart.new(self.path, @post_data)
|
100
|
+
request.basic_auth api_key, ""
|
101
|
+
request['User-Agent'] = "cloudxls-ruby #{VERSION}"
|
102
|
+
|
103
|
+
if block_given?
|
104
|
+
http.request(request) do |response|
|
105
|
+
if Net::HTTPSuccess === response
|
106
|
+
response.read_body(&block)
|
107
|
+
else
|
108
|
+
raise ApiError.new("#{response.code} #{response.class.name.to_s}: #{response.body}")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
else
|
112
|
+
http.request(request)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
@finished = true
|
116
|
+
self
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
module BaseResponse
|
121
|
+
# Response as string
|
94
122
|
#
|
95
|
-
|
96
|
-
|
123
|
+
# @return [String]
|
124
|
+
#
|
125
|
+
def response_body
|
126
|
+
# TODO: optimize
|
127
|
+
str = ""
|
128
|
+
each do |chunk|
|
129
|
+
str << chunk
|
130
|
+
end
|
131
|
+
str
|
132
|
+
end
|
133
|
+
|
134
|
+
# Response body. Required to make Rails send_data work out of the box.
|
135
|
+
#
|
136
|
+
def to_s
|
137
|
+
response_body
|
97
138
|
end
|
98
139
|
|
99
140
|
# Writes to IO object
|
@@ -119,32 +160,24 @@ class Cloudxls
|
|
119
160
|
write_to File.open(path, 'wb')
|
120
161
|
end
|
121
162
|
|
122
|
-
# Starts request and yields response to block
|
123
|
-
#
|
124
|
-
# @params [String] path
|
125
|
-
#
|
126
163
|
def each(&block)
|
127
|
-
|
164
|
+
@request.each(&block)
|
165
|
+
end
|
166
|
+
end
|
128
167
|
|
129
|
-
|
130
|
-
|
131
|
-
request.basic_auth api_key, ""
|
132
|
-
request['User-Agent'] = "cloudxls-ruby #{Cloudxls::VERSION}"
|
168
|
+
class ReadResponse
|
169
|
+
include BaseResponse
|
133
170
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
end
|
145
|
-
end
|
146
|
-
@finished = true
|
147
|
-
self
|
171
|
+
def initialize(req)
|
172
|
+
@request = req
|
173
|
+
end
|
174
|
+
|
175
|
+
# Response as Hash (used with json)
|
176
|
+
#
|
177
|
+
# @return [String]
|
178
|
+
#
|
179
|
+
def to_h
|
180
|
+
JSON.load(response_body)
|
148
181
|
end
|
149
182
|
end
|
150
183
|
|
@@ -173,43 +206,22 @@ class Cloudxls
|
|
173
206
|
self
|
174
207
|
end
|
175
208
|
|
176
|
-
# Response as string
|
177
|
-
#
|
178
|
-
# @return [String]
|
179
|
-
#
|
180
|
-
def response_body
|
181
|
-
# TODO: optimize
|
182
|
-
str = ""
|
183
|
-
each do |chunk|
|
184
|
-
str << chunk
|
185
|
-
end
|
186
|
-
str
|
187
|
-
end
|
188
|
-
|
189
209
|
# Set request to JSON
|
190
210
|
#
|
191
|
-
# @returns [
|
211
|
+
# @returns [ReadResponse] read response
|
192
212
|
#
|
193
213
|
def as_json
|
194
214
|
self.file_format = "json"
|
195
|
-
self
|
215
|
+
ReadResponse.new(self)
|
196
216
|
end
|
197
217
|
|
198
218
|
# Set request to CSV
|
199
219
|
#
|
200
|
-
# @returns [
|
220
|
+
# @returns [ReadResponse] returns self
|
201
221
|
#
|
202
222
|
def as_csv
|
203
223
|
self.file_format = "csv"
|
204
|
-
self
|
205
|
-
end
|
206
|
-
|
207
|
-
# Response as Hash (used with json)
|
208
|
-
#
|
209
|
-
# @return [String]
|
210
|
-
#
|
211
|
-
def to_h
|
212
|
-
JSON.load(response_body)
|
224
|
+
ReadResponse.new(self)
|
213
225
|
end
|
214
226
|
|
215
227
|
protected
|
@@ -219,6 +231,14 @@ class Cloudxls
|
|
219
231
|
end
|
220
232
|
end
|
221
233
|
|
234
|
+
class WriteResponse
|
235
|
+
include BaseResponse
|
236
|
+
|
237
|
+
def initialize(req)
|
238
|
+
@request = req
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
222
242
|
class WriteRequest
|
223
243
|
include BaseRequest
|
224
244
|
# post_data is an array of key,value arrays. Reason:
|
@@ -257,20 +277,20 @@ class Cloudxls
|
|
257
277
|
|
258
278
|
# Set request to XLS
|
259
279
|
#
|
260
|
-
# @returns [
|
280
|
+
# @returns [WriteResponse] response object without starting the request
|
261
281
|
#
|
262
282
|
def as_xls
|
263
283
|
self.file_format = "xls"
|
264
|
-
self
|
284
|
+
WriteResponse.new(self)
|
265
285
|
end
|
266
286
|
|
267
287
|
# Set request to XLSX
|
268
288
|
#
|
269
|
-
# @returns [
|
289
|
+
# @returns [WriteResponse] response object without starting the request
|
270
290
|
#
|
271
291
|
def as_xlsx
|
272
292
|
self.file_format = "xlsx"
|
273
|
-
self
|
293
|
+
WriteResponse.new(self)
|
274
294
|
end
|
275
295
|
|
276
296
|
# Sets request to XLSX
|
data/test/cloudxls_test.rb
CHANGED
@@ -3,15 +3,15 @@ require 'test_helper'
|
|
3
3
|
class CloudxlsTest < Minitest::Test
|
4
4
|
def test_defaults
|
5
5
|
Cloudxls.api_key = "test_foo"
|
6
|
-
file = Cloudxls.write(csv: "hello,world").save_as("/tmp/foo.xls")
|
6
|
+
file = Cloudxls.write(csv: "hello,world").as_xls.save_as("/tmp/foo.xls")
|
7
7
|
hash = Cloudxls.read(excel: File.new(file.path)).to_h
|
8
8
|
assert_equal ["hello", "world"], hash.first["rows"][0]
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_defaults
|
12
12
|
Cloudxls.api_key = "test_foo"
|
13
|
-
file = Cloudxls.write(csv: File.new("test/test.csv")).save_as("/tmp/foo.xls")
|
14
|
-
hash = Cloudxls.read(excel: File.new(file.path)).to_h
|
13
|
+
file = Cloudxls.write(csv: File.new("test/test.csv")).as_xls.save_as("/tmp/foo.xls")
|
14
|
+
hash = Cloudxls.read(excel: File.new(file.path)).as_json.to_h
|
15
15
|
assert_equal ["hello", "world"], hash.first["rows"][0]
|
16
16
|
end
|
17
17
|
|