Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into error-extracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Jan 13, 2014
2 parents 31bf9c6 + e864f34 commit 9541ab6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions features/information.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Return information
"""
And it is encoded as "utf-8"
And the content type is "text/csv"
And it is stored at the url "http://example.com/example1.csv"
And it is stored at the url "http://example.com/example1.csv?query=true"

Scenario: Return encoding
Then the "encoding" should be "utf-8"
Expand All @@ -19,4 +19,4 @@ Feature: Return information
Then the "extension" should be ".csv"

Scenario: Return meta
Then the metadata content type should be "text/csv; charset=utf-8"
Then the metadata content type should be "text/csv; charset=utf-8"
12 changes: 11 additions & 1 deletion features/validation_warnings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ Feature: Validation warnings
And I ask if there are warnings
Then there should be 0 warnings

Scenario: Allow query params after extension
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "text/csv"
And it is stored at the url "http://example.com/example1.csv?query=param"
And I ask if there are warnings
Then there should be 0 warnings

Scenario: User doesn't supply encoding
Given I have a CSV with the following content:
"""
Expand All @@ -81,4 +91,4 @@ Feature: Validation warnings
And it is stored at the url "http://example.com/example1.csv" with no character set
When I ask if there are warnings
Then there should be 1 warnings
And that warning should have the type "no_encoding"
And that warning should have the type "no_encoding"
17 changes: 12 additions & 5 deletions lib/csvlint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Validator
"Unclosed quoted field" => :quoting,
}

def initialize(stream, dialect = nil)
def initialize(url, dialect = nil)
@errors = []
@warnings = []
@stream = stream
@extension = File.extname(@stream)
@url = url
@extension = parse_extension(url)
@csv_options = dialect_to_csv_options(dialect)
@csv_options[:row_sep] == nil ? @line_terminator = $/ : @line_terminator = @csv_options[:row_sep]
@formats = []
Expand All @@ -35,11 +35,11 @@ def validate
current_line = 0
single_col = false
reported_invalid_encoding = false
open(@stream) do |s|
open(@url) do |s|
@encoding = s.charset rescue nil
@content_type = s.content_type rescue nil
@headers = s.meta
mime_types = MIME::Types.type_for(@stream)
mime_types = MIME::Types.type_for(@url)
if mime_types.count > 0 && mime_types.select { |m| @content_type == m.content_type }.count == 0
build_warnings(:extension, nil)
end
Expand Down Expand Up @@ -140,6 +140,13 @@ def check_consistency
end
end

private

def parse_extension(url)
parsed = URI.parse(url)
File.extname(parsed.path)
end

end

end

0 comments on commit 9541ab6

Please sign in to comment.