Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedvalidator attachment fixes #873

Merged
merged 3 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/feed_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def download_url
end

def feedvalidator_url
if self.try(:file).try(:url)
if self.try(:file_feedvalidator).try(:url)
# we don't want to include any query parameters
self.file_feedvalidator.url.split('?').first
end
Expand Down
19 changes: 8 additions & 11 deletions app/services/feed_fetcher_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ def self.fetch_and_return_feed_version(feed)
# Return if there was not a successful fetch.
return unless feed_version
return unless feed_version.valid?
if feed_version.persisted?
log "File downloaded from #{feed.url} has an existing sha1 hash: #{feed_version.sha1}"
else
log "File downloaded from #{feed.url} has a new sha1 hash: #{feed_version.sha1}"
feed_version.save!
end
log "File downloaded from #{feed.url}, sha1 hash: #{feed_version.sha1}"
# Return found/created FeedVersion
feed_version
end
Expand All @@ -84,7 +79,7 @@ def self.url_fragment(url)

def self.run_google_feedvalidator(filename)
# Validate
return unless (Figaro.env.run_google_feedvalidator.present? && Figaro.env.run_google_feedvalidator == 'true')
return unless Figaro.env.run_google_feedvalidator.presence == 'true'
# Create a tempfile to use the filename.
outfile = nil
Tempfile.open(['feedvalidator', '.html']) do |tmpfile|
Expand All @@ -98,9 +93,10 @@ def self.run_google_feedvalidator(filename)
outfile,
filename
]).read
return unless File.exists?(outfile)
# Unlink temporary file
file_feedvalidator = File.open(outfile)
File.unlink(outfile) if File.exists?(outfile)
File.unlink(outfile)
file_feedvalidator
end

Expand Down Expand Up @@ -129,11 +125,12 @@ def self.fetch_and_normalize_feed_version(feed)
sha1 = Digest::SHA1.file(gtfs_file).hexdigest
end

file_feedvalidator = run_google_feedvalidator(gtfs_file.path)

# Create a new FeedVersion
feed_version = FeedVersion.find_by(sha1: sha1)
if !feed_version
# Validate the new data
file_feedvalidator = run_google_feedvalidator(gtfs_file.path)
# New FeedVersion
data = {
feed: feed,
url: feed.url,
Expand All @@ -143,7 +140,7 @@ def self.fetch_and_normalize_feed_version(feed)
fetched_at: DateTime.now
}
data = data.merge!(read_gtfs_info(gtfs))
feed_version = FeedVersion.new(data)
feed_version = FeedVersion.create!(data)
end
feed_version
end
Expand Down
9 changes: 1 addition & 8 deletions spec/services/feed_fetcher_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
expect(feed_version.earliest_calendar_date).to eq Date.parse('2007-01-01')
expect(feed_version.latest_calendar_date).to eq Date.parse('2010-12-31')
end

it 'reads feed_info.txt and puts into tags' do
feed = create(:feed, url: example_url)
feed_version = nil
Expand Down Expand Up @@ -204,15 +205,7 @@
end
fv1, fv2 = feed_versions
expect(fv1.sha1).to eq fv2.sha1
expect(fv1.fetched_at).not_to eq(fv2.fetched_at)
end

# it 'fails if files already exist' do
# feed_version = create(:feed_version_bart)
# VCR.use_cassette('feed_fetch_bart') do
# expect { feed_version.fetch_and_normalize }.to raise_error(StandardError)
# end
# end
end

end