faraday_csv 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/faraday_csv.gemspec +1 -1
- data/lib/faraday/response/csv.rb +22 -8
- data/lib/faraday_csv/version.rb +1 -1
- data/spec/faraday/response/csv_spec.rb +24 -8
- data/spec/spec_helper.rb +1 -0
- metadata +5 -5
data/faraday_csv.gemspec
CHANGED
data/lib/faraday/response/csv.rb
CHANGED
@@ -1,18 +1,32 @@
|
|
1
|
-
require '
|
1
|
+
require 'faraday_middleware/response_middleware'
|
2
2
|
|
3
3
|
module Faraday
|
4
|
-
class Response::CSV <
|
5
|
-
dependency
|
4
|
+
class Response::CSV < FaradayMiddleware::ResponseMiddleware
|
5
|
+
dependency do
|
6
|
+
require 'csv' unless defined?(::CSV)
|
7
|
+
end
|
8
|
+
|
9
|
+
define_parser do |body, opts|
|
10
|
+
::CSV.parse(body, opts) unless body.strip.empty?
|
11
|
+
end
|
6
12
|
|
7
13
|
def initialize(app = nil, options = {})
|
8
|
-
super(app)
|
9
|
-
@
|
14
|
+
super(app, options)
|
15
|
+
@parser_options = options[:parser_options] || {}
|
10
16
|
end
|
11
17
|
|
18
|
+
# Parse the response body.
|
12
19
|
def parse(body)
|
13
|
-
|
14
|
-
|
15
|
-
|
20
|
+
if self.class.parser
|
21
|
+
begin
|
22
|
+
self.class.parser.call(body, @parser_options)
|
23
|
+
rescue StandardError, SyntaxError => err
|
24
|
+
raise err if err.is_a? SyntaxError and err.class.name != 'Psych::SyntaxError'
|
25
|
+
raise Faraday::Error::ParsingError, err
|
26
|
+
end
|
27
|
+
else
|
28
|
+
body
|
29
|
+
end
|
16
30
|
end
|
17
31
|
end
|
18
32
|
end
|
data/lib/faraday_csv/version.rb
CHANGED
@@ -1,27 +1,43 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Faraday::Response::CSV do
|
4
|
-
let(:
|
4
|
+
let(:options) { Hash.new }
|
5
|
+
let(:headers) { Hash.new }
|
6
|
+
let(:app) do
|
7
|
+
lambda { |env| Faraday::Response.new(env) }
|
8
|
+
end
|
9
|
+
let(:middleware) { Faraday::Response::CSV.new(app, options) }
|
5
10
|
let(:body) { "integer,string\n12,foobar" }
|
6
11
|
|
12
|
+
def process(response_body, content_type = nil, opts = {})
|
13
|
+
env = {
|
14
|
+
:body => response_body,
|
15
|
+
:request => opts,
|
16
|
+
:response_headers => Faraday::Utils::Headers.new(headers)
|
17
|
+
}
|
18
|
+
env[:response_headers]['content-type'] = content_type if content_type
|
19
|
+
middleware.call(env)
|
20
|
+
end
|
21
|
+
|
7
22
|
it "parses valid csv syntax" do
|
8
|
-
|
9
|
-
expect(parsed).to be_an Array
|
23
|
+
expect(process(body).body).to be_an Array
|
10
24
|
end
|
11
25
|
|
12
26
|
it 'raises Faraday::Error::ParsingError on invalid syntax' do
|
27
|
+
allow(::CSV).to receive(:parse).and_raise(StandardError)
|
28
|
+
|
13
29
|
expect{
|
14
|
-
|
30
|
+
process('foo')
|
15
31
|
}.to raise_error Faraday::Error::ParsingError
|
16
32
|
end
|
17
33
|
|
18
34
|
context 'with options' do
|
19
|
-
let(:options)
|
20
|
-
|
35
|
+
let(:options) do
|
36
|
+
{ :parser_options => { :headers => true } }
|
37
|
+
end
|
21
38
|
|
22
39
|
it "parses valid csv syntax" do
|
23
|
-
|
24
|
-
expect(parsed).to be_a CSV::Table
|
40
|
+
expect(process(body).body).to be_a CSV::Table
|
25
41
|
end
|
26
42
|
end
|
27
43
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: faraday_middleware
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
segments:
|
61
61
|
- 0
|
62
|
-
hash:
|
62
|
+
hash: 3263273483119408602
|
63
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
version: '0'
|
69
69
|
segments:
|
70
70
|
- 0
|
71
|
-
hash:
|
71
|
+
hash: 3263273483119408602
|
72
72
|
requirements: []
|
73
73
|
rubyforge_project:
|
74
74
|
rubygems_version: 1.8.23
|