faraday_middleware-parse_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40b3149d1315532a7e3d05cdfe4e59c367907268
4
+ data.tar.gz: ee65d8f5f7995758c04c43f9594b492151e6a792
5
+ SHA512:
6
+ metadata.gz: 2a86fb9e5e220872391a272c66e2b9179a3fbb7e058fa4a55feb0d45e9b17f7291ba738e4161ec3785c6aae2c1165f71c8fc3c2ddd75e8494d7ae3bad11e82b9
7
+ data.tar.gz: 695e54c326045a5886a66c42495b44d0f24d73a34c62466431ea163479be59675e66531422497c305e498c799140acdafae620756de66c6c62153560866ae34d
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ben Slaughter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,10 @@
1
+ faraday_middleware-parse_csv
2
+ ============================
3
+
4
+ [![Dependency Status](https://gemnasium.com/benSlaughter/faraday_middleware-parse_csv.svg)](https://gemnasium.com/benSlaughter/faraday_middleware-parse_csv)
5
+ [![Coverage Status](https://img.shields.io/coveralls/benSlaughter/faraday_middleware-parse_csv.svg)](https://coveralls.io/r/benSlaughter/faraday_middleware-parse_csv)
6
+ [![Build Status](https://travis-ci.org/benSlaughter/faraday_middleware-parse_csv.svg)](https://travis-ci.org/benSlaughter/faraday_middleware-parse_csv)
7
+ [![Code Climate](https://codeclimate.com/github/benSlaughter/faraday_middleware-parse_csv/badges/gpa.svg)](https://codeclimate.com/github/benSlaughter/faraday_middleware-parse_csv)
8
+ [![Test Coverage](https://codeclimate.com/github/benSlaughter/faraday_middleware-parse_csv/badges/coverage.svg)](https://codeclimate.com/github/benSlaughter/faraday_middleware-parse_csv)
9
+
10
+ Faraday middleware for parsing CSV
@@ -0,0 +1,29 @@
1
+ require 'csv'
2
+ require 'faraday'
3
+ require 'faraday_middleware/response_middleware'
4
+ require 'tempfile'
5
+
6
+ module FaradayMiddleware
7
+ # The parse CSV class
8
+ class ParseCSV < FaradayMiddleware::ResponseMiddleware
9
+ def call(request)
10
+ @app.call(request).on_complete do |response|
11
+ parse_csv(response, @options[:parser])
12
+ end
13
+ end
14
+
15
+ def parse_csv(response, options)
16
+ return unless response_type(response) =~ /\bcsv$/
17
+ begin
18
+ file = Tempfile.new('csv')
19
+ file.write(response.body)
20
+ file.close
21
+ response.body = CSV.read(file.path, options)
22
+ ensure
23
+ file.unlink
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Faraday::Response.register_middleware csv: FaradayMiddleware::ParseCSV
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe FaradayMiddleware::ParseCSV do
4
+ before(:each) do
5
+ headers = { 'Content-Type' => 'text/csv;charset=UTF-8' }
6
+ body = 'one,two,three'
7
+ utf8bom = "\xEF\xBB\xBF"
8
+
9
+ @connection = Faraday.new do |builder|
10
+ builder.response :csv, parser: { encoding: 'bom|utf-8' }
11
+ builder.adapter :test do |stub|
12
+ stub.get('/normal') do
13
+ [200, headers, body]
14
+ end
15
+
16
+ stub.get('/utf8') do
17
+ [200, headers, "#{utf8bom}#{body}"]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ it 'parses the response of a csv string' do
24
+ expect(@connection.get('/normal').body).to eq [%w(one two three)]
25
+ end
26
+
27
+ it 'parses the response of a utf-8 bom string' do
28
+ expect(@connection.get('/utf8').body).to eq [%w(one two three)]
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'codeclimate-test-reporter'
5
+ require 'coveralls'
6
+ require 'rspec'
7
+
8
+ CodeClimate::TestReporter.start
9
+ Coveralls.wear!
10
+
11
+ require 'faraday_middleware/parse_csv'
12
+
13
+ RSpec.configure do |config|
14
+ config.color = true
15
+ config.formatter = :documentation
16
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_middleware-parse_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Slaughter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ description: Faraday middleware for parsing CSV using tempfile to include encoding
98
+ email: b.p.slaughter@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - LICENSE
104
+ - README.md
105
+ - lib/faraday_middleware/parse_csv.rb
106
+ - spec/faraday_middleware/parse_csv_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: http://benslaughter.github.io/faraday_middleware-parse_csv/
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.2.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Faraday middleware for parsing CSV
132
+ test_files:
133
+ - spec/faraday_middleware/parse_csv_spec.rb
134
+ - spec/spec_helper.rb
135
+ has_rdoc: