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.
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.description = 'CSV Parsing Middleware for use with Faraday'
14
14
  spec.summary = spec.description
15
15
 
16
- spec.add_dependency 'faraday'
16
+ spec.add_dependency 'faraday_middleware'
17
17
 
18
18
  spec.licenses = ['MIT']
19
19
 
@@ -1,18 +1,32 @@
1
- require 'faraday'
1
+ require 'faraday_middleware/response_middleware'
2
2
 
3
3
  module Faraday
4
- class Response::CSV < Response::Middleware
5
- dependency 'csv'
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
- @options = options
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
- CSV.parse(body, @options)
14
- rescue Object => err
15
- raise Faraday::Error::ParsingError.new(err)
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
@@ -1,3 +1,3 @@
1
1
  module FaradayCSV
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,27 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Faraday::Response::CSV do
4
- let(:csv) { Faraday::Response::CSV.new }
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
- parsed = csv.on_complete(:body => body)
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
- csv.on_complete(:body => ['foo'])
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) { { :headers => true } }
20
- let(:csv) { Faraday::Response::CSV.new(lambda { |env| env }, options) }
35
+ let(:options) do
36
+ { :parser_options => { :headers => true } }
37
+ end
21
38
 
22
39
  it "parses valid csv syntax" do
23
- parsed = csv.on_complete(:body => body)
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
 
@@ -4,6 +4,7 @@ unless ENV['CI']
4
4
  SimpleCov.start do
5
5
  add_group 'FaradayCSV', 'lib/faraday'
6
6
  add_group 'Specs', 'spec'
7
+ add_filter '.bundle'
7
8
  end
8
9
  end
9
10
 
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.3
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-09-30 00:00:00.000000000 Z
12
+ date: 2013-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: faraday
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: 1541736267580842307
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: 1541736267580842307
71
+ hash: 3263273483119408602
72
72
  requirements: []
73
73
  rubyforge_project:
74
74
  rubygems_version: 1.8.23